use of com.linkedin.r2.transport.http.server.HttpServerFactory in project rest.li by linkedin.
the class MockHttpServerFactory method create.
/**
* Creates a {@link HttpServer} that contains a {@link RestLiServer} to be used for testing a set of Rest.li
* resources.
*
* The {@link HttpServer} uses an empty {@link FilterChain} and uses "/" as the context path.
*
* If the server is run in async mode (by calling this function with the last parameter {@code true}), the
* timeout used is {@link #ASYNC_TIMEOUT}.
*
* Both the async and sync servers will use {@link #NUM_THREADS} threads.
*
* @param port the port the server will run on on localhost
* @param config the {@link RestLiConfig} to be used by the {@link RestLiServer}
* @param beans beans you want to inject into your Rest.li resource.
* @param enableAsync true if the server should be async , false otherwise
* @return
*/
private static HttpServer create(int port, RestLiConfig config, Map<String, ?> beans, boolean enableAsync) {
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(NUM_THREADS);
final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
EngineBuilder engineBuilder = new EngineBuilder().setTaskExecutor(scheduler).setTimerScheduler(scheduler);
com.linkedin.parseq.AsyncCallableTask.register(engineBuilder, executor);
final Engine engine = engineBuilder.build();
ResourceFactory resourceFactory = createResourceFactory(beans);
TransportDispatcher dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, resourceFactory, engine));
final FilterChain fc = FilterChains.empty().addLastRest(new SimpleLoggingFilter());
final HttpServer server = new HttpServerFactory(fc).createServer(port, HttpServerFactory.DEFAULT_CONTEXT_PATH, NUM_THREADS, dispatcher, enableAsync ? HttpJettyServer.ServletType.ASYNC_EVENT : HttpJettyServer.ServletType.RAP, enableAsync ? ASYNC_TIMEOUT : -1);
return new HttpServer() {
@Override
public void start() throws IOException {
server.start();
}
@Override
public void stop() throws IOException {
server.stop();
engine.shutdown();
executor.shutdown();
scheduler.shutdown();
}
@Override
public void waitForStop() throws InterruptedException {
server.waitForStop();
}
};
}
use of com.linkedin.r2.transport.http.server.HttpServerFactory in project rest.li by linkedin.
the class TestChannelPoolBehavior method setup.
@BeforeClass
public void setup() throws IOException {
_scheduler = Executors.newSingleThreadScheduledExecutor();
_clientFactory = new HttpClientFactory();
_client1 = new TransportClientAdapter(_clientFactory.getClient(getClientProperties()), true);
_client2 = new TransportClientAdapter(_clientFactory.getClient(getClientProperties()), true);
_server = new HttpServerFactory().createServer(PORT, getTransportDispatcher(), true);
_server.start();
}
use of com.linkedin.r2.transport.http.server.HttpServerFactory in project rest.li by linkedin.
the class TestClientShutdown method testShutdown.
@Test(dataProvider = "configs")
public void testShutdown(boolean clientROS, boolean serverROS, String protocolVersion) throws Exception {
_clientFactory = new HttpClientFactory();
Map<String, String> clientProperties = new HashMap<String, String>();
// very long shutdown timeout
clientProperties.put(HttpClientFactory.HTTP_SHUTDOWN_TIMEOUT, "60000");
clientProperties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
_client = new TransportClientAdapter(_clientFactory.getClient(clientProperties), clientROS);
TransportDispatcher dispatcher = new TransportDispatcherBuilder().addRestHandler(ECHO_URI, new EchoHandler()).build();
_server = new HttpServerFactory(HttpJettyServer.ServletType.RAP).createH2cServer(PORT, dispatcher, serverROS);
try {
_server.start();
RestRequestBuilder builder = new RestRequestBuilder(URI.create("http://localhost:" + PORT + ECHO_URI));
byte[] content = new byte[100];
builder.setEntity(content);
Future<RestResponse> future = _client.restRequest(builder.build());
RestResponse response = future.get(30, TimeUnit.SECONDS);
Assert.assertEquals(response.getEntity().copyBytes(), content);
final FutureCallback<None> clientShutdownCallback = new FutureCallback<None>();
_client.shutdown(clientShutdownCallback);
// we should catch those clients that do not shutdown properly in 5 seconds
clientShutdownCallback.get(5000, TimeUnit.MILLISECONDS);
final FutureCallback<None> factoryShutdownCallback = new FutureCallback<None>();
_clientFactory.shutdown(factoryShutdownCallback);
factoryShutdownCallback.get();
} finally {
if (_server != null) {
_server.stop();
_server.waitForStop();
}
}
}
use of com.linkedin.r2.transport.http.server.HttpServerFactory in project rest.li by linkedin.
the class TestJetty404 method setup.
@BeforeClass
public void setup() throws IOException {
_clientFactory = new HttpClientFactory();
_client = new TransportClientAdapter(_clientFactory.getClient(Collections.<String, String>emptyMap()), true);
_server = new HttpServerFactory().createH2cServer(PORT, "/correct-path", 50, new TransportDispatcher() {
@Override
public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
callback.onResponse(TransportResponseImpl.success(new RestResponseBuilder().build()));
}
@Override
public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
req.getEntityStream().setReader(new DrainReader());
callback.onResponse(TransportResponseImpl.success(new StreamResponseBuilder().build(EntityStreams.emptyStream())));
}
}, true);
_server.start();
}
use of com.linkedin.r2.transport.http.server.HttpServerFactory in project rest.li by linkedin.
the class TestDisruptor method setup.
@BeforeClass
public void setup() throws IOException {
final TransportDispatcher dispatcher = new TransportDispatcherBuilder(REST_OVER_STREAM).addRestHandler(URI.create(PATH), new AbstractHttpServerTest.HeaderEchoHandler()).build();
final HttpServerFactory factory = new HttpServerFactory();
_server = factory.createH2cServer(PORT, dispatcher, REST_OVER_STREAM);
_server.start();
}
Aggregations