Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,252 @@
#if NET_4_0
// BlockingCollectionTests.cs
//
// Copyright (c) 2008 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using System.Threading;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;
namespace MonoTests.System.Collections.Concurrent
{
[TestFixture()]
public class BlockingCollectionTests
{
BlockingCollection<int> defaultCollection;
BlockingCollection<int> boundedCollection;
[SetUpAttribute]
public void Setup()
{
defaultCollection = new BlockingCollection<int>();
boundedCollection = new BlockingCollection<int>(10);
}
[TestAttribute]
public void DefaultAddTestCase()
{
defaultCollection.Add(1);
defaultCollection.Add(2);
Assert.AreEqual(2, defaultCollection.Count, "#1");
}
[TestAttribute]
public void BoundedAddTestCase()
{
boundedCollection.Add(1);
boundedCollection.Add(2);
Assert.AreEqual(2, boundedCollection.Count, "#1");
}
[TestAttribute]
public void BoundedIsFullTestCase()
{
boundedCollection.Add(1);
boundedCollection.Add(2);
boundedCollection.Add(3);
boundedCollection.Add(4);
boundedCollection.Add(5);
boundedCollection.Add(6);
boundedCollection.Add(7);
boundedCollection.Add(8);
boundedCollection.Add(9);
boundedCollection.Add(10);
Assert.AreEqual(boundedCollection.BoundedCapacity, boundedCollection.Count, "#1");
}
[TestAttribute]
public void TakeTestCase()
{
defaultCollection.Add(1);
defaultCollection.Add(2);
boundedCollection.Add(1);
boundedCollection.Add(2);
int value = defaultCollection.Take();
Assert.AreEqual(1, value, "#1");
value = boundedCollection.Take();
Assert.AreEqual(1, value, "#2");
}
[TestAttribute, ExpectedExceptionAttribute(typeof(InvalidOperationException))]
public void DefaultAddCompletedTestCase()
{
defaultCollection.Add(1);
defaultCollection.Add(2);
defaultCollection.CompleteAdding();
Assert.IsTrue(defaultCollection.IsAddingCompleted, "#1");
defaultCollection.Add(3);
}
[TestAttribute, ExpectedExceptionAttribute(typeof(InvalidOperationException))]
public void BoundedAddCompletedTestCase()
{
boundedCollection.Add(1);
boundedCollection.Add(2);
boundedCollection.Add(3);
boundedCollection.Add(4);
boundedCollection.Add(5);
boundedCollection.Add(6);
boundedCollection.Add(7);
boundedCollection.Add(8);
boundedCollection.Add(9);
boundedCollection.Add(10);
boundedCollection.CompleteAdding();
Assert.IsTrue(boundedCollection.IsAddingCompleted, "#1");
boundedCollection.Add(3);
}
[TestAttribute]
public void IsCompletedTestCase()
{
defaultCollection.Add(1);
defaultCollection.Add(2);
defaultCollection.CompleteAdding();
Assert.IsFalse(defaultCollection.IsCompleted, "#3");
defaultCollection.Take();
defaultCollection.Take();
Assert.IsTrue(defaultCollection.IsAddingCompleted, "#1");
Assert.AreEqual(0, defaultCollection.Count, "#2");
Assert.IsTrue(defaultCollection.IsCompleted, "#4");
}
[TestAttribute]
public void IsCompletedEmptyTestCase ()
{
defaultCollection.CompleteAdding ();
Assert.IsTrue (defaultCollection.IsCompleted);
}
[TestAttribute]
public void ConsumingEnumerableTestCase()
{
defaultCollection.Add(1);
defaultCollection.Add(2);
defaultCollection.Add(3);
defaultCollection.Add(4);
defaultCollection.Add(5);
defaultCollection.Add(6);
defaultCollection.CompleteAdding ();
IEnumerable<int> enumerable = defaultCollection.GetConsumingEnumerable();
Assert.IsNotNull(enumerable, "#1");
int i = 1;
foreach (int j in enumerable) {
int temp = i++;
Assert.AreEqual(temp, j, "#" + temp);
}
Assert.AreEqual(0, defaultCollection.Count, "#" + i);
}
[TestAttribute]
public void TryTakeTestCase ()
{
defaultCollection.Add (1);
int value = default (int);
bool firstTake = defaultCollection.TryTake (out value);
int value2 = default (int);
bool secondTake = defaultCollection.TryTake (out value2);
Assert.AreEqual (1, value);
Assert.IsTrue (firstTake);
Assert.AreEqual (default (int), value2);
Assert.IsFalse (secondTake);
}
[Test]
public void EmptyTryTakeWithTimeout ()
{
object o = null;
var queue = new BlockingCollection<object> ();
bool success = queue.TryTake (out o, 500);
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

View File

@@ -0,0 +1,9 @@
2010-04-23 Jérémie Laval <jeremie.laval@gmail.com>
* BlockingCollectionTests.cs: Add IsCompletedEmptyTestCase,
fix namespace name
2009-08-19 Jérémie Laval <jeremie.laval@gmail.com>
* BlockingCollectionTests.cs: track API changes

View File

@@ -0,0 +1,134 @@
#if NET_4_0
//
// CollectionStressTestHelper.cs
//
// Author:
// Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
//
// Copyright (c) 2009 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Linq;
using System.Threading.Tasks;
using NUnit;
using NUnit.Framework;
using NUnit.Framework.Constraints;
namespace MonoTests.System.Collections.Concurrent
{
public enum CheckOrderingType {
InOrder,
Reversed,
DontCare
}
public static class CollectionStressTestHelper
{
public static void AddStressTest (IProducerConsumerCollection<int> coll)
{
ParallelTestHelper.Repeat (delegate {
int amount = -1;
const int count = 10;
const int threads = 5;
ParallelTestHelper.ParallelStressTest (coll, (q) => {
int t = Interlocked.Increment (ref amount);
for (int i = 0; i < count; i++)
coll.TryAdd (t);
}, threads);
Assert.AreEqual (threads * count, coll.Count, "#-1");
int[] values = new int[threads];
int temp;
while (coll.TryTake (out temp)) {
values[temp]++;
}
for (int i = 0; i < threads; i++)
Assert.AreEqual (count, values[i], "#" + i);
});
}
public static void RemoveStressTest (IProducerConsumerCollection<int> coll, CheckOrderingType order)
{
ParallelTestHelper.Repeat (delegate {
const int count = 10;
const int threads = 5;
const int delta = 5;
for (int i = 0; i < (count + delta) * threads; i++)
while (!coll.TryAdd (i));
bool state = true;
bool ran = false;
Assert.AreEqual ((count + delta) * threads, coll.Count, "#0");
ParallelTestHelper.ParallelStressTest (coll, (q) => {
ran = true;
bool s = true;
int t;
for (int i = 0; i < count; i++) {
s &= coll.TryTake (out t);
// try again in case it was a transient failure
if (!s && coll.TryTake (out t))
s = true;
}
if (!s)
state = false;
}, threads);
Assert.IsTrue (ran, "#1-pre");
Assert.IsTrue (state, "#1");
Assert.AreEqual (delta * threads, coll.Count, "#2");
string actual = string.Empty;
int temp;
while (coll.TryTake (out temp)) {
actual += temp.ToString ();;
}
IEnumerable<int> range = Enumerable.Range (order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads);
if (order == CheckOrderingType.Reversed)
range = range.Reverse ();
string expected = range.Aggregate (string.Empty, (acc, v) => acc + v);
if (order == CheckOrderingType.DontCare) {
Assert.That (actual, new CollectionEquivalentConstraint (expected), "#3, same");
} else {
Assert.AreEqual (expected, actual, "#3");
Assert.That (actual, new EqualConstraint (expected), "#3, in order");
}
}, 100);
}
}
}
#endif

View File

@@ -0,0 +1,252 @@
//
// ConcurrentBagTests.cs
//
// Author:
// Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
//
// Copyright (c) 2009 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if NET_4_0
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Linq;
using NUnit;
using NUnit.Framework;
using NUnit.Framework.Constraints;
namespace MonoTests.System.Collections.Concurrent
{
[TestFixture]
public class ConcurrentBagTests
{
ConcurrentBag<int> bag;
[SetUp]
public void Setup ()
{
bag = new ConcurrentBag<int> ();
}
[Test]
public void BasicAddTakeTest ()
{
bag.Add (1);
Assert.IsFalse (bag.IsEmpty);
Assert.AreEqual (1, bag.Count);
var array = bag.ToArray ();
Assert.AreEqual (1, array.Length);
Assert.AreEqual (1, array[0]);
int result;
Assert.IsTrue (bag.TryTake (out result));
Assert.AreEqual (1, result);
Assert.IsTrue (bag.IsEmpty);
}
[Test]
public void BasicAddTakeFromOtherThread ()
{
var t = new Thread (() => bag.Add (1));
t.Start ();
Assert.IsTrue (t.Join (300));
Assert.IsFalse (bag.IsEmpty);
Assert.AreEqual (1, bag.Count);
var array = bag.ToArray ();
Assert.AreEqual (1, array.Length);
Assert.AreEqual (1, array[0]);
int result;
Assert.IsTrue (bag.TryTake (out result));
Assert.AreEqual (1, result);
Assert.IsTrue (bag.IsEmpty);
}
[Test]
public void AddFromMultipleThreadTakeFromOneThread ()
{
var threads = new Thread[10];
for (int i = 0; i < threads.Length; i++) {
threads[i] = new Thread (() => bag.Add (1));
threads[i].Start ();
}
foreach (var t in threads)
Assert.IsTrue (t.Join (2000));
Assert.IsFalse (bag.IsEmpty);
Assert.AreEqual (threads.Length, bag.Count);
var array = bag.ToArray ();
Assert.AreEqual (threads.Length, array.Length);
Assert.That (array, new CollectionEquivalentConstraint (Enumerable.Repeat (1, 10).ToArray ()), "#1, same");
int result;
for (int i = 0; i < threads.Length; i++) {
Assert.IsTrue (bag.TryTake (out result));
Assert.AreEqual (1, result);
}
Assert.IsTrue (bag.IsEmpty);
}
[Test]
public void AddFromOneThreadTakeFromMultiple ()
{
var threads = new Thread[10];
for (int i = 0; i < threads.Length; i++)
bag.Add (1);
Assert.IsFalse (bag.IsEmpty);
Assert.AreEqual (threads.Length, bag.Count);
bool valid = true;
for (int i = 0; i < threads.Length; i++) {
int result;
threads[i] = new Thread (() => valid &= bag.TryTake (out result) && result == 1);
threads[i].Start ();
}
foreach (var t in threads)
Assert.IsTrue (t.Join (200));
Assert.IsTrue (valid, "Aggregate test");
}
[Test]
public void BasicAddPeekTest ()
{
bag.Add (1);
Assert.IsFalse (bag.IsEmpty);
Assert.AreEqual (1, bag.Count);
int result;
Assert.IsTrue (bag.TryPeek (out result));
Assert.AreEqual (1, result);
Assert.IsFalse (bag.IsEmpty);
}
[Test]
public void BasicAddPeekFromOtherThread ()
{
var t = new Thread (() => bag.Add (1));
t.Start ();
Assert.IsTrue (t.Join (300));
Assert.IsFalse (bag.IsEmpty);
Assert.AreEqual (1, bag.Count);
int result;
Assert.IsTrue (bag.TryPeek (out result));
Assert.AreEqual (1, result);
Assert.IsFalse (bag.IsEmpty);
}
[Test]
public void AddFromOneThreadPeekFromMultiple ()
{
var threads = new Thread[10];
for (int i = 0; i < threads.Length; i++)
bag.Add (1);
Assert.IsFalse (bag.IsEmpty);
Assert.AreEqual (threads.Length, bag.Count);
bool valid = true;
for (int i = 0; i < threads.Length; i++) {
int result;
threads[i] = new Thread (() => valid &= bag.TryPeek (out result) && result == 1);
threads[i].Start ();
}
foreach (var t in threads)
Assert.IsTrue (t.Join (200));
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 ()
{
CollectionStressTestHelper.AddStressTest (bag);
}
[Test]
public void RemoveStressTest ()
{
CollectionStressTestHelper.RemoveStressTest (bag, CheckOrderingType.DontCare);
}
}
}
#endif

View File

@@ -0,0 +1,89 @@
#if NET_4_0
// TestHelper.cs
//
// Copyright (c) 2008 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using System.Threading;
using System.Collections.Concurrent;
namespace MonoTests.System.Collections.Concurrent
{
static class ParallelTestHelper
{
const int NumRun = 500;
public static void Repeat (Action action)
{
Repeat (action, NumRun);
}
public static void Repeat (Action action, int numRun)
{
for (int i = 0; i < numRun; i++) {
action ();
}
}
public static void ParallelStressTest<TSource>(TSource obj, Action<TSource> action)
{
ParallelStressTest(obj, action, Environment.ProcessorCount + 2);
}
public static void ParallelStressTest<TSource>(TSource obj, Action<TSource> action, int numThread)
{
Thread[] threads = new Thread[numThread];
for (int i = 0; i < numThread; i++) {
threads[i] = new Thread(new ThreadStart(delegate { action(obj); }));
threads[i].Start();
}
// Wait for the completion
for (int i = 0; i < numThread; i++)
threads[i].Join();
}
public static void ParallelAdder(IProducerConsumerCollection<int> collection, int numThread)
{
int startIndex = -10;
ParallelTestHelper.ParallelStressTest(collection, delegate (IProducerConsumerCollection<int> c) {
int start = Interlocked.Add(ref startIndex, 10);
for (int i = start; i < start + 10; i++) {
c.TryAdd(i);
}
}, numThread);
}
public static void ParallelRemover(IProducerConsumerCollection<int> collection, int numThread, int times)
{
int t = -1;
ParallelTestHelper.ParallelStressTest(collection, delegate (IProducerConsumerCollection<int> c) {
int num = Interlocked.Increment(ref t);
int value;
if (num < times)
c.TryTake (out value);
}, numThread);
}
}
}
#endif