use of org.apache.ignite.IgniteQueue in project ignite by apache.
the class GridCacheQueueApiSelfAbstractTest method testReuseCache.
/**
* @throws Exception If failed.
*/
public void testReuseCache() throws Exception {
CollectionConfiguration colCfg = collectionConfiguration();
IgniteQueue queue1 = grid(0).queue("Queue1", 0, colCfg);
IgniteQueue queue2 = grid(0).queue("Queue2", 0, colCfg);
assertEquals(getQueueCache(queue1), getQueueCache(queue2));
}
use of org.apache.ignite.IgniteQueue in project ignite by apache.
the class IgniteDataStructureUniqueNameTest method testUniqueName.
/**
* @param singleGrid If {@code true} uses single grid.
* @throws Exception If failed.
*/
private void testUniqueName(final boolean singleGrid) throws Exception {
final String name = IgniteUuid.randomUuid().toString();
final int DS_TYPES = 9;
final int THREADS = DS_TYPES * 3;
for (int iter = 0; iter < 20; iter++) {
log.info("Iteration: " + iter);
List<IgniteInternalFuture<Object>> futs = new ArrayList<>(THREADS);
final CyclicBarrier barrier = new CyclicBarrier(THREADS);
for (int i = 0; i < THREADS; i++) {
final int idx = i;
IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
Thread.currentThread().setName("test thread-" + idx);
barrier.await();
Ignite ignite = singleGrid ? ignite(0) : ignite(idx % gridCount());
Object res;
switch(idx % DS_TYPES) {
case 0:
log.info("Create atomic long, grid: " + ignite.name());
res = ignite.atomicLong(name, 0, true);
break;
case 1:
log.info("Create atomic sequence, grid: " + ignite.name());
res = ignite.atomicSequence(name, 0, true);
break;
case 2:
log.info("Create atomic stamped, grid: " + ignite.name());
res = ignite.atomicStamped(name, 0, true, true);
break;
case 3:
log.info("Create atomic latch, grid: " + ignite.name());
res = ignite.countDownLatch(name, 0, true, true);
break;
case 4:
log.info("Create atomic reference, grid: " + ignite.name());
res = ignite.atomicReference(name, null, true);
break;
case 5:
log.info("Create queue, grid: " + ignite.name());
res = ignite.queue(name, 0, config(false));
break;
case 6:
log.info("Create set, grid: " + ignite.name());
res = ignite.set(name, config(false));
break;
case 7:
log.info("Create atomic semaphore, grid: " + ignite.name());
res = ignite.semaphore(name, 0, false, true);
break;
case 8:
log.info("Create atomic reentrant lock, grid: " + ignite.name());
res = ignite.reentrantLock(name, true, true, true);
break;
default:
fail();
return null;
}
log.info("Thread created: " + res);
return res;
} catch (IgniteException e) {
log.info("Failed: " + e);
return e;
}
}
});
futs.add(fut);
}
Closeable dataStructure = null;
int createdCnt = 0;
for (IgniteInternalFuture<Object> fut : futs) {
Object res = fut.get();
if (res instanceof IgniteException || res instanceof IgniteCheckedException)
continue;
assertTrue("Unexpected object: " + res, res instanceof IgniteAtomicLong || res instanceof IgniteAtomicSequence || res instanceof IgniteAtomicReference || res instanceof IgniteAtomicStamped || res instanceof IgniteCountDownLatch || res instanceof IgniteQueue || res instanceof IgniteSet || res instanceof IgniteSemaphore || res instanceof IgniteLock);
log.info("Data structure created: " + dataStructure);
createdCnt++;
if (dataStructure != null)
assertEquals(dataStructure.getClass(), res.getClass());
else
dataStructure = (Closeable) res;
}
assertNotNull(dataStructure);
assertEquals(3, createdCnt);
dataStructure.close();
}
}
use of org.apache.ignite.IgniteQueue in project ignite by apache.
the class IgniteClientDataStructuresAbstractTest method testQueue.
/**
* @param creator Creator node.
* @param other Other node.
* @throws Exception If failed.
*/
private void testQueue(Ignite creator, final Ignite other) throws Exception {
assertNull(creator.queue("q1", 0, null));
assertNull(other.queue("q1", 0, null));
try (IgniteQueue<Integer> queue = creator.queue("q1", 0, new CollectionConfiguration())) {
assertNotNull(queue);
queue.add(1);
assertEquals(1, queue.poll().intValue());
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
U.sleep(1000);
IgniteQueue<Integer> queue0 = other.queue("q1", 0, null);
assertEquals(0, queue0.size());
log.info("Add in queue.");
queue0.add(2);
return null;
}
});
log.info("Try take.");
assertEquals(2, queue.take().intValue());
log.info("Finished take.");
fut.get();
}
assertNull(creator.queue("q1", 0, null));
assertNull(other.queue("q1", 0, null));
}
use of org.apache.ignite.IgniteQueue in project ignite by apache.
the class GridCacheAbstractDataStructuresFailoverSelfTest method doTestQueue.
/**
* Tests the queue.
*
* @param topWorker Topology change worker.
* @throws Exception If failed.
*/
private void doTestQueue(ConstantTopologyChangeWorker topWorker) throws Exception {
int queueMaxSize = 100;
try (IgniteQueue<Integer> s = grid(0).queue(STRUCTURE_NAME, 0, config(false))) {
s.put(1);
IgniteInternalFuture<?> fut = topWorker.startChangingTopology(new IgniteClosure<Ignite, Object>() {
@Override
public Object apply(Ignite ignite) {
IgniteQueue<Integer> queue = ignite.queue(STRUCTURE_NAME, 0, null);
assertNotNull(queue);
Integer val = queue.peek();
assertNotNull(val);
assert val > 0;
return null;
}
});
int val = s.peek();
while (!fut.isDone()) {
if (s.size() == queueMaxSize) {
int last = 0;
for (int i = 0, size = s.size() - 1; i < size; i++) {
int cur = s.poll();
if (i == 0) {
last = cur;
continue;
}
assertEquals(last, cur - 1);
last = cur;
}
}
s.put(++val);
}
fut.get();
val = s.peek();
for (Ignite g : G.allGrids()) assertEquals(val, (int) g.<Integer>queue(STRUCTURE_NAME, 0, null).peek());
}
}
use of org.apache.ignite.IgniteQueue in project ignite by apache.
the class IgniteClientReconnectApiExceptionTest method dataStructureOperationsTest.
/**
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
public void dataStructureOperationsTest() throws Exception {
clientMode = true;
final Ignite client = startGrid(serverCount());
doTestIgniteOperationOnDisconnect(client, Arrays.asList(// Check atomic long.
new T2<Callable, C1<Object, Boolean>>(new Callable() {
@Override
public Object call() throws Exception {
boolean failed = false;
try {
client.atomicLong("testAtomic", 41, true);
} catch (IgniteClientDisconnectedException e) {
failed = true;
checkAndWait(e);
}
assertTrue(failed);
return client.atomicLong("testAtomic", 41, true);
}
}, new C1<Object, Boolean>() {
@Override
public Boolean apply(Object o) {
assertNotNull(o);
IgniteAtomicLong atomicLong = (IgniteAtomicLong) o;
assertEquals(42, atomicLong.incrementAndGet());
return true;
}
}), // Check set.
new T2<Callable, C1<Object, Boolean>>(new Callable() {
@Override
public Object call() throws Exception {
boolean failed = false;
try {
client.set("testSet", new CollectionConfiguration());
} catch (IgniteClientDisconnectedException e) {
failed = true;
checkAndWait(e);
}
assertTrue(failed);
return client.set("testSet", new CollectionConfiguration());
}
}, new C1<Object, Boolean>() {
@Override
public Boolean apply(Object o) {
assertNotNull(o);
IgniteSet set = (IgniteSet) o;
String val = "testVal";
set.add(val);
assertEquals(1, set.size());
assertTrue(set.contains(val));
return true;
}
}), // Check ignite queue.
new T2<Callable, C1<Object, Boolean>>(new Callable() {
@Override
public Object call() throws Exception {
boolean failed = false;
try {
client.queue("TestQueue", 10, new CollectionConfiguration());
} catch (IgniteClientDisconnectedException e) {
failed = true;
checkAndWait(e);
}
assertTrue(failed);
return client.queue("TestQueue", 10, new CollectionConfiguration());
}
}, new C1<Object, Boolean>() {
@Override
public Boolean apply(Object o) {
assertNotNull(o);
IgniteQueue queue = (IgniteQueue) o;
String val = "Test";
queue.add(val);
assertEquals(val, queue.poll());
return true;
}
})));
clientMode = false;
}
Aggregations