use of java.util.concurrent.Semaphore in project hazelcast by hazelcast.
the class DistributedMapperClientMapReduceTest 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);
IMap<Integer, Integer> m1 = client.getMap(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 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 bazel by bazelbuild.
the class PackageFactoryTest method testCreatePackageIsolatedFromOuterErrors.
@Test
public void testCreatePackageIsolatedFromOuterErrors() throws Exception {
ExecutorService e = Executors.newCachedThreadPool();
final Semaphore beforeError = new Semaphore(0);
final Semaphore afterError = new Semaphore(0);
Reporter reporter = new Reporter(new EventBus());
ParsingTracker parser = new ParsingTracker(beforeError, afterError, reporter);
final Logger log = Logger.getLogger(PackageFactory.class.getName());
log.addHandler(parser);
Level originalLevel = log.getLevel();
log.setLevel(Level.FINE);
e.execute(new ErrorReporter(reporter, beforeError, afterError));
e.execute(parser);
// wait for all to finish
e.shutdown();
assertTrue(e.awaitTermination(TestUtils.WAIT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS));
log.removeHandler(parser);
log.setLevel(originalLevel);
assertTrue(parser.hasParsed());
}
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);
}
}
Aggregations