use of com.google.common.util.concurrent.ListeningExecutorService in project cdap by caskdata.
the class PayloadTableTest method testConcurrentWrites.
@Test
public void testConcurrentWrites() throws Exception {
// Create two threads, each of them writes to a different topic with two events in one store call.
// The iterators in the two threads would alternate to produce payload. This is for testing CDAP-12013
ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
final CyclicBarrier barrier = new CyclicBarrier(2);
final CountDownLatch storeCompletion = new CountDownLatch(2);
for (int i = 0; i < 2; i++) {
final TopicId topicId = NamespaceId.DEFAULT.topic("testConcurrentWrites" + i);
TopicMetadata metadata = new TopicMetadata(topicId, DEFAULT_PROPERTY);
try (MetadataTable metadataTable = getMetadataTable()) {
metadataTable.createTopic(metadata);
}
final int threadId = i;
executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
try (PayloadTable payloadTable = getPayloadTable()) {
payloadTable.store(new AbstractIterator<PayloadTable.Entry>() {
short messageCount = 0;
@Override
protected PayloadTable.Entry computeNext() {
if (messageCount >= 2) {
return endOfData();
}
try {
barrier.await();
} catch (Exception e) {
throw Throwables.propagate(e);
}
return new TestPayloadEntry(topicId, GENERATION, threadId, 0, messageCount, Bytes.toBytes("message " + threadId + " " + messageCount++));
}
});
storeCompletion.countDown();
} catch (Exception e) {
LOG.error("Failed to store to MessageTable", e);
}
return null;
}
});
}
executor.shutdown();
Assert.assertTrue(storeCompletion.await(5, TimeUnit.SECONDS));
// Read from each topic. Each topic should have two messages
for (int i = 0; i < 2; i++) {
TopicId topicId = NamespaceId.DEFAULT.topic("testConcurrentWrites" + i);
TopicMetadata metadata = new TopicMetadata(topicId, DEFAULT_PROPERTY);
byte[] rawId = new byte[MessageId.RAW_ID_SIZE];
MessageId.putRawId(0L, (short) 0, 0, (short) 0, rawId, 0);
MessageId messageId = new MessageId(rawId);
try (PayloadTable payloadTable = getPayloadTable();
CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(metadata, i, messageId, true, 10)) {
List<PayloadTable.Entry> entries = Lists.newArrayList(iterator);
Assert.assertEquals(2, entries.size());
int count = 0;
for (PayloadTable.Entry entry : entries) {
Assert.assertEquals("message " + i + " " + count++, Bytes.toString(entry.getPayload()));
}
}
}
}
use of com.google.common.util.concurrent.ListeningExecutorService in project cdap by caskdata.
the class HBaseTestBase method parallelRun.
/**
* Executes the given list of Runnable in parallel using a fixed thread pool executor. This method blocks
* until all runnables finished.
*/
private void parallelRun(List<? extends Runnable> runnables) {
ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(runnables.size()));
try {
List<ListenableFuture<?>> futures = new ArrayList<>(runnables.size());
for (Runnable r : runnables) {
futures.add(executor.submit(r));
}
Futures.getUnchecked(Futures.allAsList(futures));
} finally {
executor.shutdownNow();
try {
executor.awaitTermination(60, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOG.error("Interrupted", e);
}
}
}
use of com.google.common.util.concurrent.ListeningExecutorService in project controller by opendaylight.
the class AbstractDataServiceTest method setUp.
@Before
public void setUp() {
ListeningExecutorService executor = MoreExecutors.newDirectExecutorService();
BindingBrokerTestFactory factory = new BindingBrokerTestFactory();
factory.setExecutor(executor);
factory.setStartWithParsedSchema(getStartWithSchema());
testContext = factory.getTestContext();
testContext.start();
}
use of com.google.common.util.concurrent.ListeningExecutorService in project tutorials by eugenp.
the class MoreExecutorsUnitTest method whenExecutingRunnableInListeningExecutor_shouldLogThreadExecution.
@Test
public void whenExecutingRunnableInListeningExecutor_shouldLogThreadExecution() throws Exception {
ConcurrentHashMap<String, Boolean> threadExecutions = new ConcurrentHashMap<>();
Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true);
ListeningExecutorService executor = MoreExecutors.newDirectExecutorService();
executor.execute(logThreadRun);
Assert.assertTrue(threadExecutions.get("main"));
}
use of com.google.common.util.concurrent.ListeningExecutorService in project bgpcep by opendaylight.
the class MockedExecutorWrapper method getMockedExecutor.
ListeningExecutorService getMockedExecutor() {
final ListeningExecutorService mockedExecutor = mock(ListeningExecutorService.class);
final Answer<ListenableFuture<?>> submitAnswer = invocation -> {
final Object task = invocation.getArguments()[0];
this.submittedTasksToExecutor.add(task);
Object result = null;
if (task instanceof Runnable) {
((Runnable) task).run();
} else if (task instanceof Callable) {
result = ((Callable<?>) task).call();
}
final ListenableFuture<?> mockedFuture = mock(ListenableFuture.class);
doReturn(result).when(mockedFuture).get();
return mockedFuture;
};
doAnswer(submitAnswer).when(mockedExecutor).submit(any(Runnable.class));
doAnswer(submitAnswer).when(mockedExecutor).submit(any(Callable.class));
return mockedExecutor;
}
Aggregations