Search in sources :

Example 91 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class CacheEntryListenersTest method testJCacheListeners.

/**
 * Test JCache entry listeners.
 */
@Test
public void testJCacheListeners() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        ClientCache<Integer, Integer> cache = client.getOrCreateCache("testJCacheListeners");
        JCacheEntryListener<Integer, Integer> lsnr = new JCacheEntryListener<>();
        cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr, null, true, false));
        for (int i = 0; i < 10; i++) cache.put(i, i);
        assertEquals(F.asMap(EventType.CREATED, IntStream.range(0, 10).boxed().collect(Collectors.toMap(i -> i, i -> i))), aggregateListenerEvents(lsnr, 10));
        for (int i = 0; i < 10; i++) cache.put(i, -i);
        assertEquals(F.asMap(EventType.UPDATED, IntStream.range(0, 10).boxed().collect(Collectors.toMap(i -> i, i -> -i))), aggregateListenerEvents(lsnr, 10));
        for (int i = 0; i < 10; i++) cache.remove(i);
        assertEquals(F.asMap(EventType.REMOVED, IntStream.range(0, 10).boxed().collect(Collectors.toMap(i -> i, i -> -i))), aggregateListenerEvents(lsnr, 10));
        assertTrue(lsnr.isQueueEmpty());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntStream(java.util.stream.IntStream) ClientDisconnectListener(org.apache.ignite.client.ClientDisconnectListener) Person(org.apache.ignite.client.Person) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) CacheEntryExpiredListener(javax.cache.event.CacheEntryExpiredListener) ClientCache(org.apache.ignite.client.ClientCache) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) Map(java.util.Map) Cache(javax.cache.Cache) CacheEntryCreatedListener(javax.cache.event.CacheEntryCreatedListener) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Duration(javax.cache.expiry.Duration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) F(org.apache.ignite.internal.util.typedef.F) EventType(javax.cache.event.EventType) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) EnumMap(java.util.EnumMap) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) T2(org.apache.ignite.internal.util.typedef.T2) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryRemovedListener(javax.cache.event.CacheEntryRemovedListener) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) IgniteClient(org.apache.ignite.client.IgniteClient) Test(org.junit.Test)

Example 92 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class CacheEntryListenersTest method testDisconnectListeners.

/**
 * Test disconnect event for cache entry listeners.
 */
@Test
public void testDisconnectListeners() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        ClientCache<Object, Object> cache = client.getOrCreateCache("testDisconnect");
        ContinuousQueryListener<Object, Object> lsnr1 = new ContinuousQueryListener<>();
        cache.query(new ContinuousQuery<>().setLocalListener(lsnr1), lsnr1);
        JCacheEntryListener<Object, Object> lsnr2 = new JCacheEntryListener<>();
        CacheEntryListenerConfiguration<Object, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(() -> lsnr2, null, true, false);
        cache.registerCacheEntryListener(lsnrCfg, lsnr2);
        cache.put(0, 0);
        lsnr1.assertNextCacheEvent(EventType.CREATED, 0, 0);
        lsnr2.assertNextCacheEvent(EventType.CREATED, 0, 0);
        dropAllThinClientConnections();
        // Can't detect channel failure until we send something to server.
        cache.put(1, 1);
        assertTrue(lsnr1.isQueueEmpty());
        assertTrue(lsnr2.isQueueEmpty());
        assertTrue(waitForCondition(lsnr1::isDisconnected, TIMEOUT));
        assertTrue(waitForCondition(lsnr2::isDisconnected, TIMEOUT));
        // Should be able to register the same listener on the same cache again.
        cache.registerCacheEntryListener(lsnrCfg);
    }
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteClient(org.apache.ignite.client.IgniteClient) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) Test(org.junit.Test)

Example 93 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class CacheEntryListenersTest method testContinuousQueries.

/**
 * Test continuous queries.
 */
@Test
public void testContinuousQueries() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        ClientCache<Integer, Integer> cache = client.getOrCreateCache("testCQ");
        ContinuousQueryListener<Integer, Integer> lsnr = new ContinuousQueryListener<>();
        cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr));
        for (int i = 0; i < 10; i++) cache.put(i, i);
        assertEquals(F.asMap(EventType.CREATED, IntStream.range(0, 10).boxed().collect(Collectors.toMap(i -> i, i -> i))), aggregateListenerEvents(lsnr, 10));
        for (int i = 0; i < 10; i++) cache.put(i, -i);
        assertEquals(F.asMap(EventType.UPDATED, IntStream.range(0, 10).boxed().collect(Collectors.toMap(i -> i, i -> -i))), aggregateListenerEvents(lsnr, 10));
        for (int i = 0; i < 10; i++) cache.remove(i);
        assertEquals(F.asMap(EventType.REMOVED, IntStream.range(0, 10).boxed().collect(Collectors.toMap(i -> i, i -> -i))), aggregateListenerEvents(lsnr, 10));
        assertTrue(lsnr.isQueueEmpty());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntStream(java.util.stream.IntStream) ClientDisconnectListener(org.apache.ignite.client.ClientDisconnectListener) Person(org.apache.ignite.client.Person) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) CacheEntryExpiredListener(javax.cache.event.CacheEntryExpiredListener) ClientCache(org.apache.ignite.client.ClientCache) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) Map(java.util.Map) Cache(javax.cache.Cache) CacheEntryCreatedListener(javax.cache.event.CacheEntryCreatedListener) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Duration(javax.cache.expiry.Duration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) F(org.apache.ignite.internal.util.typedef.F) EventType(javax.cache.event.EventType) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) EnumMap(java.util.EnumMap) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) T2(org.apache.ignite.internal.util.typedef.T2) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryRemovedListener(javax.cache.event.CacheEntryRemovedListener) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteClient(org.apache.ignite.client.IgniteClient) Test(org.junit.Test)

Example 94 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class CacheEntryListenersTest method testListenersUnsupportedParameters.

/**
 */
@Test
@SuppressWarnings({ "ThrowableNotThrown", "deprecation" })
public void testListenersUnsupportedParameters() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        ClientCache<Integer, Integer> cache = client.getOrCreateCache("testUnsupportedParams");
        // Check null listener factory.
        CacheEntryListenerConfiguration<Integer, Integer> lsnrCfg1 = new MutableCacheEntryListenerConfiguration<>(null, null, true, false);
        assertThrowsWithCause(() -> cache.registerCacheEntryListener(lsnrCfg1), NullPointerException.class);
        // Check synchronous flag.
        CacheEntryListenerConfiguration<Integer, Integer> lsnrCfg2 = new MutableCacheEntryListenerConfiguration<>(JCacheEntryListener::new, null, true, true);
        assertThrowsWithCause(() -> cache.registerCacheEntryListener(lsnrCfg2), IllegalArgumentException.class);
        // Check local flag.
        ContinuousQueryListener<Integer, Integer> cqLsnr = new ContinuousQueryListener<>();
        ContinuousQuery<Integer, Integer> qry1 = new ContinuousQuery<Integer, Integer>().setLocalListener(cqLsnr).setLocal(true);
        assertThrowsWithCause(() -> cache.query(qry1), IllegalArgumentException.class);
        // Check null listener.
        ContinuousQuery<Integer, Integer> qry2 = new ContinuousQuery<>();
        assertThrowsWithCause(() -> cache.query(qry2), NullPointerException.class);
        // Check auto unsubscribe flag.
        ContinuousQuery<Integer, Integer> qry3 = new ContinuousQuery<Integer, Integer>().setLocalListener(cqLsnr).setAutoUnsubscribe(false);
        assertThrowsWithCause(() -> cache.query(qry3), IllegalArgumentException.class);
        // Check continuous query as initial query.
        ContinuousQuery<Integer, Integer> qry4 = new ContinuousQuery<Integer, Integer>().setLocalListener(cqLsnr).setInitialQuery(new ContinuousQuery<>());
        assertThrowsWithCause(() -> cache.query(qry4), IllegalArgumentException.class);
        // Check filter factory and filter defined at the same time.
        CacheEntryEventSerializableFilter<Integer, Integer> rmtFilter = r -> true;
        ContinuousQuery<Integer, Integer> qry5 = new ContinuousQuery<Integer, Integer>().setLocalListener(cqLsnr).setRemoteFilter(rmtFilter);
        qry5.setRemoteFilterFactory(FactoryBuilder.factoryOf(rmtFilter));
        assertThrowsWithCause(() -> cache.query(qry5), IllegalArgumentException.class);
    }
}
Also used : IntStream(java.util.stream.IntStream) ClientDisconnectListener(org.apache.ignite.client.ClientDisconnectListener) Person(org.apache.ignite.client.Person) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) CacheEntryExpiredListener(javax.cache.event.CacheEntryExpiredListener) ClientCache(org.apache.ignite.client.ClientCache) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) Map(java.util.Map) Cache(javax.cache.Cache) CacheEntryCreatedListener(javax.cache.event.CacheEntryCreatedListener) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Duration(javax.cache.expiry.Duration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) F(org.apache.ignite.internal.util.typedef.F) EventType(javax.cache.event.EventType) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) EnumMap(java.util.EnumMap) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) T2(org.apache.ignite.internal.util.typedef.T2) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryRemovedListener(javax.cache.event.CacheEntryRemovedListener) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteClient(org.apache.ignite.client.IgniteClient) Test(org.junit.Test)

Example 95 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class ServicesTest method testServiceTimeout.

/**
 * Test services timeout.
 */
@Test
public void testServiceTimeout() throws Exception {
    long timeout = 100L;
    try (IgniteClient client = startClient(0)) {
        TestServiceInterface svc = client.services().serviceProxy(CLUSTER_SINGLTON_SERVICE_NAME, TestServiceInterface.class, timeout);
        TestService.latch = new CountDownLatch(1);
        GridTestUtils.assertThrowsAnyCause(log, svc::waitLatch, ClientException.class, "timed out");
    } finally {
        if (TestService.latch != null) {
            TestService.latch.countDown();
            TestService.latch = null;
        }
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

IgniteClient (org.apache.ignite.client.IgniteClient)106 Test (org.junit.Test)76 ClientConfiguration (org.apache.ignite.configuration.ClientConfiguration)43 ThinClientConfiguration (org.apache.ignite.configuration.ThinClientConfiguration)26 UUID (java.util.UUID)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 Ignite (org.apache.ignite.Ignite)14 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)14 HashSet (java.util.HashSet)13 T2 (org.apache.ignite.internal.util.typedef.T2)13 Set (java.util.Set)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)12 List (java.util.List)10 BinaryObject (org.apache.ignite.binary.BinaryObject)10 GridTestUtils (org.apache.ignite.testframework.GridTestUtils)10 GridTestUtils.assertThrowsWithCause (org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause)10 Map (java.util.Map)9 Duration (javax.cache.expiry.Duration)9 ClientCacheConfiguration (org.apache.ignite.client.ClientCacheConfiguration)9