public void HandleRemainStory(bool dedicated, bool discardRemain)
        {
            var counter   = 0;
            var syncEvent = new AutoResetEvent(false);
            var stopEvent = new AutoResetEvent(false);
            var eventLoop = new EventLoop <Action>(x => x())
            {
                Dedicated     = dedicated,
                DiscardRemain = discardRemain,
            };

            eventLoop.Start();

            eventLoop.Add(() => syncEvent.Set());
            eventLoop.Add(() => stopEvent.WaitOne());
            eventLoop.Add(() => Interlocked.Increment(ref counter));

            syncEvent.WaitOne();
            Task.Run(() =>
            {
                while (eventLoop.State == EventLoopStates.Active || eventLoop.State == EventLoopStates.Wait)
                {
                    Thread.Yield();
                }
                stopEvent.Set();
            });
            eventLoop.Stop();

            Assert.That(counter, Is.EqualTo(discardRemain ? 0 : 1));
        }
        public void IdealStory(bool dedicated)
        {
            var counter   = 0;
            var syncEvent = new AutoResetEvent(false);
            var eventLoop = new EventLoop <Action>(x => x())
            {
                Dedicated = dedicated,
            };

            eventLoop.Start();

            for (var i = 0; i < LOOPS; ++i)
            {
                eventLoop.Add(() =>
                {
                    Interlocked.Increment(ref counter);
                    Thread.Yield();
                });
            }
            eventLoop.Add(() => syncEvent.Set());

            syncEvent.WaitOne();
            eventLoop.Stop();

            Assert.That(counter, Is.EqualTo(LOOPS));
        }