Java Code Examples for reactor.netty.resources.ConnectionProvider#create()

The following examples show how to use reactor.netty.resources.ConnectionProvider#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpClientWithTomcatTest.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
@Test
public void contentHeader() {
	ConnectionProvider fixed = ConnectionProvider.create("contentHeader", 1);
	HttpClient client =
			HttpClient.create(fixed)
			          .wiretap(true)
			          .headers(h -> h.add("content-length", "1"));

	HttpResponseStatus r =
			client.request(HttpMethod.GET)
			      .uri(getURL())
			      .send(ByteBufFlux.fromString(Mono.just(" ")))
			      .responseSingle((res, buf) -> Mono.just(res.status()))
			      .block(Duration.ofSeconds(30));

	client.request(HttpMethod.GET)
	      .uri(getURL())
	      .send(ByteBufFlux.fromString(Mono.just(" ")))
	      .responseSingle((res, buf) -> Mono.just(res.status()))
	      .block(Duration.ofSeconds(30));

	Assert.assertEquals(r, HttpResponseStatus.BAD_REQUEST);
	fixed.dispose();
}
 
Example 2
Source File: HttpClientTest.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
@Test
public void testExplicitSendMonoErrorOnGet() {
	disposableServer =
			HttpServer.create()
			          .port(0)
			          .handle((req, res) -> res.send(req.receive().retain()))
			          .bindNow();

	ConnectionProvider pool = ConnectionProvider.create("test", 1);

	StepVerifier.create(
			Flux.range(0, 1000)
			    .flatMapDelayError(i ->
			        createHttpClientForContextWithAddress(pool)
			                .request(HttpMethod.GET)
			                .uri("/")
			                .send((req, out) -> out.send(Mono.error(new Exception("test"))))
			                .responseContent(), Queues.SMALL_BUFFER_SIZE, Queues.XS_BUFFER_SIZE))
			    .expectError()
			    .verify(Duration.ofSeconds(30));

	pool.dispose();
}
 
Example 3
Source File: HttpClientTest.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
@Test
//https://github.com/reactor/reactor-pool/issues/82
public void testConnectionRefusedConcurrentRequests(){
	ConnectionProvider provider = ConnectionProvider.create("testConnectionRefusedConcurrentRequests", 1);

	HttpClient httpClient = HttpClient.create(provider)
	                                  .port(8282);

	Mono<String> mono1 =
			httpClient.get()
			          .responseSingle((r, buf) -> buf.asString())
			          .log("mono1");

	Mono<String> mono2 =
			httpClient.get()
			          .responseSingle((r, buf) -> buf.asString())
			          .log("mono2");

	StepVerifier.create(Flux.just(mono1.onErrorResume(e -> Mono.empty()), mono2)
	                        .flatMap(Function.identity()))
	            .expectError()
	            .verify(Duration.ofSeconds(5));

	provider.disposeLater()
	        .block(Duration.ofSeconds(5));
}
 
Example 4
Source File: Http2Tests.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void testConcurrentStreamsH2() throws Exception {
	doTestConcurrentStreams(HttpClient.create(), true, HttpProtocol.H2);

	ConnectionProvider provider = ConnectionProvider.create("testConcurrentStreams", 1);
	doTestConcurrentStreams(HttpClient.create(provider), true, HttpProtocol.H2);
	provider.disposeLater()
	        .block();

	doTestConcurrentStreams(HttpClient.newConnection(), true, HttpProtocol.H2);
}
 
Example 5
Source File: Http2Tests.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void testConcurrentStreamsH2C() throws Exception {
	doTestConcurrentStreams(HttpClient.create(), false, HttpProtocol.H2C);

	ConnectionProvider provider = ConnectionProvider.create("testConcurrentStreams", 1);
	doTestConcurrentStreams(HttpClient.create(provider), false, HttpProtocol.H2C);
	provider.disposeLater()
	        .block();

	doTestConcurrentStreams(HttpClient.newConnection(), false, HttpProtocol.H2C);
}
 
Example 6
Source File: HttpClientTest.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectionNoLifeTimeElasticPool() throws Exception {
	ConnectionProvider provider =
			ConnectionProvider.create("testConnectionNoLifeTimeElasticPool", Integer.MAX_VALUE);
	ChannelId[] ids = doTestConnectionLifeTime(provider);
	assertThat(ids[0]).isEqualTo(ids[1]);
}
 
