org.apache.ignite.IgniteQueue - java examples

Here are the examples of the java api org.apache.ignite.IgniteQueue taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

65 Examples 7

19 View Complete Implementation : IgniteQueueExample.java
Copyright Apache License 2.0
Author : apache
/**
 * Ignite cache distributed queue example. This example demonstrates {@code FIFO} unbounded
 * cache queue.
 * <p>
 * Remote nodes should always be started with special configuration file which
 * enables P2P clreplaced loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}.
 * <p>
 * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will
 * start node with {@code examples/config/example-ignite.xml} configuration.
 */
public clreplaced IgniteQueueExample {

    /**
     * Number of retries
     */
    private static final int RETRIES = 20;

    /**
     * Queue instance.
     */
    private static IgniteQueue<String> queue;

    /**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws Exception If example execution failed.
     */
    public static void main(String[] args) throws Exception {
        try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
            System.out.println();
            System.out.println(">>> Ignite queue example started.");
            // Make queue name.
            String queueName = UUID.randomUUID().toString();
            queue = initializeQueue(ignite, queueName);
            readFromQueue(ignite);
            writeToQueue(ignite);
            clearAndRemoveQueue();
        }
        System.out.println("Cache queue example finished.");
    }

    /**
     * Initialize queue.
     *
     * @param ignite Ignite.
     * @param queueName Name of queue.
     * @return Queue.
     * @throws IgniteException If execution failed.
     */
    private static IgniteQueue<String> initializeQueue(Ignite ignite, String queueName) throws IgniteException {
        CollectionConfiguration colCfg = new CollectionConfiguration();
        colCfg.setCacheMode(PARreplacedIONED);
        // Initialize new FIFO queue.
        IgniteQueue<String> queue = ignite.queue(queueName, 0, colCfg);
        // Initialize queue items.
        // We will be use blocking operation and queue size must be appropriated.
        for (int i = 0; i < ignite.cluster().nodes().size() * RETRIES * 2; i++) queue.put(Integer.toString(i));
        System.out.println("Queue size after initializing: " + queue.size());
        return queue;
    }

    /**
     * Read items from head and tail of queue.
     *
     * @param ignite Ignite.
     * @throws IgniteException If failed.
     */
    private static void readFromQueue(Ignite ignite) throws IgniteException {
        final String queueName = queue.name();
        // Read queue items on each node.
        ignite.compute().broadcast(new QueueClosure(queueName, false));
        System.out.println("Queue size after reading [expected=0, actual=" + queue.size() + ']');
    }

    /**
     * Write items into queue.
     *
     * @param ignite Ignite.
     * @throws IgniteException If failed.
     */
    private static void writeToQueue(Ignite ignite) throws IgniteException {
        final String queueName = queue.name();
        // Write queue items on each node.
        ignite.compute().broadcast(new QueueClosure(queueName, true));
        System.out.println("Queue size after writing [expected=" + ignite.cluster().nodes().size() * RETRIES + ", actual=" + queue.size() + ']');
        System.out.println("Iterate over queue.");
        // Iterate over queue.
        for (String item : queue) System.out.println("Queue item: " + item);
    }

    /**
     * Clear and remove queue.
     *
     * @throws IgniteException If execution failed.
     */
    private static void clearAndRemoveQueue() throws IgniteException {
        System.out.println("Queue size before clearing: " + queue.size());
        // Clear queue.
        queue.clear();
        System.out.println("Queue size after clearing: " + queue.size());
        // Remove queue.
        queue.close();
        // Try to work with removed queue.
        try {
            queue.poll();
        } catch (IllegalStateException expected) {
            System.out.println("Expected exception - " + expected.getMessage());
        }
    }

    /**
     * Closure to populate or poll the queue.
     */
    private static clreplaced QueueClosure implements IgniteRunnable {

        /**
         * Queue name.
         */
        private final String queueName;

        /**
         * Flag indicating whether to put or poll.
         */
        private final boolean put;

        /**
         * @param queueName Queue name.
         * @param put Flag indicating whether to put or poll.
         */
        QueueClosure(String queueName, boolean put) {
            this.queueName = queueName;
            this.put = put;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void run() {
            IgniteQueue<String> queue = Ignition.ignite().queue(queueName, 0, null);
            if (put) {
                UUID locId = Ignition.ignite().cluster().localNode().id();
                for (int i = 0; i < RETRIES; i++) {
                    String item = locId + "_" + Integer.toString(i);
                    queue.put(item);
                    System.out.println("Queue item has been added: " + item);
                }
            } else {
                // Take items from queue head.
                for (int i = 0; i < RETRIES; i++) System.out.println("Queue item has been read from queue head: " + queue.take());
                // Take items from queue head once again.
                for (int i = 0; i < RETRIES; i++) System.out.println("Queue item has been read from queue head: " + queue.poll());
            }
        }
    }
}

19 View Complete Implementation : AlertReceiver.java
Copyright Apache License 2.0
Author : Symantec
/**
 * @param ruleId
 */
public void openChannel(Short ruleId) {
    if (ruleId != null) {
        String ruleIdStr = String.valueOf(ruleId);
        if (!channelCache.containsKey(ruleIdStr)) {
            IgniteQueue<Map<String, Object>> queue = ignite.queue(ruleIdStr, channelSize, colCfg);
            if (queue != null) {
                channelCache.put(ruleIdStr, 1);
                logger.info("Adding channel for :" + ruleId);
            } else {
                logger.info("Failed to create channel for rule:" + ruleId);
            }
        } else {
            Integer result = channelCache.get(ruleIdStr);
            channelCache.put(ruleIdStr, result + 1);
            logger.info("Channel for rule:" + ruleId + " is already open");
        }
    }
}

18 View Complete Implementation : AlertReceiver.java
Copyright Apache License 2.0
Author : Symantec
/**
 * @param ruleId
 */
public void closeChannel(short ruleId) {
    String ruleIdStr = String.valueOf(ruleId);
    if (channelCache.containsKey(ruleIdStr)) {
        Integer val = channelCache.get(ruleIdStr);
        if (val > 1) {
            channelCache.put(ruleIdStr, val - 1);
        } else {
            channelCache.remove(ruleIdStr);
            IgniteQueue<Map<String, Object>> queue = ignite.queue(ruleIdStr, channelSize, colCfg);
            queue.close();
        }
    } else {
    // do nothing
    }
}

18 View Complete Implementation : IgniteCollectionAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @param queue Ignite queue.
 * @return Cache context.
 */
