Skip to content

Commit 4d624be

Browse files
authored
Merge pull request #1166 from rabbitmq/rabbitmq-dotnet-client-1165-6.x
Manually port 39a9f2b to 6.x
2 parents b150af9 + dbced1c commit 4d624be

33 files changed

+252
-51
lines changed

projects/RabbitMQ.Client/client/api/IAutorecoveringConnection.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,8 @@
3030
//---------------------------------------------------------------------------
3131

3232
using System;
33-
using System.Collections.Generic;
34-
using System.IO;
35-
using System.Threading;
3633

3734
using RabbitMQ.Client.Events;
38-
using RabbitMQ.Client.Exceptions;
3935

4036
namespace RabbitMQ.Client
4137
{

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,14 @@ public void Init(IEndpointResolver endpoints)
668668
Init(fh);
669669
}
670670

671+
internal IFrameHandler FrameHandler
672+
{
673+
get
674+
{
675+
return _delegate.FrameHandler;
676+
}
677+
}
678+
671679
private void Init(IFrameHandler fh)
672680
{
673681
if (_disposed)

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public void Abort(ushort reasonCode, string reasonText, ShutdownInitiator initia
271271

272272
public void Close(ShutdownEventArgs reason)
273273
{
274-
Close(reason, false, Timeout.InfiniteTimeSpan);
274+
Close(reason, false, TimeSpan.FromSeconds(30));
275275
}
276276

277277
///<summary>Try to close connection in a graceful way</summary>
@@ -286,7 +286,7 @@ public void Close(ShutdownEventArgs reason)
286286
///</para>
287287
///<para>
288288
///Timeout determines how much time internal close operations should be given
289-
///to complete. System.Threading.Timeout.InfiniteTimeSpan value means infinity.
289+
///to complete.
290290
///</para>
291291
///</remarks>
292292
public void Close(ShutdownEventArgs reason, bool abort, TimeSpan timeout)
@@ -307,7 +307,10 @@ public void Close(ShutdownEventArgs reason, bool abort, TimeSpan timeout)
307307
{
308308
// Try to send connection.close
309309
// Wait for CloseOk in the MainLoop
310-
_session0.Transmit(ConnectionCloseWrapper(reason.ReplyCode, reason.ReplyText));
310+
if (!_closed)
311+
{
312+
_session0.Transmit(ConnectionCloseWrapper(reason.ReplyCode, reason.ReplyText));
313+
}
311314
}
312315
catch (AlreadyClosedException)
313316
{
@@ -453,9 +456,12 @@ public bool HardProtocolExceptionHandler(HardProtocolException hpe)
453456
_session0.SetSessionClosing(false);
454457
try
455458
{
456-
_session0.Transmit(ConnectionCloseWrapper(
457-
hpe.ShutdownReason.ReplyCode,
458-
hpe.ShutdownReason.ReplyText));
459+
if (!_closed)
460+
{
461+
_session0.Transmit(ConnectionCloseWrapper(
462+
hpe.ShutdownReason.ReplyCode,
463+
hpe.ShutdownReason.ReplyText));
464+
}
459465
return true;
460466
}
461467
catch (IOException ioe)
@@ -952,13 +958,13 @@ public void UpdateSecret(string newSecret, string reason)
952958
///<summary>API-side invocation of connection abort.</summary>
953959
public void Abort()
954960
{
955-
Abort(Timeout.InfiniteTimeSpan);
961+
Abort(TimeSpan.FromSeconds(5));
956962
}
957963

958964
///<summary>API-side invocation of connection abort.</summary>
959965
public void Abort(ushort reasonCode, string reasonText)
960966
{
961-
Abort(reasonCode, reasonText, Timeout.InfiniteTimeSpan);
967+
Abort(reasonCode, reasonText, TimeSpan.FromSeconds(5));
962968
}
963969

964970
///<summary>API-side invocation of connection abort with timeout.</summary>
@@ -976,13 +982,13 @@ public void Abort(ushort reasonCode, string reasonText, TimeSpan timeout)
976982
///<summary>API-side invocation of connection.close.</summary>
977983
public void Close()
978984
{
979-
Close(Constants.ReplySuccess, "Goodbye", Timeout.InfiniteTimeSpan);
985+
Close(Constants.ReplySuccess, "Goodbye", TimeSpan.FromSeconds(30));
980986
}
981987

982988
///<summary>API-side invocation of connection.close.</summary>
983989
public void Close(ushort reasonCode, string reasonText)
984990
{
985-
Close(reasonCode, reasonText, Timeout.InfiniteTimeSpan);
991+
Close(reasonCode, reasonText, TimeSpan.FromSeconds(30));
986992
}
987993

988994
///<summary>API-side invocation of connection.close with timeout.</summary>
@@ -1058,7 +1064,16 @@ internal OutgoingCommand ChannelCloseWrapper(ushort reasonCode, string reasonTex
10581064
return request;
10591065
}
10601066

1061-
void StartAndTune()
1067+
///<summary>Used for testing only.</summary>
1068+
internal IFrameHandler FrameHandler
1069+
{
1070+
get
1071+
{
1072+
return _frameHandler;
1073+
}
1074+
}
1075+
1076+
private void StartAndTune()
10621077
{
10631078
var connectionStartCell = new BlockingCell<ConnectionStartDetails>();
10641079
_model0.m_connectionStartCell = connectionStartCell;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ public void Close()
188188
{
189189
lock (_semaphore)
190190
{
191-
if (!_closed)
191+
if (_closed || _socket == null)
192+
{
193+
return;
194+
}
195+
else
192196
{
193197
try
194198
{

projects/Unit/Fixtures.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,10 @@
4242

4343
using NUnit.Framework;
4444

45-
using RabbitMQ.Client.Framing;
4645
using RabbitMQ.Client.Framing.Impl;
4746

4847
namespace RabbitMQ.Client.Unit
4948
{
50-
5149
public class IntegrationFixture
5250
{
5351
internal IConnectionFactory ConnFactory;
@@ -56,6 +54,12 @@ public class IntegrationFixture
5654

5755
internal Encoding encoding = new UTF8Encoding();
5856
public static TimeSpan RECOVERY_INTERVAL = TimeSpan.FromSeconds(2);
57+
protected readonly string _testDisplayName;
58+
59+
public IntegrationFixture()
60+
{
61+
_testDisplayName = TestContext.CurrentContext.Test.FullName;
62+
}
5963

6064
[SetUp]
6165
public virtual void Init()
@@ -72,6 +76,7 @@ public void Dispose()
7276
{
7377
Model.Close();
7478
}
79+
7580
if(Conn.IsOpen)
7681
{
7782
Conn.Close();
@@ -106,7 +111,7 @@ internal AutorecoveringConnection CreateAutorecoveringConnection(TimeSpan interv
106111
AutomaticRecoveryEnabled = true,
107112
NetworkRecoveryInterval = interval
108113
};
109-
return (AutorecoveringConnection)cf.CreateConnection($"UNIT_CONN:{Guid.NewGuid()}");
114+
return (AutorecoveringConnection)cf.CreateConnection($"{_testDisplayName}:{Guid.NewGuid()}");
110115
}
111116

112117
internal AutorecoveringConnection CreateAutorecoveringConnection(TimeSpan interval, IList<string> hostnames)
@@ -119,7 +124,7 @@ internal AutorecoveringConnection CreateAutorecoveringConnection(TimeSpan interv
119124
RequestedConnectionTimeout = TimeSpan.FromSeconds(1),
120125
NetworkRecoveryInterval = interval
121126
};
122-
return (AutorecoveringConnection)cf.CreateConnection(hostnames, $"UNIT_CONN:{Guid.NewGuid()}");
127+
return (AutorecoveringConnection)cf.CreateConnection(hostnames, $"{_testDisplayName}:{Guid.NewGuid()}");
123128
}
124129

125130
internal AutorecoveringConnection CreateAutorecoveringConnection(IList<AmqpTcpEndpoint> endpoints)
@@ -132,7 +137,7 @@ internal AutorecoveringConnection CreateAutorecoveringConnection(IList<AmqpTcpEn
132137
RequestedConnectionTimeout = TimeSpan.FromSeconds(1),
133138
NetworkRecoveryInterval = RECOVERY_INTERVAL
134139
};
135-
return (AutorecoveringConnection)cf.CreateConnection(endpoints, $"UNIT_CONN:{Guid.NewGuid()}");
140+
return (AutorecoveringConnection)cf.CreateConnection(endpoints, $"{_testDisplayName}:{Guid.NewGuid()}");
136141
}
137142

138143
internal AutorecoveringConnection CreateAutorecoveringConnectionWithTopologyRecoveryDisabled()
@@ -143,7 +148,7 @@ internal AutorecoveringConnection CreateAutorecoveringConnectionWithTopologyReco
143148
TopologyRecoveryEnabled = false,
144149
NetworkRecoveryInterval = RECOVERY_INTERVAL
145150
};
146-
return (AutorecoveringConnection)cf.CreateConnection($"UNIT_CONN:{Guid.NewGuid()}");
151+
return (AutorecoveringConnection)cf.CreateConnection($"{_testDisplayName}:{Guid.NewGuid()}");
147152
}
148153

149154
internal IConnection CreateNonRecoveringConnection()
@@ -153,7 +158,7 @@ internal IConnection CreateNonRecoveringConnection()
153158
AutomaticRecoveryEnabled = false,
154159
TopologyRecoveryEnabled = false
155160
};
156-
return cf.CreateConnection($"UNIT_CONN:{Guid.NewGuid()}");
161+
return cf.CreateConnection($"{_testDisplayName}:{Guid.NewGuid()}");
157162
}
158163

159164
internal IConnection CreateConnectionWithContinuationTimeout(bool automaticRecoveryEnabled, TimeSpan continuationTimeout)
@@ -163,7 +168,7 @@ internal IConnection CreateConnectionWithContinuationTimeout(bool automaticRecov
163168
AutomaticRecoveryEnabled = automaticRecoveryEnabled,
164169
ContinuationTimeout = continuationTimeout
165170
};
166-
return cf.CreateConnection($"UNIT_CONN:{Guid.NewGuid()}");
171+
return cf.CreateConnection($"{_testDisplayName}:{Guid.NewGuid()}");
167172
}
168173

169174
//
@@ -177,7 +182,7 @@ internal void WithTemporaryAutorecoveringConnection(Action<AutorecoveringConnect
177182
AutomaticRecoveryEnabled = true
178183
};
179184

180-
var connection = (AutorecoveringConnection)factory.CreateConnection($"UNIT_CONN:{Guid.NewGuid()}");
185+
var connection = (AutorecoveringConnection)factory.CreateConnection($"{_testDisplayName}:{Guid.NewGuid()}");
181186
try
182187
{
183188
action(connection);

projects/Unit/TestAsyncConsumerExceptions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public class TestAsyncConsumerExceptions : IntegrationFixture
4242
{
4343
private static Exception TestException = new Exception("oops");
4444

45+
public TestAsyncConsumerExceptions() : base()
46+
{
47+
}
48+
4549
protected void TestExceptionHandlingWith(IBasicConsumer consumer,
4650
Action<IModel, string, IBasicConsumer, string> action)
4751
{

projects/Unit/TestBasicGet.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ namespace RabbitMQ.Client.Unit
3838
[TestFixture]
3939
public class TestBasicGet : IntegrationFixture
4040
{
41+
public TestBasicGet() : base()
42+
{
43+
}
44+
4145
[Test]
4246
public void TestBasicGetWithClosedChannel()
4347
{

projects/Unit/TestBasicPublishBatch.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@
3535

3636
namespace RabbitMQ.Client.Unit
3737
{
38-
internal class TestBasicPublishBatch : IntegrationFixture
38+
public class TestBasicPublishBatch : IntegrationFixture
3939
{
40+
public TestBasicPublishBatch() : base()
41+
{
42+
}
43+
4044
[Test]
4145
public void TestBasicPublishBatchSend()
4246
{

projects/Unit/TestConcurrentAccessWithSharedConnection.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ namespace RabbitMQ.Client.Unit
4242
[TestFixture]
4343
public class TestConcurrentAccessWithSharedConnection : IntegrationFixture
4444
{
45-
4645
internal const int Threads = 32;
4746
internal CountdownEvent _latch;
4847
internal TimeSpan _completionTimeout = TimeSpan.FromSeconds(90);
4948

49+
public TestConcurrentAccessWithSharedConnection() : base()
50+
{
51+
}
52+
5053
[SetUp]
5154
public override void Init()
5255
{

projects/Unit/TestConfirmSelect.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
namespace RabbitMQ.Client.Unit
3535
{
3636
[TestFixture]
37-
public class TestConfirmSelect : IntegrationFixture {
37+
public class TestConfirmSelect : IntegrationFixture
38+
{
39+
public TestConfirmSelect() : base()
40+
{
41+
}
3842

3943
[Test]
4044
public void TestConfirmSelectIdempotency()

projects/Unit/TestConnectionBlocked.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ public class TestConnectionBlocked : IntegrationFixture
4444
private readonly object _lockObject = new object();
4545
private bool _notified;
4646

47+
public TestConnectionBlocked() : base()
48+
{
49+
}
50+
4751
public void HandleBlocked(object sender, ConnectionBlockedEventArgs args)
4852
{
4953
Unblock();
5054
}
5155

52-
5356
public void HandleUnblocked(object sender, EventArgs ea)
5457
{
5558
lock (_lockObject)

projects/Unit/TestConnectionFactoryContinuationTimeout.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@
3535
namespace RabbitMQ.Client.Unit
3636
{
3737
[TestFixture]
38-
internal class TestConnectionFactoryContinuationTimeout : IntegrationFixture
38+
public class TestConnectionFactoryContinuationTimeout : IntegrationFixture
3939
{
40+
public TestConnectionFactoryContinuationTimeout() : base()
41+
{
42+
}
43+
4044
[Test]
4145
public void TestConnectionFactoryContinuationTimeoutOnRecoveringConnection()
4246
{

projects/Unit/TestConnectionRecovery.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class TestConnectionRecovery : IntegrationFixture
5252
private readonly ushort _closeAtCount = 16;
5353
private string _queueName;
5454

55-
public TestConnectionRecovery()
55+
public TestConnectionRecovery() : base()
5656
{
5757
var rnd = new Random();
5858
_messageBody = new byte[4096];
@@ -68,10 +68,19 @@ public override void Init()
6868
Model.QueueDelete(_queueName);
6969
}
7070

71-
[TearDown]
72-
public void CleanUp()
71+
protected override void ReleaseResources()
7372
{
74-
Conn.Close();
73+
if (Model.IsOpen)
74+
{
75+
Model.Close();
76+
}
77+
78+
if (Conn.IsOpen)
79+
{
80+
Conn.Close();
81+
}
82+
83+
Unblock();
7584
}
7685

7786
[Test]
@@ -1108,11 +1117,6 @@ internal ManualResetEventSlim PrepareForShutdown(IConnection conn)
11081117
return latch;
11091118
}
11101119

1111-
protected override void ReleaseResources()
1112-
{
1113-
Unblock();
1114-
}
1115-
11161120
internal void RestartServerAndWaitForRecovery()
11171121
{
11181122
RestartServerAndWaitForRecovery((IAutorecoveringConnection)Conn);

0 commit comments

Comments
 (0)