Search code examples
javaunit-testingrestcxfhttp-conduit

SocketTimeoutException cxf httpconduit PUT


I have a test that takes a lot of time and ended up making a timeout. I tried the solution bob and nothing it does not work. The error occurs just on tests PUT.

My Test :

@Test
public void testUpdateUse(){

    setExpectedResponse("{\"id\":1,\"username\":\"test\",\"email\":\"bob@test.ca\"}");

    User userToUpdate = new User();
    userToUpdate.setEmail("bob@test.ca");
    userToUpdate.setUsername("superbob");
    userToUpdate.setId(1);

    UserOrganization userOrganization = new UserOrganization();
    userOrganization.setOrganizationId(1);
    List<UserOrganization> userOrganizations = new ArrayList<>();
    userOrganizations.add(userOrganization);

    UserOrganizationUnit userOrganizationUnit = new UserOrganizationUnit();
    userOrganizationUnit.setOrganizationUnitId(1);
    List<UserOrganizationUnit> userOrganizationUnits = new ArrayList<>();
    userOrganizationUnits.add(userOrganizationUnit);

    userToUpdate.setOrganizations(userOrganizations);
    userToUpdate.setOrganizationUnits(userOrganizationUnits);

    userAPIService.update(1, userToUpdate);

    assertLatestRequest("PUT", "/users/1");
}

@Before
public void setUpServer() throws Exception {

    Thread.sleep(500);
    expectedResponse = null;

    final Authenticator authenticator = new BasicAuthenticator("") {

        @Override
        public boolean checkCredentials(final String username, final String password) {

            return getUsername().equals(username) && getPassword().equals(password);
        }
    };

    final HttpHandler handler = new HttpHandler() {

        @Override
        public void handle(HttpExchange exchange) throws IOException {

            final Authenticator.Result authResult = authenticator.authenticate(exchange);
            if (authResult instanceof Authenticator.Success || !basicAuthRequired) {
                latestExchange = exchange;

                StringWriter writer = new StringWriter();
                try {
                    IOUtils.copy(new InputStreamReader(latestExchange.getRequestBody()), writer, 1024);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                latestRequestBody = writer.toString();

                byte[] response = expectedResponse.getBytes();

                exchange.getResponseHeaders().add("Content-type", expectedContentType);
                exchange.sendResponseHeaders(expectedStatus, response.length);
                exchange.getResponseBody().write(response);
            }

            exchange.close();
            expectedResponse = null;
        }
    };

    httpServer = HttpServer.create(new InetSocketAddress(testPort), 0);
    httpServer.createContext("/", handler);
    httpServer.start();
}

@After
public void tearDownServer() throws Exception {

    if (httpServer != null) {
        httpServer.stop(0);
    }

    context.assertIsSatisfied();

    Thread.sleep(500);
}

example code on applicationContext.xml:

<http:conduit name="{http://service.clientapi.user.com/}*.http-conduit">
    <http:client CacheControl="no-store" Connection="Keep-Alive" AllowChunking="false" ConnectionTimeout="10000" ReceiveTimeout="60000"/>
</http:conduit>

Version CXF : 2.7.3 Version Spring 3 Java 7


Solution

  • My solution was to override the httpconduit in my test context.xml

    <http:conduit name="*.http-conduit">
        <http:client CacheControl="no-store" Connection="Keep-Alive" AllowChunking="false" ConnectionTimeout="10000" ReceiveTimeout="60000"/>
    </http:conduit>