use of io.vertx.core.Handler in project vert.x by eclipse.
the class RecordParserTest method testMixed.
@Test
public /*
Test mixture of fixed and delimited
*/
void testMixed() {
final int lines = 8;
final List<Object> types = new ArrayList<Object>();
class MyHandler implements Handler<Buffer> {
RecordParser parser = RecordParser.newFixed(10, this);
int pos;
public void handle(Buffer buff) {
if (pos < lines) {
Object type = types.get(pos);
if (type instanceof byte[]) {
byte[] bytes = (byte[]) type;
parser.delimitedMode(Buffer.buffer(bytes));
} else {
int length = (Integer) type;
parser.fixedSizeMode(length);
}
}
}
}
MyHandler out = new MyHandler();
Buffer[] expected = new Buffer[lines];
Buffer input = Buffer.buffer(100);
expected[0] = TestUtils.randomBuffer(10);
input.appendBuffer(expected[0]);
types.add(expected[0].length());
expected[1] = TestUtils.randomBuffer(100);
input.appendBuffer(expected[1]);
types.add(expected[1].length());
byte[] delim = new byte[] { 23, -120, 100, 3 };
expected[2] = TestUtils.randomBuffer(50, true, delim[0]);
input.appendBuffer(expected[2]);
types.add(delim);
input.appendBuffer(Buffer.buffer(delim));
expected[3] = TestUtils.randomBuffer(1000);
input.appendBuffer(expected[3]);
types.add(expected[3].length());
expected[4] = TestUtils.randomBuffer(230, true, delim[0]);
input.appendBuffer(expected[4]);
types.add(delim);
input.appendBuffer(Buffer.buffer(delim));
delim = new byte[] { 17 };
expected[5] = TestUtils.randomBuffer(341, true, delim[0]);
input.appendBuffer(expected[5]);
types.add(delim);
input.appendBuffer(Buffer.buffer(delim));
delim = new byte[] { 54, -32, 0 };
expected[6] = TestUtils.randomBuffer(1234, true, delim[0]);
input.appendBuffer(expected[6]);
types.add(delim);
input.appendBuffer(Buffer.buffer(delim));
expected[7] = TestUtils.randomBuffer(100);
input.appendBuffer(expected[7]);
types.add(expected[7].length());
feedChunks(input, out.parser, new Integer[] { 50, 10, 3 });
}
use of io.vertx.core.Handler in project vert.x by eclipse.
the class TimerTest method periodic.
private void periodic(long delay) throws Exception {
final int numFires = 10;
final AtomicLong id = new AtomicLong(-1);
id.set(vertx.setPeriodic(delay, new Handler<Long>() {
int count;
public void handle(Long timerID) {
assertEquals(id.get(), timerID.longValue());
count++;
if (count == numFires) {
vertx.cancelTimer(timerID);
setEndTimer();
}
if (count > numFires) {
fail("Fired too many times");
}
}
}));
await();
}
use of io.vertx.core.Handler in project vert.x by eclipse.
the class Http2ServerTest method testPriorKnowledge.
@Test
public void testPriorKnowledge() throws Exception {
server.close();
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST));
server.requestHandler(req -> {
req.response().end("Hello World");
});
startServer();
TestClient client = new TestClient() {
@Override
protected ChannelInitializer channelInitializer(int port, String host, Consumer<Connection> handler) {
return new ChannelInitializer() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
Http2Connection connection = new DefaultHttp2Connection(false);
TestClientHandlerBuilder clientHandlerBuilder = new TestClientHandlerBuilder(handler);
TestClientHandler clientHandler = clientHandlerBuilder.build(connection);
p.addLast(clientHandler);
}
};
}
};
ChannelFuture fut = client.connect(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, request -> {
request.decoder.frameListener(new Http2EventAdapter() {
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
vertx.runOnContext(v -> {
testComplete();
});
}
});
int id = request.nextStreamId();
request.encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of io.vertx.core.Handler in project vert.x by eclipse.
the class Http2ServerTest method testServerClose.
@Test
public void testServerClose() throws Exception {
waitFor(2);
AtomicInteger status = new AtomicInteger();
Handler<HttpServerRequest> requestHandler = req -> {
HttpConnection conn = req.connection();
conn.shutdownHandler(v -> {
assertEquals(0, status.getAndIncrement());
});
conn.closeHandler(v -> {
assertEquals(1, status.getAndIncrement());
complete();
});
conn.close();
};
server.requestHandler(requestHandler);
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
request.channel.closeFuture().addListener(v1 -> {
vertx.runOnContext(v2 -> {
complete();
});
});
request.decoder.frameListener(new Http2EventAdapter() {
@Override
public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData) throws Http2Exception {
vertx.runOnContext(v -> {
assertEquals(0, errorCode);
});
}
});
Http2ConnectionEncoder encoder = request.encoder;
int id = request.nextStreamId();
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of io.vertx.core.Handler in project vert.x by eclipse.
the class Http2ServerTest method testPushPromise.
private void testPushPromise(Http2Headers requestHeaders, BiConsumer<HttpServerResponse, Handler<AsyncResult<HttpServerResponse>>> pusher, Consumer<Http2Headers> headerChecker) throws Exception {
Context ctx = vertx.getOrCreateContext();
server.requestHandler(req -> {
Handler<AsyncResult<HttpServerResponse>> handler = ar -> {
assertSame(ctx, Vertx.currentContext());
assertTrue(ar.succeeded());
HttpServerResponse response = ar.result();
response.end("the_content");
assertIllegalStateException(() -> response.push(HttpMethod.GET, "/wibble2", resp -> {
}));
};
pusher.accept(req.response(), handler);
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, requestHeaders, 0, true, request.context.newPromise());
Map<Integer, Http2Headers> pushed = new HashMap<>();
request.decoder.frameListener(new Http2FrameAdapter() {
@Override
public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
pushed.put(promisedStreamId, headers);
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
int delta = super.onDataRead(ctx, streamId, data, padding, endOfStream);
String content = data.toString(StandardCharsets.UTF_8);
vertx.runOnContext(v -> {
assertEquals(Collections.singleton(streamId), pushed.keySet());
assertEquals("the_content", content);
Http2Headers pushedHeaders = pushed.get(streamId);
headerChecker.accept(pushedHeaders);
testComplete();
});
return delta;
}
});
});
fut.sync();
await();
}
Aggregations