예제 #1
0
        public void TestNotifyEachWithTimespan()
        {
            MockTimeSource mockTimeSource = new MockTimeSource(new DateTime(2010, 1, 1));

            using (TestSubscriber subscriber = new TestSubscriber()) {
                using (Scheduler scheduler = new Scheduler(mockTimeSource)) {
                    scheduler.NotifyEach(
                        TimeSpan.FromHours(12), TimeSpan.FromHours(1), subscriber.Callback
                        );

                    mockTimeSource.AdvanceTime(TimeSpan.FromHours(14));

                    // Wait for 3 invocations of the callback. We might not catch each trigger
                    // of the AutoResetEvent, but we know that it will be 3 at most.
                    for (int invocation = 0; invocation < 3; ++invocation)
                    {
                        Assert.IsTrue(subscriber.WaitForCallback(1000));

                        if (subscriber.CallbackCount == 3)
                        {
                            break;
                        }
                    }
                }
            }
        }
예제 #2
0
        public void TestCancelNotification()
        {
            MockTimeSource mockTimeSource = new MockTimeSource(new DateTime(2010, 1, 1));

            using (TestSubscriber subscriber1 = new TestSubscriber()) {
                using (TestSubscriber subscriber2 = new TestSubscriber()) {
                    using (Scheduler scheduler = new Scheduler(mockTimeSource)) {
                        object handle = scheduler.NotifyIn(
                            TimeSpan.FromHours(24), subscriber1.Callback
                            );
                        scheduler.NotifyIn(TimeSpan.FromHours(36), subscriber2.Callback);

                        mockTimeSource.AdvanceTime(TimeSpan.FromHours(12));
                        scheduler.Cancel(handle);
                        mockTimeSource.AdvanceTime(TimeSpan.FromHours(24));

                        // Wait for the second subscriber to be notified. This is still a race
                        // condition (there's no guarantee the thread pool will run tasks in
                        // the order they were added), but it's the best we can do without
                        // relying on an ugly Thread.Sleep() in this test.
                        Assert.IsTrue(subscriber2.WaitForCallback(1000));
                        Assert.AreEqual(0, subscriber1.CallbackCount);
                    }
                }
            }
        }
예제 #3
0
        public void TestNotifyAt()
        {
            MockTimeSource mockTimeSource = new MockTimeSource(new DateTime(2010, 1, 1));

            using (TestSubscriber subscriber = new TestSubscriber()) {
                using (Scheduler scheduler = new Scheduler(mockTimeSource)) {
                    scheduler.NotifyAt(makeUtc(new DateTime(2010, 1, 2)), subscriber.Callback);

                    mockTimeSource.AdvanceTime(TimeSpan.FromDays(1));

                    Assert.IsTrue(subscriber.WaitForCallback(1000));
                }
            }
        }
예제 #4
0
        public void TestNotifyAtWithDateTimeAdjustment()
        {
            MockTimeSource mockTimeSource = new MockTimeSource(new DateTime(2010, 1, 1));

            using (TestSubscriber subscriber = new TestSubscriber()) {
                using (Scheduler scheduler = new Scheduler(mockTimeSource)) {
                    scheduler.NotifyAt(makeUtc(new DateTime(2010, 1, 2)), subscriber.Callback);

                    // Let 12 hours pass, after that, we simulate a time synchronization
                    // that puts the system 12 hours ahead of the original time.
                    mockTimeSource.AdvanceTime(TimeSpan.FromHours(12));
                    mockTimeSource.AdjustTime(new DateTime(2010, 1, 2));

                    Assert.IsTrue(subscriber.WaitForCallback(1000));
                }
            }
        }
예제 #5
0
        public void TestTwoNotificationsAtSameTime()
        {
            MockTimeSource mockTimeSource = new MockTimeSource(new DateTime(2010, 1, 1));

            using (TestSubscriber subscriber1 = new TestSubscriber()) {
                using (TestSubscriber subscriber2 = new TestSubscriber()) {
                    using (Scheduler scheduler = new Scheduler(mockTimeSource)) {
                        scheduler.NotifyIn(60000, subscriber1.Callback);
                        scheduler.NotifyIn(60000, subscriber2.Callback);

                        mockTimeSource.AdvanceTime(TimeSpan.FromMilliseconds(60000));

                        Assert.IsTrue(subscriber1.WaitForCallback(1000));
                        Assert.IsTrue(subscriber2.WaitForCallback(1000));
                    }
                }
            }
        }
예제 #6
0
        public void TestInverseOrderNotification()
        {
            MockTimeSource mockTimeSource = new MockTimeSource(new DateTime(2010, 1, 1));

            using (TestSubscriber subscriber1 = new TestSubscriber()) {
                using (TestSubscriber subscriber2 = new TestSubscriber()) {
                    using (Scheduler scheduler = new Scheduler(mockTimeSource)) {
                        scheduler.NotifyIn(TimeSpan.FromHours(24), subscriber1.Callback);
                        scheduler.NotifyIn(TimeSpan.FromHours(12), subscriber2.Callback);

                        mockTimeSource.AdvanceTime(TimeSpan.FromHours(18));

                        Assert.IsTrue(subscriber2.WaitForCallback(1000));
                        Assert.AreEqual(0, subscriber1.CallbackCount);

                        mockTimeSource.AdvanceTime(TimeSpan.FromHours(18));

                        Assert.IsTrue(subscriber1.WaitForCallback(1000));
                    }
                }
            }
        }
