Skip to content

Commit 8c8ddc5

Browse files
committed
use BrokerTestCase.expectError to catch expected errors
which handles the case of the connection already being closed by the time we invoke the next command after an error-inducing basicPublish. In the process I changed the code to use more of the existing BrokerTestCase infrastructure and reduce the dependence on the knowledge of client library internals.
1 parent 8e34cea commit 8c8ddc5

File tree

1 file changed

+48
-48
lines changed

1 file changed

+48
-48
lines changed

test/src/com/rabbitmq/client/test/functional/UnexpectedFrames.java

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,50 @@
1313
* Test that the server correctly handles us when we send it bad frames
1414
*/
1515
public class UnexpectedFrames extends BrokerTestCase {
16+
1617
private interface Confuser {
1718
public Frame confuse(Frame frame) throws IOException;
1819
}
1920

20-
@Override protected void setUp() throws IOException {}
21+
private static class ConfusedFrameHandler extends SocketFrameHandler {
22+
public ConfusedFrameHandler(Socket socket) throws IOException {
23+
super(socket);
24+
}
25+
26+
@Override
27+
public void writeFrame(Frame frame) throws IOException {
28+
Frame confusedFrame = new Frame();
29+
confusedFrame.accumulator = frame.accumulator;
30+
confusedFrame.channel = frame.channel;
31+
confusedFrame.type = frame.type;
32+
33+
confusedFrame = confuser.confuse(confusedFrame);
34+
if (confusedFrame != null) {
35+
super.writeFrame(confusedFrame);
36+
}
37+
}
38+
39+
public Confuser confuser = new Confuser() {
40+
public Frame confuse(Frame frame) {
41+
// Do nothing to start with, we need to negotiate before the
42+
// server will send us unexpected_frame errors
43+
return frame;
44+
}
45+
};
46+
}
47+
48+
private static class ConfusedConnectionFactory extends ConnectionFactory {
49+
50+
@Override protected FrameHandler createFrameHandler(Socket sock)
51+
throws IOException {
52+
return new ConfusedFrameHandler(sock);
53+
}
54+
}
2155

22-
@Override protected void tearDown() throws IOException {}
56+
public UnexpectedFrames() {
57+
super();
58+
connectionFactory = new ConfusedConnectionFactory();
59+
}
2360

2461
public void testMissingHeader() throws IOException {
2562
expectUnexpectedFrameError(new Confuser() {
@@ -73,53 +110,16 @@ public Frame confuse(Frame frame) throws IOException {
73110
});
74111
}
75112

76-
private void expectUnexpectedFrameError(Confuser confuser) throws IOException {
77-
ConnectionFactory factory = new ConnectionFactory();
78-
Socket socket = factory.getSocketFactory().createSocket("localhost",
79-
AMQP.PROTOCOL.PORT);
80-
ConfusedFrameHandler handler = new ConfusedFrameHandler(socket);
81-
AMQConnection connection = new AMQConnection(factory, handler);
82-
connection.start();
83-
Channel channel = connection.createChannel();
84-
85-
handler.confuser = confuser;
86-
87-
try {
88-
//NB: the frame confuser relies on the encoding of the
89-
//method field to be at least 8 bytes long
90-
channel.basicPublish("", "routing key", null, "Hello".getBytes());
91-
channel.basicQos(0);
92-
fail("We should have seen an UNEXPECTED_FRAME by now");
93-
}
94-
catch (IOException e) {
95-
checkShutdownSignal(AMQP.UNEXPECTED_FRAME, e);
96-
}
97-
}
98-
99-
private static class ConfusedFrameHandler extends SocketFrameHandler {
100-
public ConfusedFrameHandler(Socket socket) throws IOException {
101-
super(socket);
102-
}
103-
104-
@Override
105-
public void writeFrame(Frame frame) throws IOException {
106-
Frame confusedFrame = new Frame();
107-
confusedFrame.accumulator = frame.accumulator;
108-
confusedFrame.channel = frame.channel;
109-
confusedFrame.type = frame.type;
113+
private void expectUnexpectedFrameError(Confuser confuser)
114+
throws IOException {
110115

111-
confusedFrame = confuser.confuse(confusedFrame);
112-
if (confusedFrame != null) {
113-
super.writeFrame(confusedFrame);
114-
}
115-
}
116+
((ConfusedFrameHandler)((AMQConnection)connection).getFrameHandler()).
117+
confuser = confuser;
116118

117-
public Confuser confuser = new Confuser() {
118-
public Frame confuse(Frame frame) {
119-
// Do nothing to start with, we need to negotiate before the
120-
// server will send us unexpected_frame errors
121-
return frame;
122-
}
123-
};
119+
//NB: the frame confuser relies on the encoding of the
120+
//method field to be at least 8 bytes long
121+
channel.basicPublish("", "routing key", null, "Hello".getBytes());
122+
expectError(AMQP.UNEXPECTED_FRAME);
124123
}
124+
125125
}

0 commit comments

Comments
 (0)