com.netflix.ribbon.transport.netty.RibbonTransport Java Examples
The following examples show how to use
com.netflix.ribbon.transport.netty.RibbonTransport.
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: NettyClientTest.java From ribbon with Apache License 2.0 | 6 votes |
@Test public void testObservable() throws Exception { HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/person"); LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(); Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request); Person person = getPersonObservable(response).toBlocking().single(); assertEquals(EmbeddedResources.defaultPerson, person); final HttpClientListener listener = observableClient.getListener(); assertEquals(1, listener.getPoolAcquires()); assertEquals(1, listener.getConnectionCount()); waitUntilTrueOrTimeout(1000, new Func0<Boolean>() { @Override public Boolean call() { return listener.getPoolReleases() == 1; } }); }
Example #2
Source File: SecuredTransportFactory.java From thorntail with Apache License 2.0 | 6 votes |
@Override public HttpClient<ByteBuf, ByteBuf> newHttpClient(final IClientConfig config) { final List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = new ArrayList<>(); listeners.add(createBearerHeaderAdder()); final PipelineConfiguratorComposite<HttpClientResponse<ByteBuf>, HttpClientRequest<ByteBuf>> pipelineConfigurator = new PipelineConfiguratorComposite<HttpClientResponse<ByteBuf>, HttpClientRequest<ByteBuf>>(new HttpClientPipelineConfigurator<ByteBuf, ByteBuf>(), new HttpObjectAggregationConfigurator(maxChunkSize)); final LoadBalancingHttpClient<ByteBuf, ByteBuf> client = LoadBalancingHttpClient.<ByteBuf, ByteBuf>builder() .withClientConfig(config) .withExecutorListeners(listeners) .withRetryHandler(getDefaultHttpRetryHandlerWithConfig(config)) .withPipelineConfigurator(pipelineConfigurator) .withPoolCleanerScheduler(RibbonTransport.poolCleanerScheduler) .build(); return client; }
Example #3
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 6 votes |
@Test public void testStreamWithLoadBalancer() throws Exception { // NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true); // IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000"); IClientConfig config = IClientConfig.Builder.newBuilder().withRetryOnAllOperations(true) .withMaxAutoRetries(1) .withMaxAutoRetriesNextServer(3) .build(); BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule()); LoadBalancingHttpClient<ByteBuf, ServerSentEvent> lbObservables = (LoadBalancingHttpClient<ByteBuf, ServerSentEvent>) RibbonTransport.newSSEClient(lb, config); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/personStream"); List<Person> result = Lists.newArrayList(); Server goodServer = new Server("localhost:" + port); Server badServer = new Server("localhost:12245"); List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer); lb.setServersList(servers); result = getPersonListFromResponse(lbObservables.submit(request, null, null)); assertEquals(EmbeddedResources.entityStream, result); }
Example #4
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 6 votes |
@Test public void testPostWithByteBuf() throws Exception { Person myPerson = new Person("netty", 5); ObjectMapper mapper = new ObjectMapper(); byte[] raw = mapper.writeValueAsBytes(myPerson); ByteBuf buffer = Unpooled.copiedBuffer(raw); HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost(SERVICE_URI + "testAsync/person") .withHeader("Content-type", "application/json") .withHeader("Content-length", String.valueOf(raw.length)) .withContent(buffer); LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient( DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(CommonClientConfigKey.ReadTimeout, 10000)); Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request); Person person = getPersonObservable(response).toBlocking().single(); assertEquals(myPerson, person); }
Example #5
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 6 votes |
@Test public void testPoolReuse() throws Exception { HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/person"); LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient( IClientConfig.Builder.newBuilder().withDefaultValues() .withMaxAutoRetries(1) .withMaxAutoRetriesNextServer(1).build()); Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request); Person person = getPersonObservable(response).toBlocking().single(); assertEquals(EmbeddedResources.defaultPerson, person); response = observableClient.submit(request); person = getPersonObservable(response).toBlocking().single(); assertEquals(EmbeddedResources.defaultPerson, person); final HttpClientListener listener = observableClient.getListener(); assertEquals(2, listener.getPoolAcquires()); waitUntilTrueOrTimeout(1000, new Func0<Boolean>() { @Override public Boolean call() { return listener.getPoolReleases() == 2; } }); assertEquals(1, listener.getConnectionCount()); assertEquals(1, listener.getPoolReuse()); }
Example #6
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 6 votes |
@Test public void testSubmitToAbsoluteURI() throws Exception { HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/person"); LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(); // final List<Person> result = Lists.newArrayList(); Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request); Person person = getPersonObservable(response).toBlocking().single(); assertEquals(EmbeddedResources.defaultPerson, person); // need to sleep to wait until connection is released final HttpClientListener listener = observableClient.getListener(); assertEquals(1, listener.getConnectionCount()); assertEquals(1, listener.getPoolAcquires()); waitUntilTrueOrTimeout(1000, new Func0<Boolean>() { @Override public Boolean call() { return listener.getPoolReleases() == 1; } }); }
Example #7
Source File: DiscoveryLoadBalancerTest.java From ribbon with Apache License 2.0 | 6 votes |
@Test public void testLoadBalancer() { IClientConfig config = IClientConfig.Builder.newBuilder().withDefaultValues() .withDeploymentContextBasedVipAddresses(getVipAddress()).build() .set(IClientConfigKey.Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName()); LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(config); LoadBalancerContext lbContext = client.getLoadBalancerContext(); List<Server> serverList = lbContext.getLoadBalancer().getAllServers(); assertEquals(getMockServerList(), serverList); }
Example #8
Source File: SimpleGet.java From ribbon with Apache License 2.0 | 6 votes |
@edu.umd.cs.findbugs.annotations.SuppressWarnings public static void main(String[] args) throws Exception { LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://www.google.com/"); final CountDownLatch latch = new CountDownLatch(1); client.submit(request) .toBlocking() .forEach(new Action1<HttpClientResponse<ByteBuf>>() { @Override public void call(HttpClientResponse<ByteBuf> t1) { System.out.println("Status code: " + t1.getStatus()); t1.getContent().subscribe(new Action1<ByteBuf>() { @Override public void call(ByteBuf content) { System.out.println("Response content: " + content.toString(Charset.defaultCharset())); latch.countDown(); } }); } }); latch.await(2, TimeUnit.SECONDS); }
Example #9
Source File: ProxyHandler.java From Prana with Apache License 2.0 | 6 votes |
private LoadBalancingHttpClient<ByteBuf, ByteBuf> getClient(String vip) { LoadBalancingHttpClient<ByteBuf, ByteBuf> client = httpClients.get(vip); if (client == null) { IClientConfig config = IClientConfig.Builder.newBuilder("prana_backend"). withDefaultValues(). withDeploymentContextBasedVipAddresses(vip). build(). set(IClientConfigKey.Keys.MaxTotalConnections, 2000). set(IClientConfigKey.Keys.MaxConnectionsPerHost, 2000). set(IClientConfigKey.Keys.OkToRetryOnAllOperations, false). set(IClientConfigKey.Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName()); client = RibbonTransport.newHttpClient(new HttpClientPipelineConfigurator<ByteBuf, ByteBuf>(), config); httpClients.putIfAbsent(vip, client); } return client; }
Example #10
Source File: SecuredTransportFactory.java From ARCHIVE-wildfly-swarm with Apache License 2.0 | 6 votes |
@Override public HttpClient<ByteBuf, ByteBuf> newHttpClient(final IClientConfig config) { final List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = new ArrayList<>(); listeners.add(createBearerHeaderAdder()); final PipelineConfiguratorComposite<HttpClientResponse<ByteBuf>, HttpClientRequest<ByteBuf>> pipelineConfigurator = new PipelineConfiguratorComposite<HttpClientResponse<ByteBuf>, HttpClientRequest<ByteBuf>>(new HttpClientPipelineConfigurator<ByteBuf, ByteBuf>(), new HttpObjectAggregationConfigurator(maxChunkSize)); final LoadBalancingHttpClient<ByteBuf, ByteBuf> client = LoadBalancingHttpClient.<ByteBuf, ByteBuf>builder() .withClientConfig(config) .withExecutorListeners(listeners) .withRetryHandler(getDefaultHttpRetryHandlerWithConfig(config)) .withPipelineConfigurator(pipelineConfigurator) .withPoolCleanerScheduler(RibbonTransport.poolCleanerScheduler) .build(); return client; }
Example #11
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test @Ignore public void testRedirect() throws Exception { HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/redirect?port=" + port); LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient( IClientConfig.Builder.newBuilder().withDefaultValues() .withFollowRedirects(true) .build()); Person person = getPersonObservable(observableClient.submit(new Server(host, port), request)).toBlocking().single(); assertEquals(EmbeddedResources.defaultPerson, person); }
Example #12
Source File: ListenerTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testFailedExecution() { IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues() .withProperty(CommonClientConfigKey.ConnectTimeout, "100") .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 badServer2 = new Server("localhost:34567"); List<Server> servers = Lists.newArrayList(badServer, badServer2); BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder() .withRule(new AvailabilityFilteringRule()) .withPing(new DummyPing()) .buildFixedServerListLoadBalancer(servers); IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig(); 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); try { client.submit(request, null, overrideConfig).toBlocking().last(); fail("Exception expected"); } catch(Exception e) { assertNotNull(e); } assertEquals(1, listener.executionStartCounter.get()); assertEquals(4, listener.startWithServerCounter.get()); assertEquals(4, listener.exceptionWithServerCounter.get()); assertEquals(1, listener.executionFailedCounter.get()); assertTrue(listener.isContextChecked()); assertTrue(listener.isCheckExecutionInfo()); assertNotNull(listener.getFinalThrowable()); listener.getFinalThrowable().printStackTrace(); assertTrue(listener.getFinalThrowable() instanceof ClientException); assertEquals(100, listener.getContext().getClientProperty(CommonClientConfigKey.ConnectTimeout).intValue()); }
Example #13
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testObservableWithRetrySameServer() throws Exception { IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000"); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person"); Server badServer = new Server("localhost:12345"); Server goodServer = new Server("localhost:" + port); List<Server> servers = Lists.newArrayList(badServer, badServer, goodServer); BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder() .withRule(new AvailabilityFilteringRule()) .withPing(new DummyPing()) .buildFixedServerListLoadBalancer(servers); LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(1, 0, true)); Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request)); ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>(); observableWithRetries.subscribe(observer); observer.await(); assertNull(observer.obj); assertTrue(observer.error instanceof ClientException); ServerStats stats = lbObservables.getServerStats(badServer); // two requests to bad server because retry same server is set to 1 assertEquals(2, stats.getTotalRequestsCount()); assertEquals(0, stats.getActiveRequestsCount()); stats = lbObservables.getServerStats(goodServer); assertEquals(0, stats.getTotalRequestsCount()); }
Example #14
Source File: ListenerTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testFailedExecutionForAbsoluteURI() { IClientConfig config = DefaultClientConfigImpl .getClientConfigWithDefaultValues() .withProperty(CommonClientConfigKey.ConnectTimeout, "100") .withProperty(CommonClientConfigKey.MaxAutoRetries, 1) .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://xyz.unknowhost.xyz/testAsync/person"); Server badServer = new Server("localhost:12345"); Server badServer2 = new Server("localhost:34567"); List<Server> servers = Lists.newArrayList(badServer, badServer2); BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder() .withRule(new AvailabilityFilteringRule()) .withPing(new DummyPing()) .buildFixedServerListLoadBalancer(servers); IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig(); 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); try { client.submit(request, null, overrideConfig).toBlocking().last(); fail("Exception expected"); } catch(Exception e) { assertNotNull(e); } assertEquals(1, listener.executionStartCounter.get()); assertEquals(2, listener.startWithServerCounter.get()); assertEquals(2, listener.exceptionWithServerCounter.get()); assertEquals(1, listener.executionFailedCounter.get()); assertTrue(listener.isContextChecked()); assertTrue(listener.isCheckExecutionInfo()); assertTrue(listener.getFinalThrowable() instanceof ClientException); }
Example #15
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 #16
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testQuery() throws Exception { Person myPerson = new Person("hello_world", 4); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/personQuery?name=" + myPerson.name + "&age=" + myPerson.age); LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(); Person person = getPersonObservable(observableClient.submit(new Server(host, port), request)).toBlocking().single(); assertEquals(myPerson, person); }
Example #17
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testStream() throws Exception { HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/personStream"); LoadBalancingHttpClient<ByteBuf, ServerSentEvent> observableClient = (LoadBalancingHttpClient<ByteBuf, ServerSentEvent>) RibbonTransport.newSSEClient(); List<Person> result = getPersonListFromResponse(observableClient.submit(new Server(host, port), request)); assertEquals(EmbeddedResources.entityStream, result); }
Example #18
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testObservableWithMultipleServersFailed() throws Exception { IClientConfig config = IClientConfig.Builder.newBuilder() .withDefaultValues() .withRetryOnAllOperations(true) .withMaxAutoRetries(1) .withMaxAutoRetriesNextServer(3) .withConnectTimeout(100) .build(); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person"); BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule()); LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config); Server badServer = new Server("localhost:12345"); Server badServer1 = new Server("localhost:12346"); Server badServer2 = new Server("localhost:12347"); List<Server> servers = Lists.newArrayList(badServer, badServer1, badServer2); lb.setServersList(servers); Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request)); ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>(); observableWithRetries.subscribe(observer); observer.await(); assertNull(observer.obj); observer.error.printStackTrace(); assertTrue(observer.error instanceof ClientException); ServerStats stats = lbObservables.getServerStats(badServer); // two requests to bad server because retry same server is set to 1 assertEquals(2, stats.getTotalRequestsCount()); assertEquals(0, stats.getActiveRequestsCount()); assertEquals(2, stats.getSuccessiveConnectionFailureCount()); }
Example #19
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testObservableWithMultipleServersWithOverrideRxConfig() throws Exception { IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000"); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person"); Server badServer = new Server("localhost:12345"); Server goodServer = new Server("localhost:" + port); List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer); BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder() .withRule(new AvailabilityFilteringRule()) .withPing(new DummyPing()) .buildFixedServerListLoadBalancer(servers); LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(1, 3, true)); HttpClientConfig rxconfig = HttpClientConfig.Builder.newDefaultConfig(); Person person = getPersonObservable(lbObservables.submit(request, rxconfig)).toBlocking().single(); assertEquals(EmbeddedResources.defaultPerson, person); ServerStats stats = lbObservables.getServerStats(badServer); // 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()); final HttpClientListener listener = lbObservables.getListener(); assertEquals(1, listener.getConnectionCount()); waitUntilTrueOrTimeout(1000, new Func0<Boolean>() { @Override public Boolean call() { return listener.getPoolReleases() == 1; } }); }
Example #20
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testObservableWithMultipleServers() throws Exception { IClientConfig config = DefaultClientConfigImpl .getClientConfigWithDefaultValues() .withProperty(CommonClientConfigKey.ConnectTimeout, "1000"); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person"); Server badServer = new Server("localhost:12345"); Server goodServer = new Server("localhost:" + port); List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer); BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder() .withRule(new AvailabilityFilteringRule()) .withPing(new DummyPing()) .buildFixedServerListLoadBalancer(servers); LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(1, 3, true)); Person person = getPersonObservable(lbObservables.submit(request)).toBlocking().single(); assertEquals(EmbeddedResources.defaultPerson, person); ServerStats stats = lbObservables.getServerStats(badServer); // 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()); person = getPersonObservable(lbObservables.submit(request)).toBlocking().single(); assertEquals(EmbeddedResources.defaultPerson, person); HttpClientListener listener = lbObservables.getListener(); assertEquals(1, listener.getPoolReuse()); }
Example #21
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testReadTimeout() throws Exception { LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient( DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ReadTimeout, "100")); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/readTimeout"); Observable<HttpClientResponse<ByteBuf>> observable = observableClient.submit(request); ObserverWithLatch<HttpClientResponse<ByteBuf>> observer = new ObserverWithLatch<HttpClientResponse<ByteBuf>>(); observable.subscribe(observer); observer.await(); assertTrue(observer.error instanceof io.netty.handler.timeout.ReadTimeoutException); }
Example #22
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testConnectTimeout() throws Exception { LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient( DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1")); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://www.google.com:81/"); Observable<HttpClientResponse<ByteBuf>> observable = observableClient.submit(new Server("www.google.com", 81), request); ObserverWithLatch<HttpClientResponse<ByteBuf>> observer = new ObserverWithLatch<HttpClientResponse<ByteBuf>>(); observable.subscribe(observer); observer.await(); assertNotNull(observer.error); assertTrue(observer.error instanceof io.netty.channel.ConnectTimeoutException); }
Example #23
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testPostWithObservable() throws Exception { Person myPerson = new Person("netty", 5); HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost(SERVICE_URI + "testAsync/person") .withHeader("Content-type", "application/json") .withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), myPerson, null)); LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient( DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(CommonClientConfigKey.ReadTimeout, 10000)); Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(new Server(host, port), request); Person person = getPersonObservable(response).toBlocking().single(); assertEquals(myPerson, person); }
Example #24
Source File: ServerListRefreshTest.java From ribbon with Apache License 2.0 | 5 votes |
/** * This test ensures that when server list is refreshed in the load balancer, the set of servers * which equals to (oldList - newList) should be removed from the map of cached RxClient. Any server * that is not part of oldList should stay in the map. * * @throws IOException */ @Test public void testServerListRefresh() throws IOException { String content = "Hello world"; MockWebServer server1 = new MockWebServer(); MockWebServer server2 = new MockWebServer(); MockWebServer server3 = new MockWebServer(); MockResponse mockResponse = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain") .setBody(content); server1.enqueue(mockResponse); server2.enqueue(mockResponse); server3.enqueue(mockResponse); server1.play(); server2.play(); server3.play(); try { BaseLoadBalancer lb = new BaseLoadBalancer(); List<Server> initialList = Lists.newArrayList(new Server("localhost", server1.getPort()), new Server("localhost", server2.getPort())); lb.setServersList(initialList); LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/"); client.submit(request).toBlocking().last(); client.submit(request).toBlocking().last(); HttpClientRequest<ByteBuf> request2 = HttpClientRequest.createGet("http://localhost:" + server3.getPort()); client.submit(request2).toBlocking().last(); Set<Server> cachedServers = client.getRxClients().keySet(); assertEquals(Sets.newHashSet(new Server("localhost", server1.getPort()), new Server("localhost", server2.getPort()), new Server("localhost", server3.getPort())), cachedServers); List<Server> newList = Lists.newArrayList(new Server("localhost", server1.getPort()), new Server("localhost", 99999)); lb.setServersList(newList); cachedServers = client.getRxClients().keySet(); assertEquals(Sets.newHashSet(new Server("localhost", server1.getPort()), new Server("localhost", server3.getPort())), cachedServers); } finally { server1.shutdown(); server2.shutdown(); server3.shutdown(); } }
Example #25
Source File: UdpClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testUdpClientWithoutTimeout() throws Exception { int port = choosePort(); UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 0).createServer(); server.start(); BaseLoadBalancer lb = new BaseLoadBalancer(); lb.setServersList(Lists.newArrayList(new Server("localhost", port))); RxClient<DatagramPacket, DatagramPacket> client = RibbonTransport.newUdpClient(lb, DefaultClientConfigImpl.getClientConfigWithDefaultValues()); try { String response = client.connect().flatMap(new Func1<ObservableConnection<DatagramPacket, DatagramPacket>, Observable<DatagramPacket>>() { @Override public Observable<DatagramPacket> call(ObservableConnection<DatagramPacket, DatagramPacket> connection) { connection.writeStringAndFlush("Is there anybody out there?"); return connection.getInput(); } }).take(1) .map(new Func1<DatagramPacket, String>() { @Override public String call(DatagramPacket datagramPacket) { return datagramPacket.content().toString(Charset.defaultCharset()); } }) .toBlocking() .first(); assertEquals(HelloUdpServer.WELCOME_MSG, response); } finally { server.shutdown(); } }
Example #26
Source File: RxMovieTransportExample.java From ribbon with Apache License 2.0 | 4 votes |
public RxMovieTransportExample(int port) { IClientConfig clientConfig = IClientConfig.Builder.newBuilder("movieServiceClient").build(); clientConfig.set(CommonClientConfigKey.ListOfServers, "localhost:" + port); client = RibbonTransport.newHttpClient(clientConfig); }
Example #27
Source File: RibbonTransportFactory.java From ribbon with Apache License 2.0 | 4 votes |
public HttpClient<ByteBuf, ByteBuf> newHttpClient(IClientConfig config) { return RibbonTransport.newHttpClient(config); }
Example #28
Source File: ListenerTest.java From ribbon with Apache License 2.0 | 4 votes |
@Test public void testAbortedExecutionOnServer() { IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "100") .withProperty(CommonClientConfigKey.MaxAutoRetries, 1) .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person"); Server badServer = new Server("localhost:12345"); Server badServer2 = new Server("localhost:34567"); List<Server> servers = Lists.newArrayList(badServer, badServer2); BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder() .withRule(new AvailabilityFilteringRule()) .withPing(new DummyPing()) .buildFixedServerListLoadBalancer(servers); IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig(); TestExecutionListener listener = new TestExecutionListener(request, overrideConfig) { @Override public void onStartWithServer(ExecutionContext context, ExecutionInfo info) { throw new AbortExecutionException("exit now"); } }; 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); final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Throwable> ref = new AtomicReference<Throwable>(); client.submit(request, null, overrideConfig).subscribe(new Action1<HttpClientResponse<ByteBuf>>() { @Override public void call(HttpClientResponse<ByteBuf> byteBufHttpClientResponse) { } }, new Action1<Throwable>() { @Override public void call(Throwable throwable) { ref.set(throwable); latch.countDown(); } }); try { latch.await(500, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { e.printStackTrace(); } assertTrue(ref.get() instanceof AbortExecutionException); }
Example #29
Source File: ListenerTest.java From ribbon with Apache License 2.0 | 4 votes |
@Test public void testAbortedExecution() { IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "100") .withProperty(CommonClientConfigKey.MaxAutoRetries, 1) .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1); HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person"); Server badServer = new Server("localhost:12345"); Server badServer2 = new Server("localhost:34567"); List<Server> servers = Lists.newArrayList(badServer, badServer2); BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder() .withRule(new AvailabilityFilteringRule()) .withPing(new DummyPing()) .buildFixedServerListLoadBalancer(servers); IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig(); TestExecutionListener listener = new TestExecutionListener(request, overrideConfig) { @Override public void onExecutionStart(ExecutionContext context) { throw new AbortExecutionException("exit now"); } }; 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); final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Throwable> ref = new AtomicReference<Throwable>(); client.submit(request, null, overrideConfig).subscribe(new Action1<HttpClientResponse<ByteBuf>>() { @Override public void call(HttpClientResponse<ByteBuf> byteBufHttpClientResponse) { } }, new Action1<Throwable>() { @Override public void call(Throwable throwable) { ref.set(throwable); latch.countDown(); } }); try { latch.await(500, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { e.printStackTrace(); } assertTrue(ref.get() instanceof AbortExecutionException); }
Example #30
Source File: RibbonTransportFactory.java From ribbon with Apache License 2.0 | 4 votes |
public RxClient<ByteBuf, ByteBuf> newTcpClient(IClientConfig config) { return RibbonTransport.newTcpClient(config); }