Skip to content

Commit 2d04378

Browse files
Merge pull request #1148 from rabbitmq/rabbitmq-dotnet-client-1140
Fix flaky connection recovery tests.
2 parents 8324fa0 + c32ac3a commit 2d04378

File tree

6 files changed

+30
-14
lines changed

6 files changed

+30
-14
lines changed

_site

Submodule _site updated 166 files

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,13 @@ private bool TryPerformAutomaticRecovery()
143143
// 2. Recover queues
144144
// 3. Recover bindings
145145
// 4. Recover consumers
146-
using var recoveryChannel = _innerConnection.CreateModel();
147-
RecoverExchanges(recoveryChannel);
148-
RecoverQueues(recoveryChannel);
149-
RecoverBindings(recoveryChannel);
146+
using (var recoveryChannel = _innerConnection.CreateModel())
147+
{
148+
RecoverExchanges(recoveryChannel);
149+
RecoverQueues(recoveryChannel);
150+
RecoverBindings(recoveryChannel);
151+
}
152+
150153
}
151154
RecoverModelsAndItsConsumers();
152155
}

projects/RabbitMQ.Client/client/impl/AutorecoveringModel.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,19 @@ internal void AutomaticallyRecover(AutorecoveringConnection conn, bool recoverCo
166166
newChannel.TxSelect();
167167
}
168168

169+
/*
170+
* https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/1140
171+
* If this assignment is not done before recovering consumers, there is a good
172+
* chance that an invalid Model will be used to handle a basic.deliver frame,
173+
* with the resulting basic.ack never getting sent out.
174+
*/
175+
_innerChannel = newChannel;
176+
169177
if (recoverConsumers)
170178
{
171179
_connection.RecoverConsumers(this, newChannel);
172180
}
173181

174-
_innerChannel = newChannel;
175182
_innerChannel.RunRecoveryEventHandlers(this);
176183
}
177184

projects/Unit/Fixtures.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ internal void StopRabbitMQ()
382382
internal void StartRabbitMQ()
383383
{
384384
RabbitMQCtl.StartRabbitMQ();
385+
RabbitMQCtl.AwaitRabbitMQ();
385386
}
386387

387388
//

projects/Unit/RabbitMQCtl.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,10 @@ public static void StartRabbitMQ()
228228
{
229229
ExecRabbitMQCtl("start_app");
230230
}
231+
232+
public static void AwaitRabbitMQ()
233+
{
234+
ExecRabbitMQCtl("await_startup");
235+
}
231236
}
232237
}

projects/Unit/TestConnectionRecovery.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public override void Dispose()
9090

9191
}
9292

93-
[Fact(Skip="TODO flaky")]
93+
[Fact]
9494
public void TestBasicAckAfterChannelRecovery()
9595
{
9696
var allMessagesSeenLatch = new ManualResetEventSlim(false);
@@ -112,7 +112,7 @@ public void TestBasicAckAfterChannelRecovery()
112112
Wait(allMessagesSeenLatch);
113113
}
114114

115-
[Fact(Skip="TODO flaky")]
115+
[Fact]
116116
public void TestBasicNackAfterChannelRecovery()
117117
{
118118
var allMessagesSeenLatch = new ManualResetEventSlim(false);
@@ -134,7 +134,7 @@ public void TestBasicNackAfterChannelRecovery()
134134
Wait(allMessagesSeenLatch);
135135
}
136136

137-
[Fact(Skip="TODO flaky")]
137+
[Fact]
138138
public void TestBasicRejectAfterChannelRecovery()
139139
{
140140
var allMessagesSeenLatch = new ManualResetEventSlim(false);
@@ -851,22 +851,22 @@ public void TestPublishRpcRightAfterReconnect()
851851
var properties = new BasicProperties();
852852
properties.ReplyTo = "amq.rabbitmq.reply-to";
853853

854-
bool done = false;
854+
TimeSpan doneSpan = TimeSpan.FromMilliseconds(100);
855+
var done = new ManualResetEventSlim(false);
855856
Task.Run(() =>
856857
{
857858
try
858859
{
859860

860861
CloseAndWaitForRecovery();
861-
Thread.Sleep(100);
862862
}
863863
finally
864864
{
865-
done = true;
865+
done.Set();
866866
}
867867
});
868868

869-
while (!done)
869+
while (!done.IsSet)
870870
{
871871
try
872872
{
@@ -880,7 +880,7 @@ public void TestPublishRpcRightAfterReconnect()
880880
Assert.NotEqual(406, a.ShutdownReason.ReplyCode);
881881
}
882882
}
883-
Thread.Sleep(1);
883+
done.Wait(doneSpan);
884884
}
885885
}
886886

0 commit comments

Comments
 (0)