Skip to content

Commit 5a2c393

Browse files
committed
* Fix OAuth2 integration test bug
1 parent be6b8a3 commit 5a2c393

File tree

3 files changed

+22
-47
lines changed

3 files changed

+22
-47
lines changed

projects/RabbitMQ.Client.OAuth2/CredentialsRefresher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ await _onRefreshed(null, ex, _linkedCts.Token)
105105
.ConfigureAwait(false);
106106
}
107107

108-
TimeSpan delaySpan = TimeSpan.FromMinutes(5); // TODO
108+
TimeSpan delaySpan = TimeSpan.FromSeconds(30);
109109
if (_credentials != null && _credentials.ValidUntil.HasValue)
110110
{
111111
delaySpan = TimeSpan.FromMilliseconds(_credentials.ValidUntil.Value.TotalMilliseconds * (1.0 - (1 / 3.0)));

projects/RabbitMQ.Client.OAuth2/OAuth2CredentialsProvider.cs

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,26 @@ public OAuth2ClientCredentialsProvider(string name, IOAuth2Client oAuth2Client)
5353

5454
public async Task<Credentials> GetCredentialsAsync(CancellationToken cancellationToken = default)
5555
{
56-
bool mustRetrieveToken = false;
5756
await _semaphore.WaitAsync(cancellationToken)
5857
.ConfigureAwait(false);
5958
try
6059
{
61-
if (_token is null || _token.HasExpired)
60+
if (_token == null || string.IsNullOrEmpty(_token.RefreshToken))
6261
{
63-
mustRetrieveToken = true;
62+
_token = await _oAuth2Client.RequestTokenAsync(cancellationToken)
63+
.ConfigureAwait(false);
64+
}
65+
else
66+
{
67+
_token = await _oAuth2Client.RefreshTokenAsync(_token, cancellationToken)
68+
.ConfigureAwait(false);
6469
}
6570
}
6671
finally
6772
{
6873
_semaphore.Release();
6974
}
7075

71-
cancellationToken.ThrowIfCancellationRequested();
72-
73-
if (mustRetrieveToken)
74-
{
75-
await RetrieveTokenAsync(cancellationToken).ConfigureAwait(false);
76-
}
77-
7876
if (_token is null)
7977
{
8078
throw new InvalidOperationException("_token should not be null here");
@@ -90,28 +88,5 @@ public void Dispose()
9088
{
9189
_semaphore.Dispose();
9290
}
93-
94-
private async Task RetrieveTokenAsync(CancellationToken cancellationToken)
95-
{
96-
await _semaphore.WaitAsync(cancellationToken)
97-
.ConfigureAwait(false);
98-
try
99-
{
100-
if (_token == null || string.IsNullOrEmpty(_token.RefreshToken))
101-
{
102-
_token = await _oAuth2Client.RequestTokenAsync(cancellationToken)
103-
.ConfigureAwait(false);
104-
}
105-
else
106-
{
107-
_token = await _oAuth2Client.RefreshTokenAsync(_token, cancellationToken)
108-
.ConfigureAwait(false);
109-
}
110-
}
111-
finally
112-
{
113-
_semaphore.Release();
114-
}
115-
}
11691
}
11792
}

projects/Test/OAuth2/TestOAuth2.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,13 @@ public class TestOAuth2 : IAsyncLifetime
132132
private readonly SemaphoreSlim _doneEvent = new SemaphoreSlim(0, 1);
133133
private readonly ITestOutputHelper _testOutputHelper;
134134
private readonly IConnectionFactory _connectionFactory;
135-
private IConnection? _connection;
136135
private readonly int _tokenExpiresInSeconds;
137136
private readonly OAuth2ClientCredentialsProvider _credentialsProvider;
138-
private readonly CredentialsRefresher _credentialsRefresher;
139137
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
140138

139+
private IConnection? _connection;
140+
private CredentialsRefresher? _credentialsRefresher;
141+
141142
public TestOAuth2(ITestOutputHelper testOutputHelper)
142143
{
143144
_testOutputHelper = testOutputHelper;
@@ -156,15 +157,15 @@ public TestOAuth2(ITestOutputHelper testOutputHelper)
156157
};
157158

158159
_tokenExpiresInSeconds = options.TokenExpiresInSeconds;
159-
160-
_credentialsRefresher = new CredentialsRefresher(_credentialsProvider,
161-
OnCredentialsRefreshedAsync,
162-
_cancellationTokenSource.Token);
163160
}
164161

165162
public async Task InitializeAsync()
166163
{
167-
_connection = await _connectionFactory.CreateConnectionAsync(CancellationToken.None);
164+
_connection = await _connectionFactory.CreateConnectionAsync(_cancellationTokenSource.Token);
165+
166+
_credentialsRefresher = new CredentialsRefresher(_credentialsProvider,
167+
OnCredentialsRefreshedAsync,
168+
_cancellationTokenSource.Token);
168169
}
169170

170171
public async Task DisposeAsync()
@@ -189,8 +190,6 @@ public async Task DisposeAsync()
189190
private Task OnCredentialsRefreshedAsync(Credentials? credentials, Exception? exception,
190191
CancellationToken cancellationToken = default)
191192
{
192-
_testOutputHelper.WriteLine("[INFO] OnCredentialsRefreshedAsync called");
193-
194193
if (_connection is null)
195194
{
196195
Assert.Fail("_connection is unexpectedly null!");
@@ -223,10 +222,11 @@ public async void IntegrationTest()
223222
{
224223
for (int i = 0; i < 4; i++)
225224
{
226-
_testOutputHelper.WriteLine("Wait until Token expires. Attempt #" + (i + 1));
227-
228-
await Task.Delay(TimeSpan.FromSeconds(_tokenExpiresInSeconds + 10));
229-
_testOutputHelper.WriteLine("Resuming ..");
225+
var delaySpan = TimeSpan.FromSeconds(_tokenExpiresInSeconds + 10);
226+
_testOutputHelper.WriteLine("{0} [INFO] wait '{1}' until Token expires. Attempt #{1}",
227+
DateTime.Now, delaySpan, (i + 1));
228+
await Task.Delay(delaySpan);
229+
_testOutputHelper.WriteLine("{0} [INFO] Resuming ...", DateTime.Now);
230230

231231
await PublishAsync(publishChannel);
232232
await ConsumeAsync(consumeChannel);

0 commit comments

Comments
 (0)