protected static GridCacheContext cctx(IgniteQueue queue) {
    return GridTestUtils.getFieldValue(queue, "cctx");
}

18 View Complete Implementation : GridCachePartitionedQueueEntryMoveSelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * Start additional nodes above {@link #GRID_CNT}.
 *
 * @param cnt Number of additional nodes.
 * @param queueName Queue name.
 * @throws Exception If failed.
 */
private void startAdditionalNodes(int cnt, String queueName) throws Exception {
    IgniteQueue queue = ignite(0).queue(queueName, 0, null);
    CacheConfiguration cCfg = getQueueCache(queue);
    Collection<ClusterNode> aff1 = ignite(0).affinity(cCfg.getName()).mapKeyToPrimaryAndBackups(queueName);
    for (int i = 0, id = GRID_CNT; i < cnt; i++) {
        startGrid(id++);
        awaitParreplacedionMapExchange();
        Collection<ClusterNode> aff2 = ignite(0).affinity(cCfg.getName()).mapKeyToPrimaryAndBackups(queueName);
        if (!aff1.iterator().next().equals(aff2.iterator().next())) {
            info("Moved queue to new primary node [oldAff=" + aff1 + ", newAff=" + aff2 + ']');
            return;
        }
    }
    throw new IgniteCheckedException("Unable to move the queue to a new primary node");
}

17 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testPutRemoveMulreplacedhreadedUnbounded() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();
    final IgniteQueue<String> queue = initQueue(0, queueName, 0, config(false));
    mulreplacedhreaded(new Callable<String>() {

        @Override
        public String call() throws Exception {
            String thread = Thread.currentThread().getName();
            for (int i = 0; i < QUEUE_CAPACITY; i++) queue.put(thread);
            info("Finished loop 1: " + thread);
            queue.clear();
            info("Cleared queue 1: " + thread);
            return "";
        }
    }, THREAD_NUM);
    replacedert queue.isEmpty() : "Queue must be empty. " + queue.size();
}

17 View Complete Implementation : GridCacheQueueMultiNodeAbstractSelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void testPut() throws Exception {
    String queueName = UUID.randomUUID().toString();
    IgniteQueue<Integer> queue = grid(0).queue(queueName, QUEUE_CAPACITY, config(false));
    replacedertTrue(queue.isEmpty());
    grid(0).compute().broadcast(new PutJob(queueName, RETRIES));
    replacedertEquals(GRID_CNT * RETRIES, queue.size());
}

17 View Complete Implementation : AlertReceiver.java
Copyright Apache License 2.0
Author : Symantec
/**
 * @param ruleId
 * @param event
 * @return
 */
public boolean publishEvent(short ruleId, Map<String, Object> event) {
    if (channelCache.containsKey(String.valueOf(ruleId))) {
        IgniteQueue<Object> channel = ignite.queue(String.valueOf(ruleId), channelSize, colCfg);
        if (channel != null) {
            if (channel.size() >= channelSize) {
                // evict event
                channel.take();
            }
            channel.put(event);
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

17 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void testReuseCache() throws Exception {
    CollectionConfiguration colCfg = collectionConfiguration();
    IgniteQueue queue1 = initQueue(0, "Queue1", 0, colCfg);
    IgniteQueue queue2 = initQueue(0, "Queue2", 0, colCfg);
    replacedertEquals(getQueueCache(queue1), getQueueCache(queue2));
}

17 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * Intialize Ignite queue
 */
protected <T> IgniteQueue<T> initQueue(int idx, String name, int cap, @Nullable CollectionConfiguration cfg) {
    IgniteQueue<T> queue = grid(idx).queue(name, cap, cfg);
    if (queue != null && BINARY_QUEUE_MODE)
        return queue.withKeepBinary();
    return queue;
}

16 View Complete Implementation : GridCacheAbstractQueueFailoverDataConsistencySelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @param collocated Collocation flag.
 * @throws Exception If failed.
 */
private void testPollFailover(boolean collocated) throws Exception {
    CollectionConfiguration colCfg = config(collocated);
    IgniteQueue<Integer> queue = grid(0).queue(QUEUE_NAME, 0, colCfg);
    replacedertNotNull(queue);
    replacedertEquals(0, queue.size());
    int primaryNode = primaryQueueNode(queue);
    int testNodeIdx = -1;
    for (int i = 0; i < gridCount(); i++) {
        if (i != primaryNode)
            testNodeIdx = i;
    }
    log.info("Test node: " + testNodeIdx);
    log.info("Primary node: " + primaryNode);
    queue = grid(testNodeIdx).queue(QUEUE_NAME, 0, null);
    replacedertNotNull(queue);
    // Kill queue header's primary node .
    testPollFailover(queue, Arrays.asList(primaryQueueNode(queue)));
    List<Integer> killIdxs = new ArrayList<>();
    for (int i = 0; i < gridCount(); i++) {
        if (i != testNodeIdx)
            killIdxs.add(i);
    }
    // Kill random node.
    testPollFailover(queue, killIdxs);
}

16 View Complete Implementation : GridCacheAbstractQueueFailoverDataConsistencySelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @param queue Queue.
 * @return Primary node for queue's header.
 * @throws Exception If failed.
 */
private int primaryQueueNode(IgniteQueue queue) throws Exception {
    GridCacheContext cctx = GridTestUtils.getFieldValue(queue, "cctx");
    GridCacheAffinityManager aff = cctx.affinity();
    CachePeekMode[] modes = new CachePeekMode[] { CachePeekMode.ALL };
    for (int i = 0; i < gridCount(); i++) {
        for (Cache.Entry e : grid(i).context().cache().internalCache(cctx.name()).localEntries(modes)) {
            Object key = e.getKey();
            if (aff.primaryByKey(grid(i).localNode(), key, AffinityTopologyVersion.NONE) && key instanceof GridCacheQueueHeaderKey)
                return i;
        }
    }
    fail("Failed to find primary node for queue header.");
    return -1;
}

16 View Complete Implementation : GridCacheAbstractQueueFailoverDataConsistencySelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @param collocated Collocation flag.
 * @throws Exception If failed.
 */
private void testAddFailover(boolean collocated) throws Exception {
    CollectionConfiguration colCfg = config(collocated);
    IgniteQueue<Integer> queue = grid(0).queue(QUEUE_NAME, 0, colCfg);
    replacedertNotNull(queue);
    replacedertEquals(0, queue.size());
    int primaryNode = primaryQueueNode(queue);
    int testNodeIdx = -1;
    for (int i = 0; i < gridCount(); i++) {
        if (i != primaryNode)
            testNodeIdx = i;
    }
    log.info("Test node: " + testNodeIdx);
    log.info("Header primary node: " + primaryNode);
    queue = grid(testNodeIdx).queue(QUEUE_NAME, 0, null);
    replacedertNotNull(queue);
    // Kill queue header's primary node .
    testAddFailover(queue, Arrays.asList(primaryNode));
    List<Integer> killIdxs = new ArrayList<>();
    for (int i = 0; i < gridCount(); i++) {
        if (i != testNodeIdx)
            killIdxs.add(i);
    }
    // Kill random node.
    testAddFailover(queue, killIdxs);
}

16 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void testSystemCache() throws Exception {
    CollectionConfiguration colCfg = collectionConfiguration();
    IgniteQueue queue = initQueue(0, "Queue1", 0, colCfg);
    final CacheConfiguration ccfg = getQueueCache(queue);
    GridTestUtils.replacedertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            grid(0).cache(ccfg.getName());
            return null;
        }
    }, IllegalStateException.clreplaced, "Failed to get cache because it is a system cache");
    replacedertNotNull(((IgniteKernal) grid(0)).internalCache(ccfg.getName()));
}

