use of org.apache.ignite.client.IgniteClient in project ignite by apache.
the class CacheEntryListenersTest method testContinuousQueriesWithConcurrentCompute.
/**
*/
@Test
public void testContinuousQueriesWithConcurrentCompute() throws Exception {
try (IgniteClient client = startClient(0, 1, 2)) {
int threadsCnt = 20;
int iterations = 50;
Set<UUID> allNodesIds = new HashSet<>(F.nodeIds(grid(0).cluster().nodes()));
AtomicInteger threadIdxs = new AtomicInteger();
GridTestUtils.runMultiThreaded(() -> {
int threadIdx = threadIdxs.incrementAndGet();
ClientCache<Integer, Integer> cache = client.getOrCreateCache("testCQwithCompute" + threadIdx);
try {
for (int i = 0; i < iterations; i++) {
ContinuousQueryListener<Integer, Integer> lsnr = new ContinuousQueryListener<>();
QueryCursor<?> cur = cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr));
cache.put(i, i);
Future<T2<UUID, Set<UUID>>> fut = client.compute().executeAsync2(TestTask.class.getName(), null);
assertEquals(allNodesIds, fut.get().get2());
lsnr.assertNextCacheEvent(EventType.CREATED, i, i);
assertTrue(lsnr.isQueueEmpty());
cur.close();
}
} catch (Exception e) {
log.error("Failure: ", e);
fail();
}
}, threadsCnt, "run-task-async");
}
}
use of org.apache.ignite.client.IgniteClient in project ignite by apache.
the class CacheEntryListenersTest method testJCacheListenersExpiredEntries.
/**
* Test JCache entry listeners with expired entries.
*/
@Test
public void testJCacheListenersExpiredEntries() throws Exception {
try (IgniteClient client = startClient(0, 1, 2)) {
ClientCache<Integer, Integer> cache = client.getOrCreateCache("testJCacheListenersWithExp");
JCacheEntryListener<Integer, Integer> lsnr = new JCacheEntryListener<>();
cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr, null, true, false));
cache = cache.withExpirePolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 1)));
for (int i = 0; i < 100; i++) cache.put(i, i);
int cntCreated = 0;
int cntExpired = 0;
for (int i = 0; i < 200; i++) {
// There should be two events for each cache entry.
CacheEntryEvent<?, ?> evt = lsnr.poll();
if (evt.getEventType() == EventType.CREATED)
cntCreated++;
else if (evt.getEventType() == EventType.EXPIRED)
cntExpired++;
else
fail("Unexpected event type: " + evt.getEventType());
}
assertEquals(100, cntCreated);
assertEquals(100, cntExpired);
assertTrue(lsnr.isQueueEmpty());
}
}
use of org.apache.ignite.client.IgniteClient in project ignite by apache.
the class CacheEntryListenersTest method testListenersWithRemoteFilter.
/**
* Test continuous queries and JCache entry listeners with remote filters.
*/
@Test
@SuppressWarnings("deprecation")
public void testListenersWithRemoteFilter() throws Exception {
try (IgniteClient client = startClient(0, 1, 2)) {
ClientCache<Integer, Integer> cache = client.getOrCreateCache("testListenersWithRmtFilter");
CacheEntryEventSerializableFilter<Integer, Integer> rmtFilter = evt -> (evt.getKey() & 1) == 0;
ContinuousQueryListener<Integer, Integer> lsnr1 = new ContinuousQueryListener<>();
ContinuousQueryListener<Integer, Integer> lsnr2 = new ContinuousQueryListener<>();
cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr1).setRemoteFilterFactory(() -> rmtFilter));
cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr2).setRemoteFilter(rmtFilter));
JCacheEntryListener<Integer, Integer> lsnr3 = new JCacheEntryListener<>();
cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr3, () -> rmtFilter, true, false));
for (int i = 0; i < 10; i++) cache.put(i, i);
Map<EventType, Map<Integer, Integer>> expRes = F.asMap(EventType.CREATED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> i * 2)));
assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
for (int i = 0; i < 10; i++) cache.put(i, -i);
expRes = F.asMap(EventType.UPDATED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> -i * 2)));
assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
for (int i = 0; i < 10; i++) cache.remove(i);
expRes = F.asMap(EventType.REMOVED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> -i * 2)));
assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
assertTrue(lsnr1.isQueueEmpty());
assertTrue(lsnr2.isQueueEmpty());
assertTrue(lsnr3.isQueueEmpty());
}
}
use of org.apache.ignite.client.IgniteClient in project ignite by apache.
the class CacheEntryListenersTest method testContinuousQueriesWithIncludeExpired.
/**
* Test continuous queries with include expired parameter.
*/
@Test
public void testContinuousQueriesWithIncludeExpired() throws Exception {
try (IgniteClient client = startClient(0, 1, 2)) {
ClientCache<Integer, Integer> cache = client.getOrCreateCache("testCQWithInclExp");
ContinuousQueryListener<Integer, Integer> lsnr1 = new ContinuousQueryListener<>();
ContinuousQueryListener<Integer, Integer> lsnr2 = new ContinuousQueryListener<>();
ContinuousQuery<Integer, Integer> qry1 = new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr1);
ContinuousQuery<Integer, Integer> qry2 = new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr2);
qry1.setIncludeExpired(false);
qry2.setIncludeExpired(true);
cache.query(qry1);
cache.query(qry2);
cache = cache.withExpirePolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 1)));
for (int i = 0; i < 100; i++) cache.put(i, i);
int cntCreated = 0;
int cntExpired = 0;
for (int i = 0; i < 100; i++) assertEquals(EventType.CREATED, lsnr1.poll().getEventType());
for (int i = 0; i < 200; i++) {
// There should be two events for each cache entry.
CacheEntryEvent<?, ?> evt = lsnr2.poll();
if (evt.getEventType() == EventType.CREATED)
cntCreated++;
else if (evt.getEventType() == EventType.EXPIRED)
cntExpired++;
else
fail("Unexpected event type: " + evt.getEventType());
}
assertEquals(100, cntCreated);
assertEquals(100, cntExpired);
assertTrue(lsnr1.isQueueEmpty());
assertTrue(lsnr2.isQueueEmpty());
}
}
use of org.apache.ignite.client.IgniteClient in project ignite by apache.
the class CacheEntryListenersTest method testRegisterDeregisterListener.
/**
*/
@Test
@SuppressWarnings("ThrowableNotThrown")
public void testRegisterDeregisterListener() throws Exception {
try (IgniteClient client = startClient(0, 1, 2)) {
String cacheName = "registerListener";
ClientCache<Integer, Integer> cache0 = client.getOrCreateCache(cacheName);
CacheEntryListenerConfiguration<Integer, Integer> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(JCacheEntryListener::new, null, true, false);
cache0.registerCacheEntryListener(lsnrCfg);
ClientCache<Integer, Integer> cache1 = client.getOrCreateCache(cacheName + '2');
// Can register the same listener on another cache.
cache1.registerCacheEntryListener(lsnrCfg);
ClientCache<Integer, Integer> cache2 = client.cache(cacheName);
// Can't register the same listener on the same cache.
assertThrowsWithCause(() -> cache2.registerCacheEntryListener(lsnrCfg), IllegalStateException.class);
ClientCache<Integer, Integer> cache3 = client.cache(cacheName);
cache3.deregisterCacheEntryListener(lsnrCfg);
// Can register the listener after deregisteration.
cache2.registerCacheEntryListener(lsnrCfg);
}
}
Aggregations