/** * 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 testExceptionSpurious() 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(true) .anyTimes(); expect(mockExecutor.isShutdown()).andReturn(false); mockPool.releaseInAnyFreePartition( (ConnectionHandle) anyObject(), (ConnectionPartition) anyObject()); expectLastCall().andThrow(new InterruptedException()); mockLogger.error((String) anyObject(), (Exception) anyObject()); 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); }