Search in sources :

Example 1 with SimpleServer

use of org.firebirdsql.common.SimpleServer in project jaybird by FirebirdSQL.

the class TestV10EventHandling method testAsynchronousDelivery_fullEvent.

@Test
public void testAsynchronousDelivery_fullEvent() throws Exception {
    final SimpleChannelListener listener = new SimpleChannelListener();
    try (SimpleServer simpleServer = new SimpleServer()) {
        final FbWireAsynchronousChannel channel = new V10AsynchronousChannel(createDummyDatabase());
        channel.addChannelListener(listener);
        Thread establishChannel = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    channel.connect("localhost", simpleServer.getPort(), 1);
                } catch (SQLException e) {
                // suppress
                }
            }
        });
        establishChannel.start();
        simpleServer.acceptConnection();
        AsynchronousProcessor.getInstance().registerAsynchronousChannel(channel);
        establishChannel.join(500);
        assertTrue("Expected connected channel", channel.isConnected());
        final XdrOutputStream out = new XdrOutputStream(simpleServer.getOutputStream());
        out.writeInt(op_event);
        out.writeInt(513);
        out.writeBuffer(new byte[] { 1, 3, 69, 86, 84, 3, 0, 0, 0 });
        out.writeLong(0);
        out.writeInt(7);
        out.flush();
        Thread.sleep(500);
        List<AsynchronousChannelListener.Event> receivedEvents = listener.getReceivedEvents();
        assertEquals("Unexpected number of events", 1, receivedEvents.size());
        AsynchronousChannelListener.Event event = receivedEvents.get(0);
        assertEquals("Unexpected eventId", 7, event.getEventId());
        assertEquals("Unexpected event count", 3, event.getEventCount());
    }
}
Also used : SimpleServer(org.firebirdsql.common.SimpleServer) SQLException(java.sql.SQLException) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) Test(org.junit.Test)

Example 2 with SimpleServer

use of org.firebirdsql.common.SimpleServer in project jaybird by FirebirdSQL.

the class TestV10EventHandling method testAsynchronousDelivery_largeNumberOfEvents.

@Test
public void testAsynchronousDelivery_largeNumberOfEvents() throws Exception {
    final SimpleChannelListener listener = new SimpleChannelListener();
    try (SimpleServer simpleServer = new SimpleServer()) {
        final FbWireAsynchronousChannel channel = new V10AsynchronousChannel(createDummyDatabase());
        channel.addChannelListener(listener);
        Thread establishChannel = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    channel.connect("localhost", simpleServer.getPort(), 1);
                } catch (SQLException e) {
                // suppress
                }
            }
        });
        establishChannel.start();
        simpleServer.acceptConnection();
        AsynchronousProcessor.getInstance().registerAsynchronousChannel(channel);
        assertTrue("Expected connected channel", channel.isConnected());
        final XdrOutputStream out = new XdrOutputStream(simpleServer.getOutputStream());
        // Write a large number of events
        final int testEventCount = 1024;
        for (int count = 1; count <= testEventCount; count++) {
            out.writeInt(op_event);
            out.writeInt(513);
            out.writeBuffer(new byte[] { 1, 5, 69, 86, 69, 78, 84, (byte) count, (byte) (count >> 8), (byte) (count >> 16), (byte) (count >> 24) });
            out.writeLong(0);
            out.writeInt(7);
        }
        out.flush();
        // Need to sleep for thread to process all events, might still fail on slower computers
        Thread.sleep(500);
        List<AsynchronousChannelListener.Event> receivedEvents = listener.getReceivedEvents();
        assertEquals("Unexpected number of events", testEventCount, receivedEvents.size());
        AsynchronousChannelListener.Event event = receivedEvents.get(0);
        assertEquals("Unexpected eventId", 7, event.getEventId());
        assertEquals("Unexpected event count", 1, event.getEventCount());
        AsynchronousChannelListener.Event lastEvent = receivedEvents.get(testEventCount - 1);
        assertEquals("Unexpected eventId", 7, lastEvent.getEventId());
        assertEquals("Unexpected event count", testEventCount, lastEvent.getEventCount());
    }
}
Also used : SimpleServer(org.firebirdsql.common.SimpleServer) SQLException(java.sql.SQLException) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) Test(org.junit.Test)

