Search in sources :

Example 1 with P2

use of org.apache.ignite.internal.util.typedef.P2 in project ignite by apache.

the class GridCacheContinuousQueryAbstractSelfTest method testInitialQueryAndUpdates.

/**
     * @throws Exception If failed.
     */
public void testInitialQueryAndUpdates() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    qry.setInitialQuery(new ScanQuery<>(new P2<Integer, Integer>() {

        @Override
        public boolean apply(Integer k, Integer v) {
            return k >= 5;
        }
    }));
    final Map<Integer, Integer> map = new ConcurrentHashMap8<>();
    final CountDownLatch latch = new CountDownLatch(2);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                map.put(e.getKey(), e.getValue());
                latch.countDown();
            }
        }
    });
    for (int i = 0; i < 10; i++) cache.put(i, i);
    try (QueryCursor<Cache.Entry<Integer, Integer>> cur = cache.query(qry)) {
        List<Cache.Entry<Integer, Integer>> res = cur.getAll();
        Collections.sort(res, new Comparator<Cache.Entry<Integer, Integer>>() {

            @Override
            public int compare(Cache.Entry<Integer, Integer> e1, Cache.Entry<Integer, Integer> e2) {
                return e1.getKey().compareTo(e2.getKey());
            }
        });
        assertEquals(5, res.size());
        int exp = 5;
        for (Cache.Entry<Integer, Integer> e : res) {
            assertEquals(exp, e.getKey().intValue());
            assertEquals(exp, e.getValue().intValue());
            exp++;
        }
        cache.put(10, 10);
        cache.put(11, 11);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS) : latch.getCount();
        assertEquals(2, map.size());
        for (int i = 11; i < 12; i++) assertEquals(i, (int) map.get(i));
    }
}
Also used : P2(org.apache.ignite.internal.util.typedef.P2) ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 2 with P2

use of org.apache.ignite.internal.util.typedef.P2 in project ignite by apache.

the class GridEventConsumeSelfTest method testApi.

/**
     * @throws Exception If failed.
     */
public void testApi() throws Exception {
    try {
        grid(0).events().stopRemoteListen(null);
    } catch (NullPointerException ignored) {
    // No-op.
    }
    grid(0).events().stopRemoteListen(UUID.randomUUID());
    UUID consumeId = null;
    try {
        consumeId = grid(0).events().remoteListen(new P2<UUID, DiscoveryEvent>() {

            @Override
            public boolean apply(UUID uuid, DiscoveryEvent evt) {
                return false;
            }
        }, new P1<DiscoveryEvent>() {

            @Override
            public boolean apply(DiscoveryEvent e) {
                return false;
            }
        }, EVTS_DISCOVERY);
        assertNotNull(consumeId);
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
    try {
        consumeId = grid(0).events().remoteListen(new P2<UUID, DiscoveryEvent>() {

            @Override
            public boolean apply(UUID uuid, DiscoveryEvent evt) {
                return false;
            }
        }, new P1<DiscoveryEvent>() {

            @Override
            public boolean apply(DiscoveryEvent e) {
                return false;
            }
        });
        assertNotNull(consumeId);
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
    try {
        consumeId = grid(0).events().remoteListen(new P2<UUID, Event>() {

            @Override
            public boolean apply(UUID uuid, Event evt) {
                return false;
            }
        }, new P1<Event>() {

            @Override
            public boolean apply(Event e) {
                return false;
            }
        });
        assertNotNull(consumeId);
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
}
Also used : P1(org.apache.ignite.internal.util.typedef.P1) P2(org.apache.ignite.internal.util.typedef.P2) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID)

Example 3 with P2

use of org.apache.ignite.internal.util.typedef.P2 in project ignite by apache.

the class GridMessagingSelfTest method testStopLocalListen.

/**
     * @throws Exception If error occurs.
     */
@SuppressWarnings("TooBroadScope")
public void testStopLocalListen() throws Exception {
    final AtomicInteger msgCnt1 = new AtomicInteger();
    final AtomicInteger msgCnt2 = new AtomicInteger();
    final AtomicInteger msgCnt3 = new AtomicInteger();
    P2<UUID, Object> lsnr1 = new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            log.info("Listener1 received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
            msgCnt1.incrementAndGet();
            return true;
        }
    };
    P2<UUID, Object> lsnr2 = new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            log.info("Listener2 received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
            msgCnt2.incrementAndGet();
            return true;
        }
    };
    P2<UUID, Object> lsnr3 = new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            log.info("Listener3 received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
            msgCnt3.incrementAndGet();
            return true;
        }
    };
    final String topic1 = null;
    final String topic2 = "top1";
    final String topic3 = "top3";
    ignite1.message().localListen(topic1, lsnr1);
    ignite1.message().localListen(topic2, lsnr2);
    ignite1.message().localListen(topic3, lsnr3);
    ClusterGroup rNode1 = ignite2.cluster().forRemotes();
    message(rNode1).send(topic1, "msg1-1");
    message(rNode1).send(topic2, "msg1-2");
    message(rNode1).send(topic3, "msg1-3");
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return msgCnt1.get() > 0 && msgCnt2.get() > 0 && msgCnt3.get() > 0;
        }
    }, 5000);
    assertEquals(1, msgCnt1.get());
    assertEquals(1, msgCnt2.get());
    assertEquals(1, msgCnt3.get());
    ignite1.message().stopLocalListen(topic2, lsnr2);
    message(rNode1).send(topic1, "msg2-1");
    message(rNode1).send(topic2, "msg2-2");
    message(rNode1).send(topic3, "msg2-3");
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return msgCnt1.get() > 1 && msgCnt3.get() > 1;
        }
    }, 5000);
    assertEquals(2, msgCnt1.get());
    assertEquals(1, msgCnt2.get());
    assertEquals(2, msgCnt3.get());
    // Try to use wrong topic for lsnr1 removing.
    ignite1.message().stopLocalListen(topic2, lsnr1);
    message(rNode1).send(topic1, "msg3-1");
    message(rNode1).send(topic2, "msg3-2");
    message(rNode1).send(topic3, "msg3-3");
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return msgCnt1.get() > 2 && msgCnt3.get() > 2;
        }
    }, 5000);
    assertEquals(3, msgCnt1.get());
    assertEquals(1, msgCnt2.get());
    assertEquals(3, msgCnt3.get());
    ignite1.message().stopLocalListen(topic1, lsnr1);
    ignite1.message().stopLocalListen(topic3, lsnr3);
    message(rNode1).send(topic1, "msg4-1");
    message(rNode1).send(topic2, "msg4-2");
    message(rNode1).send(topic3, "msg4-3");
    U.sleep(1000);
    assertEquals(3, msgCnt1.get());
    assertEquals(1, msgCnt2.get());
    assertEquals(3, msgCnt3.get());
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) P2(org.apache.ignite.internal.util.typedef.P2) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) UUID(java.util.UUID)

