Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / Test / System.Threading.Tasks.Dataflow / EncapsulateTest.cs
1 // EncapsulateTest.cs
2 //  
3 // Author:
4 //       Petr Onderka <gsvick@gmail.com>
5 // 
6 // Copyright (c) 2012 Petr Onderka
7 // 
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 // THE SOFTWARE.
25
26 using System;
27 using System.Threading.Tasks;
28 using System.Threading.Tasks.Dataflow;
29 using NUnit.Framework;
30
31 namespace MonoTests.System.Threading.Tasks.Dataflow {
32         [TestFixture]
33         public class EncapsulateTest {
34                 [Test]
35                 public void CompletionTest ()
36                 {
37                         var target = new CompletionCheckerBlock<int, int> ();
38                         var source = new CompletionCheckerBlock<int, int> ();
39
40                         var encapsulated = DataflowBlock.Encapsulate (target, source);
41
42                         Assert.AreSame (source.Completion, encapsulated.Completion);
43                 }
44
45                 [Test]
46                 public void CompleteTest ()
47                 {
48                         var target = new CompletionCheckerBlock<int, int> ();
49                         var source = new CompletionCheckerBlock<int, int> ();
50
51                         var encapsulated = DataflowBlock.Encapsulate (target, source);
52
53                         encapsulated.Complete ();
54
55                         Assert.IsFalse (source.WasCompleted);
56                         Assert.IsTrue (target.WasCompleted);
57                         Assert.AreSame (source.Completion, encapsulated.Completion);
58                 }
59
60                 [Test]
61                 public void FaultTest ()
62                 {
63                         var target = new CompletionCheckerBlock<int, int> ();
64                         var source = new CompletionCheckerBlock<int, int> ();
65
66                         var encapsulated = DataflowBlock.Encapsulate (target, source);
67
68                         encapsulated.Fault (new Exception ());
69                         Assert.IsFalse (source.WasFaulted);
70                         Assert.IsTrue (target.WasFaulted);
71                         Assert.AreSame (source.Completion, encapsulated.Completion);
72                 }
73         }
74
75         class CompletionCheckerBlock<TInput, TOutput> :
76                 IPropagatorBlock<TInput, TOutput> {
77                 readonly Task completion = Task.FromResult (true);
78
79                 public DataflowMessageStatus OfferMessage (
80                         DataflowMessageHeader messageHeader, TInput messageValue,
81                         ISourceBlock<TInput> source, bool consumeToAccept)
82                 {
83                         throw new NotImplementedException ();
84                 }
85
86                 public void Complete ()
87                 {
88                         WasCompleted = true;
89                 }
90
91                 public bool WasCompleted { get; private set; }
92
93                 public void Fault (Exception exception)
94                 {
95                         WasFaulted = true;
96                 }
97
98                 public bool WasFaulted { get; private set; }
99
100                 public Task Completion
101                 {
102                         get { return completion; }
103                 }
104
105                 public IDisposable LinkTo (ITargetBlock<TOutput> target,
106                                            DataflowLinkOptions linkOptions)
107                 {
108                         throw new NotImplementedException ();
109                 }
110
111                 public TOutput ConsumeMessage (DataflowMessageHeader messageHeader,
112                                                ITargetBlock<TOutput> target,
113                                                out bool messageConsumed)
114                 {
115                         throw new NotImplementedException ();
116                 }
117
118                 public bool ReserveMessage (DataflowMessageHeader messageHeader,
119                                             ITargetBlock<TOutput> target)
120                 {
121                         throw new NotImplementedException ();
122                 }
123
124                 public void ReleaseReservation (DataflowMessageHeader messageHeader,
125                                                 ITargetBlock<TOutput> target)
126                 {
127                         throw new NotImplementedException ();
128                 }
129         }
130 }