Example #1
0
        public async Task UpdateStatusAsync()
        {
            int updateCalled = 0;
            var testResult   = 100;
            var testResponse = new MockResponse(200);

            var operation = new TestOperation <int>("operation-id", TimeSpan.FromMilliseconds(10), testResult, testResponse)
            {
                UpdateCalled = () => { updateCalled++; }
            };

            while (!operation.HasValue)
            {
                Response updateResponse = await operation.UpdateStatusAsync();

                await Task.Delay(1);
            }

            Assert.Greater(updateCalled, 0);
            Assert.IsTrue(operation.HasCompleted);
            Assert.IsTrue(operation.HasValue);

            Assert.AreEqual(testResult, operation.Value);
            Assert.AreEqual(testResponse, operation.GetRawResponse());
        }
        public void UpdateStatusWhenOperationFails(
            [Values(true, false)] bool async,
            [Values(true, false)] bool useDefaultException)
        {
            RequestFailedException originalException = new RequestFailedException("");
            MockResponse           mockResponse      = new MockResponse(200);
            TestOperation          testOperation     = new TestOperation()
            {
                OnUpdateState = _ => useDefaultException
                    ? OperationState <int> .Failure(mockResponse)
                    : OperationState <int> .Failure(mockResponse, originalException)
            };
            MockOperationInternal <int> operationInternal = testOperation.MockOperationInternal;

            RequestFailedException thrownException = async
                ? Assert.ThrowsAsync <RequestFailedException>(async() => await operationInternal.UpdateStatusAsync(CancellationToken.None))
                : Assert.Throws <RequestFailedException>(() => operationInternal.UpdateStatus(CancellationToken.None));

            if (!useDefaultException)
            {
                Assert.AreEqual(originalException, thrownException);
            }

            Assert.AreEqual(mockResponse, operationInternal.RawResponse);
            Assert.True(operationInternal.HasCompleted);
            Assert.False(operationInternal.HasValue);

            RequestFailedException valueException = Assert.Throws <RequestFailedException>(() => _ = operationInternal.Value);

            Assert.AreEqual(thrownException, valueException);
        }
