use of com.google.devtools.build.lib.testutil.TestThread in project bazel by bazelbuild.
the class GrpcServerTest method testDeadlockWhenDisconnectedWithQueueFull.
@Test
public void testDeadlockWhenDisconnectedWithQueueFull() throws Exception {
MockObserver observer = new MockObserver();
final GrpcServerImpl.GrpcSink sink = new GrpcServerImpl.GrpcSink("Dummy", observer, executor);
observer.ready.set(false);
TestThread sender = new TestThread() {
@Override
public void runTest() {
// Should return false due to the disconnect
assertThat(sink.offer(runResponse())).isFalse();
}
};
sender.setDaemon(true);
sender.start();
// Wait until the sink thread has processed the SEND message from #offer()
while (sink.getReceivedEventCount() < 1) {
Thread.sleep(200);
}
// Disconnect while there is an item pending
observer.onCancelHandler.run();
// Make sure that both the sink and the sender thread finish
assertThat(sink.finish()).isTrue();
sender.joinAndAssertState(TestUtils.WAIT_TIMEOUT_MILLISECONDS);
}
use of com.google.devtools.build.lib.testutil.TestThread in project bazel by bazelbuild.
the class GrpcServerTest method testInterruptsCommandThreadOnCancellation.
@Test
public void testInterruptsCommandThreadOnCancellation() throws Exception {
final CountDownLatch safety = new CountDownLatch(1);
final AtomicBoolean interrupted = new AtomicBoolean(false);
TestThread victim = new TestThread() {
@Override
public void runTest() throws Exception {
try {
safety.await();
fail("Test thread finished unexpectedly");
} catch (InterruptedException e) {
interrupted.set(true);
}
}
};
victim.setDaemon(true);
victim.start();
MockObserver observer = new MockObserver();
GrpcServerImpl.GrpcSink sink = new GrpcServerImpl.GrpcSink("Dummy", observer, executor);
sink.setCommandThread(victim);
observer.cancelled.set(true);
observer.onCancelHandler.run();
assertThat(sink.offer(runResponse())).isFalse();
assertThat(sink.finish()).isTrue();
safety.countDown();
victim.joinAndAssertState(1000);
assertThat(interrupted.get()).isTrue();
}
use of com.google.devtools.build.lib.testutil.TestThread in project bazel by bazelbuild.
the class PersistentStringIndexerTest method writeLotsOfEntriesConcurrently.
/**
* Writes lots of entries with labels "fooconcurrent[int]" at the same time.
* The set of labels written is deterministic, but the label:index mapping is
* not.
*/
private void writeLotsOfEntriesConcurrently(final int numToWrite) throws InterruptedException {
final int NUM_THREADS = 10;
final CountDownLatch synchronizerLatch = new CountDownLatch(NUM_THREADS);
class IndexAdder extends TestThread {
@Override
public void runTest() throws Exception {
for (int i = 0; i < numToWrite; i++) {
synchronizerLatch.countDown();
synchronizerLatch.await();
String value = "fooconcurrent" + i;
mappings.put(psi.getOrCreateIndex(value), value);
}
}
}
Collection<TestThread> threads = new ArrayList<>();
for (int i = 0; i < NUM_THREADS; i++) {
TestThread thread = new IndexAdder();
thread.start();
threads.add(thread);
}
for (TestThread thread : threads) {
thread.joinAndAssertState(0);
}
}
use of com.google.devtools.build.lib.testutil.TestThread in project bazel by bazelbuild.
the class GrpcServerTest method testObeysReadySignal.
@Test
public void testObeysReadySignal() throws Exception {
MockObserver observer = new MockObserver();
final GrpcServerImpl.GrpcSink sink = new GrpcServerImpl.GrpcSink("Dummy", observer, executor);
// First check if we can send a simple message
assertThat(sink.offer(runResponse())).isTrue();
observer.waitForMessages(1, 100, TimeUnit.MILLISECONDS);
observer.ready.set(false);
TestThread sender = new TestThread() {
@Override
public void runTest() {
assertThat(sink.offer(runResponse())).isTrue();
}
};
sender.setDaemon(true);
sender.start();
// Give the sender a little time to actually send the message
Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
assertThat(observer.getMessageCount()).isEqualTo(1);
// Let the sink loose again
observer.ready.set(true);
observer.onReadyHandler.run();
// Wait until the sender thread finishes and verify that the message was sent.
assertThat(sink.finish()).isFalse();
sender.joinAndAssertState(1000);
observer.waitForMessages(2, 100, TimeUnit.MILLISECONDS);
}
use of com.google.devtools.build.lib.testutil.TestThread in project bazel by bazelbuild.
the class ParallelEvaluatorTest method runPartialResultOnInterruption.
private void runPartialResultOnInterruption(boolean buildFastFirst) throws Exception {
graph = new InMemoryGraphImpl();
// Two runs for fastKey's builder and one for the start of waitKey's builder.
final CountDownLatch allValuesReady = new CountDownLatch(3);
final SkyKey waitKey = GraphTester.toSkyKey("wait");
final SkyKey fastKey = GraphTester.toSkyKey("fast");
SkyKey leafKey = GraphTester.toSkyKey("leaf");
tester.getOrCreate(waitKey).setBuilder(new SkyFunction() {
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException {
allValuesReady.countDown();
Thread.sleep(10000);
throw new AssertionError("Should have been interrupted");
}
@Override
public String extractTag(SkyKey skyKey) {
return null;
}
});
tester.getOrCreate(fastKey).setBuilder(new ChainedFunction(null, null, allValuesReady, false, new StringValue("fast"), ImmutableList.of(leafKey)));
tester.set(leafKey, new StringValue("leaf"));
if (buildFastFirst) {
eval(/*keepGoing=*/
false, fastKey);
}
final Set<SkyKey> receivedValues = Sets.newConcurrentHashSet();
revalidationReceiver = new DirtyTrackingProgressReceiver(new EvaluationProgressReceiver() {
@Override
public void invalidated(SkyKey skyKey, InvalidationState state) {
}
@Override
public void enqueueing(SkyKey key) {
}
@Override
public void computed(SkyKey skyKey, long elapsedTimeNanos) {
}
@Override
public void evaluated(SkyKey skyKey, Supplier<SkyValue> skyValueSupplier, EvaluationState state) {
receivedValues.add(skyKey);
}
});
TestThread evalThread = new TestThread() {
@Override
public void runTest() throws Exception {
try {
eval(/*keepGoing=*/
true, waitKey, fastKey);
fail();
} catch (InterruptedException e) {
// Expected.
}
}
};
evalThread.start();
assertTrue(allValuesReady.await(TestUtils.WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS));
evalThread.interrupt();
evalThread.join(TestUtils.WAIT_TIMEOUT_MILLISECONDS);
assertFalse(evalThread.isAlive());
if (buildFastFirst) {
// If leafKey was already built, it is not reported to the receiver.
assertThat(receivedValues).containsExactly(fastKey);
} else {
// On first time being built, leafKey is registered too.
assertThat(receivedValues).containsExactly(fastKey, leafKey);
}
}
Aggregations