Search in sources :

Example 1 with RepeatedTest

use of org.eclipse.xtext.testing.RepeatedTest 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)

Example 2 with RepeatedTest

use of org.eclipse.xtext.testing.RepeatedTest in project xtext-core by eclipse.

the class RequestManagerTest method testRunWriteAfterReadStarted.

@Test(timeout = 1000)
@RepeatedTest(times = 50)
public void testRunWriteAfterReadStarted() throws Exception {
    CountDownLatch readStarted = new CountDownLatch(1);
    requestManager.runRead((CancelIndicator it) -> {
        readStarted.countDown();
        return sharedState.incrementAndGet();
    });
    Uninterruptibles.awaitUninterruptibly(readStarted);
    requestManager.runWrite(() -> null, (CancelIndicator $0, Object $1) -> {
        Assert.assertEquals(1, sharedState.get());
        return sharedState.incrementAndGet();
    }).join();
    Assert.assertEquals(2, sharedState.get());
}
Also used : CancelIndicator(org.eclipse.xtext.util.CancelIndicator) CountDownLatch(java.util.concurrent.CountDownLatch) RepeatedTest(org.eclipse.xtext.testing.RepeatedTest) Test(org.junit.Test) RepeatedTest(org.eclipse.xtext.testing.RepeatedTest)

Example 3 with RepeatedTest

use of org.eclipse.xtext.testing.RepeatedTest in project xtext-eclipse by eclipse.

the class TestedWorkspace method apply.

@Override
public Statement apply(Statement statement, Description description) {
    Statement result = super.apply(statement, description);
    // Apparently there is no good solution to order TestRules
    // that's why we do inline the (simple) logic for RepeatedTest here.
    RepeatedTest repeat = description.getAnnotation(RepeatedTest.class);
    if (repeat == null) {
        repeat = description.getTestClass().getAnnotation(RepeatedTest.class);
    }
    if (repeat != null) {
        result = new RepeatedTest.Rule.RepeatedTestStatement(repeat.times(), result, description, false);
    }
    return result;
}
Also used : RepeatedTest(org.eclipse.xtext.testing.RepeatedTest) Statement(org.junit.runners.model.Statement) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule)

Example 4 with RepeatedTest

use of org.eclipse.xtext.testing.RepeatedTest in project xtext-core by eclipse.

the class RequestManagerTest method testRunWriteBeforeReadStarted.

@Test(timeout = 1000)
@RepeatedTest(times = 50)
public void testRunWriteBeforeReadStarted() throws Exception {
    CountDownLatch writeSubmitted = new CountDownLatch(1);
    AtomicBoolean firstWriteDone = new AtomicBoolean();
    requestManager.runWrite(() -> {
        Uninterruptibles.awaitUninterruptibly(writeSubmitted);
        firstWriteDone.set(true);
        return null;
    }, (CancelIndicator $0, Object $1) -> {
        return Integer.valueOf(sharedState.incrementAndGet());
    });
    requestManager.runRead((CancelIndicator it) -> {
        return Integer.valueOf(sharedState.incrementAndGet());
    });
    CompletableFuture<Integer> joinMe = requestManager.runWrite(() -> null, (CancelIndicator $0, Object $1) -> {
        Assert.assertEquals(0, sharedState.get());
        Assert.assertTrue(firstWriteDone.get());
        return sharedState.incrementAndGet();
    });
    writeSubmitted.countDown();
    joinMe.join();
    Assert.assertTrue(firstWriteDone.get());
    Assert.assertEquals(1, sharedState.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) CountDownLatch(java.util.concurrent.CountDownLatch) RepeatedTest(org.eclipse.xtext.testing.RepeatedTest) Test(org.junit.Test) RepeatedTest(org.eclipse.xtext.testing.RepeatedTest)

Aggregations

RepeatedTest (org.eclipse.xtext.testing.RepeatedTest)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)3 Test (org.junit.Test)3 CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)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 Statement (org.junit.runners.model.Statement)1