Skip to content

Commit 8b09ea6

Browse files
committed
Remove synchronous Build method.
1 parent ac52d31 commit 8b09ea6

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

projects/RabbitMQ.Client.OAuth2/OAuth2Client.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public OAuth2ClientBuilder(string clientId, string clientSecret, Uri? tokenEndpo
7474
_clientSecret = clientSecret ?? throw new ArgumentNullException(nameof(clientSecret));
7575

7676
if (tokenEndpoint is null && issuer is null)
77+
{
7778
throw new ArgumentException("Either tokenEndpoint or issuer is required");
79+
}
7880

7981
_tokenEndpoint = tokenEndpoint;
8082
_issuer = issuer;
@@ -123,24 +125,6 @@ public OAuth2ClientBuilder AddRequestParameter(string param, string paramValue)
123125
return this;
124126
}
125127

126-
/// <summary>
127-
/// Build the <see cref="OAuth2Client"/> with the provided properties of the builder.
128-
/// </summary>
129-
/// <returns>Configured OAuth2Client</returns>
130-
public IOAuth2Client Build()
131-
{
132-
// Check if Token Endpoint is missing -> Use Issuer to receive Token Endpoint
133-
if (_tokenEndpoint is null)
134-
{
135-
// Don't know how to better handle backwards compatibily
136-
Uri tokenEndpoint = GetTokenEndpointFromIssuerAsync().GetAwaiter().GetResult();
137-
return new OAuth2Client(_clientId, _clientSecret, tokenEndpoint, _scope, _additionalRequestParameters, _httpClientHandler);
138-
}
139-
140-
return new OAuth2Client(_clientId, _clientSecret, _tokenEndpoint,
141-
_scope, _additionalRequestParameters, _httpClientHandler);
142-
}
143-
144128
/// <summary>
145129
/// Build the <see cref="OAuth2Client"/> with the provided properties of the builder.
146130
/// </summary>

projects/RabbitMQ.Client.OAuth2/PublicAPI.Shipped.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ RabbitMQ.Client.OAuth2.IToken.HasExpired.get -> bool
77
RabbitMQ.Client.OAuth2.IToken.RefreshToken.get -> string
88
RabbitMQ.Client.OAuth2.OAuth2ClientBuilder
99
RabbitMQ.Client.OAuth2.OAuth2ClientBuilder.AddRequestParameter(string param, string paramValue) -> RabbitMQ.Client.OAuth2.OAuth2ClientBuilder
10-
RabbitMQ.Client.OAuth2.OAuth2ClientBuilder.Build() -> RabbitMQ.Client.OAuth2.IOAuth2Client
1110
RabbitMQ.Client.OAuth2.OAuth2ClientBuilder.OAuth2ClientBuilder(string! clientId, string! clientSecret, System.Uri? tokenEndpoint = null, System.Uri? issuer = null) -> void
1211
RabbitMQ.Client.OAuth2.OAuth2ClientBuilder.SetScope(string scope) -> RabbitMQ.Client.OAuth2.OAuth2ClientBuilder
1312
RabbitMQ.Client.OAuth2.OAuth2ClientCredentialsProvider

