Search in sources :

Example 6 with TestThread

use of com.google.devtools.build.lib.testutil.TestThread in project bazel by bazelbuild.

the class ResourceManagerTest method testThatRamCannotBeOverallocated.

@Test
public void testThatRamCannotBeOverallocated() throws Exception {
    assertFalse(rm.inUse());
    // Given RAM is partially acquired:
    acquire(500, 0, 0, 0);
    // When a request for RAM is made that would slightly overallocate RAM,
    // Then the request fails:
    TestThread thread1 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            assertThat(acquireNonblocking(600, 0, 0, 0)).isNull();
        }
    };
    thread1.start();
    thread1.joinAndAssertState(10000);
}
Also used : TestThread(com.google.devtools.build.lib.testutil.TestThread) Test(org.junit.Test)

Example 7 with TestThread

use of com.google.devtools.build.lib.testutil.TestThread in project bazel by bazelbuild.

the class ResourceManagerTest method testConcurrentLargeRequests.

@Test
public void testConcurrentLargeRequests() throws Exception {
    assertFalse(rm.inUse());
    TestThread thread1 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            acquire(2000, 2, 0, 0);
            sync.await();
            validate(1);
            sync.await();
            // Wait till other thread will be locked.
            while (rm.getWaitCount() == 0) {
                Thread.yield();
            }
            release(2000, 2, 0, 0);
            assertEquals(0, rm.getWaitCount());
            // Will be blocked by the thread2.
            acquire(2000, 2, 0, 0);
            validate(3);
            release(2000, 2, 0, 0);
        }
    };
    TestThread thread2 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            sync2.await();
            assertFalse(rm.isAvailable(2000, 2, 0, 0));
            // Will be blocked by the thread1.
            acquire(2000, 2, 0, 0);
            validate(2);
            sync2.await();
            // Wait till other thread will be locked.
            while (rm.getWaitCount() == 0) {
                Thread.yield();
            }
            release(2000, 2, 0, 0);
        }
    };
    thread1.start();
    thread2.start();
    sync.await(1, TimeUnit.SECONDS);
    assertTrue(rm.inUse());
    assertEquals(0, rm.getWaitCount());
    sync2.await(1, TimeUnit.SECONDS);
    sync.await(1, TimeUnit.SECONDS);
    sync2.await(1, TimeUnit.SECONDS);
    thread1.joinAndAssertState(1000);
    thread2.joinAndAssertState(1000);
    assertFalse(rm.inUse());
}
Also used : TestThread(com.google.devtools.build.lib.testutil.TestThread) Test(org.junit.Test)

Example 8 with TestThread

use of com.google.devtools.build.lib.testutil.TestThread in project bazel by bazelbuild.

the class ConcurrentMultimapWithHeadElementTest method testKeyRemovedAndAddedConcurrently.

@Test
public void testKeyRemovedAndAddedConcurrently() throws Exception {
    final ConcurrentMultimapWithHeadElement<String, String> multimap = new ConcurrentMultimapWithHeadElement<>();
    // chance of failure in 10,000 runs.
    for (int i = 0; i < 10000; i++) {
        assertEquals("val", multimap.putAndGet("key", "val"));
        final CountDownLatch threadStart = new CountDownLatch(1);
        TestThread testThread = new TestThread() {

            @Override
            public void runTest() throws Exception {
                threadStart.countDown();
                multimap.remove("key", "val");
            }
        };
        testThread.start();
        assertTrue(threadStart.await(TestUtils.WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS));
        // Removal may not have happened yet.
        assertNotNull(multimap.putAndGet("key", "val2"));
        // If put failed, this will be null.
        assertNotNull(multimap.get("key"));
        testThread.joinAndAssertState(2000);
        multimap.clear();
    }
}
Also used : TestThread(com.google.devtools.build.lib.testutil.TestThread) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 9 with TestThread

use of com.google.devtools.build.lib.testutil.TestThread in project bazel by bazelbuild.

the class InMemoryFileSystemTest method testConcurrentTreeConstruction.

/**
   * Tests concurrent creation of a substantial tree hierarchy including
   * files, directories, symlinks, file contents, and permissions.
   */
