Ejemplo n.º 1
0
        public void AddIdle_Twice_Returns_False_Called_Twice()
        {
            var ml = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));

            var         functionCalled = 0;
            Func <bool> fn1            = () => {
                functionCalled++;
                return(false);
            };

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

            ml.AddIdle(fnStop);
            ml.AddIdle(fn1);
            ml.AddIdle(fn1);
            ml.Run();
            Assert.True(ml.RemoveIdle(fnStop));
            Assert.False(ml.RemoveIdle(fn1));
            Assert.False(ml.RemoveIdle(fn1));

            Assert.Equal(2, functionCalled);
        }
Ejemplo n.º 2
0
        public void False_Idle_Stops_It_Being_Called_Again()
        {
            var ml = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));

            var         functionCalled = 0;
            Func <bool> fn1            = () => {
                functionCalled++;
                if (functionCalled == 10)
                {
                    return(false);
                }
                return(true);
            };

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

            ml.AddIdle(fnStop);
            ml.AddIdle(fn1);
            ml.Run();
            Assert.True(ml.RemoveIdle(fnStop));
            Assert.False(ml.RemoveIdle(fn1));

            Assert.Equal(10, functionCalled);
            Assert.Equal(20, stopCount);
        }
Ejemplo n.º 3
0
        public void AddTwice_Function_CalledTwice()
        {
            var ml = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));

            var         functionCalled = 0;
            Func <bool> fn             = () => {
                functionCalled++;
                return(true);
            };

            ml.AddIdle(fn);
            ml.AddIdle(fn);
            ml.MainIteration();
            Assert.Equal(2, functionCalled);

            functionCalled = 0;
            Assert.True(ml.RemoveIdle(fn));
            ml.MainIteration();
            Assert.Equal(1, functionCalled);

            functionCalled = 0;
            Assert.True(ml.RemoveIdle(fn));
            ml.MainIteration();
            Assert.Equal(0, functionCalled);
            Assert.False(ml.RemoveIdle(fn));
        }
Ejemplo n.º 4
0
        public void AddIdle_Adds_And_Removes()
        {
            var ml = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));

            Func <bool> fnTrue  = () => { return(true); };
            Func <bool> fnFalse = () => { return(false); };

            ml.AddIdle(fnTrue);
            ml.AddIdle(fnFalse);

            ml.RemoveIdle(fnTrue);

            // BUGBUG: This doens't throw or indicate an error. Ideally RemoveIdle would either
            // trhow an exception in this case, or return an error.
            ml.RemoveIdle(fnTrue);

            ml.RemoveIdle(fnFalse);

            // BUGBUG: This doesn't throw an exception or indicate an error. Ideally RemoveIdle would either
            // trhow an exception in this case, or return an error.
            ml.RemoveIdle(fnFalse);

            // Add again, but with dupe
            ml.AddIdle(fnTrue);
            ml.AddIdle(fnTrue);

            ml.RemoveIdle(fnTrue);
            ml.RemoveIdle(fnTrue);

            // BUGBUG: This doesn't throw an exception or indicate an error. Ideally RemoveIdle would either
            // trhow an exception in this case, or return an error.
            ml.RemoveIdle(fnTrue);
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        public void AddIdle_Function_GetsCalled_OnIteration()
        {
            var ml = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));

            var         functionCalled = 0;
            Func <bool> fn             = () => {
                functionCalled++;
                return(true);
            };

            ml.AddIdle(fn);
            ml.MainIteration();
            Assert.Equal(1, functionCalled);
        }
Ejemplo n.º 8
0
        public void Run_Runs_Idle_Stop_Stops_Idle()
        {
            var ml = new MainLoop(new FakeMainLoop(() => FakeConsole.ReadKey(true)));

            var         functionCalled = 0;
            Func <bool> fn             = () => {
                functionCalled++;
                if (functionCalled == 10)
                {
                    ml.Stop();
                }
                return(true);
            };

            ml.AddIdle(fn);
            ml.Run();
            Assert.True(ml.RemoveIdle(fn));

            Assert.Equal(10, functionCalled);
        }