Search in sources :

Example 1 with BlackholeServer

use of org.firebirdsql.common.BlackholeServer in project jaybird by FirebirdSQL.

the class WireDatabaseConnectionTest method testSocketTimeout_noResponse.

/**
 * Tests the connect timeout if the server does not respond.
 */
@Test
public void testSocketTimeout_noResponse() throws Exception {
    BlackholeServer server = new BlackholeServer();
    Thread thread = new Thread(server);
    thread.start();
    long startTime = System.currentTimeMillis();
    try {
        connectionInfo.setPortNumber(server.getPort());
        connectionInfo.setDatabaseName("somedb");
        connectionInfo.setSoTimeout(2000);
        WireDatabaseConnection gdsConnection = new WireDatabaseConnection(connectionInfo);
        gdsConnection.socketConnect();
        gdsConnection.identify();
        fail("Expected connection to fail");
    } catch (SQLTimeoutException e) {
        long endTime = System.currentTimeMillis();
        long difference = endTime - startTime;
        assertEquals("Expected error code for \"Unable to complete network request\"", 335544721, e.getErrorCode());
        assertEquals("Unexpected timeout duration (in ms)", 2000, difference, TIMEOUT_DELTA_MS);
    } catch (SQLException e) {
        e.printStackTrace();
        fail("Expected SQLTimeoutException to be thrown");
    } finally {
        server.stop();
        thread.join();
    }
}
Also used : SQLException(java.sql.SQLException) SQLTimeoutException(java.sql.SQLTimeoutException) BlackholeServer(org.firebirdsql.common.BlackholeServer) Test(org.junit.Test)

Example 2 with BlackholeServer

use of org.firebirdsql.common.BlackholeServer in project jaybird by FirebirdSQL.

the class WireDatabaseConnectionTest method testConnectTimeout_noResponse.

/**
 * Tests the connect timeout if the server does not respond.
 */
@Test
public void testConnectTimeout_noResponse() throws Exception {
    BlackholeServer server = new BlackholeServer();
    Thread thread = new Thread(server);
    thread.start();
    long startTime = System.currentTimeMillis();
    try {
        connectionInfo.setPortNumber(server.getPort());
        connectionInfo.setDatabaseName("somedb");
        connectionInfo.setConnectTimeout(2);
        WireDatabaseConnection gdsConnection = new WireDatabaseConnection(connectionInfo);
        gdsConnection.socketConnect();
        gdsConnection.identify();
        fail("Expected connection to fail");
    } catch (SQLTimeoutException e) {
        long endTime = System.currentTimeMillis();
        long difference = endTime - startTime;
        assertEquals("Expected error code for \"Unable to complete network request\"", 335544721, e.getErrorCode());
        assertEquals("Unexpected timeout duration (in ms)", 2000, difference, TIMEOUT_DELTA_MS);
    } catch (SQLException e) {
        e.printStackTrace();
        fail("Expected SQLTimeoutException to be thrown");
    } finally {
        server.stop();
        thread.join();
    }
}
Also used : SQLException(java.sql.SQLException) SQLTimeoutException(java.sql.SQLTimeoutException) BlackholeServer(org.firebirdsql.common.BlackholeServer) Test(org.junit.Test)

Aggregations

SQLException (java.sql.SQLException)2 SQLTimeoutException (java.sql.SQLTimeoutException)2 BlackholeServer (org.firebirdsql.common.BlackholeServer)2 Test (org.junit.Test)2