Example #3
0
        public void OperationId()
        {
            string testId    = "operation-id";
            var    operation = new TestOperation <int>(testId, TimeSpan.Zero, 0, null);

            Assert.AreEqual(testId, operation.Id);
        }
        public async Task WaitForCompletionUsesRightPollingInterval(bool useDefaultPollingInterval)
        {
            TimeSpan expectedDelay = TimeSpan.FromMilliseconds(100);
            TimeSpan passedDelay   = default;

            int           callsCount    = 0;
            MockResponse  mockResponse  = new MockResponse(200);
            TestOperation testOperation = new TestOperation()
            {
                OnUpdateState = _ => ++ callsCount >= 2
                    ? OperationState <int> .Success(mockResponse, 50)
                    : OperationState <int> .Pending(mockResponse)
            };
            MockOperationInternal <int> operationInternal = testOperation.MockOperationInternal;

            operationInternal.OnWait = delay => passedDelay = delay;

            if (useDefaultPollingInterval)
            {
                operationInternal.DefaultPollingInterval = expectedDelay;
                await operationInternal.WaitForCompletionAsync(CancellationToken.None);
            }
            else
            {
                await operationInternal.WaitForCompletionAsync(expectedDelay, CancellationToken.None);
            }

            Assert.AreEqual(expectedDelay, passedDelay);
        }
        public async Task WaitForCompletionCallsUntilOperationCompletes(bool useDefaultPollingInterval)
        {
            int           callsCount    = 0;
            int           expectedCalls = 5;
            int           expectedValue = 50;
            MockResponse  mockResponse  = new MockResponse(200);
            TestOperation testOperation = new TestOperation()
            {
                OnUpdateState = _ => ++ callsCount >= expectedCalls
                    ? OperationState <int> .Success(mockResponse, expectedValue)
                    : OperationState <int> .Pending(mockResponse)
            };
            MockOperationInternal <int> operationInternal = testOperation.MockOperationInternal;

            operationInternal.DefaultPollingInterval = TimeSpan.Zero;

            Response <int> operationResponse = useDefaultPollingInterval
                ? await operationInternal.WaitForCompletionAsync(CancellationToken.None)
                : await operationInternal.WaitForCompletionAsync(TimeSpan.Zero, CancellationToken.None);

            Assert.AreEqual(mockResponse, operationResponse.GetRawResponse());
            Assert.AreEqual(expectedCalls, callsCount);

            Assert.AreEqual(mockResponse, operationInternal.RawResponse);
            Assert.True(operationInternal.HasCompleted);
            Assert.True(operationInternal.HasValue);
            Assert.AreEqual(expectedValue, operationInternal.Value);
        }
        public async Task UpdateStatusSetsFailedScopeWhenOperationFails(bool async)
        {
            using ClientDiagnosticListener testListener = new ClientDiagnosticListener(DiagnosticNamespace);

            RequestFailedException originalException = new RequestFailedException("");
            MockResponse           mockResponse      = new MockResponse(200);
            TestOperation          testOperation     = new TestOperation()
            {
                OnUpdateState = _ => OperationState <int> .Failure(mockResponse, originalException)
            };
            MockOperationInternal <int> operationInternal = testOperation.MockOperationInternal;

            try
            {
                _ = async
                    ? await operationInternal.UpdateStatusAsync(CancellationToken.None)
                    : operationInternal.UpdateStatus(CancellationToken.None);
            }
            catch { }

            testListener.AssertScopeException($"{nameof(TestOperation)}.UpdateStatus", (Exception scopeException) =>
            {
                Assert.AreEqual(originalException, scopeException);
            });
        }
        public async Task UpdateStatusCreatesDiagnosticScope(
            [Values(true, false)] bool async,
            [Values(true, false)] bool useDefaultTypeName)
        {
            const string customTypeName = "CustomTypeName";

            using ClientDiagnosticListener testListener = new ClientDiagnosticListener(DiagnosticNamespace);

            string expectedTypeName = useDefaultTypeName ? nameof(TestOperation) : customTypeName;

            KeyValuePair <string, string>[] expectedAttributes = new KeyValuePair <string, string>[]
            {
                new KeyValuePair <string, string>("key1", "value1"),
                new KeyValuePair <string, string>("key2", "value2")
            };

            MockResponse  mockResponse  = new MockResponse(200);
            TestOperation testOperation = new TestOperation(operationTypeName: useDefaultTypeName ? null : customTypeName, scopeAttributes: expectedAttributes)
            {
                OnUpdateState = _ => OperationState <int> .Pending(mockResponse)
            };
            MockOperationInternal <int> operationInternal = testOperation.MockOperationInternal;

            _ = async
                ? await operationInternal.UpdateStatusAsync(CancellationToken.None)
                : operationInternal.UpdateStatus(CancellationToken.None);

            testListener.AssertScope($"{expectedTypeName}.UpdateStatus", expectedAttributes);
        }
        public void NotCompleted()
        {
            var operation = new TestOperation <int>(TimeSpan.FromMilliseconds(1000), 100, null);

            Assert.IsFalse(operation.HasCompleted);
            Assert.IsFalse(operation.HasValue);
            Assert.Throws <InvalidOperationException>(() =>
            {
                _ = operation.Value;
            });
        }
        public void DefaultPropertyInitialization()
        {
            TestOperation testOperation = new TestOperation();
            MockOperationInternal <int> operationInternal = testOperation.MockOperationInternal;

            Assert.AreEqual(TimeSpan.FromSeconds(1), operationInternal.DefaultPollingInterval);

            Assert.IsNull(operationInternal.RawResponse);
            Assert.False(operationInternal.HasCompleted);
            Assert.False(operationInternal.HasValue);
            Assert.Throws <InvalidOperationException>(() => _ = operationInternal.Value);
        }
        public async Task UpdateStatusWhenOperationIsPending(bool async)
        {
            MockResponse  mockResponse  = new MockResponse(200);
            TestOperation testOperation = new TestOperation()
            {
                OnUpdateState = _ => OperationState <int> .Pending(mockResponse)
            };
            MockOperationInternal <int> operationInternal = testOperation.MockOperationInternal;

            Response operationResponse = async
                ? await operationInternal.UpdateStatusAsync(CancellationToken.None)
                : operationInternal.UpdateStatus(CancellationToken.None);

            Assert.AreEqual(mockResponse, operationResponse);

            Assert.AreEqual(mockResponse, operationInternal.RawResponse);
            Assert.False(operationInternal.HasCompleted);
            Assert.False(operationInternal.HasValue);
            Assert.Throws <InvalidOperationException>(() => _ = operationInternal.Value);
        }
        public void WaitForCompletionPassesTheCancellationTokenToTaskDelay(bool useDefaultPollingInterval)
        {
            using CancellationTokenSource tokenSource = new CancellationTokenSource();
            CancellationToken cancellationToken = tokenSource.Token;

            tokenSource.Cancel();

            MockResponse  mockResponse  = new MockResponse(200);
            TestOperation testOperation = new TestOperation()
            {
                OnUpdateState = _ => OperationState <int> .Pending(mockResponse)
            };
            MockOperationInternal <int> operationInternal = testOperation.MockOperationInternal;

            operationInternal.DefaultPollingInterval = TimeSpan.Zero;

            _ = useDefaultPollingInterval
                ? Assert.ThrowsAsync <TaskCanceledException>(async() => await operationInternal.WaitForCompletionAsync(cancellationToken))
                : Assert.ThrowsAsync <TaskCanceledException>(async() => await operationInternal.WaitForCompletionAsync(TimeSpan.Zero, cancellationToken));
        }
        public async Task UpdateStatusWhenOperationSucceeds(bool async)
        {
            int           expectedValue = 50;
            MockResponse  mockResponse  = new MockResponse(200);
            TestOperation testOperation = new TestOperation()
            {
                OnUpdateState = _ => OperationState <int> .Success(mockResponse, expectedValue)
            };
            MockOperationInternal <int> operationInternal = testOperation.MockOperationInternal;

            Response operationResponse = async
                ? await operationInternal.UpdateStatusAsync(CancellationToken.None)
                : operationInternal.UpdateStatus(CancellationToken.None);

            Assert.AreEqual(mockResponse, operationResponse);

            Assert.AreEqual(mockResponse, operationInternal.RawResponse);
            Assert.True(operationInternal.HasCompleted);
            Assert.True(operationInternal.HasValue);
            Assert.AreEqual(expectedValue, operationInternal.Value);
        }
        public void Cancellation()
        {
            var cancel = new CancellationTokenSource();

            cancel.CancelAfter(100);

            int updateCalled = 0;

            var operation = new TestOperation <int>(TimeSpan.FromMilliseconds(1000), 100, null);

            operation.PollingInterval = TimeSpan.FromMilliseconds(10);
            operation.UpdateCalled    = () => { updateCalled++; };

            Assert.That(async() =>
            {
                _ = await operation.WaitCompletionAsync(cancel.Token);
            }, Throws.InstanceOf <OperationCanceledException>());

            Assert.IsTrue(cancel.IsCancellationRequested);
            Assert.Greater(updateCalled, 0);
            Assert.IsFalse(operation.HasValue);
        }
        public void UpdateStatus()
        {
            int updateCalled = 0;
            var testResult   = 10;
            var testResponse = new MockResponse(200);

            var operation = new TestOperation <int>(TimeSpan.FromMilliseconds(10), testResult, testResponse);

            operation.UpdateCalled = () => { updateCalled++; };

            while (!operation.HasValue)
            {
                Response updateResponse = operation.UpdateStatus();
                Thread.Sleep(1);
            }

            Assert.Greater(updateCalled, 0);
            Assert.IsTrue(operation.HasCompleted);
            Assert.IsTrue(operation.HasValue);

            Assert.AreEqual(testResult, operation.Value);
            Assert.AreEqual(testResponse, operation.GetRawResponse());
        }
        public async Task WaitCompletionAsync()
        {
            int updateCalled = 0;
            var testResult   = 100;
            var testResponse = new MockResponse(200);

            var operation = new TestOperation <int>(TimeSpan.FromMilliseconds(10), testResult, testResponse);

            operation.PollingInterval = TimeSpan.FromMilliseconds(1);
            operation.UpdateCalled    = () => { updateCalled++; };

            Response <int> operationResult = await operation.WaitCompletionAsync();

            Assert.Greater(updateCalled, 0);
            Assert.IsTrue(operation.HasCompleted);
            Assert.IsTrue(operation.HasValue);

            Assert.AreEqual(testResult, operationResult.Value);
            Assert.AreEqual(testResponse, operationResult.GetRawResponse());

            Assert.AreEqual(testResult, operation.Value);
            Assert.AreEqual(testResponse, operation.GetRawResponse());
        }
        public async Task UpdateStatusPassesTheCancellationTokenToUpdateState(bool async)
        {
            using CancellationTokenSource tokenSource = new CancellationTokenSource();
            CancellationToken originalToken = tokenSource.Token;
            CancellationToken passedToken   = default;

            MockResponse  mockResponse  = new MockResponse(200);
            TestOperation testOperation = new TestOperation()
            {
                OnUpdateState = cancellationToken =>
                {
                    passedToken = cancellationToken;
                    return(OperationState <int> .Pending(mockResponse));
                }
            };
            MockOperationInternal <int> operationInternal = testOperation.MockOperationInternal;

            _ = async
                ? await operationInternal.UpdateStatusAsync(originalToken)
                : operationInternal.UpdateStatus(originalToken);

            Assert.AreEqual(originalToken, passedToken);
        }