Search in sources :

Example 36 with Semaphore

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)));
    }
}
Also used : HashMap(java.util.HashMap) Config(com.hazelcast.config.Config) JobTracker(com.hazelcast.mapreduce.JobTracker) Semaphore(java.util.concurrent.Semaphore) HazelcastInstance(com.hazelcast.core.HazelcastInstance) HashMap(java.util.HashMap) Map(java.util.Map) IMap(com.hazelcast.core.IMap) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 37 with Semaphore

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]);
    }
}
Also used : Config(com.hazelcast.config.Config) JobTracker(com.hazelcast.mapreduce.JobTracker) Semaphore(java.util.concurrent.Semaphore) HazelcastInstance(com.hazelcast.core.HazelcastInstance) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 38 with Semaphore

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)));
    }
}
Also used : HashMap(java.util.HashMap) Config(com.hazelcast.config.Config) JobTracker(com.hazelcast.mapreduce.JobTracker) Semaphore(java.util.concurrent.Semaphore) HazelcastInstance(com.hazelcast.core.HazelcastInstance) HashMap(java.util.HashMap) MultiMap(com.hazelcast.core.MultiMap) Map(java.util.Map) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 39 with Semaphore

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());
}
Also used : Reporter(com.google.devtools.build.lib.events.Reporter) ExecutorService(java.util.concurrent.ExecutorService) Level(java.util.logging.Level) Semaphore(java.util.concurrent.Semaphore) EventBus(com.google.common.eventbus.EventBus) Logger(java.util.logging.Logger) Test(org.junit.Test)

Example 40 with Semaphore

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);
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Semaphore(java.util.concurrent.Semaphore) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Supplier(com.google.common.base.Supplier) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Test(org.junit.Test)

Aggregations

Semaphore (java.util.concurrent.Semaphore)511 Test (org.junit.Test)198 IOException (java.io.IOException)55 Context (android.content.Context)39 InvocationOnMock (org.mockito.invocation.InvocationOnMock)38 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)36 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)31 AtomicReference (java.util.concurrent.atomic.AtomicReference)31 ExecutionException (java.util.concurrent.ExecutionException)30 File (java.io.File)29 Intent (android.content.Intent)27 List (java.util.List)27 Map (java.util.Map)26 CountDownLatch (java.util.concurrent.CountDownLatch)25 Handler (android.os.Handler)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 HazelcastInstance (com.hazelcast.core.HazelcastInstance)21 BroadcastReceiver (android.content.BroadcastReceiver)20 IntentFilter (android.content.IntentFilter)20