Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mcs / class / System / Test / System.Collections.Concurrent / BlockingCollectionTests.cs
index 0098e2de927e421859e0e5ed1356c5842cd0b191..b33a5b4385f2620b87d09df9266a2e8c1963585a 100644 (file)
@@ -27,6 +27,7 @@ using System;
 using System.Threading;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
+using System.Threading.Tasks;
 
 using NUnit.Framework;
 
@@ -192,6 +193,60 @@ namespace MonoTests.System.Collections.Concurrent
                        Assert.IsNull (o);
                        Assert.IsFalse (success);
                }
+
+               [Test]
+               public void TakeAnyFromSecondCollection ()
+               {
+                       var a = new BlockingCollection<string> ();
+                       var b = new BlockingCollection<string> ();
+                       var arr = new [] { a, b };
+                       string res = null;
+
+                       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.Factory.StartNew (() => BlockingCollection<string>.TakeFromAny (arr, out res));
+                       b.Add ("bar");
+                       Assert.AreEqual (1, t.Result, "#3");
+                       Assert.AreEqual ("bar", res, "#4");
+               }
+
+               [Test]
+               public void TakeAnyCancellable ()
+               {
+                       var a = new BlockingCollection<string> ();
+                       var b = new BlockingCollection<string> ();
+                       var arr = new [] { a, b };
+                       var cts = new CancellationTokenSource ();
+                       string res = null;
+
+                       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.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.Factory.StartNew (() => {
+                               try {
+                                       return BlockingCollection<string>.TakeFromAny (arr, out res, cts.Token);
+                               } catch (OperationCanceledException) {
+                                       res = "canceled";
+                                       return -10;
+                               }
+                       });
+
+                       cts.Cancel ();
+                       Assert.AreEqual (-10, t.Result, "#5");
+                       Assert.AreEqual ("canceled", res, "#6");
+               }
        }
 }
 #endif