### Question:
BoundedWorkQueueStrategy implements QueueingStrategy { @Override public void enqueueRequest(final Operation operation) throws DirectoryException { if (!operation.getClientConnection().isConnectionValid()) { return; } if (maxNbConcurrentOperations == 0) { if (!tryEnqueueRequest(operation)) { operation.run(); } } else if (nbRunningOperations.getAndIncrement() > maxNbConcurrentOperations || !tryEnqueueRequest(wrap(operation))) { try { operation.run(); } finally { nbRunningOperations.decrementAndGet(); } } } BoundedWorkQueueStrategy(Integer maxNbConcurrentOperations); @Override void enqueueRequest(final Operation operation);  }### Answer:
@Test public void doNotEnqueueOperationWithInvalidConnection() throws Exception { final BoundedWorkQueueStrategy strategy = new BoundedWorkQueueStrategyForTest(1, true); final Operation operation = getMockedOperation(SearchOperation.class, false); strategy.enqueueRequest(operation); verify(operation, times(1)).getClientConnection(); verifyNoMoreInteractions(operation); }
@Test(dataProvider = "allOperationClasses") public void enqueueRequestWithConcurrency( Class<? extends Operation> operationClass, boolean enqueueRequestSucceeds) throws Exception { final BoundedWorkQueueStrategy strategy = new BoundedWorkQueueStrategyForTest(1, enqueueRequestSucceeds); final Operation operation = getMockedOperation(operationClass, true); strategy.enqueueRequest(operation); verify(operation, times(1)).getClientConnection(); if (!enqueueRequestSucceeds) { verify(operation, times(1)).run(); } verifyNoMoreInteractions(operation); }
@Test public void enqueueRequestWithLimitedConcurrency() throws Exception { final Class<? extends Operation> operationClass = SearchOperation.class; final BoundedWorkQueueStrategy strategy = new BoundedWorkQueueStrategyForTest(1, true); final Operation operation1 = getMockedOperation(operationClass, true); final Operation operation2 = getMockedOperation(operationClass, true); final Operation operation3 = getMockedOperation(operationClass, true); strategy.enqueueRequest(operation1); strategy.enqueueRequest(operation2); strategy.enqueueRequest(operation3); verify(operation1, times(1)).getClientConnection(); verifyNoMoreInteractions(operation1); verify(operation2, times(1)).getClientConnection(); verifyNoMoreInteractions(operation2); verify(operation3, times(1)).getClientConnection(); verify(operation3, times(1)).run(); verifyNoMoreInteractions(operation3); }
@Test(dataProvider = "allOperationClasses") public void enqueueRequestNoConcurrency( Class<? extends Operation> operationClass, boolean enqueueRequestSucceeds) throws Exception { final BoundedWorkQueueStrategy strategy = new BoundedWorkQueueStrategyForTest(0, enqueueRequestSucceeds); final Operation operation = getMockedOperation(operationClass, true); strategy.enqueueRequest(operation); verify(operation, times(1)).getClientConnection(); if (!enqueueRequestSucceeds) { verify(operation, times(1)).run(); } verifyNoMoreInteractions(operation); }