Merge pull request #1222 from LogosBible/uri-trycreate
[mono.git] / mcs / class / System / Test / System.Collections.Concurrent / BlockingCollectionTests.cs
index 9c391fc65ba139252605dafd8f2598dd769b9e57..bf41aeee21af96b1c098b5bb30e596f11cdebb69 100644 (file)
@@ -202,12 +202,12 @@ namespace MonoTests.System.Collections.Concurrent
                        var arr = new [] { a, b };
                        string res = null;
 
-                       Task<int> t = Task.Run (() => BlockingCollection<string>.TakeFromAny (arr, out res));
+                       Task<int> t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res));
                        a.Add ("foo");
                        Assert.AreEqual (0, t.Result, "#1");
                        Assert.AreEqual ("foo", res, "#2");
 
-                       t = Task.Run (() => BlockingCollection<string>.TakeFromAny (arr, out res));
+                       t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res));
                        b.Add ("bar");
                        Assert.AreEqual (1, t.Result, "#3");
                        Assert.AreEqual ("bar", res, "#4");
@@ -222,22 +222,22 @@ namespace MonoTests.System.Collections.Concurrent
                        var cts = new CancellationTokenSource ();
                        string res = null;
 
-                       Task<int> t = Task.Run (() => BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token));
+                       Task<int> t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token));
                        Thread.Sleep (100);
                        a.Add ("foo");
                        Assert.AreEqual (0, t.Result, "#1");
                        Assert.AreEqual ("foo", res, "#2");
 
-                       t = Task.Run (() => BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token));
+                       t = Task.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token));
                        Thread.Sleep (100);
                        b.Add ("bar");
                        Assert.AreEqual (1, t.Result, "#3");
                        Assert.AreEqual ("bar", res, "#4");
 
-                       t = Task.Run (() => {
+                       t = Task.Factory.StartNew (() => {
                                try {
                                        return BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token);
-                               } catch (OperationCanceledException WE_GOT_CANCELED) {
+                               } catch (OperationCanceledException) {
                                        res = "canceled";
                                        return -10;
                                }
@@ -247,6 +247,40 @@ namespace MonoTests.System.Collections.Concurrent
                        Assert.AreEqual (-10, t.Result, "#5");
                        Assert.AreEqual ("canceled", res, "#6");
                }
+
+               [Test, ExpectedException (typeof(OperationCanceledException))]
+               public void BoundedAddLimit ()
+               {
+                       const int elNumber = 5;
+
+                       var c = new BlockingCollection <int> (elNumber);
+                       var token = new CancellationTokenSource (100);
+
+                       for (var i = 0; i < elNumber + 1; i++) {
+                               c.Add (1, token.Token);
+                       }
+               }
+
+               [Test]
+               public void AddAnyCancellable ()
+               {
+                       const int elNumber = 5;
+                       const int colNumber = 5;
+
+                       var cols = new BlockingCollection <int> [colNumber];
+                       for (var i = 0; i < colNumber; i++) {
+                               cols[i] = new BlockingCollection <int> (elNumber);
+                       }
+
+                       var token = new CancellationTokenSource (100);
+                       for (var i = 0; i < colNumber * elNumber; i++) {
+                               BlockingCollection <int>.AddToAny (cols, 1, token.Token);
+                       }
+
+                       foreach (var col in cols) {
+                               Assert.AreEqual (elNumber, col.Count);
+                       }
+               }
        }
 }
 #endif