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();
}
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;
}
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);
});
}
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();
}
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();
}
Aggregations