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());
}
}
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());
}
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;
}
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());
}
Aggregations