|
12 | 12 | using System;
|
13 | 13 | using System.Collections.Generic;
|
14 | 14 | using System.Net;
|
| 15 | +using System.Net.Http; |
| 16 | +using System.Threading; |
15 | 17 | using System.Threading.Tasks;
|
16 | 18 | using Tests.Util;
|
17 | 19 | using Xunit;
|
@@ -61,6 +63,109 @@ public async Task ActiveToken()
|
61 | 63 | request.Should().ContainKey("client_secret").WhichValue.Should().Be(clientSecret);
|
62 | 64 | }
|
63 | 65 |
|
| 66 | + [Theory] |
| 67 | + [InlineData(IntrospectionEndpointHandler.Behavior.Active, HttpStatusCode.OK)] |
| 68 | + [InlineData(IntrospectionEndpointHandler.Behavior.Unauthorized, HttpStatusCode.Unauthorized)] |
| 69 | + public async Task TwoConcurrentCalls_FirstIntrospectDoesNotThrow_SecondShouldNotBeCalled( |
| 70 | + IntrospectionEndpointHandler.Behavior behavior, |
| 71 | + HttpStatusCode expectedStatusCode) |
| 72 | + { |
| 73 | + const string token = "sometoken"; |
| 74 | + var waitForTheFirstIntrospectionToStart = new ManualResetEvent(initialState: false); |
| 75 | + var waitForTheSecondRequestToStart = new ManualResetEvent(initialState: false); |
| 76 | + var handler = new IntrospectionEndpointHandler(behavior); |
| 77 | + |
| 78 | + var requestCount = 0; |
| 79 | + |
| 80 | + var messageHandler = PipelineFactory.CreateHandler(o => |
| 81 | + { |
| 82 | + _options(o); |
| 83 | + |
| 84 | + o.Events.OnSendingRequest = async context => |
| 85 | + { |
| 86 | + requestCount += 1; |
| 87 | + |
| 88 | + if (requestCount == 1) |
| 89 | + { |
| 90 | + waitForTheSecondRequestToStart.WaitOne(); |
| 91 | + waitForTheFirstIntrospectionToStart.Set(); |
| 92 | + await Task.Delay(200); // wait for second request to reach the IntrospectionDictionary |
| 93 | + } |
| 94 | + }; |
| 95 | + }, handler); |
| 96 | + |
| 97 | + var client1 = new HttpClient(messageHandler); |
| 98 | + var request1 = Task.Run(async () => |
| 99 | + { |
| 100 | + client1.SetBearerToken(token); |
| 101 | + return await client1.GetAsync("http://test"); |
| 102 | + }); |
| 103 | + |
| 104 | + var client2 = new HttpClient(messageHandler); |
| 105 | + var request2 = Task.Run(async () => |
| 106 | + { |
| 107 | + waitForTheSecondRequestToStart.Set(); |
| 108 | + waitForTheFirstIntrospectionToStart.WaitOne(); |
| 109 | + client2.SetBearerToken(token); |
| 110 | + return await client2.GetAsync("http://test"); |
| 111 | + }); |
| 112 | + |
| 113 | + await Task.WhenAll(request1, request2); |
| 114 | + |
| 115 | + var result1 = await request1; |
| 116 | + result1.StatusCode.Should().Be(expectedStatusCode); |
| 117 | + |
| 118 | + requestCount.Should().Be(1); |
| 119 | + |
| 120 | + var result2 = await request2; |
| 121 | + result2.StatusCode.Should().Be(expectedStatusCode); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public async Task ActiveToken_WithTwoConcurrentCalls_FirstCancelled_SecondShouldNotBeCancelled() |
| 126 | + { |
| 127 | + const string token = "sometoken"; |
| 128 | + var cts = new CancellationTokenSource(); |
| 129 | + var waitForTheFirstIntrospectionToStart = new ManualResetEvent(initialState: false); |
| 130 | + var waitForTheSecondRequestToStart = new ManualResetEvent(initialState: false); |
| 131 | + var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active); |
| 132 | + |
| 133 | + var messageHandler = PipelineFactory.CreateHandler(o => |
| 134 | + { |
| 135 | + _options(o); |
| 136 | + |
| 137 | + o.Events.OnSendingRequest = async context => |
| 138 | + { |
| 139 | + waitForTheSecondRequestToStart.WaitOne(); |
| 140 | + waitForTheFirstIntrospectionToStart.Set(); |
| 141 | + cts.Cancel(); |
| 142 | + await Task.Delay(200); // wait for second request to reach the IntrospectionDictionary |
| 143 | + }; |
| 144 | + }, handler); |
| 145 | + |
| 146 | + var client1 = new HttpClient(messageHandler); |
| 147 | + var request1 = Task.Run(async () => |
| 148 | + { |
| 149 | + client1.SetBearerToken(token); |
| 150 | + var doRequest = () => client1.GetAsync("http://test", cts.Token); |
| 151 | + await doRequest.Should().ThrowAsync<OperationCanceledException>(); |
| 152 | + }); |
| 153 | + |
| 154 | + var client2 = new HttpClient(messageHandler); |
| 155 | + var request2 = Task.Run(async () => |
| 156 | + { |
| 157 | + waitForTheSecondRequestToStart.Set(); |
| 158 | + waitForTheFirstIntrospectionToStart.WaitOne(); |
| 159 | + client2.SetBearerToken(token); |
| 160 | + return await client2.GetAsync("http://test"); |
| 161 | + }); |
| 162 | + |
| 163 | + await Task.WhenAll(request1, request2); |
| 164 | + |
| 165 | + var result2 = await request2; |
| 166 | + result2.StatusCode.Should().Be(HttpStatusCode.OK); |
| 167 | + } |
| 168 | + |
64 | 169 | [Theory]
|
65 | 170 | [InlineData(5000, "testAssertion1", "testAssertion1")]
|
66 | 171 | [InlineData(-5000, "testAssertion1", "testAssertion2")]
|
|
0 commit comments