Skip to content

Commit fb94c1d

Browse files
authored
Merge pull request #1450 from rabbitmq/rabbitmq-dotnet-client-1086
Add test that demonstrates the current behavior of a recovered channe…
2 parents 99359a8 + 48065f9 commit fb94c1d

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

projects/Test/Common/RabbitMQCtl.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
using System;
3333
using System.Diagnostics;
3434
using System.IO;
35-
using System.Text;
3635
using System.Text.RegularExpressions;
3736
using RabbitMQ.Client;
3837
using Xunit.Abstractions;
@@ -188,12 +187,6 @@ private static bool IsRunningOnMonoOrDotNetCore()
188187
#endif
189188
}
190189

191-
private static void Publish(IConnection conn, Encoding encoding)
192-
{
193-
IChannel ch = conn.CreateChannel();
194-
ch.BasicPublish("amq.fanout", "", encoding.GetBytes("message"));
195-
}
196-
197190
private static Process CreateProcess(string cmd, string arguments, string workDirectory = null)
198191
{
199192
return new Process

projects/Test/SequentialIntegration/TestConnectionRecovery.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,60 @@ public void TestBasicChannelRecoveryOnServerRestart()
194194
Assert.True(_channel.IsOpen);
195195
}
196196

197+
// https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/1086
198+
[Fact]
199+
public async Task TestChannelAfterDispose_GH1086()
200+
{
201+
TaskCompletionSource<bool> sawChannelShutdownTcs = new TaskCompletionSource<bool>();
202+
203+
void _channel_ChannelShutdown(object sender, ShutdownEventArgs e)
204+
{
205+
sawChannelShutdownTcs.SetResult(true);
206+
}
207+
208+
_channel.ChannelShutdown += _channel_ChannelShutdown;
209+
210+
Assert.True(_channel.IsOpen);
211+
212+
string queueName = GenerateQueueName();
213+
RabbitMQ.Client.QueueDeclareOk queueDeclareOk = await _channel.QueueDeclareAsync(queue: queueName, exclusive: false, autoDelete: false);
214+
Assert.Equal(queueName, queueDeclareOk.QueueName);
215+
216+
byte[] body = GetRandomBody(64);
217+
218+
RestartServerAndWaitForRecovery();
219+
220+
Task publishTask = Task.Run(async () =>
221+
{
222+
while (false == sawChannelShutdownTcs.Task.IsCompleted)
223+
{
224+
try
225+
{
226+
await _channel.BasicPublishAsync(exchange: "", routingKey: queueName, body: body, mandatory: true);
227+
await Task.Delay(TimeSpan.FromSeconds(1));
228+
}
229+
catch (Exception ex)
230+
{
231+
_output.WriteLine($"{_testDisplayName} caught exception: {ex}");
232+
break;
233+
}
234+
}
235+
});
236+
237+
await Task.WhenAny(sawChannelShutdownTcs.Task, publishTask);
238+
239+
bool sawChannelShutdown = await sawChannelShutdownTcs.Task;
240+
Assert.True(sawChannelShutdown);
241+
242+
// This is false because the channel has been recovered
243+
Assert.False(_channel.IsClosed);
244+
245+
_channel.Dispose();
246+
Assert.True(_channel.IsClosed);
247+
248+
await publishTask;
249+
}
250+
197251
[Fact]
198252
public void TestBlockedListenersRecovery()
199253
{

0 commit comments

Comments
 (0)