use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.
the class GridFutureAdapterSelfTest method testListenNotify.
/**
* @throws Exception If failed.
*/
public void testListenNotify() throws Exception {
GridTestKernalContext ctx = new GridTestKernalContext(log);
ctx.setExecutorService(Executors.newFixedThreadPool(1));
ctx.setSystemExecutorService(Executors.newFixedThreadPool(1));
ctx.add(new PoolProcessor(ctx));
ctx.add(new GridClosureProcessor(ctx));
ctx.start();
try {
GridFutureAdapter<String> fut = new GridFutureAdapter<>();
int lsnrCnt = 10;
final CountDownLatch latch = new CountDownLatch(lsnrCnt);
final Thread runThread = Thread.currentThread();
for (int i = 0; i < lsnrCnt; i++) {
fut.listen(new CI1<IgniteInternalFuture<String>>() {
@Override
public void apply(IgniteInternalFuture<String> t) {
assert Thread.currentThread() == runThread;
latch.countDown();
}
});
}
fut.onDone();
latch.await();
final CountDownLatch doneLatch = new CountDownLatch(1);
fut.listen(new CI1<IgniteInternalFuture<String>>() {
@Override
public void apply(IgniteInternalFuture<String> t) {
assert Thread.currentThread() == runThread;
doneLatch.countDown();
}
});
assert doneLatch.getCount() == 0;
doneLatch.await();
} finally {
ctx.stop(false);
}
}
use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.
the class GridFutureAdapterSelfTest method testChaining.
/**
* Test futures chaining.
*
* @throws Exception In case of any exception.
*/
public void testChaining() throws Exception {
checkChaining(null);
ExecutorService exec = Executors.newFixedThreadPool(1);
try {
checkChaining(exec);
GridFinishedFuture<Integer> fut = new GridFinishedFuture<>(1);
IgniteInternalFuture<Object> chain = fut.chain(new CX1<IgniteInternalFuture<Integer>, Object>() {
@Override
public Object applyx(IgniteInternalFuture<Integer> fut) throws IgniteCheckedException {
return fut.get() + 1;
}
}, exec);
assertEquals(2, chain.get());
} finally {
exec.shutdown();
}
}
use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.
the class GridFutureListenPerformanceTest method main.
/**
* @param args Args.
* @throws InterruptedException If failed.
*/
public static void main(String[] args) throws InterruptedException {
final LongAdder cnt = new LongAdder();
final ConcurrentLinkedDeque<GridFutureAdapter<Object>> futs = new ConcurrentLinkedDeque<>();
ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
Thread statThread = new Thread() {
@SuppressWarnings("BusyWait")
@Override
public void run() {
while (!done) {
try {
Thread.sleep(5000);
} catch (InterruptedException ignored) {
return;
}
System.out.println(new Date() + " Notifications per sec: " + (cnt.sumThenReset() / 5));
}
}
};
statThread.setDaemon(true);
statThread.start();
for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
pool.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
Random rnd = new Random();
while (!done) {
for (int j = 0; j < rnd.nextInt(10); j++) {
GridFutureAdapter<Object> fut = new GridFutureAdapter<>();
futs.add(fut);
for (int k = 1; k < rnd.nextInt(3); k++) {
fut.listen(new IgniteInClosure<IgniteInternalFuture<Object>>() {
@Override
public void apply(IgniteInternalFuture<Object> t) {
try {
t.get();
} catch (IgniteCheckedException e) {
e.printStackTrace();
}
cnt.increment();
}
});
}
}
GridFutureAdapter<Object> fut;
while ((fut = futs.poll()) != null) fut.onDone();
}
return null;
}
});
}
Thread.sleep(5 * 60 * 1000);
done = true;
}
use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.
the class GridNioFutureSelfTest method testListenSyncNotify.
/**
* @throws Exception If failed.
*/
public void testListenSyncNotify() throws Exception {
GridNioFutureImpl<String> fut = new GridNioFutureImpl<>(null);
int lsnrCnt = 10;
final CountDownLatch latch = new CountDownLatch(lsnrCnt);
final Thread runThread = Thread.currentThread();
final AtomicReference<Exception> err = new AtomicReference<>();
for (int i = 0; i < lsnrCnt; i++) {
fut.listen(new CI1<IgniteInternalFuture<String>>() {
@Override
public void apply(IgniteInternalFuture<String> t) {
if (Thread.currentThread() != runThread)
err.compareAndSet(null, new Exception("Wrong notification thread: " + Thread.currentThread()));
latch.countDown();
}
});
}
fut.onDone();
assertEquals(0, latch.getCount());
if (err.get() != null)
throw err.get();
final AtomicBoolean called = new AtomicBoolean();
err.set(null);
fut.listen(new CI1<IgniteInternalFuture<String>>() {
@Override
public void apply(IgniteInternalFuture<String> t) {
if (Thread.currentThread() != runThread)
err.compareAndSet(null, new Exception("Wrong notification thread: " + Thread.currentThread()));
called.set(true);
}
});
assertTrue(called.get());
if (err.get() != null)
throw err.get();
}
use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.
the class ReadWriteLockMultiThreadedTest method testTryWriteLock.
/**
* @throws Exception If failed.
*/
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testTryWriteLock() throws Exception {
final ReadWriteLock lock = new ReentrantReadWriteLock();
lock.readLock().lock();
X.println("Read lock acquired.");
IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
boolean res = lock.writeLock().tryLock();
X.println("Attempting to try write lock: " + res);
try {
return null;
} finally {
if (res)
lock.writeLock().unlock();
}
}
}, 1, "write-lock");
Thread.sleep(2000);
X.println("Read lock released: " + lock);
lock.readLock().unlock();
fut.get();
}
Aggregations