@Test public void echo() throws Exception { int count = 100; Flux<String> input = Flux.range(1, count).map(index -> "msg-" + index); ReplayProcessor<Object> output = ReplayProcessor.create(count); this.client.execute(getUrl("/echo"), session -> session .send(input.map(session::textMessage)) .thenMany(session.receive().take(count).map(WebSocketMessage::getPayloadAsText)) .subscribeWith(output) .then()) .block(TIMEOUT); assertEquals(input.collectList().block(TIMEOUT), output.collectList().block(TIMEOUT)); }
@Test public void echo() throws Exception { int count = 100; Flux<String> input = Flux.range(1, count).map(index -> "msg-" + index); ReplayProcessor<Object> output = ReplayProcessor.create(count); client.execute(getUrl("/echo"), session -> { logger.debug("Starting to send messages"); return session .send(input.doOnNext(s -> logger.debug("outbound " + s)).map(s -> session.textMessage(s))) .thenMany(session.receive().take(count).map(WebSocketMessage::getPayloadAsText)) .subscribeWith(output) .doOnNext(s -> logger.debug("inbound " + s)) .then() .doOnSuccessOrError((aVoid, ex) -> logger.debug("Done with " + (ex != null ? ex.getMessage() : "success"))); }) .block(Duration.ofMillis(5000)); assertEquals(input.collectList().block(Duration.ofMillis(5000)), output.collectList().block(Duration.ofMillis(5000))); }
@Test public void echoForHttp() throws Exception { int count = 100; Flux<String> input = Flux.range(1, count).map(index -> "msg-" + index); ReplayProcessor<Object> output = ReplayProcessor.create(count); client.execute(getHttpUrl("/echoForHttp"), session -> { logger.debug("Starting to send messages"); return session .send(input.doOnNext(s -> logger.debug("outbound " + s)).map(s -> session.textMessage(s))) .thenMany(session.receive().take(count).map(WebSocketMessage::getPayloadAsText)) .subscribeWith(output) .doOnNext(s -> logger.debug("inbound " + s)) .then() .doOnSuccessOrError((aVoid, ex) -> logger.debug("Done with " + (ex != null ? ex.getMessage() : "success"))); }) .block(Duration.ofMillis(5000)); assertEquals(input.collectList().block(Duration.ofMillis(5000)), output.collectList().block(Duration.ofMillis(5000))); }
.blockLast(Duration.ofSeconds(30)); assertThat(output.collectList().block(Duration.ofSeconds(30))) .isEqualTo(expectation.collectList().block(Duration.ofSeconds(30)));
private void doTestConnectionAliveWhenTransformationErrors(BiFunction<? super WebsocketInbound, ? super WebsocketOutbound, ? extends Publisher<Void>> handler, Flux<String> expectation, int count) { httpServer = HttpServer.create() .port(0) .handle((req, res) -> res.sendWebsocket(handler)) .wiretap(true) .bindNow(); ReplayProcessor<String> output = ReplayProcessor.create(); HttpClient.create() .port(httpServer.address().getPort()) .websocket() .uri("/") .handle((in, out) -> out.sendString(Flux.just("1", "text", "2")) .then(in.aggregateFrames() .receiveFrames() .map(WebSocketFrame::content) .map(byteBuf -> byteBuf.readCharSequence(byteBuf.readableBytes(), Charset.defaultCharset()).toString()) .take(count) .subscribeWith(output) .then())) .blockLast(Duration.ofSeconds(30)); assertThat(output.collectList().block(Duration.ofSeconds(30))) .isEqualTo(expectation.collectList().block(Duration.ofSeconds(30))); }