Permalink
Browse files

Use events to wait for completion (where applicable) in dataflow unit…

… tests
  • Loading branch information...
1 parent 58ab497 commit 639c8a49b1248fb203bbb36d59f31d318c589916 @garuma committed Aug 22, 2011
@@ -41,13 +41,13 @@ public class ActionBlockTest
public void BasicUsageTest ()
{
bool[] array = new bool[3];
- ActionBlock<int> block = new ActionBlock<int> ((i) => array[i] = true);
+ CountdownEvent evt = new CountdownEvent (array.Length);
+ ActionBlock<int> block = new ActionBlock<int> ((i) => { array[i] = true; evt.Signal (); });
for (int i = 0; i < array.Length; ++i)
Assert.IsTrue (block.Post (i), "Not accepted");
- // TODO: use a more sensible approach here based on Completion
- Thread.Sleep (1300);
+ evt.Wait ();
Assert.IsTrue (array.All (b => b), "Some false");
}
@@ -41,20 +41,21 @@ public class BatchBlockTest
public void BasicUsageTest ()
{
int[] array = null;
+ var evt = new ManualResetEventSlim (false);
var buffer = new BatchBlock<int> (10);
- var block = new ActionBlock<int[]> (i => array = i);
+ var block = new ActionBlock<int[]> (i => { array = i; evt.Set (); });
buffer.LinkTo<int[]>(block);
for (int i = 0; i < 9; i++)
Assert.IsTrue (buffer.Post (i));
- Thread.Sleep (1600);
+ evt.Wait (1600);
Assert.IsNull (array);
Assert.IsTrue (buffer.Post (42));
- Thread.Sleep (1600);
+ evt.Wait ();
Assert.IsNotNull (array);
CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 42 }, array);
@@ -64,21 +65,21 @@ public void BasicUsageTest ()
public void TriggerBatchTest ()
{
int[] array = null;
+ var evt = new ManualResetEventSlim (false);
var buffer = new BatchBlock<int> (10);
- var block = new ActionBlock<int[]> (i => array = i);
+ var block = new ActionBlock<int[]> (i => { array = i; evt.Set (); });
buffer.LinkTo(block);
for (int i = 0; i < 9; i++)
Assert.IsTrue (buffer.Post (i));
buffer.TriggerBatch ();
- Thread.Sleep (1600);
+ evt.Wait ();
Assert.IsNotNull (array);
-
Assert.IsTrue (buffer.Post (42));
- Thread.Sleep (1600);
+ evt.Wait (1600);
CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, array);
}
@@ -87,41 +88,39 @@ public void TriggerBatchTest ()
public void TriggerBatchLateBinding ()
{
int[] array = null;
+ var evt = new ManualResetEventSlim (false);
var buffer = new BatchBlock<int> (10);
- var block = new ActionBlock<int[]> (i => array = i);
+ var block = new ActionBlock<int[]> (i => { array = i; evt.Set (); });
for (int i = 0; i < 9; i++)
Assert.IsTrue (buffer.Post (i));
buffer.TriggerBatch ();
- buffer.LinkTo(block);
-
- Thread.Sleep (1600);
+ buffer.LinkTo (block);
+ evt.Wait ();
Assert.IsNotNull (array);
- Assert.IsTrue (buffer.Post (42));
- Thread.Sleep (1600);
-
CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, array);
}
[Test]
public void LateTriggerBatchKeepCountTest ()
{
int[] array = null;
+ var evt = new ManualResetEventSlim (false);
var buffer = new BatchBlock<int> (15);
- var block = new ActionBlock<int[]> (i => array = i);
+ var block = new ActionBlock<int[]> (i => { array = i; evt.Set (); });
for (int i = 0; i < 9; i++)
Assert.IsTrue (buffer.Post (i));
buffer.TriggerBatch ();
Assert.IsTrue (buffer.Post (42));
- buffer.LinkTo(block);
+ buffer.LinkTo (block);
- Thread.Sleep (1600);
+ evt.Wait ();
Assert.IsNotNull (array);
CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, array);
@@ -41,17 +41,18 @@ public class BroadcastBlockTest
public void BasicUsageTest ()
{
bool act1 = false, act2 = false;
+ var evt = new CountdownEvent (2);
var broadcast = new BroadcastBlock<int> (null);
- var action1 = new ActionBlock<int> (i => act1 = i == 42);
- var action2 = new ActionBlock<int> (i => act2 = i == 42);
+ var action1 = new ActionBlock<int> (i => { act1 = i == 42; evt.Signal (); });
+ var action2 = new ActionBlock<int> (i => { act2 = i == 42; evt.Signal (); });
broadcast.LinkTo (action1);
broadcast.LinkTo (action2);
Assert.IsTrue (broadcast.Post (42));
- Thread.Sleep (1600);
+ evt.Wait ();
Assert.IsTrue (act1);
Assert.IsTrue (act2);
@@ -61,18 +62,19 @@ public void BasicUsageTest ()
public void CloningTest ()
{
object act1 = null, act2 = null;
+ var evt = new CountdownEvent (2);
object source = new object ();
var broadcast = new BroadcastBlock<object> (o => new object ());
- var action1 = new ActionBlock<object> (i => act1 = i);
- var action2 = new ActionBlock<object> (i => act2 = i);
+ var action1 = new ActionBlock<object> (i => { act1 = i; evt.Signal (); });
+ var action2 = new ActionBlock<object> (i => { act2 = i; evt.Signal (); });
broadcast.LinkTo (action1);
broadcast.LinkTo (action2);
Assert.IsTrue (broadcast.Post (source));
- Thread.Sleep (1600);
+ evt.Wait ();
Assert.IsNotNull (act1);
Assert.IsNotNull (act2);
@@ -41,53 +41,55 @@ public class BufferBlockTest
public void BasicUsageTest ()
{
int data = -1;
+ var evt = new ManualResetEventSlim (false);
BufferBlock<int> buffer = new BufferBlock<int> ();
- ActionBlock<int> action = new ActionBlock<int> ((i) => data = i);
+ ActionBlock<int> action = new ActionBlock<int> ((i) => { data = i; evt.Set (); });
buffer.LinkTo (action);
Assert.IsTrue (buffer.Post (42));
- Thread.Sleep (600);
+ evt.Wait ();
Assert.AreEqual (42, data);
}
[Test]
public void LateBindingTest ()
{
BufferBlock<int> buffer = new BufferBlock<int> ();
+ var evt = new CountdownEvent (10);
for (int i = 0; i < 10; i++)
Assert.IsTrue (buffer.Post (i));
- int count = 0;
- ActionBlock<int> block = new ActionBlock<int> ((i) => Interlocked.Increment (ref count));
+ ActionBlock<int> block = new ActionBlock<int> ((i) => evt.Signal ());
buffer.LinkTo (block);
- Thread.Sleep (600);
-
- Assert.AreEqual (10, count);
+ evt.Wait ();
}
[Test]
public void MultipleBindingTest ()
{
BufferBlock<int> buffer = new BufferBlock<int> ();
+ var evt = new CountdownEvent (10);
int count = 0;
- ActionBlock<int> block = new ActionBlock<int> ((i) => Interlocked.Decrement (ref count));
+ ActionBlock<int> block = new ActionBlock<int> ((i) => { Interlocked.Decrement (ref count); evt.Signal (); });
IDisposable bridge = buffer.LinkTo (block);
for (int i = 0; i < 10; i++)
Assert.IsTrue (buffer.Post (i));
- Thread.Sleep (600);
+ evt.Wait ();
+ Assert.AreEqual (-10, count);
count = 0;
+ evt.Reset ();
bridge.Dispose ();
- ActionBlock<int> block2 = new ActionBlock<int> ((i) => Interlocked.Increment (ref count));
+ ActionBlock<int> block2 = new ActionBlock<int> ((i) => { Interlocked.Increment (ref count); evt.Signal (); });
buffer.LinkTo (block2);
for (int i = 0; i < 10; i++)
Assert.IsTrue (buffer.Post (i));
- Thread.Sleep (600);
+ evt.Wait ();
Assert.AreEqual (10, count);
}
@@ -41,13 +41,8 @@ public class CompletionTest
public void WithNoElements ()
{
var block = new BufferBlock<int> ();
- //var block2 = new BufferBlock<int> ();
- //block.LinkTo (block2);
block.Post (42);
- Thread.Sleep (600);
- //((IDataflowBlock)block2).Fault (new Exception ());
block.Complete ();
- Thread.Sleep (600);
Console.WriteLine (block.Completion.IsCompleted);
Console.WriteLine (block.Completion.Status);
block.Receive ();
@@ -59,13 +54,8 @@ public void WithNoElements ()
public void WithElementsStillLingering ()
{
var block = new BufferBlock<int> ();
- //var block2 = new BufferBlock<int> ();
- //block.LinkTo (block2);
block.Post (42);
- Thread.Sleep (600);
- //((IDataflowBlock)block2).Fault (new Exception ());
block.Complete ();
- Thread.Sleep (600);
Console.WriteLine (block.Completion.IsCompleted);
Console.WriteLine (block.Completion.Status);
block.Receive ();
@@ -78,9 +68,7 @@ public void EmptyAfterReceive ()
{
var block = new BufferBlock<int> ();
block.Post (42);
- Thread.Sleep (600);
block.Complete ();
- Thread.Sleep (600);
Assert.IsFalse (block.Completion.IsCompleted);
Assert.AreEqual (TaskStatus.WaitingForActivation, block.Completion.Status);
block.Receive ();
@@ -93,9 +81,7 @@ public void WithElementsStillLingeringButFaulted ()
{
var block = new BufferBlock<int> ();
block.Post (42);
- Thread.Sleep (600);
((IDataflowBlock)block).Fault (new Exception ());
- Thread.Sleep (600);
Assert.IsTrue (block.Completion.IsCompleted);
Assert.AreEqual (TaskStatus.Faulted, block.Completion.Status);
}
@@ -41,19 +41,20 @@ public class JoinBlockTest
public void BasicUsageTest ()
{
Tuple<int, int> tuple = null;
+ var evt = new ManualResetEventSlim (false);
- var ablock = new ActionBlock<Tuple<int, int>> (t => tuple = t);
+ var ablock = new ActionBlock<Tuple<int, int>> (t => { tuple = t; evt.Set (); });
var block = new JoinBlock<int, int> ();
block.LinkTo (ablock);
block.Target1.Post (42);
- Thread.Sleep (1600);
+ evt.Wait (1000);
Assert.IsNull (tuple);
block.Target2.Post (24);
- Thread.Sleep (1600);
+ evt.Wait ();
Assert.IsNotNull (tuple);
Assert.AreEqual (42, tuple.Item1);
Assert.AreEqual (24, tuple.Item2);
@@ -41,24 +41,25 @@ public class JoinBlock3Test
public void BasicUsageTest ()
{
Tuple<int, int, int> tuple = null;
+ var evt = new ManualResetEventSlim (false);
- var ablock = new ActionBlock<Tuple<int, int, int>> (t => tuple = t);
+ var ablock = new ActionBlock<Tuple<int, int, int>> (t => { tuple = t; evt.Set (); });
var block = new JoinBlock<int, int, int> ();
block.LinkTo (ablock);
block.Target1.Post (42);
- Thread.Sleep (1600);
+ evt.Wait (500);
Assert.IsNull (tuple);
block.Target2.Post (24);
- Thread.Sleep (1600);
+ evt.Wait (500);
Assert.IsNull (tuple);
block.Target3.Post (44);
- Thread.Sleep (1600);
+ evt.Wait ();
Assert.IsNotNull (tuple);
Assert.AreEqual (42, tuple.Item1);
Assert.AreEqual (24, tuple.Item2);
@@ -41,14 +41,15 @@ public class TransformBlockTest
public void BasicUsageTest ()
{
int[] array = new int[10];
- ActionBlock<int> action = new ActionBlock<int> ((i) => array[Math.Abs (i)] = i);
+ var evt = new ManualResetEventSlim (false);
+ ActionBlock<int> action = new ActionBlock<int> ((i) => { array[Math.Abs (i)] = i; evt.Set (); });
TransformBlock<int, int> block = new TransformBlock<int, int> (i => -i);
block.LinkTo (action);
for (int i = 0; i < array.Length; ++i)
Assert.IsTrue (block.Post (i), "Not accepted");
- Thread.Sleep (1300);
+ evt.Wait ();
CollectionAssert.AreEqual (new int[] { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 }, array);
}
@@ -57,6 +58,7 @@ public void BasicUsageTest ()
public void DeferredUsageTest ()
{
int[] array = new int[10];
+ var evt = new ManualResetEventSlim (false);
ActionBlock<int> action = new ActionBlock<int> ((i) => array[Math.Abs (i)] = i);
TransformBlock<int, int> block = new TransformBlock<int, int> (i => -i);
@@ -42,15 +42,16 @@ public void BasicUsageTest ()
{
int insIndex = -1;
int[] array = new int[5 + 3];
+ var evt = new CountdownEvent (array.Length);
- var block = new ActionBlock<int> (i => array[Interlocked.Increment (ref insIndex)] = i);
+ var block = new ActionBlock<int> (i => { array[Interlocked.Increment (ref insIndex)] = i; evt.Signal (); });
var trsm = new TransformManyBlock<int, int> (i => Enumerable.Range (0, i));
trsm.LinkTo (block);
trsm.Post (5);
trsm.Post (3);
- Thread.Sleep (1600);
+ evt.Wait ();
CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 0, 1, 2 }, array);
}
@@ -60,16 +61,16 @@ public void DeferredUsageTest ()
{
int insIndex = -1;
int[] array = new int[5 + 3];
+ var evt = new CountdownEvent (array.Length);
- var block = new ActionBlock<int> (i => array[Interlocked.Increment (ref insIndex)] = i);
+ var block = new ActionBlock<int> (i => { array[Interlocked.Increment (ref insIndex)] = i; evt.Signal (); });
var trsm = new TransformManyBlock<int, int> (i => Enumerable.Range (0, i));
trsm.Post (5);
trsm.Post (3);
- Thread.Sleep (1600);
trsm.LinkTo (block);
- Thread.Sleep (500);
+ evt.Wait ();
CollectionAssert.AreEquivalent (new int[] { 0, 1, 2, 3, 4, 0, 1, 2 }, array);
}
Oops, something went wrong.

0 comments on commit 639c8a4

Please sign in to comment.