public void AddTimer_In_Parallel_Wont_Throw()
        {
            var       ml = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));
            const int ms = 100;
            object    token1 = null, token2 = null;

            var callbackCount = 0;
            Func <MainLoop, bool> callback = (MainLoop loop) => {
                callbackCount++;
                if (callbackCount == 2)
                {
                    ml.Stop();
                }
                return(true);
            };

            Parallel.Invoke(
                () => token1 = ml.AddTimeout(TimeSpan.FromMilliseconds(ms), callback),
                () => token2 = ml.AddTimeout(TimeSpan.FromMilliseconds(ms), callback)
                );
            ml.Run();
            Assert.NotNull(token1);
            Assert.NotNull(token2);
            Assert.True(ml.RemoveTimeout(token1));
            Assert.True(ml.RemoveTimeout(token2));

            Assert.Equal(2, callbackCount);
        }
        public async Task AddTimer_Duplicate_Keys_Not_Allowed()
        {
            var       ml = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));
            const int ms = 100;
            object    token1 = null, token2 = null;

            var callbackCount = 0;
            Func <MainLoop, bool> callback = (MainLoop loop) => {
                callbackCount++;
                if (callbackCount == 2)
                {
                    ml.Stop();
                }
                return(true);
            };

            var task1 = new Task(() => token1 = ml.AddTimeout(TimeSpan.FromMilliseconds(ms), callback));
            var task2 = new Task(() => token2 = ml.AddTimeout(TimeSpan.FromMilliseconds(ms), callback));

            Assert.Null(token1);
            Assert.Null(token2);
            task1.Start();
            task2.Start();
            ml.Run();
            Assert.NotNull(token1);
            Assert.NotNull(token2);
            await Task.WhenAll(task1, task2);

            Assert.True(ml.RemoveTimeout(token1));
            Assert.True(ml.RemoveTimeout(token2));

            Assert.Equal(2, callbackCount);
        }
        public void AddTimer_ReturnFalse_StopsBeingCalled()
        {
            var ml = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));
            var ms = TimeSpan.FromMilliseconds(50);

            // Force stop if 10 iterations
            var         stopCount = 0;
            Func <bool> fnStop    = () => {
                Thread.Sleep(10);                  // Sleep to enable timer to fire
                stopCount++;
                if (stopCount == 10)
                {
                    ml.Stop();
                }
                return(true);
            };

            ml.AddIdle(fnStop);

            var callbackCount = 0;
            Func <MainLoop, bool> callback = (MainLoop loop) => {
                callbackCount++;
                return(false);
            };

            var token = ml.AddTimeout(ms, callback);

            ml.Run();
            Assert.Equal(1, callbackCount);
            Assert.Equal(10, stopCount);
            Assert.False(ml.RemoveTimeout(token));
        }
        public void AddTimer_Remove_NotCalled()
        {
            var ml = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));
            var ms = TimeSpan.FromMilliseconds(50);

            // Force stop if 10 iterations
            var         stopCount = 0;
            Func <bool> fnStop    = () => {
                stopCount++;
                if (stopCount == 10)
                {
                    ml.Stop();
                }
                return(true);
            };

            ml.AddIdle(fnStop);

            var callbackCount = 0;
            Func <MainLoop, bool> callback = (MainLoop loop) => {
                callbackCount++;
                return(true);
            };

            var token = ml.AddTimeout(ms, callback);

            Assert.True(ml.RemoveTimeout(token));
            ml.Run();
            Assert.Equal(0, callbackCount);
        }
        public void AddTimer_Run_CalledTwiceApproximatelyRightTime()
        {
            var ml    = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));
            var ms    = TimeSpan.FromMilliseconds(50);
            var watch = new System.Diagnostics.Stopwatch();

            var callbackCount = 0;
            Func <MainLoop, bool> callback = (MainLoop loop) => {
                callbackCount++;
                if (callbackCount == 2)
                {
                    watch.Stop();
                    ml.Stop();
                }
                return(true);
            };

            var token = ml.AddTimeout(ms, callback);

            watch.Start();
            ml.Run();
            // +/- 100ms should be good enuf
            // https://github.com/xunit/assert.xunit/pull/25
            Assert.Equal <TimeSpan> (ms * callbackCount, watch.Elapsed, new MillisecondTolerance(100));

            Assert.True(ml.RemoveTimeout(token));
            Assert.Equal(2, callbackCount);
        }
Example #6
0
    static void Main()
    {
        MainLoop ml = new MainLoop();

        Stamp("Start");
        ml.AddTimeout(TimeSpan.FromSeconds(1), x => {
            Stamp("second");
            return(true);
        });

        int i = 0;

        ml.AddTimeout(TimeSpan.FromSeconds(3), x => {
            Stamp("three");
            if (++i >= 3)
            {
                return(false);
            }
            return(true);
        });

        ml.Run();
    }
Example #7
0
        public void AddTimer_Adds_Removes_NoFaults()
        {
            var ml = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));
            var ms = 100;

            var callbackCount = 0;
            Func <MainLoop, bool> callback = (MainLoop loop) => {
                callbackCount++;
                return(true);
            };

            var token = ml.AddTimeout(TimeSpan.FromMilliseconds(ms), callback);

            ml.RemoveTimeout(token);

            // BUGBUG: This should probably fault?
            ml.RemoveTimeout(token);
        }
        public void AddTimer_Run_Called()
        {
            var ml = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));
            var ms = 100;

            var callbackCount = 0;
            Func <MainLoop, bool> callback = (MainLoop loop) => {
                callbackCount++;
                ml.Stop();
                return(true);
            };

            var token = ml.AddTimeout(TimeSpan.FromMilliseconds(ms), callback);

            ml.Run();
            Assert.True(ml.RemoveTimeout(token));

            Assert.Equal(1, callbackCount);
        }