16 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testAddPeekUnbounded() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();
    IgniteQueue<String> queue = initQueue(0, queueName, 0, config(false));
    String item1 = "1";
    replacedert queue.add(item1);
    String item2 = "2";
    replacedert queue.add(item2);
    String item3 = "3";
    replacedert queue.add(item3);
    replacedert item1.equals(queue.peek());
    replacedert item1.equals(queue.peek());
    replacedert !item2.equals(queue.peek());
}

16 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testPutGetMulreplacedhreadBounded() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();
    final IgniteQueue<String> queue = initQueue(0, queueName, QUEUE_CAPACITY, config(false));
    mulreplacedhreaded(new Callable<String>() {

        @Override
        public String call() throws Exception {
            String thName = Thread.currentThread().getName();
            for (int i = 0; i < QUEUE_CAPACITY * 5; i++) {
                queue.put(thName);
                queue.peek();
                queue.take();
            }
            return "";
        }
    }, THREAD_NUM);
    replacedert queue.isEmpty() : queue.size();
}

16 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testAddUnbounded() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();
    String val = UUID.randomUUID().toString();
    IgniteQueue<String> queue = initQueue(0, queueName, 0, config(false));
    replacedert queue.add(val);
    replacedert val.equals(queue.poll());
}

16 View Complete Implementation : IgniteQueueExample.java
Copyright Apache License 2.0
Author : apache
/**
 * Initialize queue.
 *
 * @param ignite Ignite.
 * @param queueName Name of queue.
 * @return Queue.
 * @throws IgniteException If execution failed.
 */
private static IgniteQueue<String> initializeQueue(Ignite ignite, String queueName) throws IgniteException {
    CollectionConfiguration colCfg = new CollectionConfiguration();
    colCfg.setCacheMode(PARreplacedIONED);
    // Initialize new FIFO queue.
    IgniteQueue<String> queue = ignite.queue(queueName, 0, colCfg);
    // Initialize queue items.
    // We will be use blocking operation and queue size must be appropriated.
    for (int i = 0; i < ignite.cluster().nodes().size() * RETRIES * 2; i++) queue.put(Integer.toString(i));
    System.out.println("Queue size after initializing: " + queue.size());
    return queue;
}

16 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testAddPollUnbounded() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();
    IgniteQueue<String> queue = initQueue(0, queueName, 0, config(false));
    replacedert queue.add("1");
    replacedert queue.add("2");
    replacedert queue.add("3");
    replacedertEquals("1", queue.poll());
    replacedertEquals("2", queue.poll());
    replacedertEquals("3", queue.poll());
}

16 View Complete Implementation : IgniteClientReconnectCollectionsTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @param colCfg Collection configuration.
 * @throws Exception If failed.
 */
private void serverNodeReconnect(CollectionConfiguration colCfg) throws Exception {
    final Ignite client = grid(serverCount());
    final Ignite srv = ignite(0);
    replacedertNotNull(srv.queue("q", 0, colCfg));
    replacedertNotNull(srv.set("s", colCfg));
    reconnectClientNode(client, srv, null);
    IgniteQueue<Object> q = client.queue("q", 0, null);
    replacedertNotNull(q);
}

16 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testPutGetMulreplacedhreadUnbounded() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();
    final IgniteQueue<String> queue = initQueue(0, queueName, QUEUE_CAPACITY, config(false));
    mulreplacedhreaded(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            String thName = Thread.currentThread().getName();
            for (int i = 0; i < 5; i++) {
                queue.put(thName);
                queue.peek();
                queue.take();
            }
            return null;
        }
    }, THREAD_NUM);
    replacedert queue.isEmpty() : queue.size();
}

16 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void testNotReuseCache() throws Exception {
    CollectionConfiguration colCfg1 = collectionConfiguration();
    CollectionConfiguration colCfg2 = collectionConfiguration();
    if (colCfg2.getAtomicityMode() == ATOMIC)
        colCfg2.setAtomicityMode(TRANSACTIONAL);
    else
        colCfg2.setAtomicityMode(ATOMIC);
    IgniteQueue queue1 = initQueue(0, "Queue1", 0, colCfg1);
    IgniteQueue queue2 = initQueue(0, "Queue2", 0, colCfg2);
    replacedertNotSame(getQueueCache(queue1), getQueueCache(queue2));
}

