Java Code Examples for com.google.mockwebserver.MockWebServer#getPort()
The following examples show how to use
com.google.mockwebserver.MockWebServer#getPort() .
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: ListenerTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testSuccessExecutionOnAbosoluteURI() throws IOException { MockWebServer server = new MockWebServer(); String content = "OK"; server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json") .setBody(content)); server.play(); IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "2000") .withProperty(CommonClientConfigKey.MaxAutoRetries, 1) .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://localhost:" + server.getPort() + "/testAsync/person"); Server badServer = new Server("localhost:12345"); Server goodServer = new Server("localhost:" + server.getPort()); List<Server> servers = Lists.newArrayList(goodServer, badServer); BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder() .withRule(new AvailabilityFilteringRule()) .withPing(new DummyPing()) .buildFixedServerListLoadBalancer(servers); IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.ConnectTimeout, 500); TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig); List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener); LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners); HttpClientResponse<ByteBuf> response = client.submit(request, null, overrideConfig).toBlocking().last(); assertEquals(200, response.getStatus().code()); assertEquals(1, listener.executionStartCounter.get()); assertEquals(1, listener.startWithServerCounter.get()); assertEquals(0, listener.exceptionWithServerCounter.get()); assertEquals(0, listener.executionFailedCounter.get()); assertEquals(1, listener.executionSuccessCounter.get()); assertEquals(500, listener.getContext().getClientProperty(CommonClientConfigKey.ConnectTimeout).intValue()); assertTrue(listener.isContextChecked()); assertTrue(listener.isCheckExecutionInfo()); assertSame(response, listener.getResponse()); }
Example 2
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 4 votes |
@Test public void testLoadBalancingObservablesWithReadTimeout() throws Exception { NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true); MockWebServer server = new MockWebServer(); String content = "{\"name\": \"ribbon\", \"age\": 2}"; server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json") .setBody(content)); server.play(); IClientConfig config = DefaultClientConfigImpl .getClientConfigWithDefaultValues() .set(CommonClientConfigKey.ReadTimeout, 100); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/readTimeout"); BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule()); LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler); Server goodServer = new Server("localhost:" + server.getPort()); Server badServer = new Server("localhost:" + port); lb.setServersList(Lists.newArrayList(goodServer, badServer, badServer, goodServer)); Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request)); ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>(); observableWithRetries.subscribe(observer); observer.await(); if (observer.error != null) { observer.error.printStackTrace(); } assertEquals("ribbon", observer.obj.name); assertEquals(2, observer.obj.age); ServerStats stats = lbObservables.getServerStats(badServer); server.shutdown(); final HttpClientListener listener = lbObservables.getListener(); waitUntilTrueOrTimeout(1000, new Func0<Boolean>() { @Override public Boolean call() { return listener.getPoolReleases() == 5; } }); assertEquals(0, listener.getPoolReuse()); // two requests to bad server because retry same server is set to 1 assertEquals(4, stats.getTotalRequestsCount()); assertEquals(0, stats.getActiveRequestsCount()); assertEquals(4, stats.getSuccessiveConnectionFailureCount()); stats = lbObservables.getServerStats(goodServer); assertEquals(1, stats.getTotalRequestsCount()); assertEquals(0, stats.getActiveRequestsCount()); assertEquals(0, stats.getSuccessiveConnectionFailureCount()); }
Example 3
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 4 votes |
@Test public void testLoadBalancingWithTwoServers() throws Exception { MockWebServer server = new MockWebServer(); String content = "{\"name\": \"ribbon\", \"age\": 2}"; server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json") .setBody(content)); server.play(); IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues(); HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/person") .withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null)) .withHeader("Content-type", "application/json"); NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true); BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule()); LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler); HttpClientListener externalListener = HttpClientListener.newHttpListener("external"); lbObservables.subscribe(externalListener); Server server1 = new Server("localhost:" + server.getPort()); Server server2 = new Server("localhost:" + port); lb.setServersList(Lists.newArrayList(server1, server2)); RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) { @Override public boolean isRetriableException(Throwable e, boolean sameServer) { return true; } }; Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null)); ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>(); observableWithRetries.subscribe(observer); observer.await(); if (observer.error != null) { observer.error.printStackTrace(); } assertEquals("ribbon", observer.obj.name); assertEquals(EmbeddedResources.defaultPerson.age, observer.obj.age); observer = new ObserverWithLatch<Person>(); observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null)); observableWithRetries.subscribe(observer); observer.await(); if (observer.error != null) { observer.error.printStackTrace(); } assertEquals("ribbon", observer.obj.name); assertEquals(2, observer.obj.age); ServerStats stats = lbObservables.getServerStats(server1); server.shutdown(); // assertEquals(1, stats.getTotalRequestsCount()); assertEquals(0, stats.getActiveRequestsCount()); stats = lbObservables.getServerStats(server2); // two requests to bad server because retry same server is set to 1 assertEquals(1, stats.getTotalRequestsCount()); assertEquals(0, stats.getActiveRequestsCount()); assertEquals(0, stats.getSuccessiveConnectionFailureCount()); final HttpClientListener listener = lbObservables.getListener(); assertEquals(2, listener.getPoolAcquires()); waitUntilTrueOrTimeout(1000, new Func0<Boolean>() { @Override public Boolean call() { return listener.getPoolReleases() == 2; } }); assertEquals(2, listener.getConnectionCount()); assertEquals(0, listener.getPoolReuse()); assertEquals(2, externalListener.getPoolAcquires()); }
Example 4
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 4 votes |
@Test public void testLoadBalancingPostWithReadTimeout() throws Exception { MockWebServer server = new MockWebServer(); String content = "{\"name\": \"ribbon\", \"age\": 2}"; server.enqueue(new MockResponse() .setResponseCode(200) .setHeader("Content-type", "application/json") .setBody(content)); server.play(); IClientConfig config = DefaultClientConfigImpl .getClientConfigWithDefaultValues() .set(CommonClientConfigKey.ReadTimeout, 100); HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/postTimeout") .withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null)) .withHeader("Content-type", "application/json"); NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true); BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule()); LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler); Server goodServer = new Server("localhost:" + server.getPort()); Server badServer = new Server("localhost:" + port); List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer); lb.setServersList(servers); RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) { @Override public boolean isRetriableException(Throwable e, boolean sameServer) { return true; } }; Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null)); ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>(); observableWithRetries.subscribe(observer); observer.await(); if (observer.error != null) { observer.error.printStackTrace(); } assertEquals("ribbon", observer.obj.name); assertEquals(2, observer.obj.age); ServerStats stats = lbObservables.getServerStats(badServer); server.shutdown(); assertEquals(4, stats.getTotalRequestsCount()); assertEquals(0, stats.getActiveRequestsCount()); assertEquals(4, stats.getSuccessiveConnectionFailureCount()); stats = lbObservables.getServerStats(goodServer); // two requests to bad server because retry same server is set to 1 assertEquals(1, stats.getTotalRequestsCount()); assertEquals(0, stats.getActiveRequestsCount()); assertEquals(0, stats.getSuccessiveConnectionFailureCount()); }
Example 5
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 4 votes |
@Test public void testLoadBalancingPostWithNoRetrySameServer() throws Exception { MockWebServer server = new MockWebServer(); String content = "{\"name\": \"ribbon\", \"age\": 2}"; server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json") .setBody(content)); server.play(); IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues() .set(CommonClientConfigKey.ReadTimeout, 100); HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/postTimeout") .withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null)) .withHeader("Content-type", "application/json"); NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(0, 3, true); BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule()); LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler); Server goodServer = new Server("localhost:" + server.getPort()); Server badServer = new Server("localhost:" + port); List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer); lb.setServersList(servers); RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) { @Override public boolean isRetriableException(Throwable e, boolean sameServer) { return true; } }; Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null)); ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>(); observableWithRetries.subscribe(observer); observer.await(); if (observer.error != null) { observer.error.printStackTrace(); } server.shutdown(); assertEquals("ribbon", observer.obj.name); assertEquals(2, observer.obj.age); ServerStats stats = lbObservables.getServerStats(badServer); assertEquals(2, stats.getTotalRequestsCount()); assertEquals(0, stats.getActiveRequestsCount()); assertEquals(2, stats.getSuccessiveConnectionFailureCount()); stats = lbObservables.getServerStats(goodServer); assertEquals(1, stats.getTotalRequestsCount()); assertEquals(0, stats.getActiveRequestsCount()); assertEquals(0, stats.getSuccessiveConnectionFailureCount()); }
Example 6
Source File: ListenerTest.java From ribbon with Apache License 2.0 | 4 votes |
@Test public void testSuccessExecution() throws IOException { MockWebServer server = new MockWebServer(); String content = "OK"; server.enqueue(new MockResponse() .setResponseCode(200) .setHeader("Content-type", "application/json") .setBody(content)); server.play(); IClientConfig config = DefaultClientConfigImpl .getClientConfigWithDefaultValues() .withProperty(CommonClientConfigKey.ConnectTimeout, "2000") .withProperty(CommonClientConfigKey.MaxAutoRetries, 1) .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1); System.out.println(config); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person"); Server badServer = new Server("localhost:12345"); Server goodServer = new Server("localhost:" + server.getPort()); List<Server> servers = Lists.newArrayList(goodServer, badServer); BaseLoadBalancer lb = LoadBalancerBuilder.newBuilder() .withRule(new AvailabilityFilteringRule()) .withPing(new DummyPing()) .buildFixedServerListLoadBalancer(servers); IClientConfig overrideConfig = DefaultClientConfigImpl .getEmptyConfig() .set(CommonClientConfigKey.ConnectTimeout, 500); TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<>(request, overrideConfig); List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.newArrayList(listener); LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners); HttpClientResponse<ByteBuf> response = client.submit(request, null, overrideConfig).toBlocking().last(); System.out.println(listener); assertEquals(200, response.getStatus().code()); assertEquals(1, listener.executionStartCounter.get()); assertEquals(3, listener.startWithServerCounter.get()); assertEquals(2, listener.exceptionWithServerCounter.get()); assertEquals(0, listener.executionFailedCounter.get()); assertEquals(1, listener.executionSuccessCounter.get()); assertEquals(500, listener.getContext().getClientProperty(CommonClientConfigKey.ConnectTimeout).intValue()); assertTrue(listener.isContextChecked()); assertTrue(listener.isCheckExecutionInfo()); assertSame(response, listener.getResponse()); }