use of org.forgerock.openam.sm.datalayer.api.Task in project OpenAM by OpenRock.
the class TokenDataStoreTest method testQueryError.
@Test(expectedExceptions = ServerException.class)
public void testQueryError() throws Exception {
// Given
QueryFilter<String> query = QueryFilter.alwaysTrue();
TokenFilter tokenFilter = mock(TokenFilter.class);
when(adapter.toTokenQuery(query)).thenReturn(tokenFilter);
final Token token1 = new Token("123", TokenType.GENERIC);
final Token token2 = new Token("456", TokenType.GENERIC);
Object o1 = new Object();
Object o2 = new Object();
when(adapter.fromToken(token1)).thenReturn(o1);
when(adapter.fromToken(token2)).thenReturn(o2);
final Task task = mock(Task.class);
when(taskFactory.query(any(TokenFilter.class), any(ResultHandler.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
((ResultHandler) invocation.getArguments()[1]).processError(new Exception());
return task;
}
});
// When
store.query(query);
// Then - exception;
}
use of org.forgerock.openam.sm.datalayer.api.Task in project OpenAM by OpenRock.
the class TokenDataStoreTest method testQuery.
@Test
public void testQuery() throws Exception {
// Given
QueryFilter<String> query = QueryFilter.alwaysTrue();
TokenFilter tokenFilter = mock(TokenFilter.class);
when(adapter.toTokenQuery(query)).thenReturn(tokenFilter);
final Token token1 = new Token("123", TokenType.GENERIC);
final Token token2 = new Token("456", TokenType.GENERIC);
Object o1 = new Object();
Object o2 = new Object();
when(adapter.fromToken(token1)).thenReturn(o1);
when(adapter.fromToken(token2)).thenReturn(o2);
final Task task = mock(Task.class);
when(taskFactory.query(any(TokenFilter.class), any(ResultHandler.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
((ResultHandler) invocation.getArguments()[1]).processResults(asSet(token1, token2));
return task;
}
});
// When
Set<Object> result = store.query(query);
// Then
verify(taskFactory).query(eq(tokenFilter), any(ResultHandler.class));
verify(taskExecutor).execute(null, task);
assertThat(result).containsOnly(o1, o2);
}
use of org.forgerock.openam.sm.datalayer.api.Task 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);
}
use of org.forgerock.openam.sm.datalayer.api.Task in project OpenAM by OpenRock.
the class SeriesTaskExecutorThreadTest method shouldExecuteTaskFromQueue.
@Test
public void shouldExecuteTaskFromQueue() throws Exception {
// Given
Task mockTask = mock(Task.class);
processor.setQueue(generateTestQueue(mockTask));
// When
processor.run();
// Then
verify(mockExecutor).execute(null, mockTask);
}
use of org.forgerock.openam.sm.datalayer.api.Task in project OpenAM by OpenRock.
the class SimpleTaskExecutorTest method testExecute.
@Test
public void testExecute() throws Exception {
// given
Task task = mock(Task.class);
Closeable connection = mock(Closeable.class);
when(connectionFactory.create()).thenReturn(connection);
when(connectionFactory.isValid(connection)).thenReturn(true);
executor.start();
// when
executor.execute(null, task);
// then
verify(task).execute(connection, adapter);
}
Aggregations