2

我有一个需要很长时间的测试,最后超时了。我尝试了解决方案鲍勃,但没有任何效果。该错误仅发生在测试 PUT 上。

我的测试:

@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);
}

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>

版本 CXF:2.7.3 版本 Spring 3 Java 7

4

1 回答 1

2

我的解决方案是在我的测试 context.xml 中覆盖 httpconduit

<http:conduit name="*.http-conduit">
    <http:client CacheControl="no-store" Connection="Keep-Alive" AllowChunking="false" ConnectionTimeout="10000" ReceiveTimeout="60000"/>
</http:conduit>
于 2014-05-31T00:44:35.653 回答