Search in sources :

Example 71 with Semaphore

use of java.util.concurrent.Semaphore in project buck by facebook.

the class JavaInMemoryFileObjectTest method setUp.

@Before
public void setUp() {
    outputStream = new TestCustomZipOutputStream();
    semaphore = new Semaphore(1);
}
Also used : Semaphore(java.util.concurrent.Semaphore) TestCustomZipOutputStream(com.facebook.buck.testutil.TestCustomZipOutputStream) Before(org.junit.Before)

Example 72 with Semaphore

use of java.util.concurrent.Semaphore in project Parse-SDK-Android by ParsePlatform.

the class ParseAnalyticsTest method testTrackEventInBackgroundNormalCallback.

@Test
public void testTrackEventInBackgroundNormalCallback() throws Exception {
    final Map<String, String> dimensions = new HashMap<>();
    dimensions.put("key", "value");
    final Semaphore done = new Semaphore(0);
    ParseAnalytics.trackEventInBackground("test", dimensions, new SaveCallback() {

        @Override
        public void done(ParseException e) {
            assertNull(e);
            done.release();
        }
    });
    // Make sure the callback is called
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).trackEventInBackground(eq("test"), eq(dimensions), isNull(String.class));
    final Semaphore doneAgain = new Semaphore(0);
    ParseAnalytics.trackEventInBackground("test", new SaveCallback() {

        @Override
        public void done(ParseException e) {
            assertNull(e);
            doneAgain.release();
        }
    });
    // Make sure the callback is called
    assertTrue(doneAgain.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).trackEventInBackground(eq("test"), Matchers.<Map<String, String>>eq(null), isNull(String.class));
}
Also used : HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 73 with Semaphore

use of java.util.concurrent.Semaphore in project Parse-SDK-Android by ParsePlatform.

the class ParseCloudTest method testCallFunctionNormalCallback.

@Test
public void testCallFunctionNormalCallback() throws Exception {
    ParseCloudCodeController controller = mockParseCloudCodeControllerWithResponse("result");
    ParseCorePlugins.getInstance().registerCloudCodeController(controller);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("key1", Arrays.asList(1, 2, 3));
    parameters.put("key2", "value1");
    final Semaphore done = new Semaphore(0);
    ParseCloud.callFunctionInBackground("name", parameters, new FunctionCallback<Object>() {

        @Override
        public void done(Object result, ParseException e) {
            assertNull(e);
            assertThat(result, instanceOf(String.class));
            assertEquals("result", result);
            done.release();
        }
    });
    // Make sure the callback is called
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).callFunctionInBackground(eq("name"), eq(parameters), isNull(String.class));
}
Also used : HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 74 with Semaphore

use of java.util.concurrent.Semaphore in project Parse-SDK-Android by ParsePlatform.

the class ParseCountingFileHttpBodyTest method testWriteTo.

@Test
public void testWriteTo() throws Exception {
    final Semaphore didReportIntermediateProgress = new Semaphore(0);
    final Semaphore finish = new Semaphore(0);
    ParseCountingFileHttpBody body = new ParseCountingFileHttpBody(makeTestFile(temporaryFolder.getRoot()), new ProgressCallback() {

        Integer maxProgressSoFar = 0;

        @Override
        public void done(Integer percentDone) {
            if (percentDone > maxProgressSoFar) {
                maxProgressSoFar = percentDone;
                assertTrue(percentDone >= 0 && percentDone <= 100);
                if (percentDone < 100 && percentDone > 0) {
                    didReportIntermediateProgress.release();
                } else if (percentDone == 100) {
                    finish.release();
                } else if (percentDone == 0) {
                // do nothing
                } else {
                    fail("percentDone should be within 0 - 100");
                }
            }
        }
    });
    // Check content
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    body.writeTo(output);
    assertArrayEquals(getData().getBytes(), output.toByteArray());
    // Check progress callback
    assertTrue(didReportIntermediateProgress.tryAcquire(5, TimeUnit.SECONDS));
    assertTrue(finish.tryAcquire(5, TimeUnit.SECONDS));
}
Also used : Semaphore(java.util.concurrent.Semaphore) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 75 with Semaphore

use of java.util.concurrent.Semaphore in project Parse-SDK-Android by ParsePlatform.

the class ParsePushTest method testSubscribeInBackgroundWithCallbackFail.

@Test
public void testSubscribeInBackgroundWithCallbackFail() throws Exception {
    ParsePushChannelsController controller = mock(ParsePushChannelsController.class);
    final ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error");
    when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forError(exception));
    ParseCorePlugins.getInstance().registerPushChannelsController(controller);
    ParsePush push = new ParsePush();
    final Semaphore done = new Semaphore(0);
    final Capture<Exception> exceptionCapture = new Capture<>();
    push.subscribeInBackground("test", new SaveCallback() {

        @Override
        public void done(ParseException e) {
            exceptionCapture.set(e);
            done.release();
        }
    });
    assertSame(exception, exceptionCapture.get());
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).subscribeInBackground("test");
}
Also used : Semaphore(java.util.concurrent.Semaphore) Capture(bolts.Capture) 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