Search in sources :

Example 61 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class DatagramTest method testEcho.

@Test
public void testEcho() {
    peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer1.exceptionHandler(t -> fail(t.getMessage()));
    peer2.exceptionHandler(t -> fail(t.getMessage()));
    peer2.listen(1234, "127.0.0.1", ar -> {
        assertTrue(ar.succeeded());
        Buffer buffer = TestUtils.randomBuffer(128);
        peer2.handler(packet -> {
            assertEquals("127.0.0.1", packet.sender().host());
            assertEquals(1235, packet.sender().port());
            assertEquals(buffer, packet.data());
            peer2.send(packet.data(), 1235, "127.0.0.1", ar2 -> assertTrue(ar2.succeeded()));
        });
        peer1.listen(1235, "127.0.0.1", ar2 -> {
            peer1.handler(packet -> {
                assertEquals(buffer, packet.data());
                assertEquals("127.0.0.1", packet.sender().host());
                assertEquals(1234, packet.sender().port());
                testComplete();
            });
            peer1.send(buffer, 1234, "127.0.0.1", ar3 -> assertTrue(ar3.succeeded()));
        });
    });
    await();
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) Buffer(io.vertx.core.buffer.Buffer) Test(org.junit.Test)

Example 62 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class DatagramTest method testLogging.

private TestLoggerFactory testLogging(DatagramSocketOptions sendOptions, DatagramSocketOptions listenOptions) throws Exception {
    InternalLoggerFactory prev = InternalLoggerFactory.getDefaultFactory();
    TestLoggerFactory factory = new TestLoggerFactory();
    InternalLoggerFactory.setDefaultFactory(factory);
    try {
        peer1 = vertx.createDatagramSocket(sendOptions);
        peer2 = vertx.createDatagramSocket(listenOptions);
        peer2.exceptionHandler(t -> fail(t.getMessage()));
        peer2.listen(1234, "127.0.0.1", ar -> {
            assertTrue(ar.succeeded());
            Buffer buffer = TestUtils.randomBuffer(128);
            peer2.handler(packet -> {
                assertEquals(buffer, packet.data());
                testComplete();
            });
            peer1.send(buffer, 1234, "127.0.0.1", ar2 -> assertTrue(ar2.succeeded()));
        });
        await();
    } finally {
        InternalLoggerFactory.setDefaultFactory(prev);
    }
    return factory;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory)

Example 63 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class DatagramTest method testSender.

@Test
public void testSender() {
    peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2.exceptionHandler(t -> fail(t.getMessage()));
    peer2.listen(1234, "127.0.0.1", ar -> {
        Buffer buffer = TestUtils.randomBuffer(128);
        peer2.handler(packet -> {
            assertEquals(buffer, packet.data());
            testComplete();
        });
        WriteStream<Buffer> sender1 = peer1.sender(1234, "127.0.0.1");
        sender1.write(buffer);
    });
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) Buffer(io.vertx.core.buffer.Buffer) Test(org.junit.Test)

Example 64 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class DatagramTest method testPauseResume.

@Test
public void testPauseResume() {
    peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2.exceptionHandler(t -> fail(t.getMessage()));
    peer2.listen(1234, "127.0.0.1", ar -> {
        Buffer buffer = TestUtils.randomBuffer(128);
        AtomicBoolean received = new AtomicBoolean();
        peer2.handler(packet -> received.set(true));
        peer2.pause();
        peer1.send(buffer, 1234, "127.0.0.1", ar2 -> {
            assertTrue(ar2.succeeded());
        });
        vertx.setTimer(1000, l -> {
            AtomicInteger count = new AtomicInteger();
            peer2.handler(packet -> {
                switch(count.getAndIncrement()) {
                    case 0:
                        assertEquals(buffer, packet.data());
                        peer1.send(buffer, 1234, "127.0.0.1", ar2 -> {
                            assertTrue(ar2.succeeded());
                        });
                        break;
                    case 1:
                        assertFalse(received.get());
                        assertEquals(buffer, packet.data());
                        testComplete();
                        break;
                    default:
                        fail();
                }
            });
            peer2.resume();
        });
    });
    await();
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) Buffer(io.vertx.core.buffer.Buffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 65 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class DatagramTest method testSendReceive.

@Test
public void testSendReceive() {
    peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2.exceptionHandler(t -> fail(t.getMessage()));
    peer2.listen(1234, "127.0.0.1", ar -> {
        assertTrue(ar.succeeded());
        Buffer buffer = TestUtils.randomBuffer(128);
        peer2.handler(packet -> {
            Buffer data = packet.data();
            ByteBuf buff = data.getByteBuf();
            while (buff != buff.unwrap() && buff.unwrap() != null) {
                buff = buff.unwrap();
            }
            assertTrue("Was expecting an unpooled buffer instead of " + buff.getClass().getSimpleName(), buff.getClass().getSimpleName().contains("Unpooled"));
            assertEquals(buffer, data);
            testComplete();
        });
        peer1.send(buffer, 1234, "127.0.0.1", ar2 -> assertTrue(ar2.succeeded()));
    });
    await();
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) Buffer(io.vertx.core.buffer.Buffer) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

Buffer (io.vertx.core.buffer.Buffer)1052 Test (org.junit.Test)604 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)256 Future (io.vertx.core.Future)253 AtomicReference (java.util.concurrent.atomic.AtomicReference)237 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)235 Handler (io.vertx.core.Handler)230 Vertx (io.vertx.core.Vertx)185 TestUtils (io.vertx.test.core.TestUtils)177 JsonObject (io.vertx.core.json.JsonObject)172 TimeUnit (java.util.concurrent.TimeUnit)161 CountDownLatch (java.util.concurrent.CountDownLatch)153 List (java.util.List)152 Consumer (java.util.function.Consumer)149 Function (java.util.function.Function)142 ReadStream (io.vertx.core.streams.ReadStream)139 Context (io.vertx.core.Context)135 ArrayList (java.util.ArrayList)132 MultiMap (io.vertx.core.MultiMap)130 Assume (org.junit.Assume)130