Example usage for java.util.concurrent BlockingQueue poll

List of usage examples for java.util.concurrent BlockingQueue poll

Introduction

In this page you can find the example usage for java.util.concurrent BlockingQueue poll.

Prototype

E poll(long timeout, TimeUnit unit) throws InterruptedException;

Source Link

Document

Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.

Usage

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void deleteKeyIfMatchAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    final KvMetadata obj = insertItem("key", "{}");

    final BlockingQueue<Boolean> queue = DataStructures.getLTQInstance(Boolean.class);
    client.kv(obj.getCollection(), obj.getKey()).ifMatch(obj.getRef()).delete()
            .on(new ResponseAdapter<Boolean>() {
                @Override//from  w  w w  . j  a  v  a2s  . co  m
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final Boolean object) {
                    queue.add(object);
                }
            });

    final Boolean result = queue.poll(5000, TimeUnit.MILLISECONDS);
    assertNotNull(obj);
    assertTrue(result);
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void purgeKeyIfMatchAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    final KvMetadata obj = insertItem(key, "{}");

    final BlockingQueue<Boolean> queue = DataStructures.getLTQInstance(Boolean.class);
    client.kv(obj.getCollection(), obj.getKey()).ifMatch(obj.getRef()).delete(Boolean.TRUE)
            .on(new ResponseAdapter<Boolean>() {
                @Override/*  w  w w  . j  av a  2  s  .  c  om*/
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final Boolean object) {
                    queue.add(object);
                }
            });

    final Boolean result = queue.poll(5000, TimeUnit.MILLISECONDS);

    final KvObject<String> nullObj = client.kv(obj.getCollection(), obj.getKey())
            .get(String.class, obj.getRef()).get();

    assertNotNull(obj);
    assertTrue(result);
    assertNull(nullObj);
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void upsertMergePatchKeyAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    KvObject<ObjectNode> kvBefore = client.kv(collection(), key).get(ObjectNode.class).get();

    // assert the object is not present before the patch
    assertNull(kvBefore);//from  www . j a v  a  2s .  c om

    String name = Long.toHexString(RAND.nextLong());

    final BlockingQueue<KvMetadata> queue = DataStructures.getLTQInstance(KvMetadata.class);

    client.kv(collection(), key).upsert().merge("{\"name\":\"" + name + "\"}")
            .on(new ResponseAdapter<KvMetadata>() {
                @Override
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvMetadata object) {
                    queue.add(object);
                }
            });

    final KvMetadata patched = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotNull(patched);

    final KvObject<ObjectNode> kvObject = client.kv(patched.getCollection(), patched.getKey())
            .get(ObjectNode.class).get();

    assertEquals(patched.getRef(), kvObject.getRef());
    assertEquals(name, kvObject.getValue().get("name").asText());
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void patchKeyAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    final KvMetadata kvMetadata = insertItem(key, "{}");

    String name = Long.toHexString(RAND.nextLong());

    final BlockingQueue<KvMetadata> queue = DataStructures.getLTQInstance(KvMetadata.class);
    client.kv(collection(), key).patch(JsonPatch.builder().add("name", name).build())
            .on(new ResponseAdapter<KvMetadata>() {
                @Override/*  w ww. j  a v a 2 s.c o  m*/
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvMetadata object) {
                    queue.add(object);
                }
            });

    final KvMetadata patched = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotEquals(kvMetadata, patched);

    final KvObject<ObjectNode> kvObject = client.kv(kvMetadata.getCollection(), kvMetadata.getKey())
            .get(ObjectNode.class).get();

    assertNotNull(kvMetadata);
    assertNotNull(kvObject);
    assertEquals(kvMetadata.getCollection(), kvObject.getCollection());
    assertEquals(kvMetadata.getKey(), kvObject.getKey());
    assertEquals(patched.getRef(), kvObject.getRef());
    assertEquals(name, kvObject.getValue().get("name").asText());
}

From source file:com.netflix.curator.framework.recipes.leader.TestLeaderSelector.java

