@@ -0,0 +1,2302 @@
+package zmq.io;%0A%0Aimport static org.hamcrest.CoreMatchers.is;%0Aimport static org.hamcrest.CoreMatchers.notNullValue;%0Aimport static org.junit.Assert.assertThat;%0A%0Aimport java.io.IOException;%0A%0Aimport org.junit.Test;%0A%0Aimport zmq.Config;%0Aimport zmq.Ctx;%0Aimport zmq.Msg;%0Aimport zmq.SocketBase;%0Aimport zmq.ZMQ;%0Aimport zmq.util.Utils;%0A%0Apublic class StreamEngineTest%0A%7B%0A    @Test%0A    public void testEncoderFlipIssue520() throws IOException%0A    %7B%0A        Ctx ctx = ZMQ.createContext();%0A        assertThat(ctx, notNullValue());%0A%0A        SocketBase sender = ZMQ.socket(ctx, ZMQ.ZMQ_PUSH);%0A        assertThat(sender, notNullValue());%0A%0A        boolean rc = ZMQ.setSocketOption(sender, ZMQ.ZMQ_IMMEDIATE, false);%0A        assertThat(rc, is(true));%0A%0A        SocketBase receiver = ZMQ.socket(ctx, ZMQ.ZMQ_PULL);%0A        assertThat(receiver, notNullValue());%0A%0A        final int port = Utils.findOpenPort();%0A        final String addr = %22tcp://localhost:%22 + port;%0A%0A        rc = ZMQ.bind(receiver, addr);%0A        assertThat(rc, is(true));%0A%0A        rc = ZMQ.connect(sender, addr);%0A        assertThat(rc, is(true));%0A%0A        final int headerSize = 8 + 1;%0A        // first message + second message header fill the buffer%0A        byte%5B%5D msg1 = msg(Config.OUT_BATCH_SIZE.getValue() - 2 * headerSize);%0A        // second message will go in zero-copy mode%0A        byte%5B%5D msg2 = msg(Config.OUT_BATCH_SIZE.getValue());%0A%0A        exchange(sender, receiver, msg1, msg2, msg1, msg2);%0A%0A        ZMQ.close(receiver);%0A        ZMQ.close(sender);%0A        ZMQ.term(ctx);%0A    %7D%0A%0A    private byte%5B%5D msg(int length)%0A    %7B%0A        byte%5B%5D msg = new byte%5Blength%5D;%0A        for (int idx = 0; idx %3C msg.length; ++idx) %7B%0A            msg%5Bidx%5D = (byte) idx;%0A        %7D%0A        return msg;%0A    %7D%0A%0A    private void exchange(SocketBase sender, SocketBase receiver, byte%5B%5D... msgs)%0A    %7B%0A        int repetition = 50;%0A        for (int idx = 0; idx %3C repetition; ++idx) %7B%0A            for (byte%5B%5D msg : msgs) %7B%0A                int num = ZMQ.send(sender, msg, 0);%0A                assertThat(num, is(msg.length));%0A            %7D%0A        %7D%0A        for (int idx = 0; idx %3C repetition; ++idx) %7B%0A            for (byte%5B%5D msg : msgs) %7B%0A                Msg received = ZMQ.recv(receiver, 0);%0A                assertThat(received.data(), is(msg));%0A            %7D%0A        %7D%0A    %7D%0A%7D%0A

Test for issue #520