Search in sources :

Example 1 with TestThread

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

the class DigestUtilsTest method assertDigestCalculationConcurrency.

private static void assertDigestCalculationConcurrency(boolean expectConcurrent, final boolean fastDigest, final int fileSize1, final int fileSize2, HashFunction hf) throws Exception {
    // Used to block test threads.
    final CountDownLatch barrierLatch = new CountDownLatch(2);
    // Used to block main thread.
    final CountDownLatch readyLatch = new CountDownLatch(1);
    FileSystem myfs = new InMemoryFileSystem(BlazeClock.instance()) {

        @Override
        protected byte[] getMD5Digest(Path path) throws IOException {
            try {
                barrierLatch.countDown();
                readyLatch.countDown();
                // Either both threads will be inside getMD5Digest at the same time or they
                // both will be blocked.
                barrierLatch.await();
            } catch (Exception e) {
                throw new IOException(e);
            }
            return super.getMD5Digest(path);
        }

        @Override
        protected byte[] getSHA1Digest(Path path) throws IOException {
            try {
                barrierLatch.countDown();
                readyLatch.countDown();
                // Either both threads will be inside getSHA1Digest at the same time or they
                // both will be blocked.
                barrierLatch.await();
            } catch (Exception e) {
                throw new IOException(e);
            }
            return super.getSHA1Digest(path);
        }

        @Override
        protected byte[] getFastDigest(Path path, HashFunction hashFunction) throws IOException {
            return fastDigest ? super.getDigest(path, hashFunction) : null;
        }
    };
    FileSystem.setDigestFunctionForTesting(hf);
    final Path myFile1 = myfs.getPath("/f1.dat");
    final Path myFile2 = myfs.getPath("/f2.dat");
    FileSystemUtils.writeContentAsLatin1(myFile1, Strings.repeat("a", fileSize1));
    FileSystemUtils.writeContentAsLatin1(myFile2, Strings.repeat("b", fileSize2));
    TestThread thread1 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            DigestUtils.getDigestOrFail(myFile1, fileSize1);
        }
    };
    TestThread thread2 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            DigestUtils.getDigestOrFail(myFile2, fileSize2);
        }
    };
    thread1.start();
    thread2.start();
    if (!expectConcurrent) {
        // Synchronized case.
        // Wait until at least one thread reached getMD5Digest().
        assertTrue(readyLatch.await(TestUtils.WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS));
        // Only 1 thread should be inside getMD5Digest().
        assertEquals(1, barrierLatch.getCount());
        // Release barrier latch, allowing both threads to proceed.
        barrierLatch.countDown();
    }
    // Test successful execution within 5 seconds.
    thread1.joinAndAssertState(TestUtils.WAIT_TIMEOUT_MILLISECONDS);
    thread2.joinAndAssertState(TestUtils.WAIT_TIMEOUT_MILLISECONDS);
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) HashFunction(com.google.devtools.build.lib.vfs.FileSystem.HashFunction) TestThread(com.google.devtools.build.lib.testutil.TestThread) InMemoryFileSystem(com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem) FileSystem(com.google.devtools.build.lib.vfs.FileSystem) InMemoryFileSystem(com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException)

Example 2 with TestThread

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

the class ResourceManagerTest method testThatIOCannotBeOverallocated.

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

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

Example 3 with TestThread

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

the class ResourceManagerTest method testOutOfOrderAllocation.

@Test
public void testOutOfOrderAllocation() throws Exception {
    final CyclicBarrier sync3 = new CyclicBarrier(2);
    final CyclicBarrier sync4 = new CyclicBarrier(2);
    assertFalse(rm.inUse());
    TestThread thread1 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            sync.await();
            // Will be blocked by the main thread.
            acquire(900, 0.5, 0, 0);
            validate(5);
            release(900, 0.5, 0, 0);
            sync.await();
        }
    };
    TestThread thread2 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            // Wait till other thread will be locked
            while (rm.getWaitCount() == 0) {
                Thread.yield();
            }
            acquire(100, 0.1, 0, 0);
            validate(2);
            release(100, 0.1, 0, 0);
            sync2.await();
            acquire(200, 0.5, 0, 0);
            validate(4);
            sync2.await();
            release(200, 0.5, 0, 0);
        }
    };
    TestThread thread3 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            acquire(100, 0.4, 0, 0);
            sync3.await();
            sync3.await();
            release(100, 0.4, 0, 0);
        }
    };
    TestThread thread4 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            acquire(750, 0.3, 0, 0);
            sync4.await();
            sync4.await();
            release(750, 0.3, 0, 0);
        }
    };
    // Lock 900 MB, 0.9 CPU in total (spread over three threads so that we can individually release
    // parts of it).
    acquire(50, 0.2, 0, 0);
    thread3.start();
    thread4.start();
    sync3.await(1, TimeUnit.SECONDS);
    sync4.await(1, TimeUnit.SECONDS);
    validate(1);
    // Start thread1, which will try to acquire 900 MB, 0.5 CPU, but can't, so it has to wait.
    thread1.start();
    sync.await(1, TimeUnit.SECONDS);
    // Start thread2, which will successfully acquire and release 100 MB, 0.1 CPU.
    thread2.start();
    // Signal thread2 to acquire 200 MB and 0.5 CPU, which will block.
    sync2.await(1, TimeUnit.SECONDS);
    // Waiting till both threads are locked.
    while (rm.getWaitCount() < 2) {
        Thread.yield();
    }
    // Thread1 is now first in the queue and Thread2 is second.
    validate(3);
    // Release 100 MB, 0.4 CPU. This allows Thread2 to continue out of order.
    sync3.await(1, TimeUnit.SECONDS);
    sync2.await(1, TimeUnit.SECONDS);
    // Release 750 MB, 0.3 CPU. At this point thread1 will finally acquire resources.
    sync4.await(1, TimeUnit.SECONDS);
    sync.await(1, TimeUnit.SECONDS);
    // Release all remaining resources.
    release(50, 0.2, 0, 0);
    thread1.join();
    thread2.join();
    thread3.join();
    thread4.join();
    assertFalse(rm.inUse());
}
Also used : TestThread(com.google.devtools.build.lib.testutil.TestThread) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 4 with TestThread

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

the class ResourceManagerTest method testThatTestsCannotBeOverallocated.

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

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

Example 5 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)

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