Search in sources :

Example 1 with AbstractRequest

use of org.eclipse.xtext.ide.server.concurrent.AbstractRequest 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());
    }
}
Also used : WriteRequest(org.eclipse.xtext.ide.server.concurrent.WriteRequest) AbstractRequest(org.eclipse.xtext.ide.server.concurrent.AbstractRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java.util.concurrent.CompletableFuture) RequestManager(org.eclipse.xtext.ide.server.concurrent.RequestManager) CancellationException(java.util.concurrent.CancellationException) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) RepeatedTest(org.eclipse.xtext.testing.RepeatedTest) Test(org.junit.Test) RepeatedTest(org.eclipse.xtext.testing.RepeatedTest)

Aggregations

CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AbstractRequest (org.eclipse.xtext.ide.server.concurrent.AbstractRequest)1 RequestManager (org.eclipse.xtext.ide.server.concurrent.RequestManager)1 WriteRequest (org.eclipse.xtext.ide.server.concurrent.WriteRequest)1 RepeatedTest (org.eclipse.xtext.testing.RepeatedTest)1 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)1 Test (org.junit.Test)1