Example 4 with P2

use of org.apache.ignite.internal.util.typedef.P2 in project ignite by apache.

the class GridEventConsumeSelfTest method testNodeJoin.

/**
     * @throws Exception If failed.
     */
public void testNodeJoin() throws Exception {
    final Collection<UUID> nodeIds = new HashSet<>();
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(GRID_CNT + 1);
    UUID consumeId = grid(0).events().remoteListen(notSerializableProxy(new P2<UUID, Event>() {

        @Override
        public boolean apply(UUID nodeId, Event evt) {
            info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
            assertEquals(EVT_JOB_STARTED, evt.type());
            nodeIds.add(nodeId);
            cnt.incrementAndGet();
            latch.countDown();
            return true;
        }
    }), notSerializableProxy(new P1<Event>() {

        @Override
        public boolean apply(Event evt) {
            return evt.type() == EVT_JOB_STARTED;
        }
    }), EVT_JOB_STARTED, EVT_JOB_FINISHED);
    try {
        assertNotNull(consumeId);
        include = true;
        startGrid("anotherGrid");
        grid(0).compute().broadcast(F.noop());
        assert latch.await(2, SECONDS);
        assertEquals(GRID_CNT + 1, nodeIds.size());
        assertEquals(GRID_CNT + 1, cnt.get());
    } finally {
        stopGrid("anotherGrid");
        grid(0).events().stopRemoteListen(consumeId);
    }
}
Also used : P1(org.apache.ignite.internal.util.typedef.P1) P2(org.apache.ignite.internal.util.typedef.P2) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

Example 5 with P2

use of org.apache.ignite.internal.util.typedef.P2 in project ignite by apache.

the class GridCommunicationManagerListenersSelfTest method testDifferentListeners.

/**
     * Works fine.
     */
@SuppressWarnings({ "deprecation" })
public void testDifferentListeners() {
    Ignite ignite = G.ignite(getTestIgniteInstanceName());
    for (int i = 0; i < 2000; i++) {
        P2<UUID, Object> l = new P2<UUID, Object>() {

            @Override
            public boolean apply(UUID uuid, Object o) {
                return false;
            }
        };
        ignite.message().localListen(null, l);
    }
    info(getName() + ": worked without exceptions.");
}
Also used : P2(org.apache.ignite.internal.util.typedef.P2) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Aggregations

P2 (org.apache.ignite.internal.util.typedef.P2)6 UUID (java.util.UUID)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Ignite (org.apache.ignite.Ignite)2 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)2 Event (org.apache.ignite.events.Event)2 JobEvent (org.apache.ignite.events.JobEvent)2 P1 (org.apache.ignite.internal.util.typedef.P1)2 HashSet (java.util.HashSet)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Cache (javax.cache.Cache)1 CacheEntryEvent (javax.cache.event.CacheEntryEvent)1 IgniteCache (org.apache.ignite.IgniteCache)1 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)1 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)1 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)1 PA (org.apache.ignite.internal.util.typedef.PA)1 ConcurrentHashMap8 (org.jsr166.ConcurrentHashMap8)1