44
44
45
45
namespace RabbitMQ . Client . Unit
46
46
{
47
- class DisposableConnection : IDisposable
47
+ [ TestFixture ]
48
+ public class TestConnectionRecovery : IntegrationFixture
48
49
{
49
- public DisposableConnection ( AutorecoveringConnection c )
50
- {
51
- Connection = c ;
52
- }
50
+ private readonly byte [ ] _messageBody ;
51
+ private readonly ushort _totalMessageCount = 64 ;
52
+ private string _queueName = "luke-test-queue" ;
53
53
54
- public AutorecoveringConnection Connection { get ; private set ; }
55
-
56
- public void Dispose ( )
54
+ public TestConnectionRecovery ( )
57
55
{
58
- Connection . Close ( ) ;
56
+ var rnd = new Random ( ) ;
57
+ _messageBody = new byte [ 1024 ] ;
58
+ rnd . NextBytes ( _messageBody ) ;
59
59
}
60
- }
61
- [ TestFixture ]
62
- public class TestConnectionRecovery : IntegrationFixture
63
- {
60
+
64
61
[ SetUp ]
65
62
public override void Init ( )
66
63
{
67
64
Conn = CreateAutorecoveringConnection ( ) ;
68
65
Model = Conn . CreateModel ( ) ;
66
+ Model . QueueDelete ( _queueName ) ;
69
67
}
70
68
71
69
[ TearDown ]
72
70
public void CleanUp ( )
73
71
{
72
+ Model . QueueDelete ( _queueName ) ;
73
+ Model . Close ( ) ;
74
74
Conn . Close ( ) ;
75
75
}
76
76
77
+ [ Test ]
78
+ public void LukeTest ( )
79
+ {
80
+ var allMessagesSeenLatch = new ManualResetEventSlim ( false ) ;
81
+ var cons = new LukeAckingBasicConsumer ( Model , _totalMessageCount , allMessagesSeenLatch ) ;
82
+
83
+ string queueName = Model . QueueDeclare ( _queueName , false , false , false , null ) . QueueName ;
84
+ Assert . AreEqual ( queueName , _queueName ) ;
85
+
86
+ Model . BasicQos ( 0 , 1 , false ) ;
87
+ string consumerTag = Model . BasicConsume ( queueName , false , cons ) ;
88
+
89
+ ManualResetEventSlim sl = PrepareForShutdown ( Conn ) ;
90
+ ManualResetEventSlim rl = PrepareForRecovery ( Conn ) ;
91
+
92
+ using ( IAutorecoveringConnection publishingConn = CreateAutorecoveringConnection ( ) )
93
+ {
94
+ using ( IModel publishingModel = publishingConn . CreateModel ( ) )
95
+ {
96
+ for ( ushort i = 0 ; i < _totalMessageCount ; i ++ )
97
+ {
98
+ if ( i == 16 )
99
+ {
100
+ CloseConnection ( Conn ) ;
101
+ }
102
+ publishingModel . BasicPublish ( string . Empty , queueName , null , _messageBody ) ;
103
+ }
104
+ }
105
+ }
106
+
107
+ // System.Console.Error.WriteLine("@@@@@@@@ waiting for shutdown...");
108
+ Wait ( sl ) ;
109
+ // System.Console.Error.WriteLine("@@@@@@@@ waiting for shutdown...DONE");
110
+ // System.Console.Error.WriteLine("@@@@@@@@ waiting for recovery...");
111
+ Wait ( rl ) ;
112
+ // System.Console.Error.WriteLine("@@@@@@@@ waiting for recovery...DONE");
113
+ // System.Console.Error.WriteLine("@@@@@@@@ waiting for the rest of the messages...");
114
+ Wait ( allMessagesSeenLatch ) ;
115
+ // System.Console.Error.WriteLine("@@@@@@@@ waiting for the rest of the messages...DONE");
116
+ }
117
+
118
+ /*
77
119
[Test]
78
120
public void TestBasicAckAfterChannelRecovery()
79
121
{
@@ -82,6 +124,7 @@ public void TestBasicAckAfterChannelRecovery()
82
124
83
125
TestDelayedBasicAckNackAfterChannelRecovery(cons, latch);
84
126
}
127
+ */
85
128
86
129
[ Test ]
87
130
public void TestBasicAckAfterBasicGetAndChannelRecovery ( )
@@ -224,6 +267,7 @@ public void TestBasicModelRecoveryOnServerRestart()
224
267
Assert . IsTrue ( Model . IsOpen ) ;
225
268
}
226
269
270
+ /*
227
271
[Test]
228
272
public void TestBasicNackAfterChannelRecovery()
229
273
{
@@ -241,6 +285,7 @@ public void TestBasicRejectAfterChannelRecovery()
241
285
242
286
TestDelayedBasicAckNackAfterChannelRecovery(cons, latch);
243
287
}
288
+ */
244
289
245
290
[ Test ]
246
291
public void TestBlockedListenersRecovery ( )
@@ -1033,25 +1078,29 @@ internal void CloseAndWaitForRecovery(AutorecoveringConnection conn)
1033
1078
Wait ( rl ) ;
1034
1079
}
1035
1080
1036
- internal void CloseAndWaitForShutdown ( AutorecoveringConnection conn )
1081
+ internal void CloseAndWaitForShutdown ( IAutorecoveringConnection conn )
1037
1082
{
1038
1083
ManualResetEventSlim sl = PrepareForShutdown ( conn ) ;
1039
1084
CloseConnection ( conn ) ;
1040
1085
Wait ( sl ) ;
1041
1086
}
1042
1087
1043
- internal ManualResetEventSlim PrepareForRecovery ( AutorecoveringConnection conn )
1088
+ internal ManualResetEventSlim PrepareForRecovery ( IConnection conn )
1044
1089
{
1045
1090
var latch = new ManualResetEventSlim ( false ) ;
1046
- conn . RecoverySucceeded += ( source , ea ) => latch . Set ( ) ;
1091
+
1092
+ IAutorecoveringConnection aconn = conn as IAutorecoveringConnection ;
1093
+ aconn . RecoverySucceeded += ( source , ea ) => latch . Set ( ) ;
1047
1094
1048
1095
return latch ;
1049
1096
}
1050
1097
1051
1098
internal ManualResetEventSlim PrepareForShutdown ( IConnection conn )
1052
1099
{
1053
1100
var latch = new ManualResetEventSlim ( false ) ;
1054
- conn . ConnectionShutdown += ( c , args ) => latch . Set ( ) ;
1101
+
1102
+ IAutorecoveringConnection aconn = conn as IAutorecoveringConnection ;
1103
+ aconn . ConnectionShutdown += ( c , args ) => latch . Set ( ) ;
1055
1104
1056
1105
return latch ;
1057
1106
}
@@ -1063,10 +1112,10 @@ protected override void ReleaseResources()
1063
1112
1064
1113
internal void RestartServerAndWaitForRecovery ( )
1065
1114
{
1066
- RestartServerAndWaitForRecovery ( ( AutorecoveringConnection ) Conn ) ;
1115
+ RestartServerAndWaitForRecovery ( ( IAutorecoveringConnection ) Conn ) ;
1067
1116
}
1068
1117
1069
- internal void RestartServerAndWaitForRecovery ( AutorecoveringConnection conn )
1118
+ internal void RestartServerAndWaitForRecovery ( IAutorecoveringConnection conn )
1070
1119
{
1071
1120
ManualResetEventSlim sl = PrepareForShutdown ( conn ) ;
1072
1121
ManualResetEventSlim rl = PrepareForRecovery ( conn ) ;
@@ -1075,6 +1124,7 @@ internal void RestartServerAndWaitForRecovery(AutorecoveringConnection conn)
1075
1124
Wait ( rl ) ;
1076
1125
}
1077
1126
1127
+ /*
1078
1128
internal void TestDelayedBasicAckNackAfterChannelRecovery(TestBasicConsumer1 cons, ManualResetEventSlim latch)
1079
1129
{
1080
1130
string q = Model.QueueDeclare(GenerateQueueName(), false, false, false, null).QueueName;
@@ -1096,6 +1146,7 @@ internal void TestDelayedBasicAckNackAfterChannelRecovery(TestBasicConsumer1 con
1096
1146
publishingModel.Close();
1097
1147
publishingConn.Close();
1098
1148
}
1149
+ */
1099
1150
1100
1151
internal void WaitForRecovery ( )
1101
1152
{
@@ -1117,6 +1168,42 @@ internal void WaitForShutdown(IConnection conn)
1117
1168
Wait ( PrepareForShutdown ( conn ) ) ;
1118
1169
}
1119
1170
1171
+ public class LukeAckingBasicConsumer : DefaultBasicConsumer
1172
+ {
1173
+ private readonly ManualResetEventSlim _allMessagesSeenLatch ;
1174
+ private readonly ushort _totalMessageCount ;
1175
+ private ushort _counter = 0 ;
1176
+
1177
+ public LukeAckingBasicConsumer ( IModel model , ushort totalMessageCount , ManualResetEventSlim allMessagesSeenLatch )
1178
+ : base ( model )
1179
+ {
1180
+ _totalMessageCount = totalMessageCount ;
1181
+ _allMessagesSeenLatch = allMessagesSeenLatch ;
1182
+ }
1183
+
1184
+ public override void HandleBasicDeliver ( string consumerTag ,
1185
+ ulong deliveryTag ,
1186
+ bool redelivered ,
1187
+ string exchange ,
1188
+ string routingKey ,
1189
+ IBasicProperties properties ,
1190
+ ReadOnlyMemory < byte > body )
1191
+ {
1192
+ try
1193
+ {
1194
+ Model . BasicAck ( deliveryTag , false ) ;
1195
+ }
1196
+ finally
1197
+ {
1198
+ ++ _counter ;
1199
+ if ( _counter >= _totalMessageCount )
1200
+ {
1201
+ _allMessagesSeenLatch . Set ( ) ;
1202
+ }
1203
+ }
1204
+ }
1205
+ }
1206
+
1120
1207
public class AckingBasicConsumer : TestBasicConsumer1
1121
1208
{
1122
1209
public AckingBasicConsumer ( IModel model , ManualResetEventSlim latch , Action fn )
0 commit comments