Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration Testing DropWizard Apps

I just read over DropWizard's Testing docs, and am in love with its built-in Integrated Testing capabilities. TL;DR: it allows your JUnit tests to spin up in-memory instances of Jetty and essentially serve your API endpoints (resource methods) as they will exist in the wild. This allows you to actually hit your API endpoints with a client (against localhost) and see how they do/perform. Awesome!

I'm wondering if it's possible to use this DropWizardAppRule (or something similar to it) to start up/shutdown my DropWizard app and verify no exceptions were thrown (smoke testing); and

The smoke testing would be useful because there could be some initialization-related exception that prevents the app from starting up (bad config file, etc.), and it would be nice to know about this in advance. Similarly, smoke testing on shutdown is helpful because we might have something not closing/tearing down gracefully, and might have a hanging thread that just won't die, etc.

It would also be nice to stress test the running in-memory server and see where it pushes over (perhaps throwing an OOME?).

So, given the following code snippet:

public class IntegrationTest {
    @ClassRule
    public static final DropwizardAppRule<TestConfiguration> RULE =
        new DropwizardAppRule<TestConfiguration>(MyApp.class, resourceFilePath("my-app-config.yaml"));

    @Test
    public void shouldStartWithNoExceptions() {
        // ???
    }

    @Test
    public void stressTest10kUsers() {
        // What exceptions could I check for to see if the server pushed over after
        // 10,000 random endpoints were hit?
    }

    @Test
    public void shouldShutdownGracefully() {
        // ???
    }
}

I ask:

  1. How do I test the server started without throwing exceptions?
  2. How do I test whether the server is still responding and hasn't died (due to stress testing)?
  3. How do I shut the server down and make sure no exception were thrown or that nothing prevented a graceful shutdown?
like image 806
IAmYourFaja Avatar asked Dec 14 '14 13:12

IAmYourFaja


1 Answers

1.) I think you mean Exceptions, that kill the DW application, because there are some wanted Exceptions like WebAppExceptions. So you just have to check if your app is running. If there is a major problem, your DW app will not start and so it can not respond to reuqests.

Here some extra ideas: a.) If you want to test external dependencys, tests in jenkins or your local machine are not a good idea. For testing your app in LIVE enviroment, you can create HealthChecks and check it via curl or http client tools. You should get some json like:

{"deadlocks":{"healthy":true},"database":{"healthy":true}}

Check this from extern, for example remove this DW instance from your loadbalancer if healthchecks singnals unhealthy. Add for all important things one Healthcheck, so you can be sure if your app is healthy or not.

b.) Do some resource tests after starting your app. If you get a response your DW app is running. c.) Check your log. Search for Log Level ERROR or WARN. If there are zero entries, you can assume, that your app started without exceptions.

2.) Just do an HTTP request to your resource ;-) Response means your app is running.

3.) I use ShutdownHooks. There I check all important things, for example is DB connection closed ... Normally it is ok to shut down your app gracefully.

You can add the code snipped to your Service constructor.

public YourService(){
    ...
    // In case vm shutdown
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run()
        {
            // what should be closed if forced shudown
            // ....

            LOG.info(String.format("--- End of ShutDownHook (%s) ---", APPLICATION_NAME));
        }
    });
    ...
}

If this is not the wanted answer, please provide more informations.

like image 117
heaphach Avatar answered Sep 20 '22 11:09

heaphach