summaryrefslogtreecommitdiff
path: root/mcs/class/System/Test/System.Collections.Concurrent
diff options
context:
space:
mode:
Diffstat (limited to 'mcs/class/System/Test/System.Collections.Concurrent')
-rw-r--r--mcs/class/System/Test/System.Collections.Concurrent/BlockingCollectionTests.cs55
-rw-r--r--mcs/class/System/Test/System.Collections.Concurrent/ConcurrentBagTests.cs44
2 files changed, 99 insertions, 0 deletions
diff --git a/mcs/class/System/Test/System.Collections.Concurrent/BlockingCollectionTests.cs b/mcs/class/System/Test/System.Collections.Concurrent/BlockingCollectionTests.cs
index 0098e2de92..9f0e9b0190 100644
--- a/mcs/class/System/Test/System.Collections.Concurrent/BlockingCollectionTests.cs
+++ b/mcs/class/System/Test/System.Collections.Concurrent/BlockingCollectionTests.cs
@@ -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 WE_GOT_CANCELED) {
+ res = "canceled";
+ return -10;
+ }
+ });
+
+ cts.Cancel ();
+ Assert.AreEqual (-10, t.Result, "#5");
+ Assert.AreEqual ("canceled", res, "#6");
+ }
}
}
#endif
diff --git a/mcs/class/System/Test/System.Collections.Concurrent/ConcurrentBagTests.cs b/mcs/class/System/Test/System.Collections.Concurrent/ConcurrentBagTests.cs
index 1262cc3947..b7bc6b5b7e 100644
--- a/mcs/class/System/Test/System.Collections.Concurrent/ConcurrentBagTests.cs
+++ b/mcs/class/System/Test/System.Collections.Concurrent/ConcurrentBagTests.cs
@@ -191,6 +191,50 @@ namespace MonoTests.System.Collections.Concurrent
Assert.IsTrue (valid, "Aggregate test");
}
+
+ [Test]
+ public void BasicRemoveEmptyTest ()
+ {
+ int result;
+ Assert.IsTrue(bag.IsEmpty);
+ Assert.IsFalse(bag.TryTake(out result));
+ }
+
+ [Test]
+ public void BasicRemoveTwiceTest()
+ {
+ bag.Add (1);
+ Assert.IsFalse (bag.IsEmpty);
+ Assert.AreEqual (1, bag.Count);
+
+ int result;
+ Assert.IsTrue (bag.TryTake (out result));
+ Assert.AreEqual (1, result);
+ Assert.IsTrue (bag.IsEmpty);
+ Assert.IsFalse (bag.TryTake (out result));
+ Assert.IsFalse (bag.TryTake (out result));
+ }
+
+ [Test]
+ public void AddRemoveAddTest()
+ {
+ bag.Add (1);
+ Assert.IsFalse (bag.IsEmpty);
+ Assert.AreEqual (1, bag.Count);
+
+ int result;
+ Assert.IsTrue (bag.TryTake (out result));
+ Assert.AreEqual (1, result);
+ Assert.IsTrue (bag.IsEmpty);
+
+ bag.Add (1);
+ Assert.IsFalse (bag.IsEmpty);
+ Assert.AreEqual (1, bag.Count);
+
+ Assert.IsTrue (bag.TryTake (out result));
+ Assert.AreEqual (1, result);
+ Assert.IsTrue (bag.IsEmpty);
+ }
[Test]
public void AddStressTest ()