Example 7
Source File: HttpClientTest.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectionNoIdleTimeElasticPool() throws Exception {
	ConnectionProvider provider =
			ConnectionProvider.create("testConnectionNoIdleTimeElasticPool", Integer.MAX_VALUE);
	ChannelId[] ids = doTestConnectionIdleTime(provider);
	assertThat(ids[0]).isEqualTo(ids[1]);
}
 
Example 8
Source File: Http2Tests.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void testConcurrentStreamsH2CUpgrade() throws Exception {
	doTestConcurrentStreams(HttpClient.create(), false, HttpProtocol.H2C, HttpProtocol.HTTP11);

	ConnectionProvider provider = ConnectionProvider.create("testConcurrentStreamsH2CUpgrade", 1);
	doTestConcurrentStreams(HttpClient.create(provider), false, HttpProtocol.H2C, HttpProtocol.HTTP11);
	provider.disposeLater()
	        .block();

	doTestConcurrentStreams(HttpClient.newConnection(), false, HttpProtocol.H2C, HttpProtocol.HTTP11);
}
 
Example 9
Source File: HttpClientWithTomcatTest.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void disableChunkImplicitDefault() {
	ConnectionProvider p = ConnectionProvider.create("disableChunkImplicitDefault", 1);
	HttpClient client =
			HttpClient.create(p)
			          .host("localhost")
			          .port(getPort())
			          .wiretap(true);

	Tuple2<HttpResponseStatus, Channel> r =
			client.get()
			      .uri("/status/404")
			      .responseConnection((res, conn) -> Mono.just(res.status())
			                                             .delayUntil(s -> conn.inbound().receive())
			                                             .zipWith(Mono.just(conn.channel())))
			      .blockLast(Duration.ofSeconds(30));

	assertThat(r).isNotNull();

	Channel r2 =
			client.get()
			      .uri("/status/404")
			      .responseConnection((res, conn) -> Mono.just(conn.channel())
			                                             .delayUntil(s -> conn.inbound().receive()))
			      .blockLast(Duration.ofSeconds(30));

	assertThat(r2).isNotNull();

	Assert.assertSame(r.getT2(), r2);

	Assert.assertEquals(r.getT1(), HttpResponseStatus.NOT_FOUND);
	p.dispose();
}
 
Example 10
Source File: HttpClientWithTomcatTest.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleTest404_1() {
	ConnectionProvider pool = ConnectionProvider.create("simpleTest404_1", 1);
	HttpClient client =
			HttpClient.create(pool)
			          .port(getPort())
			          .host("localhost")
			          .wiretap(true);
	doSimpleTest404(client);
	doSimpleTest404(client);
	pool.dispose();
}
 
Example 11
Source File: Http2Tests.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void testConcurrentStreamsNegotiatedProtocolHTTP11() throws Exception {
	doTestConcurrentStreams(HttpClient.create(), true, new HttpProtocol[]{HttpProtocol.HTTP11},
			new HttpProtocol[]{HttpProtocol.H2, HttpProtocol.HTTP11});

	ConnectionProvider provider = ConnectionProvider.create("testConcurrentStreamsH2CUpgrade", 1);
	doTestConcurrentStreams(HttpClient.create(provider), true, new HttpProtocol[]{HttpProtocol.HTTP11},
			new HttpProtocol[]{HttpProtocol.H2, HttpProtocol.HTTP11});
	provider.disposeLater()
	        .block();

	doTestConcurrentStreams(HttpClient.newConnection(), true, new HttpProtocol[]{HttpProtocol.HTTP11},
			new HttpProtocol[]{HttpProtocol.H2, HttpProtocol.HTTP11});
}
 
Example 12
Source File: Http2Tests.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomConnectionProvider() {
	disposableServer =
			HttpServer.create()
			          .protocol(HttpProtocol.H2C)
			          .route(routes ->
			              routes.post("/echo", (req, res) -> res.send(req.receive().retain())))
			          .port(0)
			          .wiretap(true)
			          .bindNow();

	ConnectionProvider provider = ConnectionProvider.create("testCustomConnectionProvider", 1);
	String response =
			HttpClient.create(provider, 1)
			          .port(disposableServer.port())
			          .protocol(HttpProtocol.H2C)
			          .wiretap(true)
			          .post()
			          .uri("/echo")
			          .send(ByteBufFlux.fromString(Mono.just("testCustomConnectionProvider")))
			          .responseContent()
			          .aggregate()
			          .asString()
			          .block(Duration.ofSeconds(30));

	assertThat(response).isEqualTo("testCustomConnectionProvider");

	provider.disposeLater()
	        .block(Duration.ofSeconds(30));
}
 
