Skip to content

Commit 006d541

Browse files
Improve heartbeat tests
1 parent 4a3b618 commit 006d541

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ protected void AssertPreconditionFailed(ShutdownEventArgs args)
320320
AssertShutdownError(args, Constants.PreconditionFailed);
321321
}
322322

323+
protected bool InitiatedByPeerOrLibrary(ShutdownEventArgs evt)
324+
{
325+
return !(evt.Initiator == ShutdownInitiator.Application);
326+
}
327+
323328
//
324329
// Concurrency
325330
//
@@ -372,7 +377,7 @@ protected Process ExecCommand(string ctl, string args, string changeDirTo)
372377
{
373378
var proc = new Process
374379
{
375-
StartInfo =
380+
StartInfo =
376381
{
377382
CreateNoWindow = true,
378383
UseShellExecute = false

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
using NUnit.Framework;
4242
using RabbitMQ.Client.Impl;
4343
using System;
44+
using System.Collections.Generic;
4445
using System.Threading;
4546

4647
namespace RabbitMQ.Client.Unit
@@ -67,13 +68,60 @@ public void TestThatHeartbeatWriterUsesConfigurableInterval()
6768
{
6869
lock (conn)
6970
{
70-
wasShutdown = true;
71+
if(InitiatedByPeerOrLibrary(evt))
72+
{
73+
CheckInitiator(evt);
74+
wasShutdown = true;
75+
}
7176
}
7277
};
7378
Thread.Sleep(heartbeatTimeout * 10 * 1000);
7479

7580
Assert.IsFalse(wasShutdown, "shutdown event should not have been fired");
7681
Assert.IsTrue(conn.IsOpen, "connection should be open");
82+
83+
conn.Close();
84+
}
85+
86+
[Test]
87+
[Category("Focus")]
88+
public void TestHundredsOfConnectionsWithRandomHeartbeatInterval()
89+
{
90+
var rnd = new Random();
91+
List<IConnection> xs = new List<IConnection>();
92+
for(var i = 0; i < 200; i++)
93+
{
94+
var n = Convert.ToUInt16(rnd.Next(2, 6));
95+
var cf = new ConnectionFactory() { RequestedHeartbeat = n, AutomaticRecoveryEnabled = false };
96+
var conn = cf.CreateConnection();
97+
xs.Add(conn);
98+
var ch = conn.CreateModel();
99+
bool wasShutdown = false;
100+
101+
conn.ConnectionShutdown += (sender, evt) =>
102+
{
103+
CheckInitiator(evt);
104+
};
105+
}
106+
107+
Thread.Sleep(20 * 1000);
108+
109+
foreach(var x in xs)
110+
{
111+
x.Close();
112+
}
113+
114+
}
115+
116+
private void CheckInitiator(ShutdownEventArgs evt)
117+
{
118+
if(InitiatedByPeerOrLibrary(evt))
119+
{
120+
var s = String.Format("Shutdown: {0}, initiated by: {1}",
121+
evt, evt.Initiator);
122+
Console.WriteLine(s);
123+
Assert.Fail(s);
124+
}
77125
}
78126
}
79127
}

0 commit comments

Comments
 (0)