Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
0
        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();
        }
Exemplo n.º 3
0
        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);
        }