use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class IgnitePersistentStoreDataStructuresTest method testSemaphoreVolatility.
/**
* @throws Exception If failed.
*/
public void testSemaphoreVolatility() throws Exception {
Ignite ignite = startGrids(4);
ignite.active(true);
IgniteSemaphore sem = ignite.semaphore("test", 10, false, true);
assert sem != null;
stopAllGrids();
ignite = startGrids(4);
ignite.active(true);
sem = ignite.semaphore("test", 10, false, false);
assert sem == null;
}
use of org.apache.ignite.IgniteSemaphore 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 = 6;
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 reference, grid: " + ignite.name());
res = ignite.atomicReference(name, null, true);
break;
case 4:
log.info("Create queue, grid: " + ignite.name());
res = ignite.queue(name, 0, config(false));
break;
case 5:
log.info("Create set, grid: " + ignite.name());
res = ignite.set(name, config(false));
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.IgniteSemaphore in project ignite by apache.
the class IgniteClientDataStructuresAbstractTest method testSemaphore.
/**
* @param creator Creator node.
* @param other Other node.
* @throws Exception If failed.
*/
private void testSemaphore(Ignite creator, final Ignite other) throws Exception {
assertNull(creator.semaphore("semaphore1", 1, true, false));
assertNull(other.semaphore("semaphore1", 1, true, false));
try (IgniteSemaphore semaphore = creator.semaphore("semaphore1", -1, true, true)) {
assertNotNull(semaphore);
assertEquals(-1, semaphore.availablePermits());
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
U.sleep(1000);
IgniteSemaphore semaphore0 = other.semaphore("semaphore1", -1, true, false);
assertEquals(-1, semaphore0.availablePermits());
log.info("Release semaphore.");
semaphore0.release(2);
return null;
}
});
log.info("Acquire semaphore.");
assertTrue(semaphore.tryAcquire(1, 5000, TimeUnit.MILLISECONDS));
log.info("Finished wait.");
fut.get();
assertEquals(0, semaphore.availablePermits());
}
assertNull(creator.semaphore("semaphore1", 1, true, false));
assertNull(other.semaphore("semaphore1", 1, true, false));
}
use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class IgniteSemaphoreAbstractSelfTest method checkAcquire.
/**
* @throws Exception Exception.
*/
private void checkAcquire() throws Exception {
// Check only 'false' cases here. Successful await is tested over the grid.
IgniteSemaphore semaphore = createSemaphore("acquire", 5, false);
assert !semaphore.tryAcquire(10);
assert !semaphore.tryAcquire(10, 10, MICROSECONDS);
removeSemaphore("acquire");
}
use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class IgniteSemaphoreAbstractSelfTest method checkSemaphoreSerialization.
/**
* @throws Exception If failed.
*/
private void checkSemaphoreSerialization() throws Exception {
final IgniteSemaphore sem = grid(0).semaphore("semaphore", -gridCount() + 1, true, true);
assertEquals(-gridCount() + 1, sem.availablePermits());
grid(0).compute().broadcast(new IgniteCallable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
sem.release();
return null;
}
});
assert sem.availablePermits() == 1;
sem.acquire();
assert sem.availablePermits() == 0;
sem.release();
// Test operations on removed semaphore.
sem.close();
checkRemovedSemaphore(sem);
}
Aggregations