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);
}
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));
}
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));
}
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));
}
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");
}
Aggregations