@Test
public void testConcurrentTreeConstruction() throws Exception {
    final int NUM_TO_WRITE = 10000;
    final AtomicInteger baseSelector = new AtomicInteger();
    // 1) Define the intended path structure.
    class PathCreator extends TestThread {

        @Override
        public void runTest() throws Exception {
            Path base = testFS.getPath("/base" + baseSelector.getAndIncrement());
            base.createDirectory();
            for (int i = 0; i < NUM_TO_WRITE; i++) {
                Path subdir1 = base.getRelative("subdir1_" + i);
                subdir1.createDirectory();
                Path subdir2 = base.getRelative("subdir2_" + i);
                subdir2.createDirectory();
                Path file = base.getRelative("somefile" + i);
                writeToFile(file, TEST_FILE_DATA);
                subdir1.setReadable(true);
                subdir2.setReadable(false);
                file.setReadable(true);
                subdir1.setWritable(false);
                subdir2.setWritable(true);
                file.setWritable(false);
                subdir1.setExecutable(false);
                subdir2.setExecutable(true);
                file.setExecutable(false);
                subdir1.setLastModifiedTime(100);
                subdir2.setLastModifiedTime(200);
                file.setLastModifiedTime(300);
                Path symlink = base.getRelative("symlink" + i);
                symlink.createSymbolicLink(file);
            }
        }
    }
    // 2) Construct the tree.
    Collection<TestThread> threads = Lists.newArrayListWithCapacity(NUM_THREADS_FOR_CONCURRENCY_TESTS);
    for (int i = 0; i < NUM_THREADS_FOR_CONCURRENCY_TESTS; i++) {
        TestThread thread = new PathCreator();
        thread.start();
        threads.add(thread);
    }
    for (TestThread thread : threads) {
        thread.joinAndAssertState(0);
    }
    // 3) Define the validation logic.
    class PathValidator extends TestThread {

        @Override
        public void runTest() throws Exception {
            Path base = testFS.getPath("/base" + baseSelector.getAndIncrement());
            assertTrue(base.exists());
            assertFalse(base.getRelative("notreal").exists());
            for (int i = 0; i < NUM_TO_WRITE; i++) {
                Path subdir1 = base.getRelative("subdir1_" + i);
                assertTrue(subdir1.exists());
                assertTrue(subdir1.isDirectory());
                assertTrue(subdir1.isReadable());
                assertFalse(subdir1.isWritable());
                assertFalse(subdir1.isExecutable());
                assertEquals(100, subdir1.getLastModifiedTime());
                Path subdir2 = base.getRelative("subdir2_" + i);
                assertTrue(subdir2.exists());
                assertTrue(subdir2.isDirectory());
                assertFalse(subdir2.isReadable());
                assertTrue(subdir2.isWritable());
                assertTrue(subdir2.isExecutable());
                assertEquals(200, subdir2.getLastModifiedTime());
                Path file = base.getRelative("somefile" + i);
                assertTrue(file.exists());
                assertTrue(file.isFile());
                assertTrue(file.isReadable());
                assertFalse(file.isWritable());
                assertFalse(file.isExecutable());
                assertEquals(300, file.getLastModifiedTime());
                BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream(), Charset.defaultCharset()));
                assertEquals(TEST_FILE_DATA, reader.readLine());
                assertNull(reader.readLine());
                Path symlink = base.getRelative("symlink" + i);
                assertTrue(symlink.exists());
                assertTrue(symlink.isSymbolicLink());
                assertEquals(file.asFragment(), symlink.readSymbolicLink());
            }
        }
    }
    // 4) Validate the results.
    baseSelector.set(0);
    threads = Lists.newArrayListWithCapacity(NUM_THREADS_FOR_CONCURRENCY_TESTS);
    for (int i = 0; i < NUM_THREADS_FOR_CONCURRENCY_TESTS; i++) {
        TestThread thread = new PathValidator();
        thread.start();
        threads.add(thread);
    }
    for (TestThread thread : threads) {
        thread.joinAndAssertState(0);
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) TestThread(com.google.devtools.build.lib.testutil.TestThread) InputStreamReader(java.io.InputStreamReader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BufferedReader(java.io.BufferedReader) Test(org.junit.Test) ScopeEscapableFileSystemTest(com.google.devtools.build.lib.vfs.ScopeEscapableFileSystemTest)

Example 10 with TestThread

use of com.google.devtools.build.lib.testutil.TestThread in project bazel by bazelbuild.

the class AbstractQueueVisitorTest method interruptionWithoutInterruptingWorkers.

@Test
public void interruptionWithoutInterruptingWorkers() throws Exception {
    final Thread mainThread = Thread.currentThread();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final boolean[] workerThreadCompleted = { false };
    final ConcreteQueueVisitor visitor = new ConcreteQueueVisitor();
    visitor.execute(new Runnable() {

        @Override
        public void run() {
            try {
                latch1.countDown();
                latch2.await();
                workerThreadCompleted[0] = true;
            } catch (InterruptedException e) {
            // Do not set workerThreadCompleted to true
            }
        }
    });
    TestThread interrupterThread = new TestThread() {

        @Override
        public void runTest() throws Exception {
            latch1.await();
            mainThread.interrupt();
            assertTrue(visitor.getInterruptionLatchForTestingOnly().await(TestUtils.WAIT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS));
            latch2.countDown();
        }
    };
    interrupterThread.start();
    try {
        visitor.awaitQuiescence(/*interruptWorkers=*/
        false);
        fail();
    } catch (InterruptedException e) {
    // Expected.
    }
    interrupterThread.joinAndAssertState(400);
    assertTrue(workerThreadCompleted[0]);
}
Also used : TestThread(com.google.devtools.build.lib.testutil.TestThread) CountDownLatch(java.util.concurrent.CountDownLatch) TestThread(com.google.devtools.build.lib.testutil.TestThread) Test(org.junit.Test)

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