Skip to content

Commit d7c0503

Browse files
committed
Toxiproxy manager change
* Use `Guid.NewGuid` to ensure unique name * No need to delete first as names should be unique * Use Guids for uniqueness
1 parent 103c39e commit d7c0503

File tree

6 files changed

+52
-77
lines changed

6 files changed

+52
-77
lines changed

.github/workflows/build-test.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,17 @@ jobs:
6969
- name: Integration Tests
7070
timeout-minutes: 25
7171
run: |
72-
Start-Job -Verbose -ScriptBlock { & "${{ github.workspace }}\.ci\windows\toxiproxy\toxiproxy-server.exe" }; `
72+
Start-Job -Verbose -ScriptBlock { & "${{ github.workspace }}\.ci\windows\toxiproxy\toxiproxy-server.exe" | Out-File -LiteralPath $env:APPDATA\RabbitMQ\log\toxiproxy-log.txt }; `
7373
dotnet test `
7474
--environment 'RABBITMQ_LONG_RUNNING_TESTS=true' `
7575
--environment "RABBITMQ_RABBITMQCTL_PATH=${{ steps.install-start-rabbitmq.outputs.path }}" `
7676
--environment 'RABBITMQ_TOXIPROXY_TESTS=true' `
7777
--environment 'PASSWORD=grapefruit' `
7878
--environment SSL_CERTS_DIR="${{ github.workspace }}\.ci\certs" `
79-
"${{ github.workspace }}\projects\Test\Integration\Integration.csproj" --no-restore --no-build --logger 'console;verbosity=detailed'
79+
"${{ github.workspace }}\projects\Test\Integration\Integration.csproj" --no-restore --no-build --logger 'console;verbosity=detailed'; `
80+
Get-Job | Stop-Job -Verbose -PassThru | Remove-Job -Verbose
8081
- name: Check for errors in RabbitMQ logs
8182
run: ${{ github.workspace }}\.ci\windows\gha-log-check.ps1
82-
- name: Maybe collect Toxiproxy logs
83-
if: failure()
84-
run: Get-Job | Where-Object { $_.HasMoreData } | Receive-Job | Out-File -Append -LiteralPath $env:APPDATA\RabbitMQ\log\toxiproxy-log.txt
8583
- name: Maybe upload RabbitMQ and Toxiproxy logs
8684
if: failure()
8785
uses: actions/upload-artifact@v4

projects/Test/Common/IntegrationFixture.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -443,19 +443,12 @@ protected async Task WithTemporaryChannelAsync(Func<IChannel, Task> action)
443443

444444
protected string GenerateExchangeName()
445445
{
446-
return $"{_testDisplayName}-exchange-{Now}-{GenerateShortUuid()}";
446+
return $"{_testDisplayName}-exchange-{Now}-{Guid.NewGuid()}";
447447
}
448448

449-
protected string GenerateQueueName(bool useGuid = false)
449+
protected string GenerateQueueName()
450450
{
451-
if (useGuid)
452-
{
453-
return $"{_testDisplayName}-queue-{Now}-{Guid.NewGuid()}";
454-
}
455-
else
456-
{
457-
return $"{_testDisplayName}-queue-{Now}-{GenerateShortUuid()}";
458-
}
451+
return $"{_testDisplayName}-queue-{Now}-{Guid.NewGuid()}";
459452
}
460453

461454
protected Task WithTemporaryNonExclusiveQueueAsync(Func<IChannel, string, Task> action)

projects/Test/Integration/ConnectionRecovery/TestQueueRecovery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public async Task TestQueueRecoveryWithManyQueues()
5252
int n = 1024;
5353
for (int i = 0; i < n; i++)
5454
{
55-
QueueDeclareOk q = await _channel.QueueDeclareAsync(GenerateQueueName(useGuid: true), false, false, false);
55+
QueueDeclareOk q = await _channel.QueueDeclareAsync(GenerateQueueName(), false, false, false);
5656
qs.Add(q.QueueName);
5757
}
5858
await CloseAndWaitForRecoveryAsync();

projects/Test/Integration/TestBasicPublish.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ public async Task TestMaxInboundMessageBodySize()
177177
using var cts = new CancellationTokenSource(WaitSpan);
178178
using CancellationTokenRegistration ctr = cts.Token.Register(() => tcs.SetCanceled());
179179

180-
const ushort maxMsgSize = 8192;
180+
const ushort maxMsgSize = 768;
181181

182182
int count = 0;
183183
byte[] msg0 = _encoding.GetBytes("hi");
184-
byte[] msg1 = GetRandomBody(maxMsgSize * 2);
184+
byte[] msg1 = GetRandomBody(maxMsgSize * 20);
185185

186186
ConnectionFactory cf = CreateConnectionFactory();
187187
cf.AutomaticRecoveryEnabled = false;

projects/Test/Integration/TestToxiproxy.cs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ namespace Test.Integration
4646
public class TestToxiproxy : IntegrationFixture
4747
{
4848
private readonly TimeSpan _heartbeatTimeout = TimeSpan.FromSeconds(1);
49+
private ToxiproxyManager _toxiproxyManager;
50+
private int _proxyPort;
4951

5052
public TestToxiproxy(ITestOutputHelper output) : base(output)
5153
{
@@ -59,7 +61,15 @@ public override Task InitializeAsync()
5961
Assert.Null(_conn);
6062
Assert.Null(_channel);
6163

62-
return Task.CompletedTask;
64+
_toxiproxyManager = new ToxiproxyManager(_testDisplayName, IsRunningInCI, IsWindows);
65+
_proxyPort = _toxiproxyManager.ProxyPort;
66+
return _toxiproxyManager.InitializeAsync();
67+
}
68+
69+
public override async Task DisposeAsync()
70+
{
71+
await _toxiproxyManager.DisposeAsync();
72+
await base.DisposeAsync();
6373
}
6474

6575
[SkippableFact]
@@ -68,11 +78,8 @@ public async Task TestCloseConnection()
6878
{
6979
Skip.IfNot(AreToxiproxyTestsEnabled, "RABBITMQ_TOXIPROXY_TESTS is not set, skipping test");
7080

71-
using var pm = new ToxiproxyManager(_testDisplayName, IsRunningInCI, IsWindows);
72-
await pm.InitializeAsync();
73-
7481
ConnectionFactory cf = CreateConnectionFactory();
75-
cf.Port = pm.ProxyPort;
82+
cf.Port = _proxyPort;
7683
cf.AutomaticRecoveryEnabled = true;
7784
cf.NetworkRecoveryInterval = TimeSpan.FromSeconds(1);
7885
cf.RequestedHeartbeat = TimeSpan.FromSeconds(1);
@@ -172,11 +179,11 @@ async Task PublishLoop()
172179

173180
Assert.True(await messagePublishedTcs.Task);
174181

175-
Task disableProxyTask = pm.DisableAsync();
182+
Task disableProxyTask = _toxiproxyManager.DisableAsync();
176183

177184
await Task.WhenAll(disableProxyTask, connectionShutdownTcs.Task);
178185

179-
Task enableProxyTask = pm.EnableAsync();
186+
Task enableProxyTask = _toxiproxyManager.EnableAsync();
180187

181188
Task whenAllTask = Task.WhenAll(enableProxyTask, recoverySucceededTcs.Task);
182189
await whenAllTask.WaitAsync(TimeSpan.FromSeconds(15));
@@ -193,11 +200,8 @@ public async Task TestThatStoppedSocketResultsInHeartbeatTimeout()
193200
{
194201
Skip.IfNot(AreToxiproxyTestsEnabled, "RABBITMQ_TOXIPROXY_TESTS is not set, skipping test");
195202

196-
using var pm = new ToxiproxyManager(_testDisplayName, IsRunningInCI, IsWindows);
197-
await pm.InitializeAsync();
198-
199203
ConnectionFactory cf = CreateConnectionFactory();
200-
cf.Port = pm.ProxyPort;
204+
cf.Port = _proxyPort;
201205
cf.RequestedHeartbeat = _heartbeatTimeout;
202206
cf.AutomaticRecoveryEnabled = false;
203207

@@ -229,7 +233,7 @@ public async Task TestThatStoppedSocketResultsInHeartbeatTimeout()
229233
timeoutToxic.Attributes.Timeout = 0;
230234
timeoutToxic.Toxicity = 1.0;
231235

232-
Task<TimeoutToxic> addToxicTask = pm.AddToxicAsync(timeoutToxic);
236+
Task<TimeoutToxic> addToxicTask = _toxiproxyManager.AddToxicAsync(timeoutToxic);
233237

234238
await Assert.ThrowsAsync<AlreadyClosedException>(() =>
235239
{
@@ -243,11 +247,8 @@ public async Task TestTcpReset_GH1464()
243247
{
244248
Skip.IfNot(AreToxiproxyTestsEnabled, "RABBITMQ_TOXIPROXY_TESTS is not set, skipping test");
245249

246-
using var pm = new ToxiproxyManager(_testDisplayName, IsRunningInCI, IsWindows);
247-
await pm.InitializeAsync();
248-
249250
ConnectionFactory cf = CreateConnectionFactory();
250-
cf.Endpoint = new AmqpTcpEndpoint(IPAddress.Loopback.ToString(), pm.ProxyPort);
251+
cf.Endpoint = new AmqpTcpEndpoint(IPAddress.Loopback.ToString(), _proxyPort);
251252
cf.RequestedHeartbeat = TimeSpan.FromSeconds(5);
252253
cf.AutomaticRecoveryEnabled = true;
253254

@@ -283,11 +284,11 @@ public async Task TestTcpReset_GH1464()
283284
resetPeerToxic.Attributes.Timeout = 500;
284285
resetPeerToxic.Toxicity = 1.0;
285286

286-
Task<ResetPeerToxic> addToxicTask = pm.AddToxicAsync(resetPeerToxic);
287+
Task<ResetPeerToxic> addToxicTask = _toxiproxyManager.AddToxicAsync(resetPeerToxic);
287288

288289
await Task.WhenAll(addToxicTask, connectionShutdownTcs.Task);
289290

290-
await pm.RemoveToxicAsync(toxicName);
291+
await _toxiproxyManager.RemoveToxicAsync(toxicName);
291292

292293
await recoveryTask;
293294
}
@@ -302,11 +303,8 @@ public async Task TestPublisherConfirmationThrottling()
302303
const int MaxOutstandingConfirms = 8;
303304
const int BatchSize = MaxOutstandingConfirms * 2;
304305

305-
using var pm = new ToxiproxyManager(_testDisplayName, IsRunningInCI, IsWindows);
306-
await pm.InitializeAsync();
307-
308306
ConnectionFactory cf = CreateConnectionFactory();
309-
cf.Endpoint = new AmqpTcpEndpoint(IPAddress.Loopback.ToString(), pm.ProxyPort);
307+
cf.Endpoint = new AmqpTcpEndpoint(IPAddress.Loopback.ToString(), _proxyPort);
310308
cf.RequestedHeartbeat = TimeSpan.FromSeconds(5);
311309
cf.AutomaticRecoveryEnabled = true;
312310

@@ -371,7 +369,7 @@ public async Task TestPublisherConfirmationThrottling()
371369

372370
await Task.Delay(TimeSpan.FromSeconds(1));
373371

374-
Task<BandwidthToxic> addToxicTask = pm.AddToxicAsync(bandwidthToxic);
372+
Task<BandwidthToxic> addToxicTask = _toxiproxyManager.AddToxicAsync(bandwidthToxic);
375373

376374
while (true)
377375
{
@@ -387,7 +385,7 @@ public async Task TestPublisherConfirmationThrottling()
387385
}
388386

389387
await addToxicTask.WaitAsync(WaitSpan);
390-
await pm.RemoveToxicAsync(toxicName).WaitAsync(WaitSpan);
388+
await _toxiproxyManager.RemoveToxicAsync(toxicName).WaitAsync(WaitSpan);
391389

392390
await messagesPublishedTcs.Task.WaitAsync(WaitSpan);
393391
await publishTask.WaitAsync(WaitSpan);

projects/Test/Integration/ToxiproxyManager.cs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Integration
1111
{
12-
public class ToxiproxyManager : IDisposable
12+
public class ToxiproxyManager : IAsyncDisposable
1313
{
1414
private const string ProxyNamePrefix = "rmq";
1515
private const ushort ProxyPortStart = 55669;
@@ -21,7 +21,7 @@ public class ToxiproxyManager : IDisposable
2121
private readonly Client _proxyClient;
2222
private readonly Proxy _proxy;
2323

24-
private bool _disposedValue;
24+
private bool _disposedValue = false;
2525

2626
public ToxiproxyManager(string testDisplayName, bool isRunningInCI, bool isWindows)
2727
{
@@ -35,11 +35,11 @@ public ToxiproxyManager(string testDisplayName, bool isRunningInCI, bool isWindo
3535
_proxyPort = Interlocked.Increment(ref s_proxyPort);
3636

3737
/*
38-
string now = DateTime.UtcNow.ToString("o", System.Globalization.CultureInfo.InvariantCulture);
39-
Console.WriteLine("{0} [DEBUG] {1} _proxyPort {2}", now, testDisplayName, _proxyPort);
40-
*/
41-
42-
_proxyConnection = new Connection(resetAllToxicsAndProxiesOnClose: true);
38+
* Note:
39+
* Do NOT set resetAllToxicsAndProxiesOnClose to true, because it will
40+
* clear proxies being used by parallel TFM test runs
41+
*/
42+
_proxyConnection = new Connection(resetAllToxicsAndProxiesOnClose: false);
4343
_proxyClient = _proxyConnection.Client();
4444

4545
// to start, assume everything is on localhost
@@ -70,17 +70,9 @@ public ToxiproxyManager(string testDisplayName, bool isRunningInCI, bool isWindo
7070

7171
public async Task InitializeAsync()
7272
{
73-
string proxyName = $"{ProxyNamePrefix}-{_testDisplayName}-{Util.Now}-{Util.GenerateShortUuid()}";
73+
string proxyName = $"{ProxyNamePrefix}-{_testDisplayName}-{Util.Now}-{Guid.NewGuid()}";
7474
_proxy.Name = proxyName;
7575

76-
try
77-
{
78-
await _proxyClient.DeleteAsync(_proxy);
79-
}
80-
catch
81-
{
82-
}
83-
8476
ushort retryCount = 5;
8577
do
8678
{
@@ -128,30 +120,24 @@ public Task DisableAsync()
128120
return _proxyClient.UpdateAsync(_proxy);
129121
}
130122

131-
public void Dispose()
132-
{
133-
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
134-
Dispose(disposing: true);
135-
GC.SuppressFinalize(this);
136-
}
137-
138-
protected virtual void Dispose(bool disposing)
123+
public async ValueTask DisposeAsync()
139124
{
140125
if (!_disposedValue)
141126
{
142-
if (disposing)
127+
try
143128
{
144-
try
145-
{
146-
_proxyClient.DeleteAsync(_proxy).GetAwaiter().GetResult();
147-
_proxyConnection.Dispose();
148-
}
149-
catch
150-
{
151-
}
129+
await _proxyClient.DeleteAsync(_proxy);
130+
_proxyConnection.Dispose();
131+
}
132+
catch (Exception ex)
133+
{
134+
string now = DateTime.Now.ToString("o", CultureInfo.InvariantCulture);
135+
Console.Error.WriteLine("{0} [ERROR] error disposing proxy '{1}': {2}", now, _proxy.Name, ex);
136+
}
137+
finally
138+
{
139+
_disposedValue = true;
152140
}
153-
154-
_disposedValue = true;
155141
}
156142
}
157143
}

0 commit comments

Comments
 (0)