Search in sources :

Example 11 with TestThread

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);
}
Also used : TestThread(com.google.devtools.build.lib.testutil.TestThread) Test(org.junit.Test)

Example 12 with TestThread

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestThread(com.google.devtools.build.lib.testutil.TestThread) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 13 with TestThread

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);
    }
}
Also used : TestThread(com.google.devtools.build.lib.testutil.TestThread) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 14 with TestThread

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);
}
Also used : TestThread(com.google.devtools.build.lib.testutil.TestThread) Test(org.junit.Test)

Example 15 with TestThread

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);
    }
}
Also used : TestThread(com.google.devtools.build.lib.testutil.TestThread) CountDownLatch(java.util.concurrent.CountDownLatch) Supplier(com.google.common.base.Supplier) StringValue(com.google.devtools.build.skyframe.GraphTester.StringValue)

Aggregations

TestThread (com.google.devtools.build.lib.testutil.TestThread)22 Test (org.junit.Test)18 CountDownLatch (java.util.concurrent.CountDownLatch)7 Path (com.google.devtools.build.lib.vfs.Path)5 ScopeEscapableFileSystemTest (com.google.devtools.build.lib.vfs.ScopeEscapableFileSystemTest)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 StringValue (com.google.devtools.build.skyframe.GraphTester.StringValue)2 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Supplier (com.google.common.base.Supplier)1 FileSystem (com.google.devtools.build.lib.vfs.FileSystem)1 HashFunction (com.google.devtools.build.lib.vfs.FileSystem.HashFunction)1 InMemoryFileSystem (com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem)1 NotComparableStringValue (com.google.devtools.build.skyframe.GraphTester.NotComparableStringValue)1 Environment (com.google.devtools.build.skyframe.SkyFunction.Environment)1 IOException (java.io.IOException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1