Skip to content

Commit d3ad1ca

Browse files
merge bug26439 into stable
2 parents 1bd68fc + 795d772 commit d3ad1ca

File tree

4 files changed

+104
-6
lines changed

4 files changed

+104
-6
lines changed

projects/client/RabbitMQ.Client/src/client/impl/AutorecoveringConnection.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,19 @@ public bool AutoClose
397397
}
398398
}
399399

400+
protected void EnsureIsOpen()
401+
{
402+
this.m_delegate.EnsureIsOpen();
403+
}
404+
400405
public IModel CreateModel()
401406
{
407+
this.EnsureIsOpen();
402408
AutorecoveringModel m;
409+
m = new AutorecoveringModel(this,
410+
(RecoveryAwareModel)this.CreateNonRecoveringModel());
403411
lock(this)
404412
{
405-
m = new AutorecoveringModel(this,
406-
(RecoveryAwareModel)this.CreateNonRecoveringModel());
407413
m_models.Add(m);
408414
}
409415
return m;

projects/client/RabbitMQ.Client/src/client/impl/Connection.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,17 @@ public bool AutoClose
352352
}
353353
}
354354

355+
public void EnsureIsOpen()
356+
{
357+
if(!IsOpen)
358+
{
359+
throw new AlreadyClosedException(this.CloseReason);
360+
}
361+
}
362+
355363
public IModel CreateModel()
356364
{
365+
this.EnsureIsOpen();
357366
ISession session = CreateSession();
358367
IFullModel model = (IFullModel)Protocol.CreateModel(session);
359368
model._Private_ChannelOpen("");

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,25 @@ protected Process ExecRabbitMQCtl(string args)
328328
}
329329
}
330330

331-
protected Process ExecCommand(string ctl, string args)
331+
protected Process ExecCommand(string command)
332+
{
333+
return ExecCommand(command, "");
334+
}
335+
336+
protected Process ExecCommand(string command, string args)
337+
{
338+
return ExecCommand(command, args, null);
339+
}
340+
341+
protected Process ExecCommand(string ctl, string args, string changeDirTo)
332342
{
333343
Process proc = new Process();
334344
proc.StartInfo.CreateNoWindow = true;
335345
proc.StartInfo.UseShellExecute = false;
346+
if(changeDirTo != null)
347+
{
348+
proc.StartInfo.WorkingDirectory = changeDirTo;
349+
}
336350

337351
string cmd;
338352
if(IsRunningOnMono()) {
@@ -466,6 +480,23 @@ protected void CloseConnection(string pid)
466480
pid +
467481
"\" \"Closed via rabbitmqctl\"");
468482
}
483+
484+
protected void RestartRabbitMQ()
485+
{
486+
StopRabbitMQ();
487+
Thread.Sleep(500);
488+
StartRabbitMQ();
489+
}
490+
491+
protected void StopRabbitMQ()
492+
{
493+
ExecRabbitMQCtl("stop_app");
494+
}
495+
496+
protected void StartRabbitMQ()
497+
{
498+
ExecRabbitMQCtl("start_app");
499+
}
469500
}
470501

471502
public class TimingFixture

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

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@
4444
using System.Collections.Generic;
4545
using System.Text;
4646
using System.Threading;
47+
using System.IO;
4748

4849
using RabbitMQ.Client.Impl;
4950
using RabbitMQ.Client.Framing.Impl;
5051
using RabbitMQ.Client.Events;
5152
using RabbitMQ.Client.Exceptions;
5253

54+
#pragma warning disable 0168
5355
namespace RabbitMQ.Client.Unit {
5456
[TestFixture]
5557
public class TestConnectionRecovery : IntegrationFixture {
@@ -715,6 +717,32 @@ public void TestBasicRejectAfterChannelRecovery()
715717
TestDelayedBasicAckNackAfterChannelRecovery(cons, latch);
716718
}
717719

720+
[Test]
721+
public void TestCreateModelOnClosedAutorecoveringConnectionDoesNotHang()
722+
{
723+
// we don't want this to recover quickly in this test
724+
var c = CreateAutorecoveringConnection(TimeSpan.FromSeconds(20));
725+
726+
try
727+
{
728+
c.Close();
729+
WaitForShutdown(c);
730+
Assert.IsFalse(c.IsOpen);
731+
c.CreateModel();
732+
Assert.Fail("Expected an exception");
733+
} catch (AlreadyClosedException ace)
734+
{
735+
// expected
736+
} finally
737+
{
738+
StartRabbitMQ();
739+
if(c.IsOpen)
740+
{
741+
c.Abort();
742+
}
743+
}
744+
}
745+
718746
protected void TestDelayedBasicAckNackAfterChannelRecovery(TestBasicConsumer1 cons, AutoResetEvent latch)
719747
{
720748
var q = Model.QueueDeclare(GenerateQueueName(), false, false, false, null).QueueName;
@@ -767,7 +795,19 @@ protected void CloseAllAndWaitForRecovery(AutorecoveringConnection conn)
767795
Wait(rl);
768796
}
769797

770-
protected AutoResetEvent PrepareForShutdown(AutorecoveringConnection conn)
798+
protected void CloseAndWaitForShutdown(AutorecoveringConnection conn)
799+
{
800+
var sl = PrepareForShutdown(conn);
801+
CloseConnection(conn);
802+
Wait(sl);
803+
}
804+
805+
protected void WaitForShutdown(IConnection conn)
806+
{
807+
Wait(PrepareForShutdown(conn));
808+
}
809+
810+
protected AutoResetEvent PrepareForShutdown(IConnection conn)
771811
{
772812
var latch = new AutoResetEvent(false);
773813
conn.ConnectionShutdown += (c, args) =>
@@ -845,11 +885,22 @@ protected void AssertRecordedExchanges(AutorecoveringConnection c, int n)
845885
Assert.AreEqual(n, c.RecordedExchanges.Count);
846886
}
847887

888+
protected IConnection CreateNonRecoveringConnection()
889+
{
890+
var cf = new ConnectionFactory();
891+
return cf.CreateConnection();
892+
}
893+
848894
protected AutorecoveringConnection CreateAutorecoveringConnection()
895+
{
896+
return CreateAutorecoveringConnection(RECOVERY_INTERVAL);
897+
}
898+
899+
protected AutorecoveringConnection CreateAutorecoveringConnection(TimeSpan interval)
849900
{
850901
var cf = new ConnectionFactory();
851902
cf.AutomaticRecoveryEnabled = true;
852-
cf.NetworkRecoveryInterval = RECOVERY_INTERVAL;
903+
cf.NetworkRecoveryInterval = interval;
853904
return (AutorecoveringConnection)cf.CreateConnection();
854905
}
855906

@@ -862,4 +913,5 @@ protected AutorecoveringConnection CreateAutorecoveringConnectionWithTopologyRec
862913
return (AutorecoveringConnection)cf.CreateConnection();
863914
}
864915
}
865-
}
916+
}
917+
#pragma warning restore 0168

0 commit comments

Comments
 (0)