package net.kencochrane.raven.connection;

import mockit.Delegate;
import mockit.Expectations;
import mockit.Injectable;
import mockit.Verifications;
import net.kencochrane.raven.event.Event;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

public class AsyncConnectionTest {
    private AsyncConnection asyncConnection;
    @Injectable
    private Connection mockConnection;
    @Injectable
    private ExecutorService mockExecutorService;

    @BeforeMethod
    public void setUp() throws Exception {
        asyncConnection = new AsyncConnection(mockConnection);
        asyncConnection.setExecutorService(mockExecutorService);
    }

    @Test
    public void testCloseOperation() throws Exception {
        asyncConnection.close();

        new Verifications() {{
            mockConnection.close();
            mockExecutorService.awaitTermination(anyLong, (TimeUnit) any);
        }};
    }


    @Test
    public void testSendEventQueued(@Injectable final Event mockEvent) throws Exception {
        asyncConnection.send(mockEvent);

        new Verifications() {{
            mockExecutorService.execute((Runnable) any);
        }};
    }

    @Test
    public void testQueuedEventExecuted(@Injectable final Event mockEvent) throws Exception {
        new Expectations() {{
            mockExecutorService.execute((Runnable) any);
            result = new Delegate() {
                public void execute(Runnable runnable) {
                    runnable.run();
                }
            };
        }};

        asyncConnection.send(mockEvent);

        new Verifications() {{
            mockConnection.send(mockEvent);
        }};
    }
}

@@ -54,99 +54,9 @@
 kit.
-Delegate;%0Aimport mockit.Expectations;%0Aimport mockit.Injectable;%0Aimport mockit.Verifications
+*
 ;%0Aim
@@ -459,16 +459,61 @@
 Service;
+%0A    @Mocked%0A    private Runtime mockRuntime;
 %0A%0A    @B
@@ -559,32 +559,151 @@
 ows Exception %7B%0A
+        new NonStrictExpectations() %7B%7B%0A            Runtime.getRuntime();%0A            result = mockRuntime;%0A        %7D%7D;%0A
         asyncCon
@@ -813,32 +813,525 @@
 ervice);%0A    %7D%0A%0A
+    @Test%0A    public void verifyShutdownHookClosesConnection() throws Exception %7B%0A        new NonStrictExpectations() %7B%7B%0A            mockRuntime.addShutdownHook((Thread) any);%0A            result = new Delegate%3CVoid%3E() %7B%0A                public void addShutdownHook(Thread thread) %7B%0A                    thread.run();%0A                %7D%0A            %7D;%0A        %7D%7D;%0A%0A        new AsyncConnection(mockConnection);%0A%0A        new Verifications() %7B%7B%0A            mockConnection.close();%0A        %7D%7D;%0A    %7D%0A%0A
     @Test%0A    pu
@@ -1572,16 +1572,15 @@
 %7D%7D;%0A
+
     %7D%0A%0A
-%0A
     

Add test to ensure that the shutdown hook closes the connection