예제 #7
0
    public void TestNotifyEachWithTimespan() {
      MockTimeSource mockTimeSource = new MockTimeSource(new DateTime(2010, 1, 1));
      using(TestSubscriber subscriber = new TestSubscriber()) {
        using(Scheduler scheduler = new Scheduler(mockTimeSource)) {
          scheduler.NotifyEach(
            TimeSpan.FromHours(12), TimeSpan.FromHours(1), subscriber.Callback
          );

          mockTimeSource.AdvanceTime(TimeSpan.FromHours(14));

          // Wait for 3 invocations of the callback. We might not catch each trigger
          // of the AutoResetEvent, but we know that it will be 3 at most. 
          for(int invocation = 0; invocation < 3; ++invocation) {
            Assert.IsTrue(subscriber.WaitForCallback(1000));

            if(subscriber.CallbackCount == 3) {
              break;
            }
          }
        }
      }
    }
예제 #8
0
    public void TestTwoNotificationsAtSameTime() {
      MockTimeSource mockTimeSource = new MockTimeSource(new DateTime(2010, 1, 1));
      using(TestSubscriber subscriber1 = new TestSubscriber()) {
        using(TestSubscriber subscriber2 = new TestSubscriber()) {
          using(Scheduler scheduler = new Scheduler(mockTimeSource)) {
            scheduler.NotifyIn(60000, subscriber1.Callback);
            scheduler.NotifyIn(60000, subscriber2.Callback);

            mockTimeSource.AdvanceTime(TimeSpan.FromMilliseconds(60000));

            Assert.IsTrue(subscriber1.WaitForCallback(1000));
            Assert.IsTrue(subscriber2.WaitForCallback(1000));
          }
        }
      }
    }
예제 #9
0
    public void TestInverseOrderNotification() {
      MockTimeSource mockTimeSource = new MockTimeSource(new DateTime(2010, 1, 1));
      using(TestSubscriber subscriber1 = new TestSubscriber()) {
        using(TestSubscriber subscriber2 = new TestSubscriber()) {
          using(Scheduler scheduler = new Scheduler(mockTimeSource)) {
            scheduler.NotifyIn(TimeSpan.FromHours(24), subscriber1.Callback);
            scheduler.NotifyIn(TimeSpan.FromHours(12), subscriber2.Callback);

            mockTimeSource.AdvanceTime(TimeSpan.FromHours(18));

            Assert.IsTrue(subscriber2.WaitForCallback(1000));
            Assert.AreEqual(0, subscriber1.CallbackCount);

            mockTimeSource.AdvanceTime(TimeSpan.FromHours(18));

            Assert.IsTrue(subscriber1.WaitForCallback(1000));
          }
        }
      }
    }
예제 #10
0
    public void TestCancelNotification() {
      MockTimeSource mockTimeSource = new MockTimeSource(new DateTime(2010, 1, 1));
      using(TestSubscriber subscriber1 = new TestSubscriber()) {
        using(TestSubscriber subscriber2 = new TestSubscriber()) {
          using(Scheduler scheduler = new Scheduler(mockTimeSource)) {
            object handle = scheduler.NotifyIn(
              TimeSpan.FromHours(24), subscriber1.Callback
            );
            scheduler.NotifyIn(TimeSpan.FromHours(36), subscriber2.Callback);

            mockTimeSource.AdvanceTime(TimeSpan.FromHours(12));
            scheduler.Cancel(handle);
            mockTimeSource.AdvanceTime(TimeSpan.FromHours(24));

            // Wait for the second subscriber to be notified. This is still a race
            // condition (there's no guarantee the thread pool will run tasks in
            // the order they were added), but it's the best we can do without
            // relying on an ugly Thread.Sleep() in this test.
            Assert.IsTrue(subscriber2.WaitForCallback(1000));
            Assert.AreEqual(0, subscriber1.CallbackCount);
          }
        }
      }
    }
예제 #11
0
    public void TestNotifyAtWithDateTimeAdjustment() {
      MockTimeSource mockTimeSource = new MockTimeSource(new DateTime(2010, 1, 1));
      using(TestSubscriber subscriber = new TestSubscriber()) {
        using(Scheduler scheduler = new Scheduler(mockTimeSource)) {
          scheduler.NotifyAt(makeUtc(new DateTime(2010, 1, 2)), subscriber.Callback);

          // Let 12 hours pass, after that, we simulate a time synchronization
          // that puts the system 12 hours ahead of the original time.
          mockTimeSource.AdvanceTime(TimeSpan.FromHours(12));
          mockTimeSource.AdjustTime(new DateTime(2010, 1, 2));

          Assert.IsTrue(subscriber.WaitForCallback(1000));
        }
      }
    }
예제 #12
0
    public void TestNotifyAt() {
      MockTimeSource mockTimeSource = new MockTimeSource(new DateTime(2010, 1, 1));
      using(TestSubscriber subscriber = new TestSubscriber()) {
        using(Scheduler scheduler = new Scheduler(mockTimeSource)) {
          scheduler.NotifyAt(makeUtc(new DateTime(2010, 1, 2)), subscriber.Callback);

          mockTimeSource.AdvanceTime(TimeSpan.FromDays(1));

          Assert.IsTrue(subscriber.WaitForCallback(1000));
        }
      }
    }