|
47 | 47 | using System.Collections.Generic;
|
48 | 48 | using System.Threading;
|
49 | 49 |
|
50 |
| -#pragma warning disable 0168 |
| 50 | +#pragma warning disable 0618 |
51 | 51 |
|
52 | 52 | namespace RabbitMQ.Client.Unit
|
53 | 53 | {
|
| 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 | + } |
54 | 68 | [TestFixture]
|
55 | 69 | public class TestConnectionRecovery : IntegrationFixture
|
56 | 70 | {
|
| 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 | + |
57 | 80 | [SetUp]
|
58 | 81 | public override void Init()
|
59 | 82 | {
|
@@ -97,52 +120,56 @@ public void TestBasicConnectionRecovery()
|
97 | 120 | [Test]
|
98 | 121 | public void TestBasicConnectionRecoveryWithHostnameList()
|
99 | 122 | {
|
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 | + } |
105 | 129 | }
|
106 | 130 |
|
107 | 131 | [Test]
|
108 | 132 | public void TestBasicConnectionRecoveryWithHostnameListAndUnreachableHosts()
|
109 | 133 | {
|
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 | + } |
115 | 140 | }
|
116 | 141 |
|
117 | 142 | [Test]
|
118 | 143 | public void TestBasicConnectionRecoveryWithEndpointList()
|
119 | 144 | {
|
120 |
| - var c = CreateAutorecoveringConnection( |
| 145 | + using(var c = CreateAutorecoveringConnection( |
121 | 146 | new List<AmqpTcpEndpoint>
|
122 | 147 | {
|
123 | 148 | new AmqpTcpEndpoint("127.0.0.1"),
|
124 | 149 | 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 | + } |
130 | 156 | }
|
131 | 157 |
|
132 | 158 | [Test]
|
133 | 159 | public void TestBasicConnectionRecoveryWithEndpointListAndUnreachableHosts()
|
134 | 160 | {
|
135 |
| - var c = CreateAutorecoveringConnection( |
| 161 | + using(var c = CreateAutorecoveringConnection( |
136 | 162 | new List<AmqpTcpEndpoint>
|
137 | 163 | {
|
138 | 164 | new AmqpTcpEndpoint("191.72.44.22"),
|
139 | 165 | new AmqpTcpEndpoint("127.0.0.1"),
|
140 | 166 | 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 | + } |
146 | 173 | }
|
147 | 174 |
|
148 | 175 | [Test]
|
@@ -239,57 +266,59 @@ public void TestClientNamedQueueRecoveryOnServerRestart()
|
239 | 266 | [Test]
|
240 | 267 | public void TestConsumerWorkServiceRecovery()
|
241 | 268 | {
|
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); |
249 | 277 |
|
250 |
| - CloseAndWaitForRecovery(c); |
| 278 | + CloseAndWaitForRecovery(c); |
251 | 279 |
|
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(); |
255 | 283 |
|
256 |
| - m.BasicPublish("", q, null, encoding.GetBytes("msg")); |
257 |
| - Wait(latch); |
| 284 | + m.BasicPublish("", q, null, encoding.GetBytes("msg")); |
| 285 | + Wait(latch); |
258 | 286 |
|
259 |
| - m.QueueDelete(q); |
260 |
| - c.Close(); |
| 287 | + m.QueueDelete(q); |
| 288 | + } |
261 | 289 | }
|
262 | 290 |
|
263 | 291 | [Test]
|
264 | 292 | public void TestConsumerRecoveryOnClientNamedQueueWithOneRecovery()
|
265 | 293 | {
|
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); |
273 | 302 |
|
274 |
| - string latestName = null; |
| 303 | + string latestName = null; |
275 | 304 |
|
276 |
| - c.QueueNameChangeAfterRecovery += (source, ea) => { latestName = ea.NameAfter; }; |
| 305 | + c.QueueNameChangeAfterRecovery += (source, ea) => { latestName = ea.NameAfter; }; |
277 | 306 |
|
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); |
284 | 313 |
|
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(); |
287 | 316 |
|
288 |
| - m.BasicPublish("", q, null, encoding.GetBytes("msg")); |
289 |
| - Wait(latch); |
| 317 | + m.BasicPublish("", q, null, encoding.GetBytes("msg")); |
| 318 | + Wait(latch); |
290 | 319 |
|
291 |
| - m.QueueDelete(q); |
292 |
| - c.Close(); |
| 320 | + m.QueueDelete(q); |
| 321 | + } |
293 | 322 | }
|
294 | 323 |
|
295 | 324 | [Test]
|
@@ -327,7 +356,7 @@ public void TestCreateModelOnClosedAutorecoveringConnectionDoesNotHang()
|
327 | 356 | c.CreateModel();
|
328 | 357 | Assert.Fail("Expected an exception");
|
329 | 358 | }
|
330 |
| - catch (AlreadyClosedException ace) |
| 359 | + catch (AlreadyClosedException) |
331 | 360 | {
|
332 | 361 | // expected
|
333 | 362 | }
|
@@ -604,7 +633,7 @@ public void TestRecoveryWithTopologyDisabled()
|
604 | 633 | ch.QueueDeclarePassive(s);
|
605 | 634 | Assert.Fail("Expected an exception");
|
606 | 635 | }
|
607 |
| - catch (OperationInterruptedException e) |
| 636 | + catch (OperationInterruptedException) |
608 | 637 | {
|
609 | 638 | // expected
|
610 | 639 | }
|
|
0 commit comments