Search in sources :

Example 1 with ClientConnManagerDefault

use of code.satyagraha.gfm.support.impl.conn.ClientConnManagerDefault in project gfm_viewer by satyagraha.

the class TestWebServiceClientDefault method shouldHandleSimpleRequest.

@Test
public void shouldHandleSimpleRequest() throws Exception {
    ServletHolder sh = new ServletHolder(ServletContainer.class);
    sh.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS, StubResourceConfig.class.getName());
    sh.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
    Server server = new Server(0);
    ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
    context.addServlet(sh, "/*");
    server.start();
    Connector[] connectors = server.getConnectors();
    int port = ((NetworkConnector) connectors[0]).getLocalPort();
    LOGGER.info("server port: " + port);
    Config config = mock(Config.class);
    String apiUrl = "http://localhost:" + port;
    given(config.getApiUrl()).willReturn(apiUrl);
    WebProxyConfig webProxyConfig = mock(WebProxyConfig.class);
    SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
    ClientConnectionManager connectionManager = new ClientConnManagerDefault(sslSocketFactory);
    WebServiceClient webServiceClient = new WebServiceClientDefault(config, webProxyConfig, connectionManager);
    Random random = new Random();
    // construct the tasks to run
    int threadCount = 50;
    List<WebServiceClientCallable> tasks = new ArrayList<WebServiceClientCallable>();
    for (int threadIndex = 0; threadIndex < threadCount; threadIndex++) {
        String mdText = RandomStringUtils.randomAlphanumeric(random.nextInt(100));
        WebServiceClientCallable task = new WebServiceClientCallable(webServiceClient, mdText);
        tasks.add(task);
    }
    // run the tasks
    ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
    List<Future<String>> futures = executorService.invokeAll(tasks, 10, TimeUnit.SECONDS);
    // verify results
    assertThat(futures, hasSize(threadCount));
    for (int threadIndex = 0; threadIndex < threadCount; threadIndex++) {
        String result = futures.get(threadIndex).get();
        WebServiceClientCallable task = tasks.get(threadIndex);
        assertThat(result, is(performSampleTransformation(task.mdText)));
    }
    server.stop();
}
Also used : NetworkConnector(org.eclipse.jetty.server.NetworkConnector) Connector(org.eclipse.jetty.server.Connector) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) WebProxyConfig(code.satyagraha.gfm.support.api.WebProxyConfig) DefaultResourceConfig(com.sun.jersey.api.core.DefaultResourceConfig) Config(code.satyagraha.gfm.support.api.Config) ArrayList(java.util.ArrayList) WebServiceClient(code.satyagraha.gfm.support.api.WebServiceClient) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) ClientConnManagerDefault(code.satyagraha.gfm.support.impl.conn.ClientConnManagerDefault) WebProxyConfig(code.satyagraha.gfm.support.api.WebProxyConfig) Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) Future(java.util.concurrent.Future) WebServiceClientDefault(code.satyagraha.gfm.support.impl.WebServiceClientDefault) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) Test(org.junit.Test)

Aggregations

Config (code.satyagraha.gfm.support.api.Config)1 WebProxyConfig (code.satyagraha.gfm.support.api.WebProxyConfig)1 WebServiceClient (code.satyagraha.gfm.support.api.WebServiceClient)1 WebServiceClientDefault (code.satyagraha.gfm.support.impl.WebServiceClientDefault)1 ClientConnManagerDefault (code.satyagraha.gfm.support.impl.conn.ClientConnManagerDefault)1 DefaultResourceConfig (com.sun.jersey.api.core.DefaultResourceConfig)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)1 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)1 Connector (org.eclipse.jetty.server.Connector)1 NetworkConnector (org.eclipse.jetty.server.NetworkConnector)1 Server (org.eclipse.jetty.server.Server)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 Test (org.junit.Test)1