Skip to content

Commit d93c0af

Browse files
author
Matthias Radestock
committed
add tests for Close() behaviour
1 parent 1b6e626 commit d93c0af

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/unit/TestSharedQueue.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565

6666
[TestFixture]
6767
public class TestSharedQueue {
68+
69+
public delegate void Thunk();
70+
6871
public class DelayedEnqueuer
6972
{
7073
public SharedQueue m_q;
@@ -85,6 +88,13 @@ public static void EnqueueAfter(int delayMs, SharedQueue q, object v) {
8588
new Thread(new ThreadStart(de.Run)).Start();
8689
}
8790

91+
public static void ExpectEof(Thunk thunk) {
92+
try {
93+
thunk();
94+
Assert.Fail("expected System.IO.EndOfStreamException");
95+
} catch (System.IO.EndOfStreamException) {}
96+
}
97+
8898
public DateTime m_startTime;
8999

90100
public void ResetTimer() {
@@ -248,4 +258,44 @@ public void TestDoublePoll() {
248258
Assert.IsTrue(r);
249259
Assert.AreEqual(123, v);
250260
}
261+
262+
[Test]
263+
public void TestCloseWhenEmpty() {
264+
SharedQueue q = new SharedQueue();
265+
object v;
266+
q.Close();
267+
ExpectEof(delegate () { q.Enqueue(1); });
268+
ExpectEof(delegate () { q.Dequeue(); });
269+
ExpectEof(delegate () { q.DequeueNoWait(0); });
270+
ExpectEof(delegate () { q.Dequeue(1, out v); });
271+
}
272+
273+
[Test]
274+
public void TestCloseWhenFull() {
275+
SharedQueue q = new SharedQueue();
276+
object v;
277+
q.Enqueue(1);
278+
q.Enqueue(2);
279+
q.Enqueue(3);
280+
q.Close();
281+
ExpectEof(delegate () { q.Enqueue(4); });
282+
Assert.AreEqual(1, q.Dequeue());
283+
Assert.AreEqual(2, q.DequeueNoWait(0));
284+
bool r = q.Dequeue(1, out v);
285+
Assert.IsTrue(r);
286+
Assert.AreEqual(3, v);
287+
ExpectEof(delegate () { q.Dequeue(); });
288+
}
289+
290+
[Test]
291+
public void TestCloseWhenWaiting() {
292+
SharedQueue q = new SharedQueue();
293+
Thread t = new Thread(delegate() {
294+
ExpectEof(delegate () { q.Dequeue(); });
295+
});
296+
t.Start();
297+
Thread.Sleep(10);
298+
q.Close();
299+
t.Join();
300+
}
251301
}

0 commit comments

Comments
 (0)