16 View Complete Implementation : GridCacheQueueCleanupSelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void testCleanup() throws Exception {
    IgniteQueue<Integer> queue = grid(0).queue(QUEUE_NAME1, 0, config(false));
    GridCacheContext cctx = GridTestUtils.getFieldValue(queue, "cctx");
    final String queueCacheName = cctx.name();
    ClusterNode node = grid(0).affinity(queueCacheName).mapKeyToNode(new GridCacheQueueHeaderKey(QUEUE_NAME1));
    final Ignite ignite = grid(0).localNode().equals(node) ? grid(1) : grid(0);
    /*
        replacedertNotNull(queue);

        // Add/poll some items.

        for (int i = 0; i < 500; i++)
            queue.add(i);

        for (int i = 0; i < 10; i++)
            queue.poll();

        replacedertTrue(!queue.isEmpty());

        // Kill node containing queue header.

        final String killGridName = node.attribute(IgniteNodeAttributes.ATTR_IGNITE_INSTANCE_NAME);

        stopGrid(killGridName);

        replacedertNull(((IgniteKernal)grid).cache(DEFAULT_CACHE_NAME).dataStructures().queue(QUEUE_NAME1, 0, false, false));

        final AtomicBoolean stop = new AtomicBoolean(false);

        GridFuture<?> fut1;
        GridFuture<?> fut2;

        try {
            // Start threads using cache concurrently with cleanup thread.
            fut1 = startAddPollThread(grid, stop, QUEUE_NAME1);
            fut2 = startAddPollThread(grid, stop, QUEUE_NAME2);

            U.sleep(3000); // Give some time for cleanup thread.
        }
        finally {
            stop.set(true);
        }

        fut1.get();
        fut2.get();

        ((IgniteKernal)grid).cache(DEFAULT_CACHE_NAME).dataStructures().removeQueue(QUEUE_NAME1);
        ((IgniteKernal)grid).cache(DEFAULT_CACHE_NAME).dataStructures().removeQueue(QUEUE_NAME2);

        replacedertTrue(GridTestUtils.waitForCondition(new PAX() {
            @Override public boolean applyx() {
                for (int i = 0; i < gridCount(); i++) {
                    if (getTestIgniteInstanceName(i).equals(killGridName))
                        continue;

                    Iterator<GridCacheEntryEx<Object, Object>> entries =
                        ((GridKernal)grid(i)).context().cache().internalCache(DEFAULT_CACHE_NAME).map().allEntries0().iterator();

                    if (entries.hasNext()) {
                        log.info("Found cache entries, will wait: " + entries.next());

                        return false;
                    }
                }

                return true;
            }
        }, 5000));

        startGrid(killGridName);

        // Create queue again.
        queue = ((IgniteKernal)grid).cache(DEFAULT_CACHE_NAME).dataStructures().queue(QUEUE_NAME1, 0, false, true);
        */
    replacedertEquals(0, queue.size());
    for (int i = 0; i < 500; i++) queue.add(i);
    replacedertEquals(500, queue.size());
    // Remove queue and create queue with the same name.
    queue.close();
    queue = ignite.queue(QUEUE_NAME1, 0, config(false));
    replacedertEquals(0, queue.size());
    for (int i = 0; i < 500; i++) queue.add(i);
    replacedertEquals(500, queue.size());
    // Check that items of removed queue are removed, items of new queue not.
    replacedertTrue(GridTestUtils.waitForCondition(new PAX() {

        @Override
        public boolean applyx() throws IgniteCheckedException {
            int cnt = 0;
            for (int i = 0; i < gridCount(); i++) {
                GridCacheAdapter<Object, Object> cache = grid(i).context().cache().internalCache(queueCacheName);
                for (Object e : cache.localEntries(new CachePeekMode[] { CachePeekMode.ALL })) cnt++;
            }
            if (cnt > 501) {
                // 500 items + header.
                log.info("Found more cache entries than expected, will wait: " + cnt);
                return false;
            }
            return true;
        }
    }, 5000));
    for (int i = 0; i < 500; i++) replacedertEquals((Integer) i, queue.poll());
}

16 View Complete Implementation : IgniteClientReconnectCollectionsTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @param colCfg Collection configuration.
 * @throws Exception If failed.
 */
private void queueReconnectInProgress(final CollectionConfiguration colCfg) throws Exception {
    Ignite client = grid(serverCount());
    replacedertTrue(client.cluster().localNode().isClient());
    Ignite srv = ignite(0);
    final String setName = "queue-rmv" + colCfg.getAtomicityMode();
    final IgniteQueue<String> clientQueue = client.queue(setName, 10, colCfg);
    final IgniteQueue<String> srvQueue = srv.queue(setName, 10, null);
    replacedertTrue(clientQueue.offer("1"));
    replacedertTrue(srvQueue.contains("1"));
    BlockTcpCommunicationSpi commSpi = commSpi(srv);
    if (colCfg.getAtomicityMode() == ATOMIC)
        commSpi.blockMessage(GridNearAtomicUpdateResponse.clreplaced);
    else
        commSpi.blockMessage(GridNearTxPrepareResponse.clreplaced);
    final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                clientQueue.add("2");
            } catch (IgniteClientDisconnectedException e) {
                checkAndWait(e);
                return true;
            }
            return false;
        }
    });
    // Check that client waiting operation.
    GridTestUtils.replacedertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            return fut.get(200);
        }
    }, IgniteFutureTimeoutCheckedException.clreplaced, null);
    replacedertNotDone(fut);
    commSpi.unblockMessage();
    reconnectClientNode(client, srv, null);
    replacedertTrue("Future was not failed. Atomic mode: " + colCfg.getAtomicityMode() + ".", (Boolean) fut.get());
    replacedertTrue(clientQueue.add("3"));
    replacedertEquals("1", clientQueue.poll());
}

16 View Complete Implementation : GridCacheQueueJoinedNodeSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void testTakeFromJoined() throws Exception {
    String queueName = UUID.randomUUID().toString();
    IgniteQueue<Integer> queue = grid(0).queue(queueName, 0, config(true));
    replacedertNotNull(queue);
    replacedertTrue(queue.isEmpty());
    PutJob putJob = new PutJob(queueName);
    IgniteCompute comp = compute(grid(0).cluster().forLocal());
    IgniteFuture<?> fut = comp.runAsync(putJob);
    Collection<IgniteFuture<?>> futs = new ArrayList<>(GRID_CNT - 1);
    Collection<TakeJob> jobs = new ArrayList<>(GRID_CNT - 1);
    int itemsLeft = ITEMS_CNT;
    for (int i = 1; i < GRID_CNT; i++) {
        int cnt = ITEMS_CNT / (GRID_CNT - 1);
        TakeJob job = new TakeJob(queueName, cnt, 10);
        jobs.add(job);
        comp = compute(grid(i).cluster().forLocal());
        futs.add(comp.callAsync(job));
        itemsLeft -= cnt;
    }
    replacedertEquals("Not all items will be polled", 0, itemsLeft);
    // Wait for half of items to be polled.
    for (TakeJob job : jobs) job.awaireplacedems();
    log.info("Start one more grid.");
    Ignite joined = startGrid(GRID_CNT);
    // We expect at least one item to be taken.
    TakeJob joinedJob = new TakeJob(queueName, 1, 1);
    jobs.add(joinedJob);
    Integer polled = forLocal(joined).call(joinedJob);
    replacedertNotNull("Joined node should poll item", polled);
    info(">>> Joined node polled " + polled);
    for (IgniteFuture<?> f : futs) f.cancel();
    putJob.stop(true);
    fut.get();
    for (TakeJob job : jobs) job.awaitDone();
}

