Search in sources :

Example 96 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 97 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 98 with Semaphore

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);
    }
}
Also used : MapPartitionDestroyTask(com.hazelcast.map.impl.operation.MapPartitionDestroyTask) Semaphore(java.util.concurrent.Semaphore) InternalOperationService(com.hazelcast.spi.impl.operationservice.InternalOperationService)

Example 99 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)

Example 100 with Semaphore

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);
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Semaphore(java.util.concurrent.Semaphore)

Aggregations

Semaphore (java.util.concurrent.Semaphore)447 Test (org.junit.Test)167 IOException (java.io.IOException)52 Context (android.content.Context)35 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)31 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29 File (java.io.File)27 CountDownLatch (java.util.concurrent.CountDownLatch)25 ExecutionException (java.util.concurrent.ExecutionException)25 Intent (android.content.Intent)24 Handler (android.os.Handler)24 List (java.util.List)24 Map (java.util.Map)24 InvocationOnMock (org.mockito.invocation.InvocationOnMock)23 HazelcastInstance (com.hazelcast.core.HazelcastInstance)21 BroadcastReceiver (android.content.BroadcastReceiver)20 IntentFilter (android.content.IntentFilter)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 TimeoutException (java.util.concurrent.TimeoutException)19