use of java.util.concurrent.Semaphore in project hazelcast by hazelcast.
the class DistributedMapperClientMultiMapReduceTest method testAsyncMapperReducerCollator.
@Test(timeout = 120000)
public void testAsyncMapperReducerCollator() throws Exception {
Config config = buildConfig();
HazelcastInstance h1 = hazelcastFactory.newHazelcastInstance(config);
HazelcastInstance h2 = hazelcastFactory.newHazelcastInstance(config);
HazelcastInstance h3 = hazelcastFactory.newHazelcastInstance(config);
assertClusterSizeEventually(3, h1);
assertClusterSizeEventually(3, h2);
assertClusterSizeEventually(3, h3);
HazelcastInstance client = hazelcastFactory.newHazelcastClient(null);
MultiMap<Integer, Integer> m1 = client.getMultiMap(randomString());
for (int i = 0; i < 100; i++) {
m1.put(i / 2, i);
}
final int[] result = new int[1];
final Semaphore semaphore = new Semaphore(1);
semaphore.acquire();
JobTracker tracker = client.getJobTracker("default");
Job<Integer, Integer> job = tracker.newJob(integerKvSource(m1));
ICompletableFuture<Integer> future = job.mapper(new GroupingTestMapper()).combiner(new TestCombinerFactory()).reducer(new TestReducerFactory()).submit(new TestCollator());
future.andThen(new ExecutionCallback<Integer>() {
@Override
public void onResponse(Integer response) {
try {
result[0] = response.intValue();
} finally {
semaphore.release();
}
}
@Override
public void onFailure(Throwable t) {
semaphore.release();
}
});
// Precalculate result
int expectedResult = 0;
for (int i = 0; i < 100; i++) {
expectedResult += i;
}
semaphore.acquire();
for (int i = 0; i < 4; i++) {
assertEquals(expectedResult, result[0]);
}
}
use of java.util.concurrent.Semaphore in project hazelcast by hazelcast.
the class DistributedMapperClientMultiMapReduceTest method testAsyncMapperReducer.
@Test(timeout = 120000)
public void testAsyncMapperReducer() throws Exception {
Config config = buildConfig();
HazelcastInstance h1 = hazelcastFactory.newHazelcastInstance(config);
HazelcastInstance h2 = hazelcastFactory.newHazelcastInstance(config);
HazelcastInstance h3 = hazelcastFactory.newHazelcastInstance(config);
assertClusterSizeEventually(3, h1);
assertClusterSizeEventually(3, h2);
assertClusterSizeEventually(3, h3);
HazelcastInstance client = hazelcastFactory.newHazelcastClient(null);
MultiMap<Integer, Integer> m1 = client.getMultiMap(randomString());
for (int i = 0; i < 100; i++) {
m1.put(i, i);
}
final Map<String, Integer> listenerResults = new HashMap<String, Integer>();
final Semaphore semaphore = new Semaphore(1);
semaphore.acquire();
JobTracker tracker = client.getJobTracker("default");
Job<Integer, Integer> job = tracker.newJob(integerKvSource(m1));
ICompletableFuture<Map<String, Integer>> future = job.mapper(new GroupingTestMapper()).combiner(new TestCombinerFactory()).reducer(new TestReducerFactory()).submit();
future.andThen(new ExecutionCallback<Map<String, Integer>>() {
@Override
public void onResponse(Map<String, Integer> response) {
try {
listenerResults.putAll(response);
} finally {
semaphore.release();
}
}
@Override
public void onFailure(Throwable t) {
semaphore.release();
}
});
// Precalculate results
int[] expectedResults = new int[4];
for (int i = 0; i < 100; i++) {
int index = i % 4;
expectedResults[index] += i;
}
semaphore.acquire();
for (int i = 0; i < 4; i++) {
assertEquals(expectedResults[i], (int) listenerResults.get(String.valueOf(i)));
}
}
use of java.util.concurrent.Semaphore in project hazelcast by hazelcast.
the class MapServiceContextImpl method destroyPartitionsAndMapContainer.
private void destroyPartitionsAndMapContainer(MapContainer mapContainer) {
Semaphore semaphore = new Semaphore(0);
InternalOperationService operationService = (InternalOperationService) nodeEngine.getOperationService();
for (PartitionContainer container : partitionContainers) {
MapPartitionDestroyTask partitionDestroyTask = new MapPartitionDestroyTask(container, mapContainer, semaphore);
operationService.execute(partitionDestroyTask);
}
try {
semaphore.tryAcquire(partitionContainers.length, DESTROY_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (Throwable t) {
throw ExceptionUtil.rethrow(t);
}
}
use of java.util.concurrent.Semaphore in project buck by facebook.
the class MoreSuppliersTest method weakMemoizeShouldRunDelegateOnlyOnceOnConcurrentAccess.
@Test
public void weakMemoizeShouldRunDelegateOnlyOnceOnConcurrentAccess() throws Exception {
final int numFetchers = 10;
final Semaphore semaphore = new Semaphore(0);
class TestDelegate implements Supplier<Object> {
private int timesCalled = 0;
public int getTimesCalled() {
return timesCalled;
}
@Override
public Object get() {
try {
// Wait for all the fetch threads to be ready.
semaphore.acquire(numFetchers);
// Give other threads a chance to catch up.
Thread.sleep(50);
timesCalled++;
return new Object();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
semaphore.release(numFetchers);
}
}
}
TestDelegate delegate = new TestDelegate();
final Supplier<Object> supplier = MoreSuppliers.weakMemoize(delegate);
ExecutorService threadPool = Executors.newFixedThreadPool(numFetchers);
try {
ListeningExecutorService executor = MoreExecutors.listeningDecorator(threadPool);
class Fetcher implements Callable<Object> {
@Override
public Object call() {
// Signal that this particular fetcher is ready.
semaphore.release();
return supplier.get();
}
}
ImmutableList.Builder<Callable<Object>> fetcherBuilder = ImmutableList.builder();
for (int i = 0; i < numFetchers; i++) {
fetcherBuilder.add(new Fetcher());
}
@SuppressWarnings("unchecked") List<ListenableFuture<Object>> futures = (List<ListenableFuture<Object>>) (List<?>) executor.invokeAll(fetcherBuilder.build());
// Wait for all fetchers to finish.
List<Object> results = Futures.allAsList(futures).get();
Assert.assertEquals("should only have been called once", 1, delegate.getTimesCalled());
Assert.assertThat("all result items are the same", ImmutableSet.copyOf(results), Matchers.hasSize(1));
Preconditions.checkState(threadPool.shutdownNow().isEmpty(), "All jobs should have completed");
Preconditions.checkState(threadPool.awaitTermination(10, TimeUnit.SECONDS), "Thread pool should terminate in a reasonable amount of time");
} finally {
// In case exceptions were thrown, attempt to shut down the thread pool.
threadPool.shutdownNow();
threadPool.awaitTermination(10, TimeUnit.SECONDS);
}
}
use of java.util.concurrent.Semaphore in project FirebaseUI-Android by firebase.
the class TestUtils method runAndWaitUntil.
public static void runAndWaitUntil(FirebaseArray array, Runnable task, Callable<Boolean> done) throws InterruptedException {
final Semaphore semaphore = new Semaphore(0);
array.setOnChangedListener(new ChangeEventListener() {
@Override
public void onChildChanged(ChangeEventListener.EventType type, int index, int oldIndex) {
semaphore.release();
}
@Override
public void onDataChanged() {
}
@Override
public void onCancelled(DatabaseError error) {
throw new IllegalStateException(error.toException());
}
});
task.run();
boolean isDone = false;
long startedAt = System.currentTimeMillis();
while (!isDone && System.currentTimeMillis() - startedAt < TIMEOUT) {
semaphore.tryAcquire(1, TimeUnit.SECONDS);
try {
isDone = done.call();
} catch (Exception e) {
e.printStackTrace();
// and we're not done
}
}
assertTrue("Timed out waiting for expected results on FirebaseArray", isDone);
array.setOnChangedListener(null);
}
Aggregations