예제 #1
0
 public static async Task ExecuteTaskAsync(this AsyncMockService asyncMock)
 {
     Console.WriteLine(nameof(ExecuteTaskAsync));
     Console.WriteLine("Result {0}", await asyncMock.LongRunningOperation(100).ConfigureAwait(false));
     Console.WriteLine("Press enter to continue");
     Console.ReadLine();
 }
예제 #2
0
    public static async Task ExecuteManuallyCancellableTaskAsync(this AsyncMockService asyncMock)
    {
        Console.WriteLine(nameof(ExecuteManuallyCancellableTaskAsync));
        using var cancellationTokenSource = new CancellationTokenSource();
        // Creating a task to listen to keyboard key press
        var keyBoardTask = Task.Run(() =>
        {
            Console.WriteLine("Press enter to cancel");
            Console.ReadKey();
            // Cancel the task
            cancellationTokenSource.Cancel();
        });

        try
        {
            var longRunningTask = asyncMock.LongRunningCancellableOperation(500, cancellationTokenSource.Token).ConfigureAwait(false);
            var result          = await longRunningTask;
            Console.WriteLine("Result {0}", result);
            Console.WriteLine("Press enter to continue");
        }
        catch (TaskCanceledException)
        {
            Console.WriteLine("Task was cancelled");
        }
        await keyBoardTask;
    }
예제 #3
0
    public static async Task CancelANonCancellableTaskAsync(this AsyncMockService asyncMock)
    {
        Console.WriteLine(nameof(CancelANonCancellableTaskAsync));
        using var cancellationTokenSource = new CancellationTokenSource();
        // Listening to key press to cancel
        var keyBoardTask = Task.Run(() =>
        {
            Console.WriteLine("Press enter to cancel");
            Console.ReadKey();
            // Sending the cancellation message
            cancellationTokenSource.Cancel();
        });

        try
        {
            // Running the long running task
            var longRunningTask = asyncMock.LongRunningOperationWithCancellationTokenAsync(100,
                                                                                           cancellationTokenSource.Token).ConfigureAwait(false);
            var result = await longRunningTask;
            Console.WriteLine("Result {0}", result);
            Console.WriteLine("Press enter to continue");
        }
        catch (TaskCanceledException)
        {
            Console.WriteLine("Task was cancelled");
        }
        await keyBoardTask;
    }
예제 #4
0
    public async Task LongRunningOperation_StateUnderTest_ExpectedBehavior()
    {
        // Arrange
        var asyncMock = new AsyncMockService();
        int loop      = 10;

        // Act
        var result = await asyncMock.LongRunningOperation(loop);

        // Assert
        Assert.IsNotNull(result);
        Assert.AreEqual(result, 45);
    }
예제 #5
0
    public async Task LongRunningCancellableOperation_StateUnderTest_ExpectedBehavior()
    {
        // Arrange
        var asyncMock = new AsyncMockService();
        int loop      = 10;
        CancellationToken cancellationToken = default(global::System.Threading.CancellationToken);

        // Act
        var result = await asyncMock.LongRunningCancellableOperation(
            loop,
            cancellationToken);

        // Assert
        Assert.IsNotNull(result);
        Assert.AreEqual(result, 45);
    }
예제 #6
0
    public static async Task ExecuteTaskWithTimeoutAsync(this AsyncMockService asyncMock, TimeSpan timeSpan)
    {
        Console.WriteLine(nameof(ExecuteTaskWithTimeoutAsync));
        using (var cancellationTokenSource = new CancellationTokenSource(timeSpan))
        {
            try
            {
                var result = await asyncMock.LongRunningCancellableOperation(500, cancellationTokenSource.Token).ConfigureAwait(false);

                Console.WriteLine("Result {0}", result);
            }
            catch (TaskCanceledException)
            {
                Console.WriteLine("Task was cancelled");
            }
        }
        Console.WriteLine("Press enter to continue");
        Console.ReadLine();
    }