本文整理汇总了Java中com.google.mockwebserver.MockWebServer.shutdown方法的典型用法代码示例。如果您正苦于以下问题:Java MockWebServer.shutdown方法的具体用法?Java MockWebServer.shutdown怎么用?Java MockWebServer.shutdown使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.mockwebserver.MockWebServer
的用法示例。
在下文中一共展示了MockWebServer.shutdown方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getKeepAlive
import com.google.mockwebserver.MockWebServer; //导入方法依赖的package包/类
@Test public void getKeepAlive() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setBody("ABC"));
server.play();
// The request should work once and then fail
URLConnection connection1 = client.open(server.getUrl(""));
connection1.setReadTimeout(100);
InputStream input = connection1.getInputStream();
assertEquals("ABC", readAscii(input, Integer.MAX_VALUE));
input.close();
server.shutdown();
try {
HttpURLConnection connection2 = client.open(server.getUrl(""));
connection2.setReadTimeout(100);
connection2.getInputStream();
fail();
} catch (ConnectException expected) {
}
}
示例2: testServerListRefresh
import com.google.mockwebserver.MockWebServer; //导入方法依赖的package包/类
/**
* 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();
}
}
示例3: assertCached
import com.google.mockwebserver.MockWebServer; //导入方法依赖的package包/类
private void assertCached(boolean shouldPut, int responseCode) throws Exception {
server = new MockWebServer();
MockResponse response =
new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
.setResponseCode(responseCode)
.setBody("ABCDE")
.addHeader("WWW-Authenticate: challenge");
if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH) {
response.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
response.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
}
server.enqueue(response);
server.play();
URL url = server.getUrl("/");
HttpURLConnection conn = openConnection(url);
assertEquals(responseCode, conn.getResponseCode());
// exhaust the content stream
readAscii(conn);
CacheResponse cached =
cache.get(url.toURI(), "GET", Collections.<String, List<String>>emptyMap());
if (shouldPut) {
assertNotNull(Integer.toString(responseCode), cached);
cached.getBody().close();
} else {
assertNull(Integer.toString(responseCode), cached);
}
server.shutdown(); // tearDown() isn't sufficient; this test starts multiple servers
}
示例4: testLoadBalancingObservablesWithReadTimeout
import com.google.mockwebserver.MockWebServer; //导入方法依赖的package包/类
@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());
}
示例5: testLoadBalancingWithTwoServers
import com.google.mockwebserver.MockWebServer; //导入方法依赖的package包/类
@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());
}
示例6: testLoadBalancingPostWithReadTimeout
import com.google.mockwebserver.MockWebServer; //导入方法依赖的package包/类
@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());
}
示例7: testLoadBalancingPostWithNoRetrySameServer
import com.google.mockwebserver.MockWebServer; //导入方法依赖的package包/类
@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());
}