16 View Complete Implementation : GridCacheQueueMultiNodeAbstractSelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void testPutTake() throws Exception {
    String queueName = UUID.randomUUID().toString();
    info("Queue name: " + queueName);
    IgniteQueue<Integer> queue = grid(0).queue(queueName, QUEUE_CAPACITY, config(false));
    replacedertTrue(queue.isEmpty());
    grid(0).compute().broadcast(new PutTakeJob(queueName, RETRIES));
    replacedertEquals(0, queue.size());
    queue.close();
}

16 View Complete Implementation : IgniteClientReconnectCollectionsTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @param colCfg Collection configuration.
 * @throws Exception If failed.
 */
private void queueReconnectRemoved(CollectionConfiguration colCfg) throws Exception {
    Ignite client = grid(serverCount());
    replacedertTrue(client.cluster().localNode().isClient());
    Ignite srv = ignite(0);
    final String setName = "queue-rmv" + colCfg.getAtomicityMode();
    final IgniteQueue<String> clientQueue = client.queue(setName, 10, colCfg);
    final IgniteQueue<String> srvQueue = srv.queue(setName, 10, null);
    replacedertTrue(clientQueue.add("1"));
    replacedertTrue(srvQueue.add("2"));
    reconnectClientNode(client, srv, new Runnable() {

        @Override
        public void run() {
            srvQueue.close();
        }
    });
    GridTestUtils.replacedertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            clientQueue.add("fail");
            return null;
        }
    }, IllegalStateException.clreplaced, null);
    IgniteQueue<String> newClientQueue = client.queue(setName, 10, colCfg);
    IgniteQueue<String> newSrvQueue = srv.queue(setName, 10, null);
    replacedertTrue(newClientQueue.add("1"));
    replacedertTrue(newSrvQueue.add("2"));
}

16 View Complete Implementation : IgniteClientReconnectCollectionsTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @param colCfg Collection configuration.
 * @throws Exception If failed.
 */
private void queueReconnect(CollectionConfiguration colCfg) throws Exception {
    Ignite client = grid(serverCount());
    replacedertTrue(client.cluster().localNode().isClient());
    Ignite srv = ignite(0);
    final String setName = "queue-" + colCfg.getAtomicityMode();
    IgniteQueue<String> clientQueue = client.queue(setName, 10, colCfg);
    final IgniteQueue<String> srvQueue = srv.queue(setName, 10, null);
    replacedertTrue(clientQueue.offer("1"));
    replacedertTrue(srvQueue.contains("1"));
    reconnectClientNode(client, srv, new Runnable() {

        @Override
        public void run() {
            replacedertTrue(srvQueue.add("2"));
        }
    });
    replacedertTrue(clientQueue.contains("2"));
    replacedertEquals("1", clientQueue.poll());
}

15 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testRemovePeek() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();
    IgniteQueue<String> queue = initQueue(0, queueName, 0, config(false));
    for (int i = 0; i < 5; i++) queue.put("Item-" + i);
    queue.remove("Item-1");
    replacedertEquals("Item-0", queue.peek());
    queue.remove("Item-2");
    replacedertEquals("Item-0", queue.peek());
    queue.remove("Item-0");
    replacedertEquals("Item-3", queue.peek());
    queue.remove("Item-4");
    replacedertEquals("Item-3", queue.peek());
    queue.remove("Item-3");
    replacedertNull(queue.peek());
}

15 View Complete Implementation : GridCacheQueueClientDisconnectTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void testClientDisconnect() throws Exception {
    try {
        Ignite server = startGrid(0);
        clientMode = true;
        Ignite client = startGrid(1);
        awaitParreplacedionMapExchange();
        final IgniteQueue queue = client.queue(IGNITE_QUEUE_NAME, 0, collectionConfiguration(CacheAtomicityMode.ATOMIC));
        final CountDownLatch latch = new CountDownLatch(1);
        GridTestUtils.runAsync(new Runnable() {

            @Override
            public void run() {
                try {
                    Object value = queue.take();
                } catch (IgniteClientDisconnectedException icd) {
                    latch.countDown();
                } catch (Exception e) {
                }
            }
        });
        U.sleep(5000);
        server.close();
        boolean countReachedZero = latch.await(FAILURE_DETECTION_TIMEOUT * 2, TimeUnit.MILLISECONDS);
        replacedertTrue("IgniteClientDisconnectedException was not thrown", countReachedZero);
    } finally {
        stopAllGrids();
    }
}

15 View Complete Implementation : GridCacheQueueMultiNodeAbstractSelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void testAddAll() throws Exception {
    try {
        String queueName = UUID.randomUUID().toString();
        info("Queue name: " + queueName);
        IgniteQueue<Integer> queue = grid(0).queue(queueName, QUEUE_CAPACITY, config(false));
        replacedertTrue(queue.isEmpty());
        grid(0).compute().call(new AddAllJob(queueName, RETRIES));
        replacedertEquals(GRID_CNT * RETRIES, queue.size());
        queue.clear(5);
        replacedertEquals(0, queue.size());
        queue.close();
    } catch (Throwable t) {
        error("Failure in test: " + t);
    }
}

15 View Complete Implementation : GridCacheQueueMultiNodeAbstractSelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void tesreplacederator() throws Exception {
    final String queueName = UUID.randomUUID().toString();
    info("Queue name: " + queueName);
    try (IgniteQueue<Integer> queue = grid(0).queue(queueName, QUEUE_CAPACITY, config(false))) {
        replacedertTrue(queue.isEmpty());
        grid(0).compute().broadcast(new AddAllJob(queueName, RETRIES));
        replacedertEquals(GRID_CNT * RETRIES, queue.size());
        Collection<ClusterNode> nodes = grid(0).cluster().nodes();
        for (ClusterNode node : nodes) {
            Collection<Integer> queueElements = compute(grid(0).cluster().forNode(node)).call(new IgniteCallable<Collection<Integer>>() {

                @IgniteInstanceResource
                private Ignite grid;

                /**
                 * {@inheritDoc}
                 */
                @Override
                public Collection<Integer> call() throws Exception {
                    Collection<Integer> values = new ArrayList<>();
                    grid.log().info("Running job [node=" + grid.cluster().localNode().id() + "]");
                    IgniteQueue<Integer> locQueue = grid.queue(queueName, 0, null);
                    grid.log().info("Queue size " + locQueue.size());
                    for (Integer element : locQueue) values.add(element);
                    grid.log().info("Returning: " + values);
                    return values;
                }
            });
            replacedertTrue(F.eqOrdered(queue, queueElements));
        }
    }
}

