Search in sources :

Example 1 with ConnectionConfigFactory

use of org.forgerock.openam.sm.ConnectionConfigFactory in project OpenAM by OpenRock.

the class PooledTaskExecutorTest method testExecute.

@Test
public void testExecute() throws Exception {
    // Given
    ConnectionConfigFactory configFactory = mock(ConnectionConfigFactory.class);
    ConnectionConfig config = mock(ConnectionConfig.class);
    when(configFactory.getConfig(any(ConnectionType.class))).thenReturn(config);
    when(config.getMaxConnections()).thenReturn(2);
    Debug debug = mock(Debug.class);
    when(debug.messageEnabled()).thenReturn(true);
    doAnswer(new Answer<Void>() {

        public Void answer(InvocationOnMock invocation) throws Throwable {
            System.out.println(Thread.currentThread().getName() + ":: " + invocation.getArguments()[0]);
            return null;
        }
    }).when(debug).message(anyString());
    Provider<SimpleTaskExecutor> simpleTaskExecutorProvider = mock(Provider.class);
    when(simpleTaskExecutorProvider.get()).thenAnswer(new Answer<SimpleTaskExecutor<?>>() {

        public SimpleTaskExecutor<?> answer(InvocationOnMock invocation) throws Throwable {
            return new SimpleTaskExecutor<Object>(mock(ConnectionFactory.class), null, null);
        }
    });
    Semaphore semaphore = new Semaphore(2, true);
    // When
    final TaskExecutor executor = new PooledTaskExecutor(simpleTaskExecutorProvider, debug, ConnectionType.RESOURCE_SETS, configFactory, semaphore);
    LongTask longTask1 = new LongTask();
    TaskThread task1 = new TaskThread(1, executor, longTask1);
    LongTask longTask2 = new LongTask();
    TaskThread task2 = new TaskThread(2, executor, longTask2);
    TaskThread task3 = new TaskThread(3, executor, mock(Task.class));
    debug("Starting task 1");
    task1.start();
    debug("Starting task 2");
    task2.start();
    while (semaphore.availablePermits() > 0) {
        debug("Waiting for no available permits. Currently got: {0}", semaphore.availablePermits());
        Thread.sleep(50);
    }
    debug("Tasks 1 and 2 should now be executing and will shortly be blocked - starting task 3");
    task3.start();
    long timeout = System.currentTimeMillis() + 5000;
    while (!semaphore.hasQueuedThreads()) {
        debug("Waiting for task 3 to be queued on semaphore");
        Thread.sleep(50);
        if (System.currentTimeMillis() > timeout) {
            fail("Where did my thread go?");
        }
    }
    debug("Task 3 now queued on semaphore");
    // Then
    verifyZeroInteractions(task3.task);
    // When
    debug("Unblocking task 2");
    longTask2.unblock();
    debug("Unblocking task 1");
    longTask1.unblock();
    // Then
    debug("Waiting for tasks to complete");
    task1.join(TimeUnit.SECONDS.toMillis(10));
    task2.join(TimeUnit.SECONDS.toMillis(10));
    task3.join(TimeUnit.SECONDS.toMillis(10));
    assertThat(task1.isAlive()).as("Task 1 thread running").isFalse();
    assertThat(task2.isAlive()).as("Task 2 thread running").isFalse();
    assertThat(task3.isAlive()).as("Task 3 thread running").isFalse();
    verify(task3.task).execute(null, null);
    verify(simpleTaskExecutorProvider, times(2)).get();
    assertThat(semaphore.availablePermits()).isEqualTo(2);
}
Also used : Task(org.forgerock.openam.sm.datalayer.api.Task) ConnectionType(org.forgerock.openam.sm.datalayer.api.ConnectionType) ConnectionConfigFactory(org.forgerock.openam.sm.ConnectionConfigFactory) Semaphore(java.util.concurrent.Semaphore) TaskExecutor(org.forgerock.openam.sm.datalayer.api.TaskExecutor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConnectionConfig(org.forgerock.openam.sm.ConnectionConfig) Debug(com.sun.identity.shared.debug.Debug) Test(org.testng.annotations.Test)

Example 2 with ConnectionConfigFactory

use of org.forgerock.openam.sm.ConnectionConfigFactory in project OpenAM by OpenRock.

the class ConnectionConfigFactoryTest method shouldReturnConfigForDefaultStoreMode.

@Test
public void shouldReturnConfigForDefaultStoreMode() throws InvalidConfigurationException {
    // given
    ConnectionConfigFactory factory = new ConnectionConfigFactory(mockDataLayerConfig, mockExternalCTSConfig, mockDataLayerConfiguration, mockConfigurationValidator);
    when(mockDataLayerConfiguration.getStoreMode()).thenReturn(StoreMode.DEFAULT);
    // when
    ConnectionConfig config = factory.getConfig(null);
    config.getLDAPURLs();
    config.getBindDN();
    config.getBindPassword();
    config.getLdapHeartbeat();
    config.getMaxConnections();
    // then
    verify(mockDataLayerConfig).getLDAPURLs();
    verify(mockDataLayerConfig).getBindDN();
    verify(mockDataLayerConfig).getBindPassword();
    verify(mockDataLayerConfig).getLdapHeartbeat();
    verify(mockDataLayerConfig, never()).getMaxConnections();
    verify(mockExternalCTSConfig).getMaxConnections();
}
Also used : ConnectionConfigFactory(org.forgerock.openam.sm.ConnectionConfigFactory) ConnectionConfig(org.forgerock.openam.sm.ConnectionConfig) Test(org.testng.annotations.Test)

Example 3 with ConnectionConfigFactory

use of org.forgerock.openam.sm.ConnectionConfigFactory in project OpenAM by OpenRock.

the class ConnectionConfigFactoryTest method shouldReturnConfigForExternalStoreMode.

@Test
public void shouldReturnConfigForExternalStoreMode() throws InvalidConfigurationException {
    // given
    ConnectionConfigFactory factory = new ConnectionConfigFactory(mockDataLayerConfig, mockExternalCTSConfig, mockDataLayerConfiguration, mockConfigurationValidator);
    when(mockDataLayerConfiguration.getStoreMode()).thenReturn(StoreMode.EXTERNAL);
    // when
    ConnectionConfig config = factory.getConfig(null);
    // then
    assertThat(config).isSameAs(mockExternalCTSConfig);
}
Also used : ConnectionConfigFactory(org.forgerock.openam.sm.ConnectionConfigFactory) ConnectionConfig(org.forgerock.openam.sm.ConnectionConfig) Test(org.testng.annotations.Test)

Aggregations

ConnectionConfig (org.forgerock.openam.sm.ConnectionConfig)3 ConnectionConfigFactory (org.forgerock.openam.sm.ConnectionConfigFactory)3 Test (org.testng.annotations.Test)3 Debug (com.sun.identity.shared.debug.Debug)1 Semaphore (java.util.concurrent.Semaphore)1 ConnectionType (org.forgerock.openam.sm.datalayer.api.ConnectionType)1 Task (org.forgerock.openam.sm.datalayer.api.Task)1 TaskExecutor (org.forgerock.openam.sm.datalayer.api.TaskExecutor)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1