4 // Petr Onderka <gsvick@gmail.com>
6 // Copyright (c) 2012 Petr Onderka
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:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
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
27 using System.Threading.Tasks;
28 using System.Threading.Tasks.Dataflow;
29 using NUnit.Framework;
31 namespace MonoTests.System.Threading.Tasks.Dataflow {
33 public class EncapsulateTest {
35 public void CompletionTest ()
37 var target = new CompletionCheckerBlock<int, int> ();
38 var source = new CompletionCheckerBlock<int, int> ();
40 var encapsulated = DataflowBlock.Encapsulate (target, source);
42 Assert.AreSame (source.Completion, encapsulated.Completion);
46 public void CompleteTest ()
48 var target = new CompletionCheckerBlock<int, int> ();
49 var source = new CompletionCheckerBlock<int, int> ();
51 var encapsulated = DataflowBlock.Encapsulate (target, source);
53 encapsulated.Complete ();
55 Assert.IsFalse (source.WasCompleted);
56 Assert.IsTrue (target.WasCompleted);
57 Assert.AreSame (source.Completion, encapsulated.Completion);
61 public void FaultTest ()
63 var target = new CompletionCheckerBlock<int, int> ();
64 var source = new CompletionCheckerBlock<int, int> ();
66 var encapsulated = DataflowBlock.Encapsulate (target, source);
68 encapsulated.Fault (new Exception ());
69 Assert.IsFalse (source.WasFaulted);
70 Assert.IsTrue (target.WasFaulted);
71 Assert.AreSame (source.Completion, encapsulated.Completion);
75 class CompletionCheckerBlock<TInput, TOutput> :
76 IPropagatorBlock<TInput, TOutput> {
77 readonly Task completion = Task.FromResult (true);
79 public DataflowMessageStatus OfferMessage (
80 DataflowMessageHeader messageHeader, TInput messageValue,
81 ISourceBlock<TInput> source, bool consumeToAccept)
83 throw new NotImplementedException ();
86 public void Complete ()
91 public bool WasCompleted { get; private set; }
93 public void Fault (Exception exception)
98 public bool WasFaulted { get; private set; }
100 public Task Completion
102 get { return completion; }
105 public IDisposable LinkTo (ITargetBlock<TOutput> target,
106 DataflowLinkOptions linkOptions)
108 throw new NotImplementedException ();
111 public TOutput ConsumeMessage (DataflowMessageHeader messageHeader,
112 ITargetBlock<TOutput> target,
113 out bool messageConsumed)
115 throw new NotImplementedException ();
118 public bool ReserveMessage (DataflowMessageHeader messageHeader,
119 ITargetBlock<TOutput> target)
121 throw new NotImplementedException ();
124 public void ReleaseReservation (DataflowMessageHeader messageHeader,
125 ITargetBlock<TOutput> target)
127 throw new NotImplementedException ();