15 View Complete Implementation : GridCacheQueueMultiNodeAbstractSelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @param collocated Collocation flag.
 * @throws Exception If failed.
 */
private void testAddMultinode(final boolean collocated) throws Exception {
    final String queueName = UUID.randomUUID().toString();
    info("Queue name: " + queueName + ", collocated: " + collocated);
    try {
        Collection<IgniteInternalFuture> futs = new ArrayList<>();
        final int THREADS_PER_NODE = 3;
        final int ITEMS_PER_THREAD = 1000;
        for (int i = 0; i < GRID_CNT; i++) {
            final int idx = i;
            futs.add(GridTestUtils.runMulreplacedhreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    CollectionConfiguration colCfg = config(collocated);
                    IgniteQueue<Integer> queue = grid(idx).queue(queueName, 0, colCfg);
                    for (int i = 0; i < ITEMS_PER_THREAD; i++) replacedertTrue(queue.add(i));
                    return null;
                }
            }, THREADS_PER_NODE, "testPutMultiNode"));
        }
        for (IgniteInternalFuture fut : futs) fut.get();
        IgniteQueue<Integer> queue = grid(0).queue(queueName, 0, null);
        replacedertEquals(THREADS_PER_NODE * ITEMS_PER_THREAD * GRID_CNT, queue.size());
        int[] items = new int[ITEMS_PER_THREAD];
        Integer item;
        while ((item = queue.poll()) != null) items[item]++;
        for (int i = 0; i < ITEMS_PER_THREAD; i++) replacedertEquals(THREADS_PER_NODE * GRID_CNT, items[i]);
    } finally {
        grid(0).queue(queueName, 0, null).close();
    }
}

15 View Complete Implementation : GridCacheQueueMultiNodeAbstractSelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void testGet() throws Exception {
    String queueName = UUID.randomUUID().toString();
    info("Queue name: " + queueName);
    IgniteQueue<String> queue = grid(0).queue(queueName, QUEUE_CAPACITY, config(false));
    replacedertTrue(queue.isEmpty());
    String val = UUID.randomUUID().toString();
    queue.put(val);
    grid(0).compute().call(new GetJob(queueName, config(false), RETRIES, val));
    replacedertEquals(1, queue.size());
    queue.close();
}

15 View Complete Implementation : GridCacheQueueMultiNodeConsistencySelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * Starts {@code GRID_CNT} nodes, broadcasts {@code AddAllJob} to them then starts new grid and
 * reads cache queue content and finally replacederts queue content is the same.
 *
 * @throws Exception If failed.
 */
private void checkCacheQueue() throws Exception {
    startGrids(GRID_CNT);
    final String queueName = UUID.randomUUID().toString();
    IgniteQueue<Integer> queue0 = grid(0).queue(queueName, QUEUE_CAPACITY, config(false));
    replacedertTrue(queue0.isEmpty());
    grid(0).compute().broadcast(new AddAllJob(queueName, RETRIES));
    replacedertEquals(GRID_CNT * RETRIES, queue0.size());
    if (stopRandomGrid)
        stopGrid(1 + new Random().nextInt(GRID_CNT));
    if (forceReparreplacedion) {
        for (int i = 0; i < GRID_CNT; i++) {
            IgniteKernal ignite = (IgniteKernal) grid(i);
            boolean found = false;
            for (GridCacheContext ctx : ignite.context().cache().context().cacheContexts()) {
                if (ctx.name() != null && ctx.name().startsWith("datastructures")) {
                    ctx.cache().rebalance().get();
                    found = true;
                }
            }
            replacedertTrue(found);
        }
    }
    Ignite newIgnite = startGrid(GRID_CNT + 1);
    // Intentionally commented code cause in this way inconsistent queue problem doesn't appear.
    // IgniteQueue<Integer> newQueue = newGrid.cache().queue(queueName);
    // replacedertTrue(CollectionUtils.isEqualCollection(queue0, newQueue));
    Collection<Integer> locQueueContent = compute(newIgnite.cluster().forLocal()).call(new IgniteCallable<Collection<Integer>>() {

        @IgniteInstanceResource
        private Ignite grid;

        /**
         * {@inheritDoc}
         */
        @Override
        public Collection<Integer> call() throws Exception {
            Collection<Integer> values = new ArrayList<>();
            grid.log().info("Running job [node=" + grid.cluster().localNode().id() + ", job=" + this + "]");
            IgniteQueue<Integer> locQueue = grid.queue(queueName, QUEUE_CAPACITY, config(false));
            grid.log().info("Queue size " + locQueue.size());
            for (Integer element : locQueue) values.add(element);
            return values;
        }
    });
    replacedertTrue(CollectionUtils.isEqualCollection(queue0, locQueueContent));
    queue0.close();
}

15 View Complete Implementation : GridCacheQueueRotativeMultiNodeAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testPutRotativeNodes() throws Exception {
    String queueName = UUID.randomUUID().toString();
    IgniteQueue<Integer> queue = grid(0).queue(queueName, QUEUE_CAPACITY, config(true));
    replacedertTrue(queue.isEmpty());
    // Start and stop GRID_CNT*2 new nodes.
    for (int i = GRID_CNT; i < GRID_CNT * 3; i++) {
        startGrid(i);
        forLocal(grid(i)).call(new PutJob(queueName, config(true), RETRIES));
        // last node must be alive.
        if (i < (GRID_CNT * 3) - 1)
            stopGrid(i);
    }
    queue = grid((GRID_CNT * 3) - 1).queue(queueName, 0, null);
    replacedertEquals(RETRIES * GRID_CNT * 2, queue.size());
}

15 View Complete Implementation : GridCacheQueueRotativeMultiNodeAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testPutTakeRotativeNodes() throws Exception {
    String queueName = UUID.randomUUID().toString();
    IgniteQueue<Integer> queue = grid(0).queue(queueName, QUEUE_CAPACITY, config(true));
    replacedertTrue(queue.isEmpty());
    // Start and stop GRID_CNT*2 new nodes.
    for (int i = GRID_CNT; i < GRID_CNT * 3; i++) {
        startGrid(i);
        forLocal(grid(i)).call(new PutTakeJob(queueName, config(true), RETRIES));
        // last node must be alive.
        if (i < (GRID_CNT * 3) - 1)
            stopGrid(i);
    }
    queue = grid((GRID_CNT * 3) - 1).queue(queueName, QUEUE_CAPACITY, config(true));
    replacedertEquals(0, queue.size());
}

