use of org.eclipse.xtext.ide.server.concurrent.WriteRequest in project xtext-core by eclipse.
the class RequestManagerTest method testReadCommandSubmitsWriteCommand.
/**
* The tests assumes an implementation of a Command that has access to the
* request manager
*/
@Test(timeout = 5000)
@RepeatedTest(times = 50)
public void testReadCommandSubmitsWriteCommand() throws Exception {
Thread mainThread = Thread.currentThread();
CountDownLatch submittedFromMain = new CountDownLatch(1);
CountDownLatch addedFromReader = new CountDownLatch(1);
AtomicReference<Thread> readerThreadRef = new AtomicReference<Thread>();
RequestManager myRequestManager = new RequestManager(executorServiceProvider.get(), cancelManagerProvider.get()) {
@Override
protected void addRequest(AbstractRequest<?> request) {
if (((request instanceof WriteRequest) && Objects.equal(Thread.currentThread(), readerThreadRef.get()))) {
super.addRequest(request);
addedFromReader.countDown();
Uninterruptibles.awaitUninterruptibly(submittedFromMain, 100, TimeUnit.MILLISECONDS);
} else {
super.addRequest(request);
}
}
@Override
protected void submitRequest(AbstractRequest<?> request) {
if (((request instanceof WriteRequest) && Objects.equal(Thread.currentThread(), mainThread))) {
super.submitRequest(request);
submittedFromMain.countDown();
} else {
super.submitRequest(request);
}
}
@Override
protected CompletableFuture<Void> cancel() {
if (Objects.equal(Thread.currentThread(), mainThread)) {
Uninterruptibles.awaitUninterruptibly(addedFromReader, 100, TimeUnit.MILLISECONDS);
}
return super.cancel();
}
};
CountDownLatch threadSet = new CountDownLatch(1);
CompletableFuture<CompletableFuture<Object>> readResult = myRequestManager.runRead((CancelIndicator it) -> {
readerThreadRef.set(Thread.currentThread());
threadSet.countDown();
return myRequestManager.runWrite(() -> null, (ci, o) -> null);
});
Uninterruptibles.awaitUninterruptibly(threadSet);
Assert.assertNotNull(readerThreadRef.get());
CompletableFuture<Object> writeResult = myRequestManager.runWrite(() -> null, (ci, o) -> null);
CompletableFuture<Object> writeFromReader = readResult.get();
try {
writeFromReader.get();
try {
writeResult.get();
} catch (CancellationException e) {
Assert.assertTrue(writeFromReader.isDone());
Assert.assertTrue(writeResult.isDone());
Assert.assertTrue(writeFromReader.isCancelled() != writeResult.isCancelled());
}
} catch (CancellationException e) {
writeResult.get();
Assert.assertTrue(writeFromReader.isDone());
Assert.assertTrue(writeResult.isDone());
Assert.assertTrue(writeFromReader.isCancelled() != writeResult.isCancelled());
}
}
Aggregations