List of usage examples for java.util.concurrent Semaphore tryAcquire
public boolean tryAcquire(int permits, long timeout, TimeUnit unit) throws InterruptedException
From source file:com.parse.ParseAnalyticsTest.java
@Test public void testTrackAppOpenedInBackgroundNormalCallback() throws Exception { Intent intent = makeIntentWithParseData("test"); final Semaphore done = new Semaphore(0); ParseAnalytics.trackAppOpenedInBackground(intent, new SaveCallback() { @Override//from w w w . j a v a 2 s . c om 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)).trackAppOpenedInBackground(eq("test"), isNull(String.class)); }
From source file:com.parse.ParseAnalyticsTest.java
@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/*from w w w .j av a 2 s . c o m*/ 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)); }
From source file:com.netflix.curator.framework.recipes.cache.TestNodeCache.java
@Test public void testDeleteThenCreate() throws Exception { NodeCache cache = null;/*w w w. ja va 2s .c o m*/ CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start(); try { client.create().creatingParentsIfNeeded().forPath("/test/foo", "one".getBytes()); final AtomicReference<Throwable> error = new AtomicReference<Throwable>(); client.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() { @Override public void unhandledError(String message, Throwable e) { error.set(e); } }); final Semaphore semaphore = new Semaphore(0); cache = new NodeCache(client, "/test/foo"); cache.getListenable().addListener(new NodeCacheListener() { @Override public void nodeChanged() throws Exception { semaphore.release(); } }); cache.start(true); Assert.assertEquals(cache.getCurrentData().getData(), "one".getBytes()); client.delete().forPath("/test/foo"); Assert.assertTrue(semaphore.tryAcquire(1, 10, TimeUnit.SECONDS)); client.create().forPath("/test/foo", "two".getBytes()); Assert.assertTrue(semaphore.tryAcquire(1, 10, TimeUnit.SECONDS)); Throwable t = error.get(); if (t != null) { Assert.fail("Assert", t); } Assert.assertEquals(cache.getCurrentData().getData(), "two".getBytes()); cache.close(); } finally { IOUtils.closeQuietly(cache); IOUtils.closeQuietly(client); } }
From source file:com.netflix.curator.framework.recipes.leader.TestLeaderSelector.java
@Test public void testAutoRequeue() throws Exception { LeaderSelector selector = null;/*from w w w . j a v a2s. c o m*/ CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()) .retryPolicy(new RetryOneTime(1)).sessionTimeoutMs(1000).build(); try { client.start(); final Semaphore semaphore = new Semaphore(0); LeaderSelectorListener listener = new LeaderSelectorListener() { @Override public void takeLeadership(CuratorFramework client) throws Exception { Thread.sleep(10); semaphore.release(); } @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { } }; selector = new LeaderSelector(client, "/leader", listener); selector.autoRequeue(); selector.start(); Assert.assertTrue(semaphore.tryAcquire(2, 10, TimeUnit.SECONDS)); } finally { IOUtils.closeQuietly(selector); IOUtils.closeQuietly(client); } }
From source file:com.netflix.curator.framework.recipes.cache.TestPathChildrenCache.java
@Test public void testIssue27() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start();//from w ww .ja va 2 s . co m try { client.create().forPath("/base"); client.create().forPath("/base/a"); client.create().forPath("/base/b"); client.create().forPath("/base/c"); client.getChildren().forPath("/base"); final List<PathChildrenCacheEvent.Type> events = Lists.newArrayList(); final Semaphore semaphore = new Semaphore(0); PathChildrenCache cache = new PathChildrenCache(client, "/base", true); cache.getListenable().addListener(new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { events.add(event.getType()); semaphore.release(); } }); cache.start(); Assert.assertTrue(semaphore.tryAcquire(3, 10, TimeUnit.SECONDS)); client.delete().forPath("/base/a"); Assert.assertTrue(semaphore.tryAcquire(1, 10, TimeUnit.SECONDS)); client.create().forPath("/base/a"); Assert.assertTrue(semaphore.tryAcquire(1, 10, TimeUnit.SECONDS)); List<PathChildrenCacheEvent.Type> expected = Lists.newArrayList(PathChildrenCacheEvent.Type.CHILD_ADDED, PathChildrenCacheEvent.Type.CHILD_ADDED, PathChildrenCacheEvent.Type.CHILD_ADDED, PathChildrenCacheEvent.Type.CHILD_REMOVED, PathChildrenCacheEvent.Type.CHILD_ADDED); Assert.assertEquals(expected, events); } finally { client.close(); } }
From source file:org.apache.spark.network.RpcIntegrationSuite.java
private RpcResult sendRpcWithStream(String... streams) throws Exception { TransportClient client = clientFactory.createClient(TestUtils.getLocalHost(), server.getPort()); final Semaphore sem = new Semaphore(0); RpcResult res = new RpcResult(); res.successMessages = Collections.synchronizedSet(new HashSet<String>()); res.errorMessages = Collections.synchronizedSet(new HashSet<String>()); for (String stream : streams) { int idx = stream.lastIndexOf('/'); ManagedBuffer meta = new NioManagedBuffer(JavaUtils.stringToBytes(stream)); String streamName = (idx == -1) ? stream : stream.substring(idx + 1); ManagedBuffer data = testData.openStream(conf, streamName); client.uploadStream(meta, data, new RpcStreamCallback(stream, res, sem)); }/*w w w. j ava2s .co m*/ if (!sem.tryAcquire(streams.length, 5, TimeUnit.SECONDS)) { fail("Timeout getting response from the server"); } streamCallbacks.values().forEach(streamCallback -> { try { streamCallback.verify(); } catch (IOException e) { throw new RuntimeException(e); } }); client.close(); return res; }
From source file:com.netflix.curator.framework.imps.TestFrameworkEdges.java
@Test public void testRetry() throws Exception { final int MAX_RETRIES = 3; final int serverPort = server.getPort(); final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), 1000, 1000, new RetryOneTime(10)); client.start();//from w w w.j ava 2 s. com try { final AtomicInteger retries = new AtomicInteger(0); final Semaphore semaphore = new Semaphore(0); client.getZookeeperClient().setRetryPolicy(new RetryPolicy() { @Override public boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleeper) { semaphore.release(); if (retries.incrementAndGet() == MAX_RETRIES) { try { server = new TestingServer(serverPort); } catch (Exception e) { throw new Error(e); } } return true; } }); server.stop(); // test foreground retry client.checkExists().forPath("/hey"); Assert.assertTrue(semaphore.tryAcquire(MAX_RETRIES, 10, TimeUnit.SECONDS)); semaphore.drainPermits(); retries.set(0); server.stop(); // test background retry client.checkExists().inBackground().forPath("/hey"); Assert.assertTrue(semaphore.tryAcquire(MAX_RETRIES, 10, TimeUnit.SECONDS)); } catch (Throwable e) { Assert.fail("Error", e); } finally { IOUtils.closeQuietly(client); } }
From source file:com.parse.ParsePushTest.java
@Test public void testSubscribeInBackgroundWithCallbackSuccess() throws Exception { final ParsePushChannelsController controller = mock(ParsePushChannelsController.class); when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forResult(null)); 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/*from ww w . ja va 2 s .c o m*/ public void done(ParseException e) { exceptionCapture.set(e); done.release(); } }); assertNull(exceptionCapture.get()); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).subscribeInBackground("test"); }
From source file:com.parse.ParsePushTest.java
@Test public void testUnsubscribeInBackgroundWithCallbackSuccess() throws Exception { final ParsePushChannelsController controller = mock(ParsePushChannelsController.class); when(controller.unsubscribeInBackground(anyString())).thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerPushChannelsController(controller); ParsePush push = new ParsePush(); final Semaphore done = new Semaphore(0); final Capture<Exception> exceptionCapture = new Capture<>(); push.unsubscribeInBackground("test", new SaveCallback() { @Override//from w ww .j a va 2 s . c om public void done(ParseException e) { exceptionCapture.set(e); done.release(); } }); assertNull(exceptionCapture.get()); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).unsubscribeInBackground("test"); }
From source file:com.parse.ParsePushTest.java
@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//from w w w.ja v a2 s.c om 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"); }