15 View Complete Implementation : IgniteCollectionAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @param queue Ignite queue.
 * @return Cache configuration.
 */
protected CacheConfiguration getQueueCache(IgniteQueue queue) {
    GridCacheQueueAdapter delegate = GridTestUtils.getFieldValue(queue, "delegate");
    GridCacheAdapter cache = GridTestUtils.getFieldValue(delegate, GridCacheQueueAdapter.clreplaced, "cache");
    return cache.configuration();
}

15 View Complete Implementation : IgnitePersistentStoreDataStructuresTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void testQueue() throws Exception {
    Ignite ignite = startGrids(4);
    ignite.active(true);
    IgniteQueue<Object> queue = ignite.queue("testQueue", 100, new CollectionConfiguration());
    for (int i = 0; i < 100; i++) queue.offer(i);
    stopAllGrids();
    ignite = startGrids(4);
    ignite.active(true);
    queue = ignite.queue("testQueue", 0, null);
    for (int i = 0; i < 100; i++) replacedertEquals(i, queue.poll());
}

15 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testAddDeleteUnbounded() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();
    String val = UUID.randomUUID().toString();
    IgniteQueue<String> queue = initQueue(0, queueName, 0, config(false));
    replacedert queue.add(val);
    replacedert queue.remove(val);
    replacedert queue.isEmpty();
}

15 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * Test that queues within the same group and compatible configurations are stored in the same cache.
 *
 * @throws Exception If failed.
 */
@Test
public void testCacheReuse() throws Exception {
    CollectionConfiguration colCfg = collectionConfiguration();
    colCfg.setAtomicityMode(ATOMIC);
    colCfg.setGroupName("grp1");
    IgniteQueue queue1 = initQueue(0, "queue1", 100, colCfg);
    IgniteQueue queue2 = initQueue(0, "queue2", 100, colCfg);
    replacedert cctx(queue1).cacheId() == cctx(queue2).cacheId();
    colCfg.setAtomicityMode(TRANSACTIONAL);
    IgniteQueue queue3 = initQueue(0, "queue3", 100, colCfg);
    IgniteQueue queue4 = initQueue(0, "queue4", 100, colCfg);
    replacedert cctx(queue3).cacheId() == cctx(queue4).cacheId();
    replacedert cctx(queue1).cacheId() != cctx(queue3).cacheId();
    replacedert cctx(queue1).groupId() == cctx(queue3).groupId();
    colCfg.setGroupName("gtp2");
    IgniteQueue queue5 = initQueue(0, "queue5", 100, colCfg);
    IgniteQueue queue6 = initQueue(0, "queue6", 100, colCfg);
    replacedert cctx(queue5).cacheId() == cctx(queue6).cacheId();
    replacedert cctx(queue1).groupId() != cctx(queue5).groupId();
}

15 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testPutRemoveUnbounded() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();
    IgniteQueue<String> queue = initQueue(0, queueName, 0, config(false));
    String thread = Thread.currentThread().getName();
    for (int i = 0; i < QUEUE_CAPACITY; i++) queue.put(thread);
    info("Finished loop 1: " + thread);
    queue.clear();
    info("Cleared queue 1: " + thread);
    replacedert queue.isEmpty() : "Queue must be empty. " + queue.size();
}

14 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testPutGetUnbounded() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();
    IgniteQueue<String> queue = initQueue(0, queueName, QUEUE_CAPACITY, config(false));
    String thName = Thread.currentThread().getName();
    for (int i = 0; i < 5; i++) {
        queue.put(thName);
        queue.peek();
        queue.take();
    }
    replacedert queue.isEmpty() : queue.size();
}

14 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testPrepareQueue() throws Exception {
    // Random sequence names.
    String queueName1 = UUID.randomUUID().toString();
    String queueName2 = UUID.randomUUID().toString();
    CollectionConfiguration colCfg = config(false);
    IgniteQueue queue1 = initQueue(0, queueName1, 0, colCfg);
    IgniteQueue queue2 = initQueue(0, queueName2, 0, colCfg);
    IgniteQueue queue3 = initQueue(0, queueName1, 0, colCfg);
    replacedertNotNull(queue1);
    replacedertNotNull(queue2);
    replacedertNotNull(queue3);
    replacedert queue1.equals(queue3);
    replacedert queue3.equals(queue1);
    replacedert !queue3.equals(queue2);
    queue1.close();
    queue2.close();
    queue3.close();
    replacedertNull(initQueue(0, queueName1, 0, null));
    replacedertNull(initQueue(0, queueName2, 0, null));
}

14 View Complete Implementation : GridCacheQueueMultiNodeAbstractSelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @throws Exception If failed.
 */
@Test
public void testSerialization() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();
    final IgniteQueue<Integer> queue = grid(0).queue(queueName, 0, config(false));
    replacedertNotNull(queue);
    try {
        for (int i = 0; i < 10; i++) queue.add(i);
        Collection<Integer> c = grid(0).compute().broadcast(new QueueJob(queue));
        replacedertEquals(GRID_CNT, c.size());
        for (Integer size : c) replacedertEquals((Integer) 10, size);
    } finally {
        queue.close();
    }
}

14 View Complete Implementation : GridCacheQueueRotativeMultiNodeAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testTakeRemoveRotativeNodes() throws Exception {
    lthTake = new CountDownLatch(1);
    final String queueName = UUID.randomUUID().toString();
    final IgniteQueue<Integer> queue = grid(0).queue(queueName, QUEUE_CAPACITY, config(true));
    replacedertTrue(queue.isEmpty());
    Thread th = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                replacedert grid(1).compute().call(new TakeJob(queueName, config(true)));
            } catch (IgniteException e) {
                error(e.getMessage(), e);
            }
        }
    });
    th.start();
    replacedert lthTake.await(1, TimeUnit.MINUTES) : "Timeout happened.";
    replacedertTrue(grid(2).compute().call(new RemoveQueueJob(queueName)));
    GridTestUtils.replacedertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            queue.poll();
            return null;
        }
    }, IllegalStateException.clreplaced, null);
    info("Queue was removed: " + queue);
    th.join();
}

14 View Complete Implementation : IgniteClusterManager.java
Copyright Apache License 2.0
Author : vert-x3
/**
 * @param nodeId ID of node that left topology
 */
