Search in sources :

Example 21 with TestThread

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

the class ResourceManagerTest method testInterruptedAcquisitionClearsResources.

@Test
public void testInterruptedAcquisitionClearsResources() throws Exception {
    assertFalse(rm.inUse());
    // Acquire a small amount of resources so that future requests can block (the initial request
    // always succeeds even if it's for too much).
    TestThread smallThread = new TestThread() {

        @Override
        public void runTest() throws InterruptedException {
            acquire(1, 0, 0, 0);
        }
    };
    smallThread.start();
    smallThread.joinAndAssertState(TestUtils.WAIT_TIMEOUT_MILLISECONDS);
    TestThread thread1 = new TestThread() {

        @Override
        public void runTest() {
            Thread.currentThread().interrupt();
            try {
                acquire(1999, 0, 0, 0);
                fail("Didn't throw interrupted exception");
            } catch (InterruptedException e) {
            // Expected.
            }
        }
    };
    thread1.start();
    thread1.joinAndAssertState(TestUtils.WAIT_TIMEOUT_MILLISECONDS);
    // This should process the queue. If the request from above is still present, it will take all
    // the available memory. But it shouldn't.
    rm.setAvailableResources(ResourceSet.create(/*memoryMb=*/
    2000.0, /*cpuUsage=*/
    1.0, /*ioUsage=*/
    1.0, /*testCount=*/
    2));
    TestThread thread2 = new TestThread() {

        @Override
        public void runTest() throws InterruptedException {
            acquire(1999, 0, 0, 0);
            release(1999, 0, 0, 0);
        }
    };
    thread2.start();
    thread2.joinAndAssertState(TestUtils.WAIT_TIMEOUT_MILLISECONDS);
}
Also used : TestThread(com.google.devtools.build.lib.testutil.TestThread) Test(org.junit.Test)

Example 22 with TestThread

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

the class ResourceManagerTest method testHasResources.

@Test
public void testHasResources() throws Exception {
    assertFalse(rm.inUse());
    assertFalse(rm.threadHasResources());
    acquire(1.0, 0.1, 0.1, 1);
    assertTrue(rm.threadHasResources());
    // We have resources in this thread - make sure other threads
    // are not affected.
    TestThread thread1 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            assertFalse(rm.threadHasResources());
            acquire(1.0, 0, 0, 0);
            assertTrue(rm.threadHasResources());
            release(1.0, 0, 0, 0);
            assertFalse(rm.threadHasResources());
            acquire(0, 0.1, 0, 0);
            assertTrue(rm.threadHasResources());
            release(0, 0.1, 0, 0);
            assertFalse(rm.threadHasResources());
            acquire(0, 0, 0.1, 0);
            assertTrue(rm.threadHasResources());
            release(0, 0, 0.1, 0);
            assertFalse(rm.threadHasResources());
            acquire(0, 0, 0, 1);
            assertTrue(rm.threadHasResources());
            release(0, 0, 0, 1);
            assertFalse(rm.threadHasResources());
        }
    };
    thread1.start();
    thread1.joinAndAssertState(10000);
    release(1.0, 0.1, 0.1, 1);
    assertFalse(rm.threadHasResources());
    assertFalse(rm.inUse());
}
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