use of org.apache.ignite.client.IgniteClient in project ignite by apache.
the class CacheEventSecurityContextTest method testIgniteClient.
/**
* Tests cache event security context in case operation is initiated from the {@link IgniteClient}.
*/
@Test
public void testIgniteClient() throws Exception {
operationInitiatorLogin = "thin_client";
ClientConfiguration cfg = new ClientConfiguration().setAddresses(Config.SERVER).setUserName(operationInitiatorLogin).setUserPassword("");
try (IgniteClient cli = Ignition.startClient(cfg)) {
ClientCache<Integer, String> cache = cli.cache(cacheName);
checkEvents(cli, k -> cache.put(k, "val"), false, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, k -> cache.putAsync(k, "val").get(), false, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, k -> cache.putAll(singletonMap(k, "val")), false, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, k -> cache.putAllAsync(singletonMap(k, "val")).get(), false, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, cache::remove, true, EVT_CACHE_OBJECT_REMOVED);
checkEvents(cli, k -> cache.removeAsync(k).get(), true, EVT_CACHE_OBJECT_REMOVED);
checkEvents(cli, k -> cache.remove(k, "val"), true, EVT_CACHE_OBJECT_REMOVED);
// TODO Add test case inside transaction after resolving IGNITE-14317.
checkEvents(k -> cache.removeAsync(k, "val").get(), true, EVT_CACHE_OBJECT_REMOVED);
checkEvents(cli, k -> cache.removeAll(of(k)), true, EVT_CACHE_OBJECT_REMOVED);
checkEvents(cli, k -> cache.removeAllAsync(of(k)).get(), true, EVT_CACHE_OBJECT_REMOVED);
checkEvents(cli, k -> cache.removeAll(), true, EVT_CACHE_OBJECT_REMOVED);
checkEvents(cli, k -> cache.removeAllAsync().get(), true, EVT_CACHE_OBJECT_REMOVED);
checkEvents(cli, k -> cache.putIfAbsent(k, "val"), false, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, k -> cache.putIfAbsentAsync(k, "val").get(), false, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, k -> cache.getAndPut(k, "val"), true, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, k -> cache.getAndPutAsync(k, "val").get(), true, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, cache::get, true, EVT_CACHE_OBJECT_READ);
checkEvents(cli, k -> cache.getAsync(k).get(), true, EVT_CACHE_OBJECT_READ);
checkEvents(cli, k -> cache.getAll(of(k)), true, EVT_CACHE_OBJECT_READ);
checkEvents(cli, k -> cache.getAllAsync(of(k)).get(), true, EVT_CACHE_OBJECT_READ);
checkEvents(cli, k -> cache.getAndReplace(k, "val"), true, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, k -> cache.getAndReplaceAsync(k, "val").get(), true, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, cache::getAndRemove, true, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_REMOVED);
checkEvents(cli, k -> cache.getAndRemoveAsync(k).get(), true, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_REMOVED);
checkEvents(cli, k -> cache.replace(k, "new_val"), true, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, k -> cache.replaceAsync(k, "new_val").get(), true, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, k -> cache.replace(k, "val", "new_val"), true, EVT_CACHE_OBJECT_PUT);
checkEvents(cli, k -> cache.replaceAsync(k, "val", "new_val").get(), true, EVT_CACHE_OBJECT_PUT);
checkEvents(() -> cache.query(new ScanQuery<>()).getAll(), asList(EVT_CACHE_QUERY_EXECUTED, EVT_CACHE_QUERY_OBJECT_READ));
}
}
use of org.apache.ignite.client.IgniteClient in project ignite by apache.
the class TimeoutTest method testClientTimeoutOnOperation.
/**
* Test client timeout on operation.
*/
@Test
@SuppressWarnings("ThrowableNotThrown")
public void testClientTimeoutOnOperation() throws Exception {
try (Ignite ignite = startGrid(0)) {
try (IgniteClient client = startClient(0)) {
ClientCache<Object, Object> cache = client.getOrCreateCache(new ClientCacheConfiguration().setName("cache").setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));
doSleep(TIMEOUT * 2);
// Should not fail if connection is idle.
cache.put(0, 0);
CyclicBarrier barrier = new CyclicBarrier(2);
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> {
try (ClientTransaction tx = client.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.put(0, 0);
barrier.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
barrier.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
} catch (Exception e) {
throw new IgniteException(e);
}
});
// Wait for the key locked.
barrier.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
long ts0 = System.currentTimeMillis();
try (ClientTransaction tx = client.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
try {
GridTestUtils.assertThrowsWithCause(() -> cache.put(0, 0), ClientException.class);
} finally {
// To unlock another thread.
barrier.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
}
}
long ts1 = System.currentTimeMillis();
assertTrue("Unexpected timeout [ts0=" + ts0 + ", ts1=" + ts1 + ']', ts1 - ts0 >= TIMEOUT && ts1 - ts0 < TIMEOUT * 2);
fut.get();
}
}
}
use of org.apache.ignite.client.IgniteClient in project ignite by apache.
the class SystemViewSelfTest method testClientsConnections.
/**
*/
@Test
public void testClientsConnections() throws Exception {
try (IgniteEx g0 = startGrid(0)) {
String host = g0.configuration().getClientConnectorConfiguration().getHost();
if (host == null)
host = g0.configuration().getLocalHost();
int port = g0.configuration().getClientConnectorConfiguration().getPort();
SystemView<ClientConnectionView> conns = g0.context().systemView().view(CLI_CONN_VIEW);
try (IgniteClient cli = Ignition.startClient(new ClientConfiguration().setAddresses(host + ":" + port))) {
assertEquals(1, conns.size());
ClientConnectionView cliConn = conns.iterator().next();
assertEquals("THIN", cliConn.type());
assertEquals(cliConn.localAddress().getHostName(), cliConn.remoteAddress().getHostName());
assertEquals(g0.configuration().getClientConnectorConfiguration().getPort(), cliConn.localAddress().getPort());
assertEquals(cliConn.version(), ProtocolVersion.LATEST_VER.toString());
try (Connection conn = new IgniteJdbcThinDriver().connect("jdbc:ignite:thin://" + host, new Properties())) {
assertEquals(2, conns.size());
assertEquals(1, F.size(jdbcConnectionsIterator(conns)));
ClientConnectionView jdbcConn = jdbcConnectionsIterator(conns).next();
assertEquals("JDBC", jdbcConn.type());
assertEquals(jdbcConn.localAddress().getHostName(), jdbcConn.remoteAddress().getHostName());
assertEquals(g0.configuration().getClientConnectorConfiguration().getPort(), jdbcConn.localAddress().getPort());
assertEquals(jdbcConn.version(), JdbcConnectionContext.CURRENT_VER.asString());
}
}
boolean res = GridTestUtils.waitForCondition(() -> conns.size() == 0, 5_000);
assertTrue(res);
}
}
use of org.apache.ignite.client.IgniteClient in project ignite by apache.
the class ComputeTaskRemoteSecurityContextTest method testIgniteClient.
/**
* Tests task execution security context in case task was initiated from the {@link IgniteClient}.
*/
@Test
public void testIgniteClient() throws Exception {
String login = "thin_client";
ClientConfiguration cfg = new ClientConfiguration().setAddresses(Config.SERVER).setUserName(login).setUserPassword("");
try (IgniteClient cli = Ignition.startClient(cfg)) {
ClientCompute comp = cli.compute(cli.cluster().forNodes(cli.cluster().nodes()));
if (failWithTimeout)
comp = comp.withTimeout(TEST_TASK_TIMEOUT);
String taskName = mapAsync ? MapAsyncTestTask.class.getName() : TestTask.class.getName();
Throwable timeoutE = null;
try {
if (async)
comp.executeAsync2(taskName, login).get();
else
comp.execute(taskName, login);
checkTaskEvents("crd", login, REDUCER_SUCCEEDED_TASK_EVENTS, MAP_NODE_SUCCEEDED_TASK_EVENTS);
} catch (Throwable e) {
if (!failWithTimeout)
throw e;
timeoutE = e;
}
if (failWithTimeout) {
assertNotNull(timeoutE);
assertTrue(X.hasCause(timeoutE, "Task timed out", ClientServerError.class));
checkTaskEvents("crd", login, REDUCER_FAILED_TASK_EVENTS, MAP_NODE_FAILED_TASK_EVENTS);
}
}
}
use of org.apache.ignite.client.IgniteClient in project ignite by apache.
the class CacheEntryListenersTest method testListenersWithKeepBinary.
/**
* Test continuous queries and JCache entry listeners with keep binary flag.
*/
@Test
public void testListenersWithKeepBinary() throws Exception {
try (IgniteClient client = startClient(0, 1, 2)) {
ClientCache<Object, Object> cache1 = client.getOrCreateCache("testListenersWithKB");
ClientCache<Object, Object> cache2 = cache1.withKeepBinary();
ContinuousQueryListener<Object, Object> lsnr1 = new ContinuousQueryListener<>();
ContinuousQueryListener<Object, Object> lsnr2 = new ContinuousQueryListener<>();
cache1.query(new ContinuousQuery<>().setLocalListener(lsnr1));
cache2.query(new ContinuousQuery<>().setLocalListener(lsnr2));
JCacheEntryListener<Object, Object> lsnr3 = new JCacheEntryListener<>();
JCacheEntryListener<Object, Object> lsnr4 = new JCacheEntryListener<>();
cache1.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr3, null, true, false));
cache2.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr4, null, true, false));
Person person1 = new Person(0, "name");
Person person2 = new Person(1, "another name");
cache1.put(0, person1);
lsnr1.assertNextCacheEvent(EventType.CREATED, 0, person1);
lsnr2.assertNextCacheEvent(EventType.CREATED, 0, client.binary().toBinary(person1));
lsnr3.assertNextCacheEvent(EventType.CREATED, 0, person1);
lsnr4.assertNextCacheEvent(EventType.CREATED, 0, client.binary().toBinary(person1));
cache1.put(0, person2);
lsnr1.assertNextCacheEvent(EventType.UPDATED, 0, person2);
lsnr2.assertNextCacheEvent(EventType.UPDATED, 0, client.binary().toBinary(person2));
lsnr3.assertNextCacheEvent(EventType.UPDATED, 0, person2);
lsnr4.assertNextCacheEvent(EventType.UPDATED, 0, client.binary().toBinary(person2));
}
}
Aggregations