Skip to content

Commit cb13485

Browse files
author
Steve Powell
committed
Added test of logic
1 parent c1c28bd commit cb13485

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

test/src/com/rabbitmq/client/test/AMQConnectionTest.java

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
public class AMQConnectionTest extends TestCase {
4545
// private static final String CLOSE_MESSAGE = "terminated by test";
4646

47-
/**
48-
* Build a suite of tests
47+
/**
48+
* Build a suite of tests
4949
* @return the test suite for this class
5050
*/
5151
public static TestSuite suite() {
@@ -79,7 +79,7 @@ public static TestSuite suite() {
7979
}
8080

8181
/** 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
8383
*/
8484
public void testConnectionSendsSingleHeaderAndTimesOut() {
8585
IOException exception = new SocketTimeoutException();
@@ -100,14 +100,14 @@ public void testConnectionSendsSingleHeaderAndTimesOut() {
100100
handler).start();
101101
fail("Connection should have thrown exception");
102102
} catch(IOException signal) {
103-
// As expected
103+
// As expected
104104
}
105105
assertEquals(1, _mockFrameHandler.countHeadersSent());
106106
// _connection.close(0, CLOSE_MESSAGE);
107107
List<Throwable> exceptionList = handler.getHandledExceptions();
108108
assertEquals(Collections.<Throwable>singletonList(exception), exceptionList);
109109
}
110-
110+
111111
/** Check we can open a connection once, but not twice.
112112
* @throws IOException */
113113
// public void testCanOpenConnectionOnceOnly() throws IOException {
@@ -121,18 +121,46 @@ public void testConnectionSendsSingleHeaderAndTimesOut() {
121121
// }
122122
// }
123123

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+
MyExceptionHandler handler = new MyExceptionHandler();
129+
assertEquals(0, _mockFrameHandler.countHeadersSent());
130+
try {
131+
new AMQConnection(factory.getUsername(),
132+
factory.getPassword(),
133+
_mockFrameHandler,
134+
Executors.newFixedThreadPool(1),
135+
factory.getVirtualHost(),
136+
factory.getClientProperties(),
137+
factory.getRequestedFrameMax(),
138+
factory.getRequestedChannelMax(),
139+
factory.getRequestedHeartbeat(),
140+
factory.getSaslConfig(),
141+
handler).start();
142+
fail("Connection should have thrown exception");
143+
} catch(IOException signal) {
144+
// As expected
145+
}
146+
assertEquals(1, _mockFrameHandler.countHeadersSent());
147+
// _connection.close(0, CLOSE_MESSAGE);
148+
List<Throwable> exceptionList = handler.getHandledExceptions();
149+
assertEquals("Only one exception expected", 1, exceptionList.size());
150+
assertEquals("Wrong type of exception returned.", SocketTimeoutException.class, exceptionList.get(0).getClass());
151+
}
152+
127153
/** Mock frame handler to facilitate testing. */
128154
private static class MockFrameHandler implements FrameHandler {
129155
/** How many times has sendHeader() been called? */
130156
private int _numHeadersSent;
131-
157+
158+
private int timeout;
159+
132160
/** An optional exception for us to throw on reading frames */
133161
private IOException _exceptionOnReadingFrames;
134162

135-
/** count how many headers we've sent
163+
/** count how many headers we've sent
136164
* @return the number of sent headers
137165
*/
138166
public int countHeadersSent() {
@@ -147,16 +175,15 @@ public Frame readFrame() throws IOException {
147175
if (_exceptionOnReadingFrames != null) {
148176
throw _exceptionOnReadingFrames;
149177
}
150-
return null;
151-
// throw new SocketTimeoutException(); // simulate a socket timeout
178+
return null; // simulate a socket timeout
152179
}
153180

154181
public void sendHeader() throws IOException {
155-
_numHeadersSent++;
182+
_numHeadersSent++;
156183
}
157184

158185
public void setTimeout(int timeoutMs) throws SocketException {
159-
// no need to implement this: don't bother changing the timeout
186+
this.timeout = timeoutMs;
160187
}
161188

162189
public void writeFrame(Frame frame) throws IOException {
@@ -168,7 +195,7 @@ public void close() {
168195
}
169196

170197
public int getTimeout() throws SocketException {
171-
return 0;
198+
return this.timeout;
172199
}
173200

174201
public InetAddress getAddress() {
@@ -208,7 +235,7 @@ public void handleConsumerException(Channel ch,
208235
{
209236
fail("handleConsumerException " + consumerTag + " " + methodName + ": " + ex);
210237
}
211-
238+
212239
public List<Throwable> getHandledExceptions() {
213240
return _handledExceptions;
214241
}

0 commit comments

Comments
 (0)