Exemplo n.º 1
0
        public async Task WatchdogFailureTestAsync()
        {
            var wd = new Watchdog(petTimeout: TimeSpan.FromMilliseconds(100), autoStartOnFirstPet: false);

            Should.Throw <WatchdogNotStartedException>(() => wd.Cancel());
            Should.Throw <WatchdogNotStartedException>(() => wd.Pet(autoReset: false));

            wd.Monitor();
            wd.IsTriggered.ShouldBe(false);
            wd.IsCanceled.ShouldBe(false);
            wd.IsDisposed.ShouldBe(false);
            wd.IsMonitorStarted.ShouldBe(true);
            Should.Throw <WatchdogAlreadyStartedException>(() => wd.Monitor());

            await 200.TryDelay(CancellationToken.None);
            wd.IsTriggered.ShouldBe(true);
            wd.IsCanceled.ShouldBe(false);
            wd.IsDisposed.ShouldBe(false);
            wd.IsMonitorStarted.ShouldBe(true);

            Should.Throw <WatchdogTriggeredException>(() => wd.Monitor());
            Should.Throw <WatchdogTriggeredException>(() => wd.Cancel());

            wd.AsTask().Result.ShouldBe(true);  // Triggered

            wd.TryDispose();
            wd.IsTriggered.ShouldBe(true);
            wd.IsCanceled.ShouldBe(false);
            wd.IsDisposed.ShouldBe(true);
            wd.IsMonitorStarted.ShouldBe(true);
            Should.Throw <WatchdogDisposedException>(() => wd.Cancel());
            Should.Throw <WatchdogDisposedException>(() => wd.Pet(autoReset: false));
            Should.Throw <WatchdogDisposedException>(() => wd.Monitor());
            Should.Throw <WatchdogDisposedException>(() => throw wd.AsTask().Exception !);
        }
Exemplo n.º 2
0
        public void WatchdogCancelBeforeTriggeredTest()
        {
            var wd     = new Watchdog(petTimeout: TimeSpan.FromMilliseconds(100), autoStartOnFirstPet: false);
            var wdTask = wd.AsTask();

            wd.Monitor();
            wd.Cancel();
            wd.IsTriggered.ShouldBe(false);
            wd.IsCanceled.ShouldBe(true);
            wd.IsDisposed.ShouldBe(false);
            wd.IsMonitorStarted.ShouldBe(true);
            Should.Throw <WatchdogCanceledException>(() => throw wdTask.Exception !);
            Should.Throw <WatchdogCanceledException>(() => throw wd.AsTask().Exception !);
        }
Exemplo n.º 3
0
        public void WatchdogCancelAsTaskTest()
        {
            var wd = new Watchdog(petTimeout: TimeSpan.FromMilliseconds(100), autoStartOnFirstPet: false);

            wd.Monitor();
            wd.Cancel();
            Should.Throw <WatchdogCanceledException>(() => throw wd.AsTask().Exception !);
        }
Exemplo n.º 4
0
        public async Task WatchdogPetTestAsync()
        {
            var wd     = new Watchdog(petTimeout: TimeSpan.FromMilliseconds(100), autoStartOnFirstPet: true);
            var wdTask = wd.AsTask();

            wd.Pet(autoReset: false);
            wd.IsTriggered.ShouldBe(false);
            wd.IsCanceled.ShouldBe(false);
            wd.IsDisposed.ShouldBe(false);
            wd.IsMonitorStarted.ShouldBe(true);
            wdTask.IsCompleted.ShouldBe(false);
            wd.AsTask().IsCompleted.ShouldBe(false);

            await 20.TryDelay(CancellationToken.None);
            var ms = wd.Pet(autoReset: false);

            wd.IsTriggered.ShouldBe(false);
            ms.ShouldBeInRange(TimeSpan.FromMilliseconds(15), TimeSpan.FromMilliseconds(50));

            await 20.TryDelay(CancellationToken.None);
            ms = wd.Pet(autoReset: false);
            wd.IsTriggered.ShouldBe(false);
            ms.ShouldBeInRange(TimeSpan.FromMilliseconds(15), TimeSpan.FromMilliseconds(50));

            await 20.TryDelay(CancellationToken.None);
            ms = wd.Pet(autoReset: false);
            wd.IsTriggered.ShouldBe(false);
            ms.ShouldBeInRange(TimeSpan.FromMilliseconds(15), TimeSpan.FromMilliseconds(50));

            await 20.TryDelay(CancellationToken.None);
            ms = wd.Pet(autoReset: false);
            wd.IsTriggered.ShouldBe(false);
            ms.ShouldBeInRange(TimeSpan.FromMilliseconds(15), TimeSpan.FromMilliseconds(50));

            await 20.TryDelay(CancellationToken.None);
            ms = wd.Pet(autoReset: false);
            wd.IsTriggered.ShouldBe(false);
            ms.ShouldBeInRange(TimeSpan.FromMilliseconds(15), TimeSpan.FromMilliseconds(50));

            await 20.TryDelay(CancellationToken.None);
            ms = wd.Pet(autoReset: false);
            wd.IsTriggered.ShouldBe(false);
            ms.ShouldBeInRange(TimeSpan.FromMilliseconds(15), TimeSpan.FromMilliseconds(50));

            await 20.TryDelay(CancellationToken.None);
            ms = wd.Pet(autoReset: false);
            wd.IsTriggered.ShouldBe(false);
            ms.ShouldBeInRange(TimeSpan.FromMilliseconds(15), TimeSpan.FromMilliseconds(50));

            await 120.TryDelay(CancellationToken.None);
            wd.IsTriggered.ShouldBe(true);
            wd.IsCanceled.ShouldBe(false);
            wd.IsDisposed.ShouldBe(false);
            wd.IsMonitorStarted.ShouldBe(true);
            wdTask.IsCompleted.ShouldBe(true);
            wd.AsTask().IsCompleted.ShouldBe(true);
            Should.Throw <WatchdogTriggeredException>(() => wd.Pet(autoReset: false));

            // Test auto reset
            //
            ms = wd.Pet(autoReset: true);
            ms.ShouldBeInRange(TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(10));
            wd.IsTriggered.ShouldBe(false);
            wd.IsCanceled.ShouldBe(false);
            wd.IsDisposed.ShouldBe(false);
            wd.IsMonitorStarted.ShouldBe(true);
            wd.AsTask().IsCompleted.ShouldBe(false);
        }