Example 13
Source File: HttpClientTest.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void testClientReuseIssue405(){
	disposableServer =
			HttpServer.create()
			          .port(0)
			          .handle((in,out)->out.sendString(Flux.just("hello")))
			          .wiretap(true)
			          .bindNow();

	ConnectionProvider pool = ConnectionProvider.create("testClientReuseIssue405", 1);
	HttpClient httpClient = createHttpClientForContextWithPort(pool);

	Mono<String> mono1 =
			httpClient.get()
			          .responseSingle((r, buf) -> buf.asString())
			          .log("mono1");

	Mono<String> mono2 =
			httpClient.get()
			          .responseSingle((r, buf) -> buf.asString())
			          .log("mono1");

	StepVerifier.create(Flux.zip(mono1,mono2))
	            .expectNext(Tuples.of("hello","hello"))
	            .expectComplete()
	            .verify(Duration.ofSeconds(20));

	pool.dispose();
}
 
Example 14
Source File: HttpClientTest.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void userIssue() throws Exception {
	final ConnectionProvider pool = ConnectionProvider.create("userIssue", 1);
	CountDownLatch latch = new CountDownLatch(3);
	Set<String> localAddresses = ConcurrentHashMap.newKeySet();
	disposableServer =
			HttpServer.create()
			          .port(8080)
			          .route(r -> r.post("/",
			                  (req, resp) -> req.receive()
			                                    .asString()
			                                    .flatMap(data -> {
			                                        latch.countDown();
			                                        return resp.status(200)
			                                                   .send();
			                                    })))
			          .wiretap(true)
			          .bindNow();

	final HttpClient client = createHttpClientForContextWithAddress(pool);

	Flux.just("1", "2", "3")
	    .concatMap(data ->
	            client.doOnResponse((res, conn) ->
	                    localAddresses.add(conn.channel()
	                                           .localAddress()
	                                           .toString()))
	                  .post()
	                  .uri("/")
	                  .send(ByteBufFlux.fromString(Flux.just(data)))
	                  .responseContent())
	    .subscribe();


	latch.await();
	pool.dispose();
	System.out.println("Local Addresses used: " + localAddresses);
}
 
Example 15
Source File: Http2Tests.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void testMaxActiveStreams_2() throws Exception {
	doTestMaxActiveStreams(HttpClient.create(), 2, 2, 0);

	ConnectionProvider provider = ConnectionProvider.create("testMaxActiveStreams_2", 1);
	doTestMaxActiveStreams(HttpClient.create(provider), 2, 2, 0);
	provider.disposeLater()
	        .block();

	doTestMaxActiveStreams(HttpClient.newConnection(), 2, 2, 0);
}
 
Example 16
Source File: HttpClientWithTomcatTest.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleClientPooling() {
	ConnectionProvider p = ConnectionProvider.create("simpleClientPooling", 1);
	AtomicReference<Channel> ch1 = new AtomicReference<>();
	AtomicReference<Channel> ch2 = new AtomicReference<>();

	HttpResponseStatus r =
			HttpClient.create(p)
			          .doOnResponse((res, c) -> ch1.set(c.channel()))
			          .wiretap(true)
			          .get()
			          .uri(getURL() + "/status/404")
			          .responseSingle((res, buf) -> buf.thenReturn(res.status()))
			          .block(Duration.ofSeconds(30));

	HttpClient.create(p)
	          .doOnResponse((res, c) -> ch2.set(c.channel()))
	          .wiretap(true)
	          .get()
	          .uri(getURL() + "/status/404")
	          .responseSingle((res, buf) -> buf.thenReturn(res.status()))
	          .block(Duration.ofSeconds(30));

	AtomicBoolean same = new AtomicBoolean();

	same.set(ch1.get() == ch2.get());

	Assert.assertTrue(same.get());

	Assert.assertEquals(r, HttpResponseStatus.NOT_FOUND);
	p.dispose();
}
 
Example 17
Source File: Http2Tests.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void testMaxActiveStreams_1() throws Exception {
	doTestMaxActiveStreams(HttpClient.create(), 1, 1, 1);

	ConnectionProvider provider = ConnectionProvider.create("testMaxActiveStreams_1", 1);
	doTestMaxActiveStreams(HttpClient.create(provider), 1, 1, 1);
	provider.disposeLater()
	        .block();

	doTestMaxActiveStreams(HttpClient.newConnection(), 1, 1, 1);
}
 
