Search in sources :

Example 1 with BoltRequestMessageRecorder

use of org.neo4j.bolt.v1.messaging.BoltRequestMessageRecorder in project neo4j by neo4j.

the class MessageMatchers method messages.

public static List<RequestMessage> messages(byte[] bytes) throws IOException {
    BoltRequestMessageReader unpacker = requestReader(bytes);
    BoltRequestMessageRecorder consumer = new BoltRequestMessageRecorder();
    try {
        while (unpacker.hasNext()) {
            unpacker.read(consumer);
        }
        return consumer.asList();
    } catch (Throwable e) {
        throw new IOException("Failed to deserialize response, '" + e.getMessage() + "'. Messages read so " + "far: \n" + consumer.asList() + "\n" + "Raw data: \n" + HexPrinter.hex(bytes));
    }
}
Also used : BoltRequestMessageReader(org.neo4j.bolt.v1.messaging.BoltRequestMessageReader) BoltRequestMessageRecorder(org.neo4j.bolt.v1.messaging.BoltRequestMessageRecorder) IOException(java.io.IOException)

Example 2 with BoltRequestMessageRecorder

use of org.neo4j.bolt.v1.messaging.BoltRequestMessageRecorder in project neo4j by neo4j.

the class BoltV1DechunkerTest method shouldReadMessageWhenTheHeaderIsSplitAcrossChunks.

@Test
public void shouldReadMessageWhenTheHeaderIsSplitAcrossChunks() throws Exception {
    Random random = ThreadLocalRandom.current();
    for (int len = 1; len <= 0x8000; len = len << 1) {
        // given
        StringBuilder content = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            content.appendCodePoint('a' + random.nextInt('z' - 'a'));
        }
        RunMessage run = run(content.toString());
        byte[] message = MessageMatchers.serialize(run);
        byte head1 = (byte) (message.length >> 8), head2 = (byte) (message.length & 0xFF);
        byte[] chunk2 = new byte[message.length + 3];
        chunk2[0] = head2;
        System.arraycopy(message, 0, chunk2, 1, message.length);
        BoltRequestMessageRecorder messages = new BoltRequestMessageRecorder();
        BoltV1Dechunker dechunker = new BoltV1Dechunker(messages, () -> {
        });
        // when
        dechunker.handle(wrappedBuffer(new byte[] { head1 }));
        assertTrue("content length " + len + ": should be waiting for second chunk", messages.asList().isEmpty());
        dechunker.handle(wrappedBuffer(chunk2));
        // then
        assertEquals("content length " + len + ": should have received message", 1, messages.asList().size());
        assertEquals(run, messages.asList().get(0));
    }
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Random(java.util.Random) BoltRequestMessageRecorder(org.neo4j.bolt.v1.messaging.BoltRequestMessageRecorder) RunMessage(org.neo4j.bolt.v1.messaging.message.RunMessage) Test(org.junit.Test)

Aggregations

BoltRequestMessageRecorder (org.neo4j.bolt.v1.messaging.BoltRequestMessageRecorder)2 IOException (java.io.IOException)1 Random (java.util.Random)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 Test (org.junit.Test)1 BoltRequestMessageReader (org.neo4j.bolt.v1.messaging.BoltRequestMessageReader)1 RunMessage (org.neo4j.bolt.v1.messaging.message.RunMessage)1