public async Task WaitForCompletionPassesTheCancellationTokenToUpdateState(bool useDefaultPollingInterval) { 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> .Success(mockResponse, default)); } }; MockOperationInternal <int> operationInternal = testOperation.MockOperationInternal; operationInternal.DefaultPollingInterval = TimeSpan.Zero; _ = useDefaultPollingInterval ? await operationInternal.WaitForCompletionAsync(originalToken) : await operationInternal.WaitForCompletionAsync(TimeSpan.Zero, originalToken); Assert.AreEqual(originalToken, passedToken); }
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 WaitForCompletionUsesRetryAfterMsHeader( [Values(true, false)] bool useDefaultPollingInterval, [Values("retry-after-ms", "x-ms-retry-after-ms")] string headerName) { TimeSpan originalDelay = TimeSpan.FromMilliseconds(500); TimeSpan shortDelay = originalDelay.Subtract(TimeSpan.FromMilliseconds(250)); TimeSpan longDelay = originalDelay.Add(TimeSpan.FromMilliseconds(250)); List <TimeSpan> passedDelays = new List <TimeSpan>(); MockResponse shortDelayMockResponse = new MockResponse(200); shortDelayMockResponse.AddHeader(new HttpHeader(headerName, shortDelay.Milliseconds.ToString())); MockResponse longDelayMockResponse = new MockResponse(200); longDelayMockResponse.AddHeader(new HttpHeader(headerName, longDelay.Milliseconds.ToString())); int callsCount = 0; TestOperation testOperation = new TestOperation() { OnUpdateState = _ => ++ callsCount switch { 1 => OperationState <int> .Pending(shortDelayMockResponse), 2 => OperationState <int> .Pending(longDelayMockResponse), _ => OperationState <int> .Success(new MockResponse(200), 50) } }; MockOperationInternal <int> operationInternal = testOperation.MockOperationInternal; operationInternal.OnWait = delay => passedDelays.Add(delay); if (useDefaultPollingInterval) { operationInternal.DefaultPollingInterval = originalDelay; await operationInternal.WaitForCompletionAsync(CancellationToken.None); } else { await operationInternal.WaitForCompletionAsync(originalDelay, CancellationToken.None); } // Algorithm must choose the longest delay between the two. Assert.AreEqual(2, passedDelays.Count); Assert.AreEqual(Max(originalDelay, shortDelay), passedDelays[0]); Assert.AreEqual(Max(originalDelay, longDelay), passedDelays[1]); }
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)); }