Example 18
Source File: HttpClientTest.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
@Test
public void testIssue361() {
	disposableServer =
			HttpServer.create()
			          .port(0)
			          .handle((req, res) -> req.receive()
			                                   .aggregate()
			                                   .asString()
			          .flatMap(s -> res.sendString(Mono.just(s))
			                           .then()))
			          .bindNow();

	assertThat(disposableServer).isNotNull();

	ConnectionProvider connectionProvider = ConnectionProvider.create("testIssue361", 1);
	HttpClient client = createHttpClientForContextWithPort(connectionProvider);

	String response = client.post()
	                        .uri("/")
	                        .send(ByteBufFlux.fromString(Mono.just("test")
	                                         .then(Mono.error(new Exception("error")))))
	                        .responseContent()
	                        .aggregate()
	                        .asString()
	                        .onErrorResume(t -> Mono.just(t.getMessage()))
	                        .block(Duration.ofSeconds(30));

	assertThat(response).isEqualTo("error");

	response = client.post()
	                 .uri("/")
	                 .send(ByteBufFlux.fromString(Mono.just("test")))
	                 .responseContent()
	                 .aggregate()
	                 .asString()
	                 .block(Duration.ofSeconds(30));

	assertThat(response).isEqualTo("test");

	connectionProvider.dispose();
}
 
Example 19
Source File: HttpClientTest.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
@Test
public void testChannelGroupClosesAllConnections() throws Exception {
	disposableServer =
			HttpServer.create()
			          .port(0)
			          .route(r -> r.get("/never",
			                  (req, res) -> res.sendString(Mono.never()))
			              .get("/delay10",
			                  (req, res) -> res.sendString(Mono.just("test")
			                                                   .delayElement(Duration.ofSeconds(10))))
			              .get("/delay1",
			                  (req, res) -> res.sendString(Mono.just("test")
			                                                   .delayElement(Duration.ofSeconds(1)))))
			          .wiretap(true)
			          .bindNow(Duration.ofSeconds(30));

	ConnectionProvider connectionProvider =
			ConnectionProvider.create("testChannelGroupClosesAllConnections", Integer.MAX_VALUE);

	ChannelGroup group = new DefaultChannelGroup(new DefaultEventExecutor());

	CountDownLatch latch1 = new CountDownLatch(3);
	CountDownLatch latch2 = new CountDownLatch(3);

	HttpClient client = createHttpClientForContextWithAddress(connectionProvider);

	Flux.just("/never", "/delay10", "/delay1")
	    .flatMap(s ->
	            client.doOnConnected(c -> {
	                          c.onDispose()
	                           .subscribe(null, null, latch2::countDown);
	                          group.add(c.channel());
	                          latch1.countDown();
	                      })
	                  .get()
	                  .uri(s)
	                  .responseContent()
	                  .aggregate()
	                  .asString())
	    .subscribe();

	assertThat(latch1.await(30, TimeUnit.SECONDS)).isTrue();

	Mono.whenDelayError(FutureMono.from(group.close()), connectionProvider.disposeLater())
	    .block(Duration.ofSeconds(30));

	assertThat(latch2.await(30, TimeUnit.SECONDS)).isTrue();
}
 
Example 20
Source File: TcpClientTests.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
private void doTestIssue600(boolean withLoop) {
	DisposableServer server =
			TcpServer.create()
			         .port(0)
			         .handle((req, res) -> res.send(req.receive()
			                                           .retain()
			                                           .delaySubscription(Duration.ofSeconds(1))))
			         .wiretap(true)
			         .bindNow();

	ConnectionProvider pool = ConnectionProvider.create("doTestIssue600", 10);
	LoopResources loop = LoopResources.create("test", 4, true);
	TcpClient client;
	if (withLoop) {
		client =
				TcpClient.create(pool)
				         .remoteAddress(server::address)
				         .runOn(loop);
	}
	else {
		client =
				TcpClient.create(pool)
				         .remoteAddress(server::address);
	}

	Set<String> threadNames = new ConcurrentSkipListSet<>();
	StepVerifier.create(
			Flux.range(1,4)
			    .flatMap(i ->
			            client.handle((in, out) -> {
			                threadNames.add(Thread.currentThread().getName());
			                return out.send(Flux.empty());
			            })
			            .connect()))
	            .expectNextCount(4)
	            .expectComplete()
	            .verify(Duration.ofSeconds(30));

	pool.dispose();
	loop.dispose();
	server.disposeNow();

	Assertions.assertThat(threadNames.size()).isGreaterThan(1);
}