Skip to content

Commit fe7a8d9

Browse files
committed
ensure connection recovery tests clean up connection on failures
1 parent 8111a53 commit fe7a8d9

File tree

3 files changed

+91
-60
lines changed

3 files changed

+91
-60
lines changed

projects/client/Unit/src/unit/TestConnectionRecovery.cs

Lines changed: 87 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,32 @@
5151

5252
namespace RabbitMQ.Client.Unit
5353
{
54+
class DisposableConnection : IDisposable
55+
{
56+
public DisposableConnection(AutorecoveringConnection c)
57+
{
58+
this.Connection = c;
59+
}
60+
61+
public AutorecoveringConnection Connection {get; private set;}
62+
63+
public void Dispose()
64+
{
65+
this.Connection.Close();
66+
}
67+
}
5468
[TestFixture]
5569
public class TestConnectionRecovery : IntegrationFixture
5670
{
71+
private DisposableConnection CreateWrappedAutorecoveringConnection()
72+
{
73+
return new DisposableConnection(CreateAutorecoveringConnection());
74+
}
75+
private DisposableConnection CreateWrappedAutorecoveringConnection(IList<string> hostnames)
76+
{
77+
return new DisposableConnection(CreateAutorecoveringConnection(hostnames));
78+
}
79+
5780
[SetUp]
5881
public override void Init()
5982
{
@@ -97,52 +120,56 @@ public void TestBasicConnectionRecovery()
97120
[Test]
98121
public void TestBasicConnectionRecoveryWithHostnameList()
99122
{
100-
var c = CreateAutorecoveringConnection(new List<string> { "127.0.0.1", "localhost" });
101-
Assert.IsTrue(c.IsOpen);
102-
CloseAndWaitForRecovery(c);
103-
Assert.IsTrue(c.IsOpen);
104-
c.Close();
123+
using(var c = CreateAutorecoveringConnection(new List<string> { "127.0.0.1", "localhost" }))
124+
{
125+
Assert.IsTrue(c.IsOpen);
126+
CloseAndWaitForRecovery(c);
127+
Assert.IsTrue(c.IsOpen);
128+
}
105129
}
106130

107131
[Test]
108132
public void TestBasicConnectionRecoveryWithHostnameListAndUnreachableHosts()
109133
{
110-
var c = CreateAutorecoveringConnection(new List<string> { "191.72.44.22", "127.0.0.1", "localhost" });
111-
Assert.IsTrue(c.IsOpen);
112-
CloseAndWaitForRecovery(c);
113-
Assert.IsTrue(c.IsOpen);
114-
c.Close();
134+
using(var c = CreateAutorecoveringConnection(new List<string> { "191.72.44.22", "127.0.0.1", "localhost" }))
135+
{
136+
Assert.IsTrue(c.IsOpen);
137+
CloseAndWaitForRecovery(c);
138+
Assert.IsTrue(c.IsOpen);
139+
}
115140
}
116141

117142
[Test]
118143
public void TestBasicConnectionRecoveryWithEndpointList()
119144
{
120-
var c = CreateAutorecoveringConnection(
145+
using(var c = CreateAutorecoveringConnection(
121146
new List<AmqpTcpEndpoint>
122147
{
123148
new AmqpTcpEndpoint("127.0.0.1"),
124149
new AmqpTcpEndpoint("localhost")
125-
});
126-
Assert.IsTrue(c.IsOpen);
127-
CloseAndWaitForRecovery(c);
128-
Assert.IsTrue(c.IsOpen);
129-
c.Close();
150+
}))
151+
{
152+
Assert.IsTrue(c.IsOpen);
153+
CloseAndWaitForRecovery(c);
154+
Assert.IsTrue(c.IsOpen);
155+
}
130156
}
131157

132158
[Test]
133159
public void TestBasicConnectionRecoveryWithEndpointListAndUnreachableHosts()
134160
{
135-
var c = CreateAutorecoveringConnection(
161+
using(var c = CreateAutorecoveringConnection(
136162
new List<AmqpTcpEndpoint>
137163
{
138164
new AmqpTcpEndpoint("191.72.44.22"),
139165
new AmqpTcpEndpoint("127.0.0.1"),
140166
new AmqpTcpEndpoint("localhost")
141-
});
142-
Assert.IsTrue(c.IsOpen);
143-
CloseAndWaitForRecovery(c);
144-
Assert.IsTrue(c.IsOpen);
145-
c.Close();
167+
}))
168+
{
169+
Assert.IsTrue(c.IsOpen);
170+
CloseAndWaitForRecovery(c);
171+
Assert.IsTrue(c.IsOpen);
172+
}
146173
}
147174

148175
[Test]
@@ -239,57 +266,59 @@ public void TestClientNamedQueueRecoveryOnServerRestart()
239266
[Test]
240267
public void TestConsumerWorkServiceRecovery()
241268
{
242-
AutorecoveringConnection c = CreateAutorecoveringConnection();
243-
IModel m = c.CreateModel();
244-
string q = m.QueueDeclare("dotnet-client.recovery.consumer_work_pool1",
245-
false, false, false, null).QueueName;
246-
var cons = new EventingBasicConsumer(m);
247-
m.BasicConsume(q, true, cons);
248-
AssertConsumerCount(m, q, 1);
269+
using(var c = CreateAutorecoveringConnection())
270+
{
271+
IModel m = c.CreateModel();
272+
string q = m.QueueDeclare("dotnet-client.recovery.consumer_work_pool1",
273+
false, false, false, null).QueueName;
274+
var cons = new EventingBasicConsumer(m);
275+
m.BasicConsume(q, true, cons);
276+
AssertConsumerCount(m, q, 1);
249277

250-
CloseAndWaitForRecovery(c);
278+
CloseAndWaitForRecovery(c);
251279

252-
Assert.IsTrue(m.IsOpen);
253-
var latch = new ManualResetEvent(false);
254-
cons.Received += (s, args) => latch.Set();
280+
Assert.IsTrue(m.IsOpen);
281+
var latch = new ManualResetEvent(false);
282+
cons.Received += (s, args) => latch.Set();
255283

256-
m.BasicPublish("", q, null, encoding.GetBytes("msg"));
257-
Wait(latch);
284+
m.BasicPublish("", q, null, encoding.GetBytes("msg"));
285+
Wait(latch);
258286

259-
m.QueueDelete(q);
260-
c.Close();
287+
m.QueueDelete(q);
288+
}
261289
}
262290

263291
[Test]
264292
public void TestConsumerRecoveryOnClientNamedQueueWithOneRecovery()
265293
{
266-
AutorecoveringConnection c = CreateAutorecoveringConnection();
267-
IModel m = c.CreateModel();
268-
string q = m.QueueDeclare("dotnet-client.recovery.queue1",
269-
false, false, false, null).QueueName;
270-
var cons = new EventingBasicConsumer(m);
271-
m.BasicConsume(q, true, cons);
272-
AssertConsumerCount(m, q, 1);
294+
using (var c = CreateAutorecoveringConnection())
295+
{
296+
IModel m = c.CreateModel();
297+
string q = m.QueueDeclare("dotnet-client.recovery.queue1",
298+
false, false, false, null).QueueName;
299+
var cons = new EventingBasicConsumer(m);
300+
m.BasicConsume(q, true, cons);
301+
AssertConsumerCount(m, q, 1);
273302

274-
string latestName = null;
303+
string latestName = null;
275304

276-
c.QueueNameChangeAfterRecovery += (source, ea) => { latestName = ea.NameAfter; };
305+
c.QueueNameChangeAfterRecovery += (source, ea) => { latestName = ea.NameAfter; };
277306

278-
CloseAndWaitForRecovery(c);
279-
AssertConsumerCount(m, latestName, 1);
280-
CloseAndWaitForRecovery(c);
281-
AssertConsumerCount(m, latestName, 1);
282-
CloseAndWaitForRecovery(c);
283-
AssertConsumerCount(m, latestName, 1);
307+
CloseAndWaitForRecovery(c);
308+
AssertConsumerCount(m, latestName, 1);
309+
CloseAndWaitForRecovery(c);
310+
AssertConsumerCount(m, latestName, 1);
311+
CloseAndWaitForRecovery(c);
312+
AssertConsumerCount(m, latestName, 1);
284313

285-
var latch = new ManualResetEvent(false);
286-
cons.Received += (s, args) => latch.Set();
314+
var latch = new ManualResetEvent(false);
315+
cons.Received += (s, args) => latch.Set();
287316

288-
m.BasicPublish("", q, null, encoding.GetBytes("msg"));
289-
Wait(latch);
317+
m.BasicPublish("", q, null, encoding.GetBytes("msg"));
318+
Wait(latch);
290319

291-
m.QueueDelete(q);
292-
c.Close();
320+
m.QueueDelete(q);
321+
}
293322
}
294323

295324
[Test]

run-test.bat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ dotnet restore .\projects\client\RabbitMQ.Client || exit /b
44
dotnet build .\projects\client\RabbitMQ.Client || exit /b
55
dotnet restore .\projects\client\Unit || exit /b
66
dotnet build .\projects\client\Unit || exit /b
7-
dotnet test -f netcoreapp1.0 .\projects\client\Unit --where="cat != RequireSMP & cat != LongRunning & cat != GCTest"
7+
CD .\projects\client\Unit
8+
dotnet test -f netcoreapp1.0 --where="cat != RequireSMP & cat != LongRunning & cat != GCTest"

run-test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dotnet restore ./projects/client/RabbitMQ.Client
66
dotnet build ./projects/client/RabbitMQ.Client
77
dotnet restore ./projects/client/Unit
88
dotnet build ./projects/client/Unit
9-
dotnet test -f netcoreapp1.0 ./projects/client/Unit --where='cat != RequireSMP & cat != LongRunning & cat != GCTest'
9+
cd ./projects/client/Unit
10+
dotnet test -f netcoreapp1.0 --where='cat != RequireSMP & cat != LongRunning & cat != GCTest'
1011

1112

0 commit comments

Comments
 (0)