Search in sources :

Example 31 with IgniteClient

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

the class OptimizedMarshallerClassesCachedTest method testLocalDateTimeMetaCached.

/**
 * Test check that meta for classes serialized by {@link OptimizedMarshaller} are cached on client.
 */
@Test
public void testLocalDateTimeMetaCached() throws Exception {
    try (Ignite srv = startGrid(0)) {
        srv.getOrCreateCache(Config.DEFAULT_CACHE_NAME).put(1, LocalDateTime.now());
        IgniteClient cli = new TcpIgniteClient((cfg0, hnd) -> new TcpClientChannel(cfg0, hnd) {

            @Override
            public <T> T service(ClientOperation op, Consumer<PayloadOutputChannel> payloadWriter, Function<PayloadInputChannel, T> payloadReader) throws ClientException {
                if (op == ClientOperation.GET_BINARY_TYPE_NAME)
                    cnt.incrementAndGet();
                return super.service(op, payloadWriter, payloadReader);
            }

            @Override
            public <T> CompletableFuture<T> serviceAsync(ClientOperation op, Consumer<PayloadOutputChannel> payloadWriter, Function<PayloadInputChannel, T> payloadReader) {
                if (op == ClientOperation.GET_BINARY_TYPE_NAME)
                    cnt.incrementAndGet();
                return super.serviceAsync(op, payloadWriter, payloadReader);
            }
        }, new ClientConfiguration().setAddresses(Config.SERVER));
        try {
            cli.cache(Config.DEFAULT_CACHE_NAME).get(1);
            cli.cache(Config.DEFAULT_CACHE_NAME).get(1);
        } finally {
            cli.close();
        }
        assertEquals(1, cnt.get());
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) IgniteClient(org.apache.ignite.client.IgniteClient) Ignite(org.apache.ignite.Ignite) ClientException(org.apache.ignite.client.ClientException) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 32 with IgniteClient

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

the class ComputeTaskTest method testTaskWithNoFailover.

/**
 */
@Test
public void testTaskWithNoFailover() throws Exception {
    try (IgniteClient client = startClient(0)) {
        ClientCompute computeWithFailover = client.compute();
        ClientCompute computeWithNoFailover = client.compute().withNoFailover();
        assertTrue(computeWithFailover.execute(TestFailoverTask.class.getName(), null));
        assertFalse(computeWithNoFailover.execute(TestFailoverTask.class.getName(), null));
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) ClientCompute(org.apache.ignite.client.ClientCompute) Test(org.junit.Test)

Example 33 with IgniteClient

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

the class ComputeTaskTest method testExecuteTaskAsync.

/**
 */
@Test
public void testExecuteTaskAsync() throws Exception {
    try (IgniteClient client = startClient(0)) {
        TestLatchTask.latch = new CountDownLatch(1);
        Future<T2<UUID, Set<UUID>>> fut = client.compute().executeAsync(TestLatchTask.class.getName(), null);
        GridTestUtils.assertThrowsAnyCause(null, () -> fut.get(10L, TimeUnit.MILLISECONDS), TimeoutException.class, null);
        assertFalse(fut.isDone());
        TestLatchTask.latch.countDown();
        T2<UUID, Set<UUID>> val = fut.get();
        assertTrue(fut.isDone());
        assertEquals(nodeId(0), val.get1());
        assertEquals(new HashSet<>(F.nodeIds(grid(0).cluster().forServers().nodes())), val.get2());
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) IgniteClient(org.apache.ignite.client.IgniteClient) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) Test(org.junit.Test)

Example 34 with IgniteClient

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

the class ComputeTaskTest method testExecuteTaskAsync2.

/**
 * Tests asynchronous task execution.
 */
@Test
public void testExecuteTaskAsync2() throws Exception {
    try (IgniteClient client = startClient(0)) {
        TestLatchTask.latch = new CountDownLatch(1);
        IgniteClientFuture<T2<UUID, Set<UUID>>> fut = client.compute().executeAsync2(TestLatchTask.class.getName(), null);
        GridTestUtils.assertThrowsAnyCause(null, () -> fut.get(10L, TimeUnit.MILLISECONDS), TimeoutException.class, null);
        assertFalse(fut.isDone());
        TestLatchTask.latch.countDown();
        T2<UUID, Set<UUID>> val = fut.get();
        assertTrue(fut.isDone());
        assertEquals(nodeId(0), val.get1());
        assertEquals(new HashSet<>(F.nodeIds(grid(0).cluster().forServers().nodes())), val.get2());
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) IgniteClient(org.apache.ignite.client.IgniteClient) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) Test(org.junit.Test)

Example 35 with IgniteClient

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

the class ComputeTaskTest method testExecuteTaskConcurrentLoad.

/**
 */
@Test
public void testExecuteTaskConcurrentLoad() throws Exception {
    try (IgniteClient client = startClient(0)) {
        int threadsCnt = 20;
        int iterations = 100;
        ClientCache<Integer, Integer> cache = client.getOrCreateCache(DEFAULT_CACHE_NAME);
        AtomicInteger threadIdxs = new AtomicInteger();
        CyclicBarrier barrier = new CyclicBarrier(threadsCnt);
        GridTestUtils.runMultiThreaded(() -> {
            int threadIdx = threadIdxs.incrementAndGet();
            Random rnd = new Random();
            try {
                barrier.await();
                for (int i = 0; i < iterations; i++) {
                    int nodeIdx = rnd.nextInt(GRIDS_CNT);
                    cache.put(threadIdx, i);
                    ClientCompute compute = client.compute(client.cluster().forNodeId(nodeId(nodeIdx)));
                    Future<T2<UUID, Set<UUID>>> fut = compute.executeAsync(TestTask.class.getName(), null);
                    boolean cancelled = (i % 3 == 0) && fut.cancel(true);
                    assertEquals((Integer) i, cache.get(threadIdx));
                    if (cancelled)
                        assertTrue(fut.isCancelled());
                    else
                        assertEquals(nodeIds(nodeIdx), fut.get().get2());
                }
            } catch (ExecutionException e) {
                log.error("Task failed: ", e);
                fail("Task failed");
            } catch (InterruptedException | BrokenBarrierException ignore) {
            // No-op.
            }
        }, threadsCnt, "run-task-async");
        assertTrue(GridTestUtils.waitForCondition(() -> ((ClientComputeImpl) client.compute()).activeTasksCount() == 0, TIMEOUT));
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UUID(java.util.UUID) ExecutionException(java.util.concurrent.ExecutionException) T2(org.apache.ignite.internal.util.typedef.T2) ClientCompute(org.apache.ignite.client.ClientCompute) 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