@@ -7,50 +7,131 @@ public class ReqRepTests : IClassFixture<CleanupAfterFixture>
7
7
{
8
8
public ReqRepTests ( ) => NetMQConfig . Cleanup ( ) ;
9
9
10
+ protected void SimpleReqRepSequence ( string address , RequestSocket req , ResponseSocket rep )
11
+ {
12
+ var port = rep . BindRandomPort ( address ) ;
13
+ req . Connect ( address + ":" + port ) ;
14
+
15
+ req . SendFrame ( "Hi" ) ;
16
+ Assert . Equal ( new [ ] { "Hi" } , rep . ReceiveMultipartStrings ( ) ) ;
17
+ rep . SendFrame ( "Hi2" ) ;
18
+ Assert . Equal ( new [ ] { "Hi2" } , req . ReceiveMultipartStrings ( ) ) ;
19
+ }
20
+
21
+ protected void SendTwoReqsInSuccession ( string address , RequestSocket req , ResponseSocket rep )
22
+ {
23
+ var port = rep . BindRandomPort ( address ) ;
24
+ req . Connect ( address + ":" + port ) ;
25
+ req . SendFrame ( "Hi" ) ;
26
+ rep . SkipFrame ( ) ;
27
+ }
28
+
29
+ [ Theory ]
30
+ [ InlineData ( "tcp://localhost" ) ]
31
+ [ InlineData ( "tcp://127.0.0.1" ) ]
32
+ public void SimpleReqRepSucceeds ( string address )
33
+ {
34
+ using ( var rep = new ResponseSocket ( ) )
35
+ using ( var req = new RequestSocket ( ) )
36
+ {
37
+ SimpleReqRepSequence ( address , req , rep ) ;
38
+ }
39
+ }
40
+
41
+ [ Theory ]
42
+ [ InlineData ( "tcp://localhost" ) ]
43
+ [ InlineData ( "tcp://127.0.0.1" ) ]
44
+ public void SimpleReqRepWithCorrelationSucceeds ( string address )
45
+ {
46
+ using ( var rep = new ResponseSocket ( ) )
47
+ using ( var req = new RequestSocket ( ) )
48
+ {
49
+ req . Options . Correlate = true ;
50
+ SimpleReqRepSequence ( address , req , rep ) ;
51
+ }
52
+ }
53
+
10
54
[ Theory ]
11
55
[ InlineData ( "tcp://localhost" ) ]
12
56
[ InlineData ( "tcp://127.0.0.1" ) ]
13
- public void SimpleReqRep ( string address )
57
+ public void SimpleReqRepWithRelaxedSucceeds ( string address )
58
+ {
59
+ using ( var rep = new ResponseSocket ( ) )
60
+ using ( var req = new RequestSocket ( ) )
61
+ {
62
+ req . Options . Relaxed = true ;
63
+ SimpleReqRepSequence ( address , req , rep ) ;
64
+ req . SendFrame ( "Hi2" ) ; // ick that this not what I wanted.
65
+ }
66
+ }
67
+
68
+ [ Theory ]
69
+ [ InlineData ( "tcp://localhost" ) ]
70
+ [ InlineData ( "tcp://127.0.0.1" ) ]
71
+ public void SendingTwoRequestsInSuccessionFails ( string address )
14
72
{
15
73
using ( var rep = new ResponseSocket ( ) )
16
74
using ( var req = new RequestSocket ( ) )
17
75
{
18
76
var port = rep . BindRandomPort ( address ) ;
19
77
req . Connect ( address + ":" + port ) ;
20
-
21
78
req . SendFrame ( "Hi" ) ;
79
+ rep . SkipFrame ( ) ;
80
+ Assert . Throws < FiniteStateMachineException > ( ( ) => req . SendFrame ( "Hi2" ) ) ;
81
+ }
82
+ }
22
83
23
- Assert . Equal ( new [ ] { "Hi" } , rep . ReceiveMultipartStrings ( ) ) ;
24
-
25
- rep . SendFrame ( "Hi2" ) ;
84
+ [ Theory ]
85
+ [ InlineData ( "tcp://localhost" ) ]
86
+ [ InlineData ( "tcp://127.0.0.1" ) ]
87
+ public void SendingTwoRequestsInSuccessionWithRelaxedSucceeds ( string address )
88
+ {
89
+ using ( var rep = new ResponseSocket ( ) )
90
+ using ( var req = new RequestSocket ( ) )
91
+ {
92
+ req . Options . Relaxed = true ;
26
93
27
- Assert . Equal ( new [ ] { "Hi2" } , req . ReceiveMultipartStrings ( ) ) ;
94
+ var port = rep . BindRandomPort ( address ) ;
95
+ req . Connect ( address + ":" + port ) ;
96
+ req . SendFrame ( "Hi" ) ;
97
+ rep . SkipFrame ( ) ;
28
98
}
29
99
}
30
100
31
- [ Fact ]
32
- public void SendingTwoRequestsInARow ( )
101
+ [ Fact ]
102
+ public void ReceiveBeforeSendingFails ( )
33
103
{
34
104
using ( var rep = new ResponseSocket ( ) )
35
105
using ( var req = new RequestSocket ( ) )
36
106
{
37
107
var port = rep . BindRandomPort ( "tcp://localhost" ) ;
38
108
req . Connect ( "tcp://localhost:" + port ) ;
39
109
40
- req . SendFrame ( "Hi" ) ;
110
+ Assert . Throws < FiniteStateMachineException > ( ( ) => req . ReceiveFrameBytes ( ) ) ;
111
+ }
112
+ }
41
113
42
- rep . SkipFrame ( ) ;
114
+ [ Fact ]
115
+ public void ReceiveBeforeSendingWithRelaxedStillFails ( )
116
+ {
117
+ using ( var rep = new ResponseSocket ( ) )
118
+ using ( var req = new RequestSocket ( ) )
119
+ {
120
+ req . Options . Relaxed = true ;
121
+ var port = rep . BindRandomPort ( "tcp://localhost" ) ;
122
+ req . Connect ( "tcp://localhost:" + port ) ;
43
123
44
- Assert . Throws < FiniteStateMachineException > ( ( ) => req . SendFrame ( "Hi2" ) ) ;
124
+ Assert . Throws < FiniteStateMachineException > ( ( ) => req . ReceiveFrameBytes ( ) ) ;
45
125
}
46
126
}
47
127
48
128
[ Fact ]
49
- public void ReceiveBeforeSending ( )
129
+ public void ReceiveBeforeSendingWithCorrelateStillFails ( )
50
130
{
51
131
using ( var rep = new ResponseSocket ( ) )
52
132
using ( var req = new RequestSocket ( ) )
53
133
{
134
+ req . Options . Correlate = true ;
54
135
var port = rep . BindRandomPort ( "tcp://localhost" ) ;
55
136
req . Connect ( "tcp://localhost:" + port ) ;
56
137
@@ -59,7 +140,7 @@ public void ReceiveBeforeSending()
59
140
}
60
141
61
142
[ Fact ]
62
- public void SendMessageInResponseBeforeReceiving ( )
143
+ public void SendMessageInResponseBeforeReceivingFails ( )
63
144
{
64
145
using ( var rep = new ResponseSocket ( ) )
65
146
using ( var req = new RequestSocket ( ) )
@@ -71,8 +152,89 @@ public void SendMessageInResponseBeforeReceiving()
71
152
}
72
153
}
73
154
155
+ // make sure that a single responder sends messages back to the correct requestors.
156
+ [ Fact ]
157
+ public void SingleResponderSendsCorrectMessagesToMultipleRequestors ( )
158
+ {
159
+ using ( var rep = new ResponseSocket ( ) )
160
+ using ( var req1 = new RequestSocket ( ) )
161
+ using ( var req2 = new RequestSocket ( ) )
162
+ {
163
+ var port = rep . BindRandomPort ( "tcp://127.0.0.1" ) ;
164
+
165
+ req1 . Connect ( $ "tcp://127.0.0.1:{ port } ") ;
166
+ req2 . Connect ( $ "tcp://127.0.0.1:{ port } ") ;
167
+
168
+ req1 . SendFrame ( "From1" ) ;
169
+ req2 . SendFrame ( "From2" ) ;
170
+
171
+ rep . SendFrame ( rep . ReceiveFrameString ( ) ) ;
172
+ rep . SendFrame ( rep . ReceiveFrameString ( ) ) ;
173
+
174
+ Assert . Equal ( "From2" , req2 . ReceiveFrameString ( ) ) ;
175
+ Assert . Equal ( "From1" , req1 . ReceiveFrameString ( ) ) ;
176
+ }
177
+ }
178
+
179
+ internal void RouterBounce ( ref RouterSocket router )
180
+ {
181
+ bool more ;
182
+ do
183
+ {
184
+ var bytes = router . ReceiveFrameBytes ( out more ) ;
185
+ router . SendFrame ( bytes , more ) ;
186
+ } while ( more ) ;
187
+ }
188
+
189
+ [ Theory ]
190
+ [ InlineData ( false ) ]
191
+ [ InlineData ( true ) ]
192
+ public void CorrelationSelectsLatestRequest ( bool correlate )
193
+ {
194
+ var rep = new RouterSocket ( ) ;
195
+ var req = new RequestSocket ( ) ;
196
+
197
+ var port = rep . BindRandomPort ( "tcp://localhost" ) ;
198
+ req . Connect ( $ "tcp://localhost:{ port } ") ;
199
+
200
+ req . Options . Relaxed = true ;
201
+ req . Options . Correlate = correlate ;
202
+
203
+ // Send two requests.
204
+ req . SendFrame ( "FirstReq" ) ;
205
+ req . SendFrame ( "SecondReq" ) ;
206
+
207
+ // Bind server allowing it to receive messages.
208
+ //rep.Bind($"tcp://localhost:{port}");
209
+
210
+ // Read the two messages and send them back as is.
211
+ RouterBounce ( ref rep ) ;
212
+ RouterBounce ( ref rep ) ;
213
+
214
+ // Read the reply. When Options.Correlate is active,
215
+ // "FirstReq" should be ditched and "SecondReq" should be read. Vice
216
+ // versa when Options.Corellate is not active.
217
+ var result = req . ReceiveFrameString ( ) ;
218
+
219
+ if ( correlate )
220
+ {
221
+ // if correlate is on, we get SecondReq which is the typical desired behavior; this
222
+ // is the last request that was sent.
223
+ Assert . Equal ( "SecondReq" , result ) ;
224
+ }
225
+ else
226
+ {
227
+ // if correlate is off, we get FirstReq, which is not desired behavior; this is not
228
+ // the last request that was sent.
229
+ Assert . Equal ( "FirstReq" , result ) ;
230
+ }
231
+
232
+ rep . Dispose ( ) ;
233
+ req . Dispose ( ) ;
234
+ }
235
+
74
236
[ Fact ]
75
- public void SendMultipartMessage ( )
237
+ public void SendMultipartMessageSucceeds ( )
76
238
{
77
239
using ( var rep = new ResponseSocket ( ) )
78
240
using ( var req = new RequestSocket ( ) )
0 commit comments