@SuppressWarnings({ "ForLoopReplaceableByForEach" })
@Test//from  www  .ja  v  a 2 s  . c o m
public void testRotatingLeadership() throws Exception {
    final int LEADER_QTY = 5;
    final int REPEAT_QTY = 3;

    final Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(),
            timing.connection(), new RetryOneTime(1));
    client.start();
    try {
        final BlockingQueue<Integer> leaderList = new LinkedBlockingQueue<Integer>();
        List<LeaderSelector> selectors = Lists.newArrayList();
        for (int i = 0; i < LEADER_QTY; ++i) {
            final int ourIndex = i;
            LeaderSelector leaderSelector = new LeaderSelector(client, PATH_NAME, new LeaderSelectorListener() {
                @Override
                public void takeLeadership(CuratorFramework client) throws Exception {
                    timing.sleepABit();
                    leaderList.add(ourIndex);
                }

                @Override
                public void stateChanged(CuratorFramework client, ConnectionState newState) {
                }
            });
            selectors.add(leaderSelector);
        }

        List<Integer> localLeaderList = Lists.newArrayList();
        for (int i = 1; i <= REPEAT_QTY; ++i) {
            for (LeaderSelector leaderSelector : selectors) {
                if (i > 1) {
                    leaderSelector.requeue();
                } else {
                    leaderSelector.start();
                }
            }

            while (localLeaderList.size() != (i * selectors.size())) {
                Integer polledIndex = leaderList.poll(10, TimeUnit.SECONDS);
                Assert.assertNotNull(polledIndex);
                localLeaderList.add(polledIndex);
            }
            timing.sleepABit();
        }

        for (LeaderSelector leaderSelector : selectors) {
            leaderSelector.close();
        }
        System.out.println(localLeaderList);

        for (int i = 0; i < REPEAT_QTY; ++i) {
            Set<Integer> uniques = Sets.newHashSet();
            for (int j = 0; j < selectors.size(); ++j) {
                Assert.assertTrue(localLeaderList.size() > 0);

                int thisIndex = localLeaderList.remove(0);
                Assert.assertFalse(uniques.contains(thisIndex));
                uniques.add(thisIndex);
            }
        }
    } finally {
        client.close();
    }
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void upsertPatchKeyAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    KvObject<ObjectNode> kvBefore = client.kv(collection(), key).get(ObjectNode.class).get();

    // assert the object is not present before the patch
    assertNull(kvBefore);//from  w ww . j a  v a 2s .c  o m

    String name = Long.toHexString(RAND.nextLong());

    final BlockingQueue<KvMetadata> queue = DataStructures.getLTQInstance(KvMetadata.class);
    client.kv(collection(), key).upsert().patch(JsonPatch.builder().add("name", name).build())
            .on(new ResponseAdapter<KvMetadata>() {
                @Override
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvMetadata object) {
                    queue.add(object);
                }
            });

    final KvMetadata patched = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotNull(patched);

    final KvObject<ObjectNode> kvObject = client.kv(patched.getCollection(), patched.getKey())
            .get(ObjectNode.class).get();

    assertNotNull(kvObject);
    assertEquals(patched.getCollection(), kvObject.getCollection());
    assertEquals(patched.getKey(), kvObject.getKey());
    assertEquals(patched.getRef(), kvObject.getRef());
    assertEquals(name, kvObject.getValue().get("name").asText());
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void putKeyIfMatchAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    final String collection = collection();
    final KvMetadata kvMetadata = client.kv(collection, key).put("{}").get();

    final BlockingQueue<KvMetadata> queue = DataStructures.getLTQInstance(KvMetadata.class);
    client.kv(kvMetadata.getCollection(), kvMetadata.getKey()).ifMatch(kvMetadata.getRef()).put("{}")
            .on(new ResponseAdapter<KvMetadata>() {
                @Override//from  w w  w  . j  a va2 s .co m
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvMetadata object) {
                    queue.add(object);
                }
            });

    final KvMetadata kvMetadata2 = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotNull(kvMetadata);
    assertNotNull(kvMetadata2);
    assertEquals(collection, kvMetadata2.getCollection());
    assertEquals(key, kvMetadata2.getKey());
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void conditionalMergePatchKeyAsync(@ForAll(sampleSize = 10) final String key)
        throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    String name1 = Long.toHexString(RAND.nextLong());

    final KvMetadata kvMetadata = insertItem(key, "{`name1`:`%s`}", name1);

    String name2 = Long.toHexString(RAND.nextLong());

    final BlockingQueue<KvMetadata> queue = DataStructures.getLTQInstance(KvMetadata.class);

    client.kv(collection(), key).ifMatch(kvMetadata.getRef()).merge("{\"name2\":\"" + name2 + "\"}")
            .on(new ResponseAdapter<KvMetadata>() {
                @Override/*from  www  .ja v  a 2s .  co  m*/
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvMetadata object) {
                    queue.add(object);
                }
            });

    final KvMetadata patched = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotEquals(kvMetadata, patched);

    final KvObject<ObjectNode> kvObject = client.kv(kvMetadata.getCollection(), kvMetadata.getKey())
            .get(ObjectNode.class).get();

    assertEquals(patched.getRef(), kvObject.getRef());
    assertEquals(name1, kvObject.getValue().get("name1").asText());
    assertEquals(name2, kvObject.getValue().get("name2").asText());
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void conditionalPatchKeyAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    final KvMetadata kvMetadata = insertItem(key, "{}");

    String name = Long.toHexString(RAND.nextLong());

    final BlockingQueue<KvMetadata> queue = DataStructures.getLTQInstance(KvMetadata.class);
    client.kv(collection(), key).ifMatch(kvMetadata.getRef())
            .patch(JsonPatch.builder().add("name", name).build()).on(new ResponseAdapter<KvMetadata>() {
                @Override/*w  w  w  .j  a  va 2s. c o  m*/
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvMetadata object) {
                    queue.add(object);
                }
            });

    final KvMetadata patched = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotEquals(kvMetadata, patched);

    final KvObject<ObjectNode> kvObject = client.kv(kvMetadata.getCollection(), kvMetadata.getKey())
            .get(ObjectNode.class).get();

    assertNotNull(kvMetadata);
    assertNotNull(kvObject);
    assertEquals(kvMetadata.getCollection(), kvObject.getCollection());
    assertEquals(kvMetadata.getKey(), kvObject.getKey());
    assertEquals(patched.getRef(), kvObject.getRef());
    assertEquals(name, kvObject.getValue().get("name").asText());
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void getKeyAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    final KvMetadata kvMetadata = insertItem(key, "{}");

    final BlockingQueue<KvObject> queue = DataStructures.getLTQInstance(KvObject.class);
    client.kv(kvMetadata.getCollection(), kvMetadata.getKey()).get(String.class)
            .on(new ResponseAdapter<KvObject<String>>() {
                @Override//from  ww w .  ja  va  2s .  com
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvObject<String> object) {
                    queue.add(object);
                }
            });

    @SuppressWarnings("unchecked")
    final KvObject<String> object = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotNull(kvMetadata);
    assertNotNull(object);
    assertEquals(kvMetadata.getCollection(), object.getCollection());
    assertEquals(kvMetadata.getKey(), object.getKey());
    assertEquals(kvMetadata.getRef(), object.getRef());
    assertEquals("{}", object.getValue());
}