/**
   * Tests fake exceptions, connection should be shutdown if the scheduler was marked as going down.
   * Same test except just used to check for a spurious interrupted exception (should be logged).
   *
   * @throws SQLException
   * @throws InterruptedException
   * @throws NoSuchFieldException
   * @throws SecurityException
   * @throws IllegalAccessException
   * @throws IllegalArgumentException
   */
  @Test
  public void testExceptionOnCloseConnection()
      throws SQLException, InterruptedException, SecurityException, NoSuchFieldException,
          IllegalArgumentException, IllegalAccessException {
    ArrayBlockingQueue<ConnectionHandle> fakeFreeConnections =
        new ArrayBlockingQueue<ConnectionHandle>(1);
    fakeFreeConnections.add(mockConnection);

    config.setIdleConnectionTestPeriod(1);
    expect(mockPool.getConfig()).andReturn(config).anyTimes();
    expect(mockConnectionPartition.getFreeConnections()).andReturn(fakeFreeConnections).anyTimes();
    expect(mockConnectionPartition.getMinConnections()).andReturn(10).once();
    expect(mockConnection.isPossiblyBroken()).andReturn(false);
    expect(mockConnection.getConnectionLastUsed()).andReturn(0L);
    expect(mockPool.isConnectionHandleAlive((ConnectionHandle) anyObject()))
        .andReturn(false)
        .anyTimes();

    // connection should be closed
    mockConnection.internalClose();
    expectLastCall().andThrow(new SQLException());

    replay(mockPool, mockConnection, mockConnectionPartition, mockExecutor, mockLogger);
    this.testClass = new ConnectionTesterThread(mockConnectionPartition, mockExecutor, mockPool);
    Field loggerField = this.testClass.getClass().getDeclaredField("logger");
    loggerField.setAccessible(true);
    loggerField.set(this.testClass, mockLogger);
    this.testClass.run();
    verify(mockPool, mockConnectionPartition, mockExecutor, mockConnection, mockLogger);
  }
  /**
   * Tests fake exceptions, connection should be shutdown if the scheduler was marked as going down.
   * Mostly for code coverage.
   *
   * @throws SQLException
   * @throws InterruptedException
   */
  @Test
  public void testInterruptedException() throws SQLException, InterruptedException {
    ArrayBlockingQueue<ConnectionHandle> fakeFreeConnections =
        new ArrayBlockingQueue<ConnectionHandle>(1);
    fakeFreeConnections.add(mockConnection);

    config.setIdleConnectionTestPeriod(1);
    expect(mockPool.getConfig()).andReturn(config).anyTimes();
    expect(mockConnectionPartition.getFreeConnections()).andReturn(fakeFreeConnections).anyTimes();
    expect(mockConnectionPartition.getMinConnections()).andReturn(10).once();
    expect(mockConnection.isPossiblyBroken()).andReturn(false);
    expect(mockConnection.getConnectionLastUsed()).andReturn(0L);
    expect(mockPool.isConnectionHandleAlive((ConnectionHandle) anyObject()))
        .andReturn(true)
        .anyTimes();
    expect(mockExecutor.isShutdown()).andReturn(true);
    mockPool.releaseInAnyFreePartition(
        (ConnectionHandle) anyObject(), (ConnectionPartition) anyObject());
    expectLastCall().andThrow(new InterruptedException());
    // connection should be closed
    mockConnection.internalClose();
    mockPool.postDestroyConnection(mockConnection);
    expectLastCall().once();

    replay(mockPool, mockConnection, mockConnectionPartition, mockExecutor);
    this.testClass = new ConnectionTesterThread(mockConnectionPartition, mockExecutor, mockPool);
    this.testClass.run();
    verify(mockPool, mockConnectionPartition, mockExecutor, mockConnection);
  }
Пример #3
0
  /**
   * Tests various getter/setters.
   *
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   * @throws SecurityException
   * @throws NoSuchFieldException
   */
  @SuppressWarnings("deprecation")
  @Test
  public void testSettersGetters()
      throws IllegalArgumentException, IllegalAccessException, SecurityException,
          NoSuchFieldException {
    ConnectionPartition mockPartition = createNiceMock(ConnectionPartition.class);
    testClass.setOriginatingPartition(mockPartition);
    assertEquals(mockPartition, testClass.getOriginatingPartition());

    testClass.setConnectionLastReset(123);
    assertEquals(testClass.getConnectionLastReset(), 123);

    testClass.setConnectionLastUsed(456);
    assertEquals(testClass.getConnectionLastUsed(), 456);

    Field field = testClass.getClass().getDeclaredField("possiblyBroken");
    field.setAccessible(true);
    field.setBoolean(testClass, true);
    assertTrue(testClass.isPossiblyBroken());

    Object debugHandle = new Object();
    testClass.setDebugHandle(debugHandle);
    assertEquals(debugHandle, testClass.getDebugHandle());

    testClass.setInternalConnection(mockConnection);
    assertEquals(mockConnection, testClass.getInternalConnection());
    assertEquals(mockConnection, testClass.getRawConnection());

    field = testClass.getClass().getDeclaredField("logicallyClosed");
    field.setAccessible(true);
    field.setBoolean(testClass, true);
    assertTrue(testClass.isClosed());

    testClass.setLogStatementsEnabled(true);
    assertTrue(testClass.isLogStatementsEnabled());

    assertEquals(testClass.getPool(), mockPool);
    ArrayList<ReplayLog> testLog = new ArrayList<ReplayLog>();
    testClass.setReplayLog(testLog);
    assertEquals(testClass.getReplayLog(), testLog);
    testClass.setInReplayMode(true);
    assertTrue(testClass.isInReplayMode());
    testClass.setInReplayMode(false);

    testClass.threadUsingConnection = Thread.currentThread();
    assertEquals(Thread.currentThread(), testClass.getThreadUsingConnection());
  }
  /**
   * Tests that a connection that has been idle for more than the set time is closed off.
   *
   * @throws SQLException
   */
  @Test
  public void testIdleConnectionIsKilled() throws SQLException {
    ArrayBlockingQueue<ConnectionHandle> fakeFreeConnections =
        new ArrayBlockingQueue<ConnectionHandle>(2);
    fakeFreeConnections.add(mockConnection);
    fakeFreeConnections.add(mockConnection);

    expect(mockPool.getConfig()).andReturn(config).anyTimes();
    expect(mockConnectionPartition.getFreeConnections()).andReturn(fakeFreeConnections).anyTimes();
    expect(mockConnectionPartition.getMinConnections()).andReturn(0).once();
    expect(mockConnection.isPossiblyBroken()).andReturn(false);
    expect(mockConnection.getConnectionLastUsed()).andReturn(0L);

    // connection should be closed
    mockConnection.internalClose();
    mockPool.postDestroyConnection(mockConnection);
    expectLastCall().once();

    replay(mockPool, mockConnection, mockConnectionPartition, mockExecutor);
    this.testClass = new ConnectionTesterThread(mockConnectionPartition, mockExecutor, mockPool);
    this.testClass.run();
    verify(mockPool, mockConnectionPartition, mockExecutor, mockConnection);
  }