Example #1
0
        public void Dispose_IfAlreadyDisposed_DoesNotThrow()
        {
            // Arrange
            ITaskSeriesCommand command = CreateDummyCommand();
            ITaskSeriesTimer   product = CreateProductUnderTest(command);

            product.Dispose();

            // Act & Assert
            ExceptionAssert.DoesNotThrow(() => product.Dispose());
        }
Example #2
0
        public void Dispose_IfStarted_DoesNotWaitForExecuteToFinish()
        {
            // Arrange
            using (EventWaitHandle executeStarted = new ManualResetEvent(initialState: false))
                using (EventWaitHandle stopExecute = new ManualResetEvent(initialState: false))
                {
                    bool waitedForCommandToFinish = false;

                    ITaskSeriesCommand command = CreateCommand(() =>
                    {
                        Assert.True(executeStarted.Set()); // Guard
                        stopExecute.WaitOne(1000);
                        waitedForCommandToFinish = true;
                        return(new TaskSeriesCommandResult(wait: Task.Delay(0)));
                    });

                    using (ITaskSeriesTimer product = CreateProductUnderTest(command))
                    {
                        product.Start();
                        Assert.True(executeStarted.WaitOne(1000)); // Guard

                        // Act & Assert
                        product.Dispose();

                        // Assert
                        Assert.False(waitedForCommandToFinish);

                        // Cleanup
                        Assert.True(stopExecute.Set()); // Guard
                    }
                }
        }
Example #3
0
        public void Dispose_TriggersCommandCancellationToken()
        {
            // Arrange
            TimeSpan interval = TimeSpan.Zero;

            using (EventWaitHandle executeStarted = new ManualResetEvent(initialState: false))
                using (EventWaitHandle executeFinished = new ManualResetEvent(initialState: false))
                {
                    bool cancellationTokenSignalled = false;

                    ITaskSeriesCommand command = CreateCommand((cancellationToken) =>
                    {
                        Assert.True(executeStarted.Set());  // Guard
                        cancellationTokenSignalled = cancellationToken.WaitHandle.WaitOne(1000);
                        Assert.True(executeFinished.Set()); // Guard
                        return(new TaskSeriesCommandResult(wait: Task.Delay(0)));
                    });

                    using (ITaskSeriesTimer product = CreateProductUnderTest(command))
                    {
                        product.Start();
                        Assert.True(executeStarted.WaitOne(1000)); // Guard

                        // Act
                        product.Dispose();

                        // Assert
                        Assert.True(executeFinished.WaitOne(1000)); // Guard
                        Assert.True(cancellationTokenSignalled);
                    }
                }
        }
 public void Dispose()
 {
     if (!_disposed)
     {
         _timer.Dispose();
         _disposed = true;
     }
 }
Example #5
0
 public void Dispose()
 {
     if (!_disposed)
     {
         _timer.Dispose();
         _shutdownCancellationTokenSource.Dispose();
         _disposed = true;
     }
 }
Example #6
0
        public void Cancel_IfDisposed_Throws()
        {
            // Arrange
            ITaskSeriesCommand command = CreateDummyCommand();
            ITaskSeriesTimer   product = CreateProductUnderTest(command);

            product.Dispose();

            // Act & Assert
            ExceptionAssert.ThrowsObjectDisposed(() => product.Cancel());
        }
Example #7
0
        public void Dispose_IfStopped_DoesNotThrow()
        {
            // Arrange
            ITaskSeriesCommand command = CreateStubCommand(TimeSpan.Zero);
            ITaskSeriesTimer   product = CreateProductUnderTest(command);

            product.Start();
            product.StopAsync(CancellationToken.None).GetAwaiter().GetResult();

            // Act & Assert
            ExceptionAssert.DoesNotThrow(() => product.Dispose());
        }
Example #8
0
        public void StopAsync_IfDisposed_Throws()
        {
            // Arrange
            ITaskSeriesCommand command = CreateDummyCommand();
            ITaskSeriesTimer   product = CreateProductUnderTest(command);

            product.Dispose();

            CancellationToken cancellationToken = CancellationToken.None;

            // Act & Assert
            ExceptionAssert.ThrowsObjectDisposed(() => product.StopAsync(cancellationToken));
        }