public void SameFrameReceiveAsync() { var test = new EventSystemTest(); var frameCounter = 0; test.AddTask(async() => { while (test.IsRunning) { frameCounter++; await test.NextFrame(); } }, 100); test.AddTask(async() => { var key = new EventKey(); var recv = new EventReceiver(key); key.Broadcast(); var currentFrame = frameCounter; await recv.ReceiveAsync(); Assert.Equal(currentFrame, frameCounter); test.Exit(); }); test.Run(); }
public void ReceiveManyCheck() { var game = new EventSystemTest(); var frameCount = 0; game.AddTask(async() => { var evt = new EventKey(); var rcv = new EventReceiver(evt, EventReceiverOptions.Buffered); while (frameCount < 25) { evt.Broadcast(); if (frameCount == 20) { var manyEvents = rcv.TryReceiveAll(); Assert.Equal(21, manyEvents); game.Exit(); } await game.NextFrame(); frameCount++; } }); game.Run(); }
public void EveryFrameClear() { var game = new EventSystemTest(); var frameCount = 0; var evt = new EventKey(); game.AddTask(async() => { while (frameCount < 25) { evt.Broadcast(); evt.Broadcast(); await game.NextFrame(); } }, 10); game.AddTask(async() => { var rcv = new EventReceiver(evt, EventReceiverOptions.Buffered); while (frameCount < 25) { if (frameCount == 20) { var manyEvents = rcv.TryReceiveAll(); Assert.Equal(2, manyEvents); game.Exit(); } rcv.Reset(); await game.NextFrame(); frameCount++; } }, -10); game.Run(); }
public void DelayedReceiverCreation() { var game = new EventSystemTest(); var frameCount = 0; game.AddTask(async() => { var evt = new EventKey(); EventReceiver rcv = null; while (frameCount < 25) { if (frameCount == 5) { evt.Broadcast(); } if (frameCount == 20) { rcv = new EventReceiver(evt); Assert.False(rcv.TryReceive()); evt.Broadcast(); } if (frameCount == 22) { Assert.NotNull(rcv); Assert.True(rcv.TryReceive()); game.Exit(); } await game.NextFrame(); frameCount++; } }); game.Run(); }
public void DifferentSyntax() { var game = new EventSystemTest(); var frameCounter = 0; var broadcaster = new EventKey(); game.AddTask(async() => { var tests = 5; var recv = new EventReceiver(broadcaster); var threadId = Thread.CurrentThread.ManagedThreadId; while (tests-- > 0) { await recv; Assert.Equal(threadId, Thread.CurrentThread.ManagedThreadId); } }); game.AddTask(async() => { var tests = 5; var recv = new EventReceiver(broadcaster); var threadId = Thread.CurrentThread.ManagedThreadId; while (tests-- > 0) { await recv; Assert.Equal(threadId, Thread.CurrentThread.ManagedThreadId); } }); game.AddTask(async() => { var tests = 5; var recv = new EventReceiver(broadcaster); var threadId = Thread.CurrentThread.ManagedThreadId; while (tests-- > 0) { await recv; Assert.Equal(threadId, Thread.CurrentThread.ManagedThreadId); } }); Task.Run(async() => { while (!game.IsRunning) { await Task.Delay(100); } while (true) { frameCounter++; broadcaster.Broadcast(); if (frameCounter == 20) { game.Exit(); } await Task.Delay(50); } }); game.Run(); }
public void DifferentThreadBroadcast() { var game = new EventSystemTest(); var counter = 0; var broadcaster = new EventKey(); var readyCount = 0; game.AddTask(async() => { var recv = new EventReceiver(broadcaster, EventReceiverOptions.Buffered); Interlocked.Increment(ref readyCount); for (;;) { await recv.ReceiveAsync(); Interlocked.Increment(ref counter); } }); game.AddTask(async() => { var recv = new EventReceiver(broadcaster, EventReceiverOptions.Buffered); Interlocked.Increment(ref readyCount); for (;;) { await recv.ReceiveAsync(); Interlocked.Increment(ref counter); } }); game.AddTask(async() => { var recv = new EventReceiver(broadcaster, EventReceiverOptions.Buffered); Interlocked.Increment(ref readyCount); for (;;) { await recv.ReceiveAsync(); Interlocked.Increment(ref counter); } }); var t1W = new AutoResetEvent(false); var t2W = new AutoResetEvent(false); var waitHandles = new WaitHandle[] { t1W, t2W }; Exception threadException = null; new Thread(() => { try { while (!game.IsRunning && readyCount < 3) { Thread.Sleep(200); } var frameCounter = 0; while (true) { Thread.Sleep(50); frameCounter++; broadcaster.Broadcast(); if (frameCounter < 200) { continue; } t1W.Set(); return; } } catch (Exception e) { threadException = e; } }).Start(); new Thread(() => { try { while (!game.IsRunning && readyCount < 3) { Thread.Sleep(200); } var frameCounter = 0; while (true) { Thread.Sleep(50); frameCounter++; broadcaster.Broadcast(); if (frameCounter < 200) { continue; } t2W.Set(); return; } } catch (Exception e) { threadException = e; } }).Start(); new Thread(() => { try { //wait until both threads have broadcasted 200 times each if (!WaitHandle.WaitAll(waitHandles, TimeSpan.FromMinutes(2))) { throw new Exception("DifferentThreadBroadcast test timedout."); } Thread.Sleep(2000); game.Exit(); } catch (Exception e) { threadException = e; } }).Start(); game.Run(); Assert.Null(threadException); Assert.Equal(1200, counter); }