@@ -46,29 +46,33 @@ public class TestOAuth2 : IAsyncLifetime
46
46
{
47
47
private const string Exchange = "test_direct" ;
48
48
49
+ private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource ( ) ;
49
50
private readonly SemaphoreSlim _doneEvent = new SemaphoreSlim ( 0 , 1 ) ;
50
51
private readonly ITestOutputHelper _testOutputHelper ;
51
- private readonly IConnectionFactory _connectionFactory ;
52
52
private readonly int _tokenExpiresInSeconds ;
53
- private readonly OAuth2ClientCredentialsProvider _producerCredentialsProvider ;
54
- private readonly OAuth2ClientCredentialsProvider _httpApiCredentialsProvider ;
55
- private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource ( ) ;
56
53
54
+ private OAuth2ClientCredentialsProvider ? _producerCredentialsProvider ;
55
+ private OAuth2ClientCredentialsProvider ? _httpApiCredentialsProvider ;
56
+ private IConnectionFactory ? _connectionFactory ;
57
57
private IConnection ? _connection ;
58
58
private CredentialsRefresher ? _credentialsRefresher ;
59
59
60
60
public TestOAuth2 ( ITestOutputHelper testOutputHelper )
61
61
{
62
62
_testOutputHelper = testOutputHelper ;
63
+ _tokenExpiresInSeconds = OAuth2OptionsBase . TokenExpiresInSeconds ;
64
+ }
63
65
66
+ public async Task InitializeAsync ( )
67
+ {
64
68
string modeStr = Environment . GetEnvironmentVariable ( "OAUTH2_MODE" ) ?? "uaa" ;
65
69
Mode mode = ( Mode ) Enum . Parse ( typeof ( Mode ) , modeStr . ToLowerInvariant ( ) ) ;
66
70
67
71
var producerOptions = new OAuth2ProducerOptions ( mode ) ;
68
- _producerCredentialsProvider = GetCredentialsProvider ( producerOptions ) ;
72
+ _producerCredentialsProvider = await GetCredentialsProviderAsync ( producerOptions ) ;
69
73
70
74
var httpApiOptions = new OAuth2HttpApiOptions ( mode ) ;
71
- _httpApiCredentialsProvider = GetCredentialsProvider ( httpApiOptions ) ;
75
+ _httpApiCredentialsProvider = await GetCredentialsProviderAsync ( httpApiOptions ) ;
72
76
73
77
_connectionFactory = new ConnectionFactory
74
78
{
@@ -77,11 +81,6 @@ public TestOAuth2(ITestOutputHelper testOutputHelper)
77
81
ClientProvidedName = nameof ( TestOAuth2 )
78
82
} ;
79
83
80
- _tokenExpiresInSeconds = OAuth2OptionsBase . TokenExpiresInSeconds ;
81
- }
82
-
83
- public async Task InitializeAsync ( )
84
- {
85
84
_connection = await _connectionFactory . CreateConnectionAsync ( _cancellationTokenSource . Token ) ;
86
85
87
86
_connection . ConnectionShutdown += ( sender , ea ) =>
@@ -119,7 +118,7 @@ public async Task DisposeAsync()
119
118
finally
120
119
{
121
120
_doneEvent . Dispose ( ) ;
122
- _producerCredentialsProvider . Dispose ( ) ;
121
+ _producerCredentialsProvider ? . Dispose ( ) ;
123
122
_connection ? . Dispose ( ) ;
124
123
}
125
124
}
@@ -174,6 +173,7 @@ public async Task IntegrationTest()
174
173
async Task CloseConnection ( )
175
174
{
176
175
Assert . NotNull ( _connection ) ;
176
+ Assert . NotNull ( _httpApiCredentialsProvider ) ;
177
177
Credentials httpApiCredentials = await _httpApiCredentialsProvider . GetCredentialsAsync ( ) ;
178
178
closeConnectionUtil = new Util ( _testOutputHelper , "mgt_api_client" , httpApiCredentials . Password ) ;
179
179
await closeConnectionUtil . CloseConnectionAsync ( _connection . ClientProvidedName ) ;
@@ -217,6 +217,7 @@ async Task CloseConnection()
217
217
[ Fact ]
218
218
public async Task SecondConnectionCrashes_GH1429 ( )
219
219
{
220
+ Assert . NotNull ( _connectionFactory ) ;
220
221
// https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/1429
221
222
IConnection secondConnection = await _connectionFactory . CreateConnectionAsync ( CancellationToken . None ) ;
222
223
secondConnection . Dispose ( ) ;
@@ -267,7 +268,7 @@ private async Task ConsumeAsync(IChannel consumeChannel)
267
268
await consumeChannel . BasicCancelAsync ( consumerTag ) ;
268
269
}
269
270
270
- private OAuth2ClientCredentialsProvider GetCredentialsProvider ( OAuth2OptionsBase opts )
271
+ private async Task < OAuth2ClientCredentialsProvider > GetCredentialsProviderAsync ( OAuth2OptionsBase opts )
271
272
{
272
273
_testOutputHelper . WriteLine ( "OAuth2Client " ) ;
273
274
_testOutputHelper . WriteLine ( $ "- ClientId: { opts . ClientId } ") ;
@@ -276,7 +277,8 @@ private OAuth2ClientCredentialsProvider GetCredentialsProvider(OAuth2OptionsBase
276
277
_testOutputHelper . WriteLine ( $ "- Scope: { opts . Scope } ") ;
277
278
278
279
var tokenEndpointUri = new Uri ( opts . TokenEndpoint ) ;
279
- IOAuth2Client oAuth2Client = new OAuth2ClientBuilder ( opts . ClientId , opts . ClientSecret , tokenEndpointUri ) . Build ( ) ;
280
+ var builder = new OAuth2ClientBuilder ( opts . ClientId , opts . ClientSecret , tokenEndpointUri ) ;
281
+ IOAuth2Client oAuth2Client = await builder . BuildAsync ( ) ;
280
282
return new OAuth2ClientCredentialsProvider ( opts . Name , oAuth2Client ) ;
281
283
}
282
284
0 commit comments