Skip to content

Test hangs indefinitely #9246

Closed
Closed

Description

@karussell

See the discussion here.

This test hangs after completing the first ~6 requests. The same happens on the release/3.0.x branch, but the release/2.0.x branch does not show this problem:

package com.graphhopper.application.resources;

import io.dropwizard.core.Application;
import io.dropwizard.core.Configuration;
import io.dropwizard.core.setup.Environment;
import io.dropwizard.testing.junit5.DropwizardAppExtension;
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;

@ExtendWith(DropwizardExtensionsSupport.class)
public class Dropwizard3Test {

    @Path("/")
    public static class MyResource {
        @POST
        public Response doPost() {
            return Response.ok().build();
        }
    }

    public static class MyApp extends Application<Configuration> {

        @Override
        public void run(Configuration configuration, Environment environment) throws Exception {
            environment.jersey().register(new MyResource());
        }
    }

    private static final DropwizardAppExtension<Configuration> app = new DropwizardAppExtension<>(MyApp.class, new Configuration());

    @Test
    public void myTest() {
        for (int i = 0; i < 10; i++) {
            post();
            System.out.println("post " + i + " complete");
        }
    }

    private static void post() {
        Response response = app.client().target("http://localhost:8080/not_found").request().post(Entity.json("{}"));
        System.out.println("success: " + response.getStatus());
    }
}

From my investigations it seems this is a client issue, which is "fixed" with:

app.client().property(Apache5ClientProperties.REUSE_STRATEGY, (ConnectionReuseStrategy) (request, response, context) -> false);

But maybe the proper fix should be different?

@otbutz pointed out in the discussion that maybe response.close(); could be used as a fix? Or maybe the issue is related to #8884?

Activity

  1. joschi commented on Sep 1, 2024

    @joschi
    Member

    @karussell Could you please try with the latest release (Dropwizard 4.0.8) which contains Jersey 3.0.15?

  2. karussell commented on Sep 3, 2024

    @karussell
    Author

    Unfortunately it still hangs.

    Here is what I tried:

    I checkout a fresh dropwizard repository at 7cbfd4e (2024-08-31) and copied the above class into dropwizard-testing/src/test/java/io/dropwizard/testing/Dropwizard3Test.java ... a few imports are different - see the following file:

    Details
    package io.dropwizard.testing;
    
    
    import io.dropwizard.core.Application;
    import io.dropwizard.core.Configuration;
    import io.dropwizard.core.setup.Environment;
    import io.dropwizard.testing.junit5.DropwizardAppExtension;
    import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
    import jakarta.ws.rs.POST;
    import jakarta.ws.rs.Path;
    import jakarta.ws.rs.client.Entity;
    import jakarta.ws.rs.core.Response;
    import org.apache.hc.core5.http.ConnectionReuseStrategy;
    import org.glassfish.jersey.apache5.connector.Apache5ClientProperties;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    
    @ExtendWith(DropwizardExtensionsSupport.class)
    public class Dropwizard3Test {
    
        @Path("/")
        public static class MyResource {
            @POST
            public Response doPost() {
                return Response.ok().build();
            }
        }
    
        public static class MyApp extends Application<Configuration> {
    
            @Override
            public void run(Configuration configuration, Environment environment) throws Exception {
                environment.jersey().register(new MyResource());
            }
        }
    
        private static final DropwizardAppExtension<Configuration> app = new DropwizardAppExtension<>(MyApp.class, new Configuration());
    
        @Test
        public void myTest() {
            for (int i = 0; i < 10; i++) {
                post();
                System.out.println("post " + i + " complete");
            }
        }
    
        private static void post() {
            // app.client().property(Apache5ClientProperties.REUSE_STRATEGY, (ConnectionReuseStrategy) (request, response, context) -> false);
            Response response = app.client().target("http://localhost:8080/not_found").request().post(Entity.json("{}"));
            System.out.println("success: " + response.getStatus());
        }
    }

    Now either run mvn clean install -pl dropwizard-testing or open the project in IntelliJ and run the test. In both cases it hangs at request number 4 and when I include the line with the REUSE_STRATEGY config it runs through successfully.

  3. zUniQueX commented on Sep 14, 2024

    @zUniQueX
    Member

    Hi @karussell. Thanks for the example to reproduce the issue. However, your code doesn't explicitly close the Response. The JavaDoc of Response#close() states:

    Close the underlying message entity input stream (if available and open) as well as releases any other resources associated with the response.

    That means, the resources (e.g. threads) of the Apache client will only be released when the response is finally closed.

    When modifying your post() method to use try-with-resources the test runs without problems.

    private static void post() {
        try (Response response = app.client().target("http://localhost:8080/not_found").request().post(Entity.json("{}"))) {
            System.out.println("success: " + response.getStatus());
        }
    }

    I don't see this as a regression as the Jersey docs (Section 5.7) state one has to free the resources explicitly:

    If you don't read the entity, then you need to close the response manually by response.close()

    It seems that this wasn't an issue in previous versions because the Grizzly client didn't keep the resources busy as much as the Apache client does. However, from the implementation perspective everything works as intended.

    Nevertheless, we could set the closing strategy as a convenience property as we already did with the compression.

  4. karussell commented on Sep 17, 2024

    @karussell
    Author

    Thanks a lot for taking the time for this to investigate it. This makes a lot of sense - so closing here. We'll try this out soonish :)

  5. easbar commented on Sep 17, 2024

    @easbar

    @zUniQueX I just looked at the migration from Dropwizard from version 3 to 4. The migration guide points to the migration guide for the Apache Http Client 5.x library where I found the HttpClient#execute method that returns a Response was deprecated in favor of a method consuming a response callback which closes the response automatically. Does JerseyClient offer something similar? Btw how come Dropwizard testing uses JerseyClient even though Dropwizard already includes Apache client?

  6. zUniQueX commented on Sep 17, 2024

    @zUniQueX
    Member

    @easbar The Jersey client is just a high level abstraction for http clients. Then a connector is utilized to defer the actual http part to another library like Apache http client.

    The Apache http client 5.x has done a lot of effort towards streaming processing. For an easier migration, currently the synchronous compat API is used in the Jersey client connector.

  7. added a commit that references this issue on Sep 30, 2024
    acfe0cb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions