44
44
public class AMQConnectionTest extends TestCase {
45
45
// private static final String CLOSE_MESSAGE = "terminated by test";
46
46
47
- /**
48
- * Build a suite of tests
47
+ /**
48
+ * Build a suite of tests
49
49
* @return the test suite for this class
50
50
*/
51
51
public static TestSuite suite () {
@@ -79,7 +79,7 @@ public static TestSuite suite() {
79
79
}
80
80
81
81
/** Check the AMQConnection does send exactly 1 initial header, and deal correctly with
82
- * the frame handler throwing an exception when we try to read data
82
+ * the frame handler throwing an exception when we try to read data
83
83
*/
84
84
public void testConnectionSendsSingleHeaderAndTimesOut () {
85
85
IOException exception = new SocketTimeoutException ();
@@ -100,14 +100,14 @@ public void testConnectionSendsSingleHeaderAndTimesOut() {
100
100
handler ).start ();
101
101
fail ("Connection should have thrown exception" );
102
102
} catch (IOException signal ) {
103
- // As expected
103
+ // As expected
104
104
}
105
105
assertEquals (1 , _mockFrameHandler .countHeadersSent ());
106
106
// _connection.close(0, CLOSE_MESSAGE);
107
107
List <Throwable > exceptionList = handler .getHandledExceptions ();
108
108
assertEquals (Collections .<Throwable >singletonList (exception ), exceptionList );
109
109
}
110
-
110
+
111
111
/** Check we can open a connection once, but not twice.
112
112
* @throws IOException */
113
113
// public void testCanOpenConnectionOnceOnly() throws IOException {
@@ -121,18 +121,49 @@ public void testConnectionSendsSingleHeaderAndTimesOut() {
121
121
// }
122
122
// }
123
123
124
- // add test that we time out if no initial Start command is received,
125
- // setting a timeout and having the FrameHandler return null
126
-
124
+ /**
125
+ * Test that we catch timeout between connect and negotiation of the connection being finished.
126
+ */
127
+ public void testConnectionHangInNegotiation () {
128
+ this ._mockFrameHandler .setTimeoutCount (10 ); // to limit hang
129
+ MyExceptionHandler handler = new MyExceptionHandler ();
130
+ assertEquals (0 , this ._mockFrameHandler .countHeadersSent ());
131
+ try {
132
+ new AMQConnection (factory .getUsername (),
133
+ factory .getPassword (),
134
+ this ._mockFrameHandler ,
135
+ Executors .newFixedThreadPool (1 ),
136
+ factory .getVirtualHost (),
137
+ factory .getClientProperties (),
138
+ factory .getRequestedFrameMax (),
139
+ factory .getRequestedChannelMax (),
140
+ factory .getRequestedHeartbeat (),
141
+ factory .getSaslConfig (),
142
+ handler ).start ();
143
+ fail ("Connection should have thrown exception" );
144
+ } catch (IOException signal ) {
145
+ // As expected
146
+ }
147
+ assertEquals (1 , this ._mockFrameHandler .countHeadersSent ());
148
+ // _connection.close(0, CLOSE_MESSAGE);
149
+ List <Throwable > exceptionList = handler .getHandledExceptions ();
150
+ assertEquals ("Only one exception expected" , 1 , exceptionList .size ());
151
+ assertEquals ("Wrong type of exception returned." , SocketTimeoutException .class , exceptionList .get (0 ).getClass ());
152
+ }
153
+
127
154
/** Mock frame handler to facilitate testing. */
128
155
private static class MockFrameHandler implements FrameHandler {
129
156
/** How many times has sendHeader() been called? */
130
157
private int _numHeadersSent ;
131
-
158
+
159
+ private int timeout ;
160
+
132
161
/** An optional exception for us to throw on reading frames */
133
162
private IOException _exceptionOnReadingFrames ;
134
163
135
- /** count how many headers we've sent
164
+ private int timeoutCount = 0 ;
165
+
166
+ /** count how many headers we've sent
136
167
* @return the number of sent headers
137
168
*/
138
169
public int countHeadersSent () {
@@ -143,20 +174,27 @@ public void setExceptionOnReadingFrames(IOException exception) {
143
174
_exceptionOnReadingFrames = exception ;
144
175
}
145
176
177
+ public void setTimeoutCount (int timeoutCount ) {
178
+ this .timeoutCount = timeoutCount ;
179
+ }
180
+
146
181
public Frame readFrame () throws IOException {
147
182
if (_exceptionOnReadingFrames != null ) {
148
183
throw _exceptionOnReadingFrames ;
149
184
}
150
- return null ;
151
- // throw new SocketTimeoutException(); // simulate a socket timeout
185
+ if (this .timeoutCount > 0 ) {
186
+ if (--this .timeoutCount == 0 )
187
+ throw new IOException ("Mock Framehandler: too many timeouts." );
188
+ }
189
+ return null ; // simulate a socket timeout
152
190
}
153
191
154
192
public void sendHeader () throws IOException {
155
- _numHeadersSent ++;
193
+ _numHeadersSent ++;
156
194
}
157
195
158
196
public void setTimeout (int timeoutMs ) throws SocketException {
159
- // no need to implement this: don't bother changing the timeout
197
+ this . timeout = timeoutMs ;
160
198
}
161
199
162
200
public void writeFrame (Frame frame ) throws IOException {
@@ -168,7 +206,7 @@ public void close() {
168
206
}
169
207
170
208
public int getTimeout () throws SocketException {
171
- return 0 ;
209
+ return this . timeout ;
172
210
}
173
211
174
212
public InetAddress getAddress () {
@@ -180,7 +218,7 @@ public int getPort() {
180
218
}
181
219
}
182
220
183
- /** Mock frame handler to facilitate testing. */
221
+ /** Exception handler to facilitate testing. */
184
222
private class MyExceptionHandler implements ExceptionHandler {
185
223
private List <Throwable > _handledExceptions = new ArrayList <Throwable >();
186
224
@@ -208,7 +246,7 @@ public void handleConsumerException(Channel ch,
208
246
{
209
247
fail ("handleConsumerException " + consumerTag + " " + methodName + ": " + ex );
210
248
}
211
-
249
+
212
250
public List <Throwable > getHandledExceptions () {
213
251
return _handledExceptions ;
214
252
}
0 commit comments