Example 3 with SimpleServer

use of org.firebirdsql.common.SimpleServer in project jaybird by FirebirdSQL.

the class TestV10EventHandling method checkAsynchronousDisconnection.

private void checkAsynchronousDisconnection(int disconnectOperation) throws Exception {
    try (SimpleServer simpleServer = new SimpleServer()) {
        final FbWireAsynchronousChannel channel = new V10AsynchronousChannel(createDummyDatabase());
        Thread establishChannel = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    channel.connect("localhost", simpleServer.getPort(), 1);
                } catch (SQLException e) {
                // suppress
                }
            }
        });
        establishChannel.start();
        simpleServer.acceptConnection();
        AsynchronousProcessor.getInstance().registerAsynchronousChannel(channel);
        establishChannel.join(500);
        assertTrue("Expected connected channel", channel.isConnected());
        final XdrOutputStream out = new XdrOutputStream(simpleServer.getOutputStream());
        out.writeInt(disconnectOperation);
        out.flush();
        Thread.sleep(500);
        assertFalse("Expected disconnected channel", channel.isConnected());
    }
}
Also used : SimpleServer(org.firebirdsql.common.SimpleServer) SQLException(java.sql.SQLException) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream)

Example 4 with SimpleServer

use of org.firebirdsql.common.SimpleServer in project jaybird by FirebirdSQL.

the class TestV10EventHandling method testAsynchronousDelivery_partialEvent.

@Test
public void testAsynchronousDelivery_partialEvent() throws Exception {
    final SimpleChannelListener listener = new SimpleChannelListener();
    try (SimpleServer simpleServer = new SimpleServer()) {
        final FbWireAsynchronousChannel channel = new V10AsynchronousChannel(createDummyDatabase());
        channel.addChannelListener(listener);
        Thread establishChannel = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    channel.connect("localhost", simpleServer.getPort(), 1);
                } catch (SQLException e) {
                // suppress
                }
            }
        });
        establishChannel.start();
        simpleServer.acceptConnection();
        AsynchronousProcessor.getInstance().registerAsynchronousChannel(channel);
        establishChannel.join(500);
        assertTrue("Expected connected channel", channel.isConnected());
        final XdrOutputStream out = new XdrOutputStream(simpleServer.getOutputStream());
        out.writeInt(op_dummy);
        out.writeInt(op_event);
        out.writeInt(513);
        out.writeBuffer(new byte[] { 1, 3, 69, 86, 84, 3, 0, 0, 0 });
        // Flushing partial event to test if processing works as expected
        out.flush();
        Thread.sleep(500);
        List<AsynchronousChannelListener.Event> receivedEvents = listener.getReceivedEvents();
        assertEquals("Unexpected number of events", 0, receivedEvents.size());
        out.writeLong(0);
        out.writeInt(7);
        out.flush();
        Thread.sleep(500);
        receivedEvents = listener.getReceivedEvents();
        assertEquals("Unexpected number of events", 1, receivedEvents.size());
        AsynchronousChannelListener.Event event = receivedEvents.get(0);
        assertEquals("Unexpected eventId", 7, event.getEventId());
        assertEquals("Unexpected event count", 3, event.getEventCount());
    }
}
Also used : SimpleServer(org.firebirdsql.common.SimpleServer) SQLException(java.sql.SQLException) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) Test(org.junit.Test)

Aggregations

SQLException (java.sql.SQLException)4 SimpleServer (org.firebirdsql.common.SimpleServer)4 XdrOutputStream (org.firebirdsql.gds.impl.wire.XdrOutputStream)4 Test (org.junit.Test)3