package net.nanopool;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.SQLException;
import net.nanopool.contention.ThrowingContentionHandler;
import org.junit.Test;

/**
 *
 * @author cvh
 */
public class ContentionTest extends NanoPoolTestBase {
    @Override
    protected Configuration buildConfig() {
        return super.buildConfig().setContentionHandler(
                new ThrowingContentionHandler());
    }

    @Test
    public void contentionHandlerMustRun() throws SQLException {
        pool = npds();
        Connection[] cons = new Connection[pool.allConnectors.length];
        for (int i = 0; i < cons.length; i++) {
            cons[i] = pool.getConnection();
        }

        // pool is now empty. Next getConnection must throw.

        try {
            pool.getConnection();
            fail("Expected getConnection to throw.");
        } catch (NanoPoolRuntimeException npre) {
            assertEquals(ThrowingContentionHandler.MESSAGE, npre.getMessage());
        }

        for (int i = 0; i < cons.length; i++) {
            cons[i].close();
        }
    }
}

@@ -16,16 +16,81 @@
 opool;%0A%0A
+import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A
 import s
@@ -115,16 +115,16 @@
 sert.*;%0A
-
 %0Aimport 
@@ -170,24 +170,74 @@
 LException;%0A
+import java.util.concurrent.atomic.AtomicBoolean;%0A
 import net.n
@@ -823,32 +823,32 @@
 n();%0A        %7D%0A%0A
-
         // pool 
@@ -893,16 +893,728 @@
 throw.%0A%0A
+        final Thread thisThread = Thread.currentThread();%0A        final AtomicBoolean finishedMarker = new AtomicBoolean(false);%0A        Thread killer = new Thread(new Runnable() %7B%0A            @SuppressWarnings(%22deprecation%22)%0A            public void run() %7B%0A                try %7B%0A                    Thread.sleep(10000);%0A                    if (!finishedMarker.get()) %7B%0A                        thisThread.stop(new SQLException(%0A                                %22Die you gravy sucking pig-dog.%22));%0A                    %7D%0A                %7D catch (Exception ex) %7B%0A                    ex.printStackTrace();%0A                %7D%0A            %7D%0A        %7D, %22killer%22);%0A        killer.setDaemon(true);%0A        killer.start();%0A%0A
         

make sure that the contention test ends