Merge pull request #799 from kebby/master
[mono.git] / mcs / class / System / Test / System.Collections.Concurrent / BlockingCollectionTests.cs
1 #if NET_4_0
2 // BlockingCollectionTests.cs
3 //
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 //
24 //
25
26 using System;
27 using System.Threading;
28 using System.Collections.Concurrent;
29 using System.Collections.Generic;
30 using System.Threading.Tasks;
31
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Collections.Concurrent
35 {
36         [TestFixture()]
37         public class BlockingCollectionTests
38         {
39                 BlockingCollection<int> defaultCollection;
40                 BlockingCollection<int> boundedCollection;
41                 
42                 [SetUpAttribute]
43                 public void Setup()
44                 {
45                         defaultCollection = new BlockingCollection<int>();
46                         boundedCollection = new BlockingCollection<int>(10);
47                 }
48                 
49                 [TestAttribute]
50                 public void DefaultAddTestCase()
51                 {
52                         defaultCollection.Add(1);
53                         defaultCollection.Add(2);
54                         Assert.AreEqual(2, defaultCollection.Count, "#1");
55
56                 }
57                 
58                 [TestAttribute]
59                 public void BoundedAddTestCase()
60                 {
61                         boundedCollection.Add(1);
62                         boundedCollection.Add(2);
63                         Assert.AreEqual(2, boundedCollection.Count, "#1");
64                 }
65                 
66                 [TestAttribute]
67                 public void BoundedIsFullTestCase()
68                 {
69                         boundedCollection.Add(1);
70                         boundedCollection.Add(2);
71                         boundedCollection.Add(3);
72                         boundedCollection.Add(4);
73                         boundedCollection.Add(5);
74                         boundedCollection.Add(6);
75                         boundedCollection.Add(7);
76                         boundedCollection.Add(8);
77                         boundedCollection.Add(9);
78                         boundedCollection.Add(10);
79                         Assert.AreEqual(boundedCollection.BoundedCapacity, boundedCollection.Count, "#1");
80                 }
81                 
82                 [TestAttribute]
83                 public void TakeTestCase()
84                 {
85                         defaultCollection.Add(1);
86                         defaultCollection.Add(2);
87                         boundedCollection.Add(1);
88                         boundedCollection.Add(2);
89                         
90                         int value = defaultCollection.Take();
91                         Assert.AreEqual(1, value, "#1");
92                         value = boundedCollection.Take();
93                         Assert.AreEqual(1, value, "#2");
94                 }
95                 
96                 [TestAttribute, ExpectedExceptionAttribute(typeof(InvalidOperationException))]
97                 public void DefaultAddCompletedTestCase()
98                 {
99                         defaultCollection.Add(1);
100                         defaultCollection.Add(2);
101                         defaultCollection.CompleteAdding();
102                         Assert.IsTrue(defaultCollection.IsAddingCompleted, "#1");
103                         
104                         defaultCollection.Add(3);
105                 }
106                 
107                 [TestAttribute, ExpectedExceptionAttribute(typeof(InvalidOperationException))]
108                 public void BoundedAddCompletedTestCase()
109                 {
110                         boundedCollection.Add(1);
111                         boundedCollection.Add(2);
112                         boundedCollection.Add(3);
113                         boundedCollection.Add(4);
114                         boundedCollection.Add(5);
115                         boundedCollection.Add(6);
116                         boundedCollection.Add(7);
117                         boundedCollection.Add(8);
118                         boundedCollection.Add(9);
119                         boundedCollection.Add(10);
120                         boundedCollection.CompleteAdding();
121                         Assert.IsTrue(boundedCollection.IsAddingCompleted, "#1");
122                         
123                         boundedCollection.Add(3);
124                 }
125                 
126                 [TestAttribute]
127                 public void IsCompletedTestCase()
128                 {
129                         defaultCollection.Add(1);
130                         defaultCollection.Add(2);
131                         
132                         defaultCollection.CompleteAdding();
133                         Assert.IsFalse(defaultCollection.IsCompleted, "#3");
134                         
135                         defaultCollection.Take();
136                         defaultCollection.Take();
137                         
138                         Assert.IsTrue(defaultCollection.IsAddingCompleted, "#1");
139                         Assert.AreEqual(0, defaultCollection.Count, "#2");
140                         Assert.IsTrue(defaultCollection.IsCompleted, "#4");
141                 }
142                 
143                 [TestAttribute]
144                 public void IsCompletedEmptyTestCase ()
145                 {
146                         defaultCollection.CompleteAdding ();
147                         Assert.IsTrue (defaultCollection.IsCompleted);
148                 }
149
150                 [TestAttribute]
151                 public void ConsumingEnumerableTestCase()
152                 {
153                         defaultCollection.Add(1);
154                         defaultCollection.Add(2);
155                         defaultCollection.Add(3);
156                         defaultCollection.Add(4);
157                         defaultCollection.Add(5);
158                         defaultCollection.Add(6);
159                         defaultCollection.CompleteAdding ();
160                         
161                         IEnumerable<int> enumerable = defaultCollection.GetConsumingEnumerable();
162                         Assert.IsNotNull(enumerable, "#1");
163                         int i = 1;
164                         foreach (int j in enumerable) {
165                                 int temp = i++;
166                                 Assert.AreEqual(temp, j, "#" + temp);
167                         }
168                         Assert.AreEqual(0, defaultCollection.Count, "#" + i);
169                 }
170
171                 [TestAttribute]
172                 public void TryTakeTestCase ()
173                 {
174                         defaultCollection.Add (1);
175
176                         int value = default (int);
177                         bool firstTake = defaultCollection.TryTake (out value);
178                         int value2 = default (int);
179                         bool secondTake = defaultCollection.TryTake (out value2);
180
181                         Assert.AreEqual (1, value);
182                         Assert.IsTrue (firstTake);
183                         Assert.AreEqual (default (int), value2);
184                         Assert.IsFalse (secondTake);
185                 }
186
187                 [Test]
188                 public void EmptyTryTakeWithTimeout ()
189                 {
190                         object o = null;
191                         var queue = new BlockingCollection<object> ();
192                         bool success = queue.TryTake (out o, 500);
193                         Assert.IsNull (o);
194                         Assert.IsFalse (success);
195                 }
196
197                 [Test]
198                 public void TakeAnyFromSecondCollection ()
199                 {
200                         var a = new BlockingCollection<string> ();
201                         var b = new BlockingCollection<string> ();
202                         var arr = new [] { a, b };
203                         string res = null;
204
205                         Task<int> t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res));
206                         a.Add ("foo");
207                         Assert.AreEqual (0, t.Result, "#1");
208                         Assert.AreEqual ("foo", res, "#2");
209
210                         t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res));
211                         b.Add ("bar");
212                         Assert.AreEqual (1, t.Result, "#3");
213                         Assert.AreEqual ("bar", res, "#4");
214                 }
215
216                 [Test]
217                 public void TakeAnyCancellable ()
218                 {
219                         var a = new BlockingCollection<string> ();
220                         var b = new BlockingCollection<string> ();
221                         var arr = new [] { a, b };
222                         var cts = new CancellationTokenSource ();
223                         string res = null;
224
225                         Task<int> t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token));
226                         Thread.Sleep (100);
227                         a.Add ("foo");
228                         Assert.AreEqual (0, t.Result, "#1");
229                         Assert.AreEqual ("foo", res, "#2");
230
231                         t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token));
232                         Thread.Sleep (100);
233                         b.Add ("bar");
234                         Assert.AreEqual (1, t.Result, "#3");
235                         Assert.AreEqual ("bar", res, "#4");
236
237                         t = Task.Factory.StartNew (() => {
238                                 try {
239                                         return BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token);
240                                 } catch (OperationCanceledException WE_GOT_CANCELED) {
241                                         res = "canceled";
242                                         return -10;
243                                 }
244                         });
245
246                         cts.Cancel ();
247                         Assert.AreEqual (-10, t.Result, "#5");
248                         Assert.AreEqual ("canceled", res, "#6");
249                 }
250         }
251 }
252 #endif