Skip to content

Commit d606122

Browse files
committed
* Use OAuth2 for EasyNetQ.Management.Client in OAuth2 tests.
1 parent aaab4f3 commit d606122

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

projects/Test/Common/TestConnectionRecoveryBase.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040

4141
namespace Test
4242
{
43-
public class TestConnectionRecoveryBase : IntegrationFixture
43+
public class TestConnectionRecoveryBase : IntegrationFixture, IDisposable
4444
{
45+
private readonly Util _util = new Util();
4546
protected readonly byte[] _messageBody;
4647
protected const ushort TotalMessageCount = 16384;
4748
protected const ushort CloseAtCount = 16;
@@ -52,6 +53,8 @@ public TestConnectionRecoveryBase(ITestOutputHelper output)
5253
_messageBody = GetRandomBody(4096);
5354
}
5455

56+
public void Dispose() => _util.Dispose();
57+
5558
protected Task AssertConsumerCountAsync(string q, int count)
5659
{
5760
return WithTemporaryChannelAsync(async ch =>
@@ -166,7 +169,7 @@ internal async Task<AutorecoveringConnection> CreateAutorecoveringConnectionWith
166169

167170
protected Task CloseConnectionAsync(IConnection conn)
168171
{
169-
return Util.CloseConnectionAsync(conn);
172+
return _util.CloseConnectionAsync(conn);
170173
}
171174

172175
protected Task CloseAndWaitForRecoveryAsync()

projects/Test/Common/Util.cs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,41 @@
88

99
namespace Test
1010
{
11-
public static class Util
11+
public class Util : IDisposable
1212
{
13-
private static readonly ManagementClient s_managementClient;
13+
private readonly ManagementClient _managementClient;
1414
private static readonly bool s_isWindows = false;
1515

1616
static Util()
1717
{
18-
var managementUri = new Uri("http://localhost:15672");
19-
s_managementClient = new ManagementClient(managementUri, "guest", "guest");
2018
s_isWindows = InitIsWindows();
2119
}
2220

23-
public static string Now => DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture);
24-
25-
public static bool IsWindows => s_isWindows;
21+
public Util() : this("guest", "guest")
22+
{
23+
}
2624

27-
private static bool InitIsWindows()
25+
public Util(string managementUsername, string managementPassword)
2826
{
29-
PlatformID platform = Environment.OSVersion.Platform;
30-
if (platform == PlatformID.Win32NT)
27+
if (string.IsNullOrEmpty(managementUsername))
3128
{
32-
return true;
29+
throw new ArgumentNullException(nameof(managementUsername));
3330
}
3431

35-
string os = Environment.GetEnvironmentVariable("OS");
36-
if (os != null)
32+
if (string.IsNullOrEmpty(managementPassword))
3733
{
38-
os = os.Trim();
39-
return os == "Windows_NT";
34+
throw new ArgumentNullException(nameof(managementPassword));
4035
}
4136

42-
return false;
37+
var managementUri = new Uri("http://localhost:15672");
38+
_managementClient = new ManagementClient(managementUri, managementUsername, managementPassword);
4339
}
4440

45-
public static async Task CloseConnectionAsync(IConnection conn)
41+
public static string Now => DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture);
42+
43+
public static bool IsWindows => s_isWindows;
44+
45+
public async Task CloseConnectionAsync(IConnection conn)
4646
{
4747
ushort tries = 1;
4848
EasyNetQ.Management.Client.Model.Connection connectionToClose = null;
@@ -61,7 +61,7 @@ public static async Task CloseConnectionAsync(IConnection conn)
6161

6262
await Task.Delay(TimeSpan.FromSeconds(delaySeconds));
6363

64-
connections = await s_managementClient.GetConnectionsAsync();
64+
connections = await _managementClient.GetConnectionsAsync();
6565
} while (connections.Count == 0);
6666

6767
connectionToClose = connections.Where(c0 =>
@@ -79,7 +79,7 @@ public static async Task CloseConnectionAsync(IConnection conn)
7979
{
8080
try
8181
{
82-
await s_managementClient.CloseConnectionAsync(connectionToClose);
82+
await _managementClient.CloseConnectionAsync(connectionToClose);
8383
return;
8484
}
8585
catch (UnexpectedHttpStatusCodeException)
@@ -94,5 +94,25 @@ public static async Task CloseConnectionAsync(IConnection conn)
9494
throw new InvalidOperationException($"Could not delete connection: '{conn.ClientProvidedName}'");
9595
}
9696
}
97+
98+
public void Dispose() => _managementClient.Dispose();
99+
100+
private static bool InitIsWindows()
101+
{
102+
PlatformID platform = Environment.OSVersion.Platform;
103+
if (platform == PlatformID.Win32NT)
104+
{
105+
return true;
106+
}
107+
108+
string os = Environment.GetEnvironmentVariable("OS");
109+
if (os != null)
110+
{
111+
os = os.Trim();
112+
return os == "Windows_NT";
113+
}
114+
115+
return false;
116+
}
97117
}
98118
}

projects/Test/OAuth2/TestOAuth2.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public class TestOAuth2 : IAsyncLifetime
137137
private readonly OAuth2ClientCredentialsProvider _credentialsProvider;
138138
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
139139

140+
private Util? _util;
140141
private IConnection? _connection;
141142
private CredentialsRefresher? _credentialsRefresher;
142143

@@ -206,6 +207,12 @@ private Task OnCredentialsRefreshedAsync(Credentials? credentials, Exception? ex
206207
Assert.Fail("credentials arg is unexpectedly null!");
207208
}
208209

210+
if (_util != null)
211+
{
212+
_util.Dispose();
213+
}
214+
_util = new Util(credentials.UserName, credentials.Password);
215+
209216
return _connection.UpdateSecretAsync(credentials.Password, "Token refresh", cancellationToken);
210217
}
211218

@@ -231,7 +238,8 @@ public async void IntegrationTest()
231238

232239
if (i == 2)
233240
{
234-
closeConnectionTask = Util.CloseConnectionAsync(_connection);
241+
Assert.NotNull(_util);
242+
closeConnectionTask = _util.CloseConnectionAsync(_connection);
235243
}
236244

237245
await Task.Delay(delaySpan);

0 commit comments

Comments
 (0)