|
26 | 26 | using System.Net.Http; |
27 | 27 | using System.Runtime.CompilerServices; |
28 | 28 | using System.Security; |
| 29 | +using System.Threading; |
29 | 30 | using System.Threading.Tasks; |
30 | 31 | using Xunit; |
31 | 32 | using Xunit.Abstractions; |
@@ -104,6 +105,23 @@ public void TestThrowDisposedOperationCheck() |
104 | 105 | }); |
105 | 106 | } |
106 | 107 |
|
| 108 | + [Fact] |
| 109 | + public async Task ThrowsOperationCanceledExceptionWhenCancelled() |
| 110 | + { |
| 111 | + testSupport.SetupMockAndSupport(out var orgSvc, out var fakHttpMethodHander, out var cli); |
| 112 | + var cts = new CancellationTokenSource(); |
| 113 | + |
| 114 | + var cancelledToken = cts.Token; |
| 115 | + cts.Cancel(); |
| 116 | + |
| 117 | + Func<Task> cancelledRequest = async() => |
| 118 | + { |
| 119 | + await cli.ExecuteAsync(new WhoAmIRequest(), cts.Token).ConfigureAwait(false); |
| 120 | + }; |
| 121 | + |
| 122 | + await cancelledRequest.Should().ThrowAsync<OperationCanceledException>().ConfigureAwait(false); |
| 123 | + } |
| 124 | + |
107 | 125 | [Fact] |
108 | 126 | public void ExecuteMessageTests() |
109 | 127 | { |
@@ -807,6 +825,65 @@ public async void RetryOperationAyncTest() |
807 | 825 | Assert.True(retrycount == 2); |
808 | 826 | } |
809 | 827 |
|
| 828 | + [Fact] |
| 829 | + public async Task RetryOperationCancelledDuringDelayTest() |
| 830 | + { |
| 831 | + testSupport.SetupMockAndSupport(out var orgSvc, out var fakHttpMethodHander, out var cli); |
| 832 | + |
| 833 | + Guid testGuid = Guid.NewGuid(); |
| 834 | + |
| 835 | + CreateRequest exampleRequest = new CreateRequest(); |
| 836 | + exampleRequest.Target = new Entity("account"); |
| 837 | + exampleRequest.Target.Attributes.Add("id", testGuid); |
| 838 | + |
| 839 | + Stopwatch testwatch = Stopwatch.StartNew(); |
| 840 | + |
| 841 | + var cts = new CancellationTokenSource(); |
| 842 | + var cancellationToken = cts.Token; |
| 843 | + |
| 844 | + var delay = TimeSpan.FromSeconds(5); |
| 845 | + |
| 846 | + var retryTask = Task.Run(async () => |
| 847 | + { |
| 848 | + await Utilities.RetryRequest(exampleRequest, testGuid, new TimeSpan(0), testwatch, cli._logEntry, null, false, delay, new Exception("Fake_TEST_MSG"), "test retry logic", 0, false, null, cancellationToken: cancellationToken).ConfigureAwait(false); |
| 849 | + }); |
| 850 | + |
| 851 | + await Task.Delay(TimeSpan.FromMilliseconds(100)).ConfigureAwait(false); |
| 852 | + cts.Cancel(); |
| 853 | + await Task.Delay(TimeSpan.FromMilliseconds(50)).ConfigureAwait(false); |
| 854 | + |
| 855 | + retryTask.IsCompleted.Should().BeTrue("Task.Delay within Utilities.RetryRequest should just return early, allowing the task to complete"); |
| 856 | + testwatch.Elapsed.Should().BeLessThan(delay, "Task should return before its delay timer can complete due to cancellation"); |
| 857 | + } |
| 858 | + |
| 859 | + [Fact] |
| 860 | + public async Task RetryOperationShouldNotThrowWhenAlreadyCanceledTest() |
| 861 | + { |
| 862 | + testSupport.SetupMockAndSupport(out var orgSvc, out var fakHttpMethodHander, out var cli); |
| 863 | + |
| 864 | + Guid testGuid = Guid.NewGuid(); |
| 865 | + |
| 866 | + CreateRequest exampleRequest = new CreateRequest(); |
| 867 | + exampleRequest.Target = new Entity("account"); |
| 868 | + exampleRequest.Target.Attributes.Add("id", testGuid); |
| 869 | + |
| 870 | + Stopwatch testwatch = Stopwatch.StartNew(); |
| 871 | + |
| 872 | + var cts = new CancellationTokenSource(); |
| 873 | + var cancellationToken = cts.Token; |
| 874 | + cts.Cancel(); // Cancel before the token is even provided to the method |
| 875 | + |
| 876 | + var delay = TimeSpan.FromSeconds(5); |
| 877 | + |
| 878 | + Func<Task> cancelledRetry = async () => |
| 879 | + { |
| 880 | + await Utilities.RetryRequest(exampleRequest, testGuid, new TimeSpan(0), testwatch, cli._logEntry, null, false, delay, new Exception("Fake_TEST_MSG"), "test retry logic", 0, false, null, cancellationToken: cancellationToken).ConfigureAwait(false); |
| 881 | + }; |
| 882 | + |
| 883 | + await cancelledRetry.Should().NotThrowAsync<TaskCanceledException>().ConfigureAwait(false); |
| 884 | + testwatch.Elapsed.Should().BeLessThan(delay, "Task should return before its delay timer can complete due to cancellation"); |
| 885 | + } |
| 886 | + |
810 | 887 | #region LiveConnectedTests |
811 | 888 |
|
812 | 889 | [SkippableConnectionTest] |
|
0 commit comments