private void releasePendingLocksForFailedNode(final String nodeId) {
    Set<String> processed = new HashSet<>();
    pendingLocks.forEach(name -> {
        if (processed.add(name)) {
            IgniteQueue<String> queue = getQueue(name, false);
            if (queue != null && nodeId.equals(queue.peek())) {
                queue.remove(nodeId);
            }
        }
    });
}

13 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testPutRemovePeekPollUnbounded() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();
    IgniteQueue<String> queue = initQueue(0, queueName, 0, config(false));
    for (int i = 0; i < QUEUE_CAPACITY; i++) queue.put("Item-" + i);
    replacedertEquals(QUEUE_CAPACITY, queue.size());
    queue.remove("Item-1");
    replacedertEquals(QUEUE_CAPACITY - 1, queue.size());
    replacedertEquals("Item-0", queue.peek());
    replacedertEquals("Item-0", queue.poll());
    replacedertEquals("Item-2", queue.poll());
    replacedertEquals(0, queue.size());
    queue.clear();
    replacedertTrue(queue.isEmpty());
}

13 View Complete Implementation : GridCacheQueueApiSelfAbstractTest.java
Copyright Apache License 2.0
Author : apache
/**
 * Tests that basic API works correctly when there are multiple structures in multiple groups.
 *
 * @throws Exception If failed.
 */
@Test
public void testMultipleStructuresInDifferentGroups() throws Exception {
    CollectionConfiguration cfg1 = collectionConfiguration();
    CollectionConfiguration cfg2 = collectionConfiguration().setGroupName("grp2");
    IgniteQueue<String> queue1 = initQueue(0, "queue1", 100, cfg1);
    IgniteQueue<String> queue2 = initQueue(0, "queue2", 100, cfg1);
    IgniteQueue<String> queue3 = initQueue(0, "queue3", 100, cfg2);
    IgniteQueue<String> queue4 = initQueue(0, "queue4", 100, cfg2);
    replacedertTrue(queue1.offer("a"));
    replacedertTrue(queue2.offer("b"));
    replacedertTrue(queue3.offer("c"));
    replacedertTrue(queue4.offer("d"));
    replacedertEquals("a", queue1.peek());
    replacedertEquals("b", queue2.peek());
    replacedertEquals("c", queue3.peek());
    replacedertEquals("d", queue4.peek());
    replacedertTrue(queue1.add("A"));
    replacedertTrue(queue2.add("B"));
    replacedertTrue(queue3.add("C"));
    replacedertTrue(queue4.add("D"));
    replacedertEquals(2, queue1.size());
    replacedertEquals(2, queue2.size());
    replacedertEquals(2, queue3.size());
    replacedertEquals(2, queue4.size());
    replacedertEquals("a", queue1.poll());
    replacedertEquals("b", queue2.poll());
    replacedertEquals("c", queue3.poll());
    replacedertEquals("d", queue4.poll());
    replacedertEquals("A", queue1.peek());
    replacedertEquals("B", queue2.peek());
    replacedertEquals("C", queue3.peek());
    replacedertEquals("D", queue4.peek());
    replacedertEquals(1, queue1.size());
    replacedertEquals(1, queue2.size());
    replacedertEquals(1, queue3.size());
    replacedertEquals(1, queue4.size());
    queue2.close();
    queue4.close();
    replacedertTrue(queue2.removed());
    replacedertTrue(queue4.removed());
    replacedertFalse(queue1.removed());
    replacedertFalse(queue3.removed());
    replacedertNotNull(initQueue(0, "queue1", 100, null));
    replacedertNull(initQueue(0, "queue2", 100, null));
    queue1.close();
    queue3.close();
}

12 View Complete Implementation : GridCacheQueueMultiNodeAbstractSelfTest.java
Copyright Apache License 2.0
Author : apache
/**
 * @param collocated Collocation flag.
 * @throws Exception If failed.
 */
private void testAddPollMultinode(final boolean collocated) throws Exception {
    final String queueName = UUID.randomUUID().toString();
    info("Queue name: " + queueName + ", collocated: " + collocated);
    try {
        Collection<IgniteInternalFuture> putFuts = new ArrayList<>();
        Collection<IgniteInternalFuture> pollFuts = new ArrayList<>();
        final int PUT_THREADS_PER_NODE = 3;
        final int POLL_THREADS_PER_NODE = 2;
        final int ITEMS_PER_THREAD = 1000;
        final AtomicBoolean stopPoll = new AtomicBoolean();
        Collection<int[]> pollData = new ArrayList<>();
        for (int i = 0; i < GRID_CNT; i++) {
            final int idx = i;
            putFuts.add(GridTestUtils.runMulreplacedhreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    CollectionConfiguration colCfg = config(collocated);
                    IgniteQueue<Integer> queue = grid(idx).queue(queueName, 0, colCfg);
                    for (int i = 0; i < ITEMS_PER_THREAD; i++) replacedertTrue(queue.add(i));
                    return null;
                }
            }, PUT_THREADS_PER_NODE, "testAddPollMultinode"));
            for (int j = 0; j < POLL_THREADS_PER_NODE; j++) {
                final int[] items = new int[ITEMS_PER_THREAD];
                pollData.add(items);
                pollFuts.add(GridTestUtils.runAsync(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        CollectionConfiguration colCfg = config(collocated);
                        IgniteQueue<Integer> queue = grid(idx).queue(queueName, 0, colCfg);
                        while (!stopPoll.get()) {
                            Integer val = queue.poll();
                            if (val != null)
                                items[val]++;
                        }
                        return null;
                    }
                }));
            }
        }
        for (IgniteInternalFuture fut : putFuts) fut.get();
        stopPoll.set(true);
        for (IgniteInternalFuture fut : pollFuts) fut.get();
        CollectionConfiguration colCfg = config(collocated);
        IgniteQueue<Integer> queue = grid(0).queue(queueName, 0, colCfg);
        int[] resItems = new int[ITEMS_PER_THREAD];
        Integer item;
        while ((item = queue.poll()) != null) resItems[item]++;
        for (int[] items : pollData) {
            for (int i = 0; i < ITEMS_PER_THREAD; i++) resItems[i] += items[i];
        }
        for (int i = 0; i < ITEMS_PER_THREAD; i++) replacedertEquals(PUT_THREADS_PER_NODE * GRID_CNT, resItems[i]);
        replacedertTrue(queue.isEmpty());
    } finally {
        grid(0).queue(queueName, 0, null).close();
    }
}