projects/Test/OAuth2/TestOAuth2.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,33 @@ public class TestOAuth2 : IAsyncLifetime
4646
{
4747
private const string Exchange = "test_direct";
4848

49+
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
4950
private readonly SemaphoreSlim _doneEvent = new SemaphoreSlim(0, 1);
5051
private readonly ITestOutputHelper _testOutputHelper;
51-
private readonly IConnectionFactory _connectionFactory;
5252
private readonly int _tokenExpiresInSeconds;
53-
private readonly OAuth2ClientCredentialsProvider _producerCredentialsProvider;
54-
private readonly OAuth2ClientCredentialsProvider _httpApiCredentialsProvider;
55-
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
5653

54+
private OAuth2ClientCredentialsProvider? _producerCredentialsProvider;
55+
private OAuth2ClientCredentialsProvider? _httpApiCredentialsProvider;
56+
private IConnectionFactory? _connectionFactory;
5757
private IConnection? _connection;
5858
private CredentialsRefresher? _credentialsRefresher;
5959

6060
public TestOAuth2(ITestOutputHelper testOutputHelper)
6161
{
6262
_testOutputHelper = testOutputHelper;
63+
_tokenExpiresInSeconds = OAuth2OptionsBase.TokenExpiresInSeconds;
64+
}
6365

66+
public async Task InitializeAsync()
67+
{
6468
string modeStr = Environment.GetEnvironmentVariable("OAUTH2_MODE") ?? "uaa";
6569
Mode mode = (Mode)Enum.Parse(typeof(Mode), modeStr.ToLowerInvariant());
6670

6771
var producerOptions = new OAuth2ProducerOptions(mode);
68-
_producerCredentialsProvider = GetCredentialsProvider(producerOptions);
72+
_producerCredentialsProvider = await GetCredentialsProviderAsync(producerOptions);
6973

7074
var httpApiOptions = new OAuth2HttpApiOptions(mode);
71-
_httpApiCredentialsProvider = GetCredentialsProvider(httpApiOptions);
75+
_httpApiCredentialsProvider = await GetCredentialsProviderAsync(httpApiOptions);
7276

7377
_connectionFactory = new ConnectionFactory
7478
{
@@ -77,11 +81,6 @@ public TestOAuth2(ITestOutputHelper testOutputHelper)
7781
ClientProvidedName = nameof(TestOAuth2)
7882
};
7983

80-
_tokenExpiresInSeconds = OAuth2OptionsBase.TokenExpiresInSeconds;
81-
}
82-
83-
public async Task InitializeAsync()
84-
{
8584
_connection = await _connectionFactory.CreateConnectionAsync(_cancellationTokenSource.Token);
8685

8786
_connection.ConnectionShutdown += (sender, ea) =>
@@ -119,7 +118,7 @@ public async Task DisposeAsync()
119118
finally
120119
{
121120
_doneEvent.Dispose();
122-
_producerCredentialsProvider.Dispose();
121+
_producerCredentialsProvider?.Dispose();
123122
_connection?.Dispose();
124123
}
125124
}
@@ -174,6 +173,7 @@ public async Task IntegrationTest()
174173
async Task CloseConnection()
175174
{
176175
Assert.NotNull(_connection);
176+
Assert.NotNull(_httpApiCredentialsProvider);
177177
Credentials httpApiCredentials = await _httpApiCredentialsProvider.GetCredentialsAsync();
178178
closeConnectionUtil = new Util(_testOutputHelper, "mgt_api_client", httpApiCredentials.Password);
179179
await closeConnectionUtil.CloseConnectionAsync(_connection.ClientProvidedName);
@@ -217,6 +217,7 @@ async Task CloseConnection()
217217
[Fact]
218218
public async Task SecondConnectionCrashes_GH1429()
219219
{
220+
Assert.NotNull(_connectionFactory);
220221
// https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/1429
221222
IConnection secondConnection = await _connectionFactory.CreateConnectionAsync(CancellationToken.None);
222223
secondConnection.Dispose();
@@ -267,7 +268,7 @@ private async Task ConsumeAsync(IChannel consumeChannel)
267268
await consumeChannel.BasicCancelAsync(consumerTag);
268269
}
269270

270-
private OAuth2ClientCredentialsProvider GetCredentialsProvider(OAuth2OptionsBase opts)
271+
private async Task<OAuth2ClientCredentialsProvider> GetCredentialsProviderAsync(OAuth2OptionsBase opts)
271272
{
272273
_testOutputHelper.WriteLine("OAuth2Client ");
273274
_testOutputHelper.WriteLine($"- ClientId: {opts.ClientId}");
@@ -276,7 +277,8 @@ private OAuth2ClientCredentialsProvider GetCredentialsProvider(OAuth2OptionsBase
276277
_testOutputHelper.WriteLine($"- Scope: {opts.Scope}");
277278

278279
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();
280282
return new OAuth2ClientCredentialsProvider(opts.Name, oAuth2Client);
281283
}
282284

projects/Test/OAuth2/TestOAuth2Client.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,36 @@
4141

4242
namespace OAuth2Test
4343
{
44-
public class TestOAuth2Client
44+
public class TestOAuth2Client : IAsyncLifetime
4545
{
4646
protected string _client_id = "producer";
4747
protected string _client_secret = "kbOFBXI9tANgKUq8vXHLhT6YhbivgXxn";
4848
protected WireMockServer _oauthServer;
4949

50-
protected IOAuth2Client _client;
50+
protected IOAuth2Client? _client;
5151

5252
public TestOAuth2Client()
5353
{
5454
_oauthServer = WireMockServer.Start();
55+
}
56+
57+
public async Task InitializeAsync()
58+
{
5559
var uri = new Uri(_oauthServer.Url + "/token");
56-
_client = new OAuth2ClientBuilder(_client_id, _client_secret, uri).Build();
60+
var builder = new OAuth2ClientBuilder(_client_id, _client_secret, uri);
61+
_client = await builder.BuildAsync();
62+
}
63+
64+
public Task DisposeAsync()
65+
{
66+
return Task.CompletedTask;
5767
}
5868

5969
[Fact]
6070
public async Task TestRequestToken()
6171
{
72+
Assert.NotNull(_client);
73+
6274
JsonToken expectedJsonToken = new JsonToken("the_access_token", "the_refresh_token", TimeSpan.FromSeconds(10));
6375
ExpectTokenRequest(new RequestFormMatcher()
6476
.WithParam("client_id", _client_id)
@@ -76,6 +88,8 @@ public async Task TestRequestToken()
7688
[Fact]
7789
public async Task TestRefreshToken()
7890
{
91+
Assert.NotNull(_client);
92+
7993
const string accessToken0 = "the_access_token";
8094
const string accessToken1 = "the_access_token_2";
8195
const string refreshToken = "the_refresh_token";
@@ -110,6 +124,8 @@ public async Task TestRefreshToken()
110124
[Fact]
111125
public async Task TestInvalidCredentials()
112126
{
127+
Assert.NotNull(_client);
128+
113129
_oauthServer
114130
.Given(
115131
Request.Create()

0 commit comments

Comments
 (0)