Search in sources :

Example 61 with Semaphore

use of java.util.concurrent.Semaphore in project AndroidAsync by koush.

the class FutureTests method testContinuationArray.

public void testContinuationArray() throws Exception {
    final ArrayList<Integer> results = new ArrayList<Integer>();
    final Semaphore semaphore = new Semaphore(0);
    final Continuation c = new Continuation(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            semaphore.release();
        }
    });
    for (int i = 0; i < 10; i++) {
        final int j = i;
        c.add(new ContinuationCallback() {

            @Override
            public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
                results.add(j);
                next.onCompleted(null);
            }
        });
    }
    new Thread() {

        public void run() {
            c.start();
        }

        ;
    }.start();
    assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
    assertEquals(10, results.size());
    for (int i = 0; i < 10; i++) {
        assertEquals((int) results.get(i), i);
    }
}
Also used : Continuation(com.koushikdutta.async.future.Continuation) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ArrayList(java.util.ArrayList) ContinuationCallback(com.koushikdutta.async.callback.ContinuationCallback) Semaphore(java.util.concurrent.Semaphore) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 62 with Semaphore

use of java.util.concurrent.Semaphore in project AndroidAsync by koush.

the class FutureTests method testFutureChain.

public void testFutureChain() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Continuation c = new Continuation(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            semaphore.release();
        }
    });
    IntegerFuture i1;
    c.add(i1 = IntegerFuture.create(2, 200));
    IntegerFuture i2;
    c.add(i2 = IntegerFuture.create(3, 200));
    new Thread() {

        public void run() {
            c.start();
        }

        ;
    }.start();
    assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
    assertEquals((int) i1.get(), 2);
    assertEquals((int) i2.get(), 3);
}
Also used : Continuation(com.koushikdutta.async.future.Continuation) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) Semaphore(java.util.concurrent.Semaphore) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 63 with Semaphore

use of java.util.concurrent.Semaphore in project AndroidAsync by koush.

the class FutureTests method testContinuationFail.

public void testContinuationFail() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Continuation c = new Continuation(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            assertNotNull(ex);
            semaphore.release();
        }
    });
    c.add(new ContinuationCallback() {

        @Override
        public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
            throw new Exception("fail");
        }
    });
    new Thread() {

        public void run() {
            c.start();
        }

        ;
    }.start();
    assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
}
Also used : Continuation(com.koushikdutta.async.future.Continuation) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ContinuationCallback(com.koushikdutta.async.callback.ContinuationCallback) Semaphore(java.util.concurrent.Semaphore) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 64 with Semaphore

use of java.util.concurrent.Semaphore in project AndroidAsync by koush.

the class FutureTests method testFutureCallback.

public void testFutureCallback() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final IntegerFuture future = IntegerFuture.create(20, 1000);
    final Thread mainThread = Thread.currentThread();
    future.setCallback(new FutureCallback<Integer>() {

        @Override
        public void onCompleted(Exception e, Integer result) {
            assertNotSame(Thread.currentThread(), mainThread);
            semaphore.release();
        }
    });
    assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
}
Also used : Semaphore(java.util.concurrent.Semaphore) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 65 with Semaphore

use of java.util.concurrent.Semaphore in project AndroidAsync by koush.

the class CacheTests method testFilteredDataEmitter.

public void testFilteredDataEmitter() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    FilteredDataEmitter f = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    f.setDataCallback(new DataCallback() {

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            assertEquals(bb.readString(), "hello");
            bb.recycle();
            semaphore.release();
        }
    });
    f.onDataAvailable(f, new ByteBufferList().add(ByteBuffer.wrap("hello".getBytes())));
    assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
    f.setDataCallback(new DataCallback() {

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            fail();
        }
    });
    f.close();
    f.onDataAvailable(f, new ByteBufferList().add(ByteBuffer.wrap("hello".getBytes())));
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) ByteBufferList(com.koushikdutta.async.ByteBufferList) DataEmitter(com.koushikdutta.async.DataEmitter) FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) Semaphore(java.util.concurrent.Semaphore) DataCallback(com.koushikdutta.async.callback.DataCallback)

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