| | |
| | | import org.forgerock.opendj.ldap.DN; |
| | | import org.forgerock.opendj.ldap.LdapException; |
| | | import org.forgerock.opendj.ldap.FailoverLoadBalancingAlgorithm; |
| | | import org.forgerock.opendj.ldap.FutureResult; |
| | | import org.forgerock.opendj.ldap.LdapPromise; |
| | | import org.forgerock.opendj.ldap.IntermediateResponseHandler; |
| | | import org.forgerock.opendj.ldap.LDAPClientContext; |
| | | import org.forgerock.opendj.ldap.LDAPConnectionFactory; |
| | |
| | | import static org.fest.assertions.Assertions.*; |
| | | import static org.forgerock.opendj.ldap.Connections.*; |
| | | import static org.forgerock.opendj.ldap.LdapException.*; |
| | | import static org.forgerock.opendj.ldap.FutureResultWrapper.*; |
| | | import static org.forgerock.opendj.ldap.TestCaseUtils.*; |
| | | import static org.forgerock.opendj.ldap.spi.LdapPromises.*; |
| | | import static org.mockito.Matchers.*; |
| | | import static org.mockito.Mockito.*; |
| | | import static org.testng.Assert.*; |
| | |
| | | } |
| | | |
| | | /** |
| | | * Tests the async connection in the blocking mode. This is not fully async |
| | | * as it blocks on the future. |
| | | * Tests the async connection in the blocking mode. This is not fully async as it blocks on the promise. |
| | | * |
| | | * @throws Exception |
| | | */ |
| | | @Test(dataProvider = "connectionFactories", timeOut = TEST_TIMEOUT_MS) |
| | | public void testBlockingFutureNoHandler(ConnectionFactory factory) throws Exception { |
| | | public void testBlockingPromiseNoHandler(ConnectionFactory factory) throws Exception { |
| | | final Promise<? extends Connection, LdapException> promise = factory.getConnectionAsync(); |
| | | final Connection con = promise.get(); |
| | | // quickly check if it is a valid connection. |
| | |
| | | * @throws Exception |
| | | */ |
| | | @Test(dataProvider = "connectionFactories", timeOut = TEST_TIMEOUT_MS) |
| | | public void testNonBlockingFutureWithHandler(ConnectionFactory factory) throws Exception { |
| | | // Use the handler to get the result asynchronously. |
| | | public void testNonBlockingPromiseWithHandler(ConnectionFactory factory) throws Exception { |
| | | // Use the promise to get the result asynchronously. |
| | | final PromiseImpl<Connection, LdapException> promise = PromiseImpl.create(); |
| | | |
| | | factory.getConnectionAsync().onSuccess(new SuccessHandler<Connection>() { |
| | |
| | | promise.handleResult(con); |
| | | } |
| | | }).onFailure(new FailureHandler<LdapException>() { |
| | | |
| | | @Override |
| | | public void handleError(LdapException error) { |
| | | promise.handleError(error); |
| | |
| | | |
| | | // Since we don't have anything to do, we would rather |
| | | // be notified by the promise when the other thread calls our handler. |
| | | promise.getOrThrow(); // should do a timed wait rather? |
| | | // should do a timed wait rather? |
| | | promise.getOrThrow(); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | // Mock underlying connection factory which always succeeds. |
| | | final ConnectionFactory mockFactory = mock(ConnectionFactory.class); |
| | | when(mockFactory.getConnectionAsync()).thenAnswer(new Answer<FutureResult<Connection>>() { |
| | | when(mockFactory.getConnectionAsync()).thenAnswer(new Answer<LdapPromise<Connection>>() { |
| | | |
| | | @Override |
| | | public FutureResult<Connection> answer(InvocationOnMock invocation) throws Throwable { |
| | | public LdapPromise<Connection> answer(InvocationOnMock invocation) throws Throwable { |
| | | // Update state. |
| | | final int connectionID = realConnectionCount.getAndIncrement(); |
| | | realConnectionIsClosed[connectionID] = false; |
| | |
| | | when(mockConnection.isValid()).thenReturn(true); |
| | | when(mockConnection.toString()).thenReturn("Mock connection " + connectionID); |
| | | |
| | | return newSuccessfulFutureResult(mockConnection); |
| | | return newSuccessfulLdapPromise(mockConnection); |
| | | } |
| | | }); |
| | | |
| | |
| | | contextHolder.set((LDAPClientContext) invocation.getArguments()[0]); |
| | | connectLatch.countDown(); /* is this needed? */ |
| | | if (config.closeOnAccept) { |
| | | throw newErrorResult(ResultCode.UNAVAILABLE); |
| | | throw newLdapException(ResultCode.UNAVAILABLE); |
| | | } else { |
| | | // Return a mock connection which always succeeds for binds. |
| | | ServerConnection<Integer> mockConnection = mock(ServerConnection.class); |