Search in sources :

Example 1 with SocketFrameHandler

use of com.rabbitmq.client.impl.SocketFrameHandler in project rabbitmq-java-client by rabbitmq.

the class ConnectionOpen method crazyProtocolHeader.

@Test
public void crazyProtocolHeader() throws IOException {
    ConnectionFactory factory = TestUtils.connectionFactory();
    // keep the frame handler's socket
    Socket fhSocket = SocketFactory.getDefault().createSocket("localhost", AMQP.PROTOCOL.PORT);
    SocketFrameHandler fh = new SocketFrameHandler(fhSocket);
    // major, minor
    fh.sendHeader(100, 3);
    DataInputStream in = fh.getInputStream();
    // we should get a valid protocol header back
    byte[] header = new byte[4];
    in.read(header);
    // The protocol header is "AMQP" plus a version that the server
    // supports.  We can really only test for the first bit.
    assertEquals("AMQP", new String(header));
    in.read(header);
    assertEquals(in.available(), 0);
    // gives an error.
    if (!fhSocket.isClosed()) {
        fh.setTimeout(500);
        // NB the frame handler will return null if the socket times out
        try {
            fh.readFrame();
            fail("Expected socket read to fail due to socket being closed");
        } catch (MalformedFrameException mfe) {
            fail("Expected nothing, rather than a badly-formed something");
        } catch (IOException ioe) {
        }
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) MalformedFrameException(com.rabbitmq.client.MalformedFrameException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) SocketFrameHandler(com.rabbitmq.client.impl.SocketFrameHandler) Socket(java.net.Socket) Test(org.junit.Test)

Example 2 with SocketFrameHandler

use of com.rabbitmq.client.impl.SocketFrameHandler in project rabbitmq-java-client by rabbitmq.

the class ConnectionOpen method correctProtocolHeader.

@Test
public void correctProtocolHeader() throws IOException {
    SocketFrameHandler fh = new SocketFrameHandler(SocketFactory.getDefault().createSocket("localhost", AMQP.PROTOCOL.PORT));
    fh.sendHeader();
    AMQCommand command = new AMQCommand();
    while (!command.handleFrame(fh.readFrame())) {
    }
    Method m = command.getMethod();
    assertTrue("First command must be Connection.start", m instanceof AMQP.Connection.Start);
    AMQP.Connection.Start start = (AMQP.Connection.Start) m;
    assertTrue("Version in Connection.start is <= what we sent", start.getVersionMajor() < AMQP.PROTOCOL.MAJOR || (start.getVersionMajor() == AMQP.PROTOCOL.MAJOR && start.getVersionMinor() <= AMQP.PROTOCOL.MINOR));
}
Also used : AMQP(com.rabbitmq.client.AMQP) Method(com.rabbitmq.client.Method) SocketFrameHandler(com.rabbitmq.client.impl.SocketFrameHandler) AMQCommand(com.rabbitmq.client.impl.AMQCommand) Test(org.junit.Test)

Aggregations

SocketFrameHandler (com.rabbitmq.client.impl.SocketFrameHandler)2 Test (org.junit.Test)2 AMQP (com.rabbitmq.client.AMQP)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 MalformedFrameException (com.rabbitmq.client.MalformedFrameException)1 Method (com.rabbitmq.client.Method)1 AMQCommand (com.rabbitmq.client.impl.AMQCommand)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 Socket (java.net.Socket)1