Пример #1
0
        public void ShouldCancelActionByIdAfterRemove()
        {
            VirtualClock clock = new VirtualClock();

            using (TimerGroup timers = new TimerGroup()
            {
                Create = clock.CreateAction
            })
            {
                int  invokeCount1 = 0;
                int  invokeCount2 = 0;
                Guid id1          = timers.Add(TimeSpan.FromSeconds(1.0d), () => ++ invokeCount1);
                Guid id2          = timers.Add(TimeSpan.FromSeconds(1.5d), () => ++ invokeCount2);

                Assert.AreNotEqual(id1, id2);
                Assert.AreEqual(0, invokeCount1);
                Assert.AreEqual(0, invokeCount2);

                clock.Sleep(TimeSpan.FromSeconds(1.1d));

                Assert.AreEqual(1, invokeCount1);
                Assert.AreEqual(0, invokeCount2);

                clock.Sleep(TimeSpan.FromSeconds(1.0d));

                Assert.AreEqual(2, invokeCount1);
                Assert.AreEqual(1, invokeCount2);

                timers.Remove(id1);

                clock.Sleep(TimeSpan.FromSeconds(1.0d));

                Assert.AreEqual(2, invokeCount1);
                Assert.AreEqual(2, invokeCount2);
            }
        }