Search in sources :

Example 1 with Connection

use of org.adbcj.Connection in project adbcj by mheath.

the class ConnectSpecialCaseTest method testConnectBadCredentials.

@Parameters({ "url", "user", "password" })
@Test(timeOut = 60000)
public void testConnectBadCredentials(String url, String user, String password) throws InterruptedException {
    final boolean[] callbacks = { false };
    final CountDownLatch latch = new CountDownLatch(1);
    ConnectionManager connectionManager = ConnectionManagerProvider.createConnectionManager(url, user, "__BADPASSWORD__");
    try {
        DbFuture<Connection> connectFuture = connectionManager.connect().addListener(new DbListener<Connection>() {

            public void onCompletion(DbFuture<Connection> future) throws Exception {
                callbacks[0] = true;
                latch.countDown();
            }
        });
        try {
            connectFuture.get();
            fail("Connect should have failed because of bad credentials");
        } catch (DbException e) {
            assertTrue(connectFuture.isDone(), "Connect future should be marked done even though it failed");
            assertTrue(!connectFuture.isCancelled(), "Connect future should not be marked as cancelled");
        }
        assertTrue(latch.await(1, TimeUnit.SECONDS), "Callback was not invoked in time");
        assertTrue(callbacks[0], "Connect future callback was not invoked with connect failure");
    } finally {
        connectionManager.close(true);
    }
}
Also used : ConnectionManager(org.adbcj.ConnectionManager) Connection(org.adbcj.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) DbException(org.adbcj.DbException) DbException(org.adbcj.DbException) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test)

Example 2 with Connection

use of org.adbcj.Connection in project adbcj by mheath.

the class ConnectTest method testConnectImmediateClose.

public void testConnectImmediateClose() throws Exception {
    final boolean[] callbacks = { false, false };
    final CountDownLatch latch = new CountDownLatch(2);
    DbFuture<Connection> connectFuture = connectionManager.connect().addListener(new DbListener<Connection>() {

        public void onCompletion(DbFuture<Connection> future) throws Exception {
            // Indicate that callback has been invoked
            callbacks[0] = true;
            latch.countDown();
        }
    });
    Connection connection = connectFuture.get(5, TimeUnit.SECONDS);
    assertTrue(!connection.isClosed());
    DbFuture<Void> closeFuture = connection.close(true).addListener(new DbListener<Void>() {

        public void onCompletion(DbFuture<Void> future) throws Exception {
            // Indicate that callback has been invoked
            callbacks[1] = true;
            latch.countDown();
        }
    });
    closeFuture.get(5, TimeUnit.SECONDS);
    assertTrue(connection.isClosed());
    latch.await(1, TimeUnit.SECONDS);
    assertTrue(callbacks[0], "Callback on connection future was not invoked");
    assertTrue(callbacks[1], "Callback on finalizeClose future was not invoked");
}
Also used : Connection(org.adbcj.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) DbException(org.adbcj.DbException)

Example 3 with Connection

use of org.adbcj.Connection in project adbcj by mheath.

the class ConnectTest method testNonImmediateClose.

public void testNonImmediateClose() throws Exception {
    Connection connection = connectionManager.connect().get();
    List<DbSessionFuture<ResultSet>> futures = new ArrayList<DbSessionFuture<ResultSet>>();
    for (int i = 0; i < 5; i++) {
        futures.add(connection.executeQuery(String.format("SELECT *, %d FROM simple_values", i)));
    }
    try {
        connection.close(false).get(10, TimeUnit.SECONDS);
    } catch (TimeoutException e) {
        for (DbSessionFuture<ResultSet> future : futures) {
            if (future.isDone()) {
                // Will throw exception if failed
                future.get();
            } else {
                throw new AssertionError("future " + future + " did not complete in time");
            }
        }
        throw new AssertionError("finalizeClose future failed to complete");
    }
    assertTrue(connection.isClosed(), "Connection should be closed");
    for (DbSessionFuture<ResultSet> future : futures) {
        assertTrue(future.isDone(), "Request did not finish before connection was closed: " + future);
        assertFalse(future.isCancelled(), "Future was cancelled and should have been");
    }
}
Also used : Connection(org.adbcj.Connection) ArrayList(java.util.ArrayList) ResultSet(org.adbcj.ResultSet) DbSessionFuture(org.adbcj.DbSessionFuture) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with Connection

use of org.adbcj.Connection in project adbcj by mheath.

the class ConnectTest method testConnectNonImmediateClose.

public void testConnectNonImmediateClose() throws DbException, InterruptedException {
    final boolean[] callbacks = { false };
    final CountDownLatch latch = new CountDownLatch(1);
    Connection connection = connectionManager.connect().get();
    assertTrue(!connection.isClosed());
    connection.close(true).addListener(new DbListener<Void>() {

        public void onCompletion(DbFuture<Void> future) throws Exception {
            // Indicate that finalizeClose callback has been invoked
            callbacks[0] = true;
            latch.countDown();
        }
    }).get();
    assertTrue(connection.isClosed());
    latch.await(1, TimeUnit.SECONDS);
    assertTrue(callbacks[0], "Callback on finalizeClose future was not invoked");
}
Also used : Connection(org.adbcj.Connection) DbListener(org.adbcj.DbListener) CountDownLatch(java.util.concurrent.CountDownLatch) DbFuture(org.adbcj.DbFuture)

Example 5 with Connection

use of org.adbcj.Connection in project adbcj by mheath.

the class SelectTest method testMultipleSelectStatements.

public void testMultipleSelectStatements() throws Exception {
    Connection connection = connectionManager.connect().get();
    List<DbFuture<ResultSet>> futures = new LinkedList<DbFuture<ResultSet>>();
    for (int i = 0; i < 1000; i++) {
        futures.add(connection.executeQuery(String.format("SELECT *, %d FROM simple_values", i)));
    }
    for (DbFuture<ResultSet> future : futures) {
        try {
            future.get(5, TimeUnit.MINUTES);
        } catch (TimeoutException e) {
            throw new AssertionError("Timed out waiting on future: " + future);
        }
    }
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) DbFuture(org.adbcj.DbFuture) LinkedList(java.util.LinkedList) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Connection (org.adbcj.Connection)21 ResultSet (org.adbcj.ResultSet)11 DbException (org.adbcj.DbException)8 CountDownLatch (java.util.concurrent.CountDownLatch)5 ConnectionManager (org.adbcj.ConnectionManager)5 TimeoutException (java.util.concurrent.TimeoutException)4 DbFuture (org.adbcj.DbFuture)4 Result (org.adbcj.Result)4 DbListener (org.adbcj.DbListener)3 ArrayList (java.util.ArrayList)2 DbSessionFuture (org.adbcj.DbSessionFuture)2 Row (org.adbcj.Row)2 Value (org.adbcj.Value)2 Parameters (org.testng.annotations.Parameters)2 Test (org.testng.annotations.Test)2 SQLException (java.sql.SQLException)1 LinkedList (java.util.LinkedList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 DbFutureConcurrentProxy (org.adbcj.support.DbFutureConcurrentProxy)1 DefaultResult (org.adbcj.support.DefaultResult)1