コード例 #1
0
  /**
   * Tests that a partition with expired connections should those connections killed off.
   *
   * @throws SQLException
   */
  @Test
  @SuppressWarnings({"unchecked", "rawtypes"})
  public void testConnectionExpired() throws SQLException {

    TransferQueue<ConnectionHandle> mockQueue = createNiceMock(TransferQueue.class);
    expect(mockConnectionPartition.getAvailableConnections()).andReturn(1);
    expect(mockConnectionPartition.getFreeConnections()).andReturn(mockQueue).anyTimes();
    ConnectionHandle mockConnectionExpired = createNiceMock(ConnectionHandle.class);
    ConnectionHandle mockConnection = createNiceMock(ConnectionHandle.class);
    expect(mockQueue.poll()).andReturn(mockConnectionExpired).once();

    expect(mockConnectionExpired.isExpired(anyLong())).andReturn(true).once();

    expect(mockExecutor.isShutdown()).andReturn(false).once();

    mockConnectionExpired.internalClose();
    expectLastCall().once();

    mockPool.postDestroyConnection(mockConnectionExpired);
    expectLastCall().once();

    expect(mockExecutor.schedule((Callable) anyObject(), anyLong(), (TimeUnit) anyObject()))
        .andReturn(null)
        .once();
    replay(
        mockQueue,
        mockExecutor,
        mockConnectionPartition,
        mockConnection,
        mockPool,
        mockConnectionExpired);
    testClass.run();
    verify(mockConnectionExpired);
  }
コード例 #2
0
  /** @throws SQLException */
  @Test
  public void testCloseConnectionWithException() throws SQLException {
    ConnectionHandle mockConnection = createNiceMock(ConnectionHandle.class);
    mockPool.postDestroyConnection(mockConnection);
    expectLastCall().once();

    mockConnection.internalClose();
    expectLastCall().andThrow(new SQLException());

    replay(mockConnection, mockPool);
    testClass.closeConnection(mockConnection);
    verify(mockConnection, mockPool);
  }
コード例 #3
0
  /** @throws SQLException */
  @Test
  public void testCloseConnectionWithExceptionCoverage() throws SQLException {
    ConnectionHandle mockConnection = createNiceMock(ConnectionHandle.class);
    mockPool.postDestroyConnection(mockConnection);
    expectLastCall().once();
    ConnectionMaxAgeThread.logger = null; // make it break.
    mockConnection.internalClose();
    expectLastCall().andThrow(new SQLException());

    replay(mockConnection, mockPool);
    try {
      testClass.closeConnection(mockConnection);
    } catch (Exception e) {
      // do nothing
    }
    verify(mockConnection, mockPool);
  }