Skip to content

Commit 9bf0e5c

Browse files
committed
Add test code for issue #1464
1 parent bfba294 commit 9bf0e5c

File tree

4 files changed

+77
-23
lines changed

4 files changed

+77
-23
lines changed

projects/Test/Common/IntegrationFixture.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,22 @@ protected static byte[] GetRandomBody(ushort size = 1024)
551551
return body;
552552
}
553553

554+
protected static Task WaitForRecoveryAsync(IConnection conn)
555+
{
556+
TaskCompletionSource<bool> tcs = PrepareForRecovery((AutorecoveringConnection)conn);
557+
return WaitAsync(tcs, "recovery succeded");
558+
}
559+
560+
protected static TaskCompletionSource<bool> PrepareForRecovery(IConnection conn)
561+
{
562+
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
563+
564+
AutorecoveringConnection aconn = conn as AutorecoveringConnection;
565+
aconn.RecoverySucceeded += (source, ea) => tcs.SetResult(true);
566+
567+
return tcs;
568+
}
569+
554570
public static string Now => DateTime.UtcNow.ToString("s", CultureInfo.InvariantCulture);
555571
}
556572
}

projects/Test/Common/TestConnectionRecoveryBase.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,6 @@ protected static TaskCompletionSource<bool> PrepareForShutdown(IConnection conn)
231231
return tcs;
232232
}
233233

234-
protected static TaskCompletionSource<bool> PrepareForRecovery(IConnection conn)
235-
{
236-
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
237-
238-
AutorecoveringConnection aconn = conn as AutorecoveringConnection;
239-
aconn.RecoverySucceeded += (source, ea) => tcs.SetResult(true);
240-
241-
return tcs;
242-
}
243-
244234
protected static Task<bool> WaitForConfirmsWithCancellationAsync(IChannel m)
245235
{
246236
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(4)))
@@ -249,18 +239,6 @@ protected static Task<bool> WaitForConfirmsWithCancellationAsync(IChannel m)
249239
}
250240
}
251241

252-
protected Task WaitForRecoveryAsync()
253-
{
254-
TaskCompletionSource<bool> tcs = PrepareForRecovery((AutorecoveringConnection)_conn);
255-
return WaitAsync(tcs, "recovery succeded");
256-
}
257-
258-
internal Task WaitForRecoveryAsync(AutorecoveringConnection conn)
259-
{
260-
TaskCompletionSource<bool> tcs = PrepareForRecovery(conn);
261-
return WaitAsync(tcs, "recovery succeeded");
262-
}
263-
264242
protected Task WaitForShutdownAsync()
265243
{
266244
TaskCompletionSource<bool> tcs = PrepareForShutdown(_conn);

projects/Test/Integration/TestToxiproxy.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,66 @@ await Assert.ThrowsAsync<AlreadyClosedException>(() =>
169169
_output.WriteLine($"[INFO] heartbeat timeout took {sw.Elapsed}");
170170
}
171171

172+
[SkippableFact]
173+
[Trait("Category", "Toxiproxy")]
174+
public async Task TestTcpReset_GH1464()
175+
{
176+
Skip.IfNot(AreToxiproxyTestsEnabled, "RABBITMQ_TOXIPROXY_TESTS is not set, skipping test");
177+
178+
ConnectionFactory cf = CreateConnectionFactory();
179+
cf.Endpoint = new AmqpTcpEndpoint(IPAddress.Loopback.ToString(), ProxyPort);
180+
cf.Port = ProxyPort;
181+
cf.RequestedHeartbeat = TimeSpan.FromSeconds(5);
182+
cf.AutomaticRecoveryEnabled = true;
183+
184+
var channelCreatedTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
185+
var connectionShutdownTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
186+
187+
Task recoveryTask = Task.Run(async () =>
188+
{
189+
using (IConnection conn = await cf.CreateConnectionAsync())
190+
{
191+
conn.ConnectionShutdown += (o, ea) =>
192+
{
193+
connectionShutdownTcs.SetResult(true);
194+
};
195+
196+
using (IChannel ch = await conn.CreateChannelAsync())
197+
{
198+
channelCreatedTcs.SetResult(true);
199+
await WaitForRecoveryAsync(conn);
200+
await ch.CloseAsync();
201+
}
202+
203+
await conn.CloseAsync();
204+
}
205+
});
206+
207+
Assert.True(await channelCreatedTcs.Task);
208+
209+
const string toxicName = "rmq-localhost-reset_peer";
210+
var resetPeerToxic = new ResetPeerToxic();
211+
resetPeerToxic.Name = toxicName;
212+
resetPeerToxic.Attributes.Timeout = 500;
213+
resetPeerToxic.Toxicity = 1.0;
214+
215+
var sw = new Stopwatch();
216+
sw.Start();
217+
218+
await _rmqProxy.AddAsync(resetPeerToxic);
219+
Task<Proxy> updateProxyTask = _rmqProxy.UpdateAsync();
220+
221+
await Task.WhenAll(updateProxyTask, connectionShutdownTcs.Task);
222+
223+
await _rmqProxy.RemoveToxicAsync(toxicName);
224+
225+
await recoveryTask;
226+
227+
sw.Stop();
228+
229+
_output.WriteLine($"[INFO] reset peer took {sw.Elapsed}");
230+
}
231+
172232
private bool AreToxiproxyTestsEnabled
173233
{
174234
get

0 commit comments

Comments
 (0)