use of org.apache.knox.gateway.servlet.SynchronousServletOutputStreamAdapter in project knox by apache.
the class WebHdfsHaDispatchTest method testConnectivityFailover.
@Test
public void testConnectivityFailover() throws Exception {
String serviceName = "WEBHDFS";
HaDescriptor descriptor = HaDescriptorFactory.createDescriptor();
descriptor.addServiceConfig(HaDescriptorFactory.createServiceConfig(serviceName, "true", "1", "1000", "2", "1000", null, null));
HaProvider provider = new DefaultHaProvider(descriptor);
URI uri1 = new URI("http://unreachable-host");
URI uri2 = new URI("http://reachable-host");
ArrayList<String> urlList = new ArrayList<String>();
urlList.add(uri1.toString());
urlList.add(uri2.toString());
provider.addHaService(serviceName, urlList);
FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
EasyMock.expect(servletContext.getAttribute(HaServletContextListener.PROVIDER_ATTRIBUTE_NAME)).andReturn(provider).anyTimes();
BasicHttpParams params = new BasicHttpParams();
HttpUriRequest outboundRequest = EasyMock.createNiceMock(HttpRequestBase.class);
EasyMock.expect(outboundRequest.getMethod()).andReturn("GET").anyTimes();
EasyMock.expect(outboundRequest.getURI()).andReturn(uri1).anyTimes();
EasyMock.expect(outboundRequest.getParams()).andReturn(params).anyTimes();
HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(inboundRequest.getRequestURL()).andReturn(new StringBuffer(uri2.toString())).once();
EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(0)).once();
EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(1)).once();
HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.expect(outboundResponse.getOutputStream()).andAnswer(new IAnswer<SynchronousServletOutputStreamAdapter>() {
@Override
public SynchronousServletOutputStreamAdapter answer() throws Throwable {
return new SynchronousServletOutputStreamAdapter() {
@Override
public void write(int b) throws IOException {
throw new IOException("unreachable-host");
}
};
}
}).once();
EasyMock.replay(filterConfig, servletContext, outboundRequest, inboundRequest, outboundResponse);
Assert.assertEquals(uri1.toString(), provider.getActiveURL(serviceName));
WebHdfsHaDispatch dispatch = new WebHdfsHaDispatch();
HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient client = builder.build();
dispatch.setHttpClient(client);
dispatch.setHaProvider(provider);
dispatch.init();
long startTime = System.currentTimeMillis();
try {
dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse);
} catch (IOException e) {
// this is expected after the failover limit is reached
}
long elapsedTime = System.currentTimeMillis() - startTime;
Assert.assertEquals(uri2.toString(), provider.getActiveURL(serviceName));
// test to make sure the sleep took place
Assert.assertTrue(elapsedTime > 1000);
}
use of org.apache.knox.gateway.servlet.SynchronousServletOutputStreamAdapter in project knox by apache.
the class DefaultDispatchTest method testJiraKnox58.
// Make sure Hadoop cluster topology isn't exposed to client when there is a connectivity issue.
@Test
public void testJiraKnox58() throws URISyntaxException, IOException {
URI uri = new URI("http://unreachable-host");
BasicHttpParams params = new BasicHttpParams();
HttpUriRequest outboundRequest = EasyMock.createNiceMock(HttpUriRequest.class);
EasyMock.expect(outboundRequest.getMethod()).andReturn("GET").anyTimes();
EasyMock.expect(outboundRequest.getURI()).andReturn(uri).anyTimes();
RequestLine requestLine = EasyMock.createNiceMock(RequestLine.class);
EasyMock.expect(requestLine.getMethod()).andReturn("GET").anyTimes();
EasyMock.expect(requestLine.getProtocolVersion()).andReturn(HttpVersion.HTTP_1_1).anyTimes();
EasyMock.expect(outboundRequest.getRequestLine()).andReturn(requestLine).anyTimes();
EasyMock.expect(outboundRequest.getParams()).andReturn(params).anyTimes();
HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.expect(outboundResponse.getOutputStream()).andAnswer(new IAnswer<SynchronousServletOutputStreamAdapter>() {
@Override
public SynchronousServletOutputStreamAdapter answer() throws Throwable {
return new SynchronousServletOutputStreamAdapter() {
@Override
public void write(int b) throws IOException {
throw new IOException("unreachable-host");
}
};
}
});
EasyMock.replay(outboundRequest, inboundRequest, outboundResponse, requestLine);
DefaultDispatch dispatch = new DefaultDispatch();
HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient client = builder.build();
dispatch.setHttpClient(client);
try {
dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse);
fail("Should have thrown IOException");
} catch (IOException e) {
assertThat(e.getMessage(), not(containsString("unreachable-host")));
assertThat(e, not(instanceOf(UnknownHostException.class)));
assertThat("Message needs meaningful content.", e.getMessage().trim().length(), greaterThan(12));
}
}
use of org.apache.knox.gateway.servlet.SynchronousServletOutputStreamAdapter in project knox by apache.
the class DefaultHaDispatchTest method testConnectivityFailover.
@Test
public void testConnectivityFailover() throws Exception {
String serviceName = "OOZIE";
HaDescriptor descriptor = HaDescriptorFactory.createDescriptor();
descriptor.addServiceConfig(HaDescriptorFactory.createServiceConfig(serviceName, "true", "1", "1000", "2", "1000", null, null));
HaProvider provider = new DefaultHaProvider(descriptor);
URI uri1 = new URI("http://unreachable-host");
URI uri2 = new URI("http://reachable-host");
ArrayList<String> urlList = new ArrayList<String>();
urlList.add(uri1.toString());
urlList.add(uri2.toString());
provider.addHaService(serviceName, urlList);
FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
EasyMock.expect(servletContext.getAttribute(HaServletContextListener.PROVIDER_ATTRIBUTE_NAME)).andReturn(provider).anyTimes();
BasicHttpParams params = new BasicHttpParams();
HttpUriRequest outboundRequest = EasyMock.createNiceMock(HttpRequestBase.class);
EasyMock.expect(outboundRequest.getMethod()).andReturn("GET").anyTimes();
EasyMock.expect(outboundRequest.getURI()).andReturn(uri1).anyTimes();
EasyMock.expect(outboundRequest.getParams()).andReturn(params).anyTimes();
HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(inboundRequest.getRequestURL()).andReturn(new StringBuffer(uri2.toString())).once();
EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(0)).once();
EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(1)).once();
HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.expect(outboundResponse.getOutputStream()).andAnswer(new IAnswer<SynchronousServletOutputStreamAdapter>() {
@Override
public SynchronousServletOutputStreamAdapter answer() throws Throwable {
return new SynchronousServletOutputStreamAdapter() {
@Override
public void write(int b) throws IOException {
throw new IOException("unreachable-host");
}
};
}
}).once();
EasyMock.replay(filterConfig, servletContext, outboundRequest, inboundRequest, outboundResponse);
Assert.assertEquals(uri1.toString(), provider.getActiveURL(serviceName));
DefaultHaDispatch dispatch = new DefaultHaDispatch();
HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient client = builder.build();
dispatch.setHttpClient(client);
dispatch.setHaProvider(provider);
dispatch.setServiceRole(serviceName);
dispatch.init();
long startTime = System.currentTimeMillis();
try {
dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse);
} catch (IOException e) {
// this is expected after the failover limit is reached
}
long elapsedTime = System.currentTimeMillis() - startTime;
Assert.assertEquals(uri2.toString(), provider.getActiveURL(serviceName));
// test to make sure the sleep took place
Assert.assertTrue(elapsedTime > 1000);
}
Aggregations