Search in sources :

Example 16 with TestThread

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

the class InMemoryFileSystemTest method testConcurrentRenaming.

/**
   * Tests concurrent file renaming.
   */
@Test
public void testConcurrentRenaming() throws Exception {
    final int NUM_TO_WRITE = 10000;
    final AtomicInteger baseSelector = new AtomicInteger();
    final Path base = testFS.getPath("/base");
    base.createDirectory();
    // 1) Create a bunch of files.
    for (int i = 0; i < NUM_TO_WRITE; i++) {
        writeToFile(base.getRelative("file" + i), TEST_FILE_DATA);
    }
    // 2) Define our renaming strategy.
    class FileDeleter extends TestThread {

        @Override
        public void runTest() throws Exception {
            for (int i = 0; i < NUM_TO_WRITE / NUM_THREADS_FOR_CONCURRENCY_TESTS; i++) {
                int whichFile = baseSelector.getAndIncrement();
                Path file = base.getRelative("file" + whichFile);
                if (whichFile % 25 != 0) {
                    Path newName = base.getRelative("newname" + whichFile);
                    file.renameTo(newName);
                } else {
                    // Throw another concurrent access point into the mix.
                    file.setExecutable(whichFile % 2 == 0);
                }
                assertFalse(base.getRelative("doesnotexist" + whichFile).delete());
            }
        }
    }
    // 3) Rename some files.
    Collection<TestThread> threads = Lists.newArrayListWithCapacity(NUM_THREADS_FOR_CONCURRENCY_TESTS);
    for (int i = 0; i < NUM_THREADS_FOR_CONCURRENCY_TESTS; i++) {
        TestThread thread = new FileDeleter();
        thread.start();
        threads.add(thread);
    }
    for (TestThread thread : threads) {
        thread.joinAndAssertState(0);
    }
    // 4) Check the results.
    for (int i = 0; i < NUM_TO_WRITE; i++) {
        Path file = base.getRelative("file" + i);
        if (i % 25 != 0) {
            assertFalse(file.exists());
            assertTrue(base.getRelative("newname" + i).exists());
        } else {
            assertTrue(file.exists());
            assertEquals(i % 2 == 0, file.isExecutable());
        }
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) TestThread(com.google.devtools.build.lib.testutil.TestThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test) ScopeEscapableFileSystemTest(com.google.devtools.build.lib.vfs.ScopeEscapableFileSystemTest)

Example 17 with TestThread

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

the class InMemoryFileSystemTest method testConcurrentDirectoryConstruction.

/**
   * Tests concurrent creation of many files, all within the same directory.
   */
@Test
public void testConcurrentDirectoryConstruction() 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 {
            final int threadId = baseSelector.getAndIncrement();
            Path base = testFS.getPath("/common_dir");
            base.createDirectory();
            for (int i = 0; i < NUM_TO_WRITE; i++) {
                Path file = base.getRelative("somefile_" + threadId + "_" + i);
                writeToFile(file, TEST_FILE_DATA);
                file.setReadable(i % 2 == 0);
                file.setWritable(i % 3 == 0);
                file.setExecutable(i % 4 == 0);
                file.setLastModifiedTime(i);
                Path symlink = base.getRelative("symlink_" + threadId + "_" + i);
                symlink.createSymbolicLink(file);
            }
        }
    }
    // 2) Create the files.
    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 {
            final int threadId = baseSelector.getAndIncrement();
            Path base = testFS.getPath("/common_dir");
            assertTrue(base.exists());
            for (int i = 0; i < NUM_TO_WRITE; i++) {
                Path file = base.getRelative("somefile_" + threadId + "_" + i);
                assertTrue(file.exists());
                assertTrue(file.isFile());
                assertEquals(i % 2 == 0, file.isReadable());
                assertEquals(i % 3 == 0, file.isWritable());
                assertEquals(i % 4 == 0, file.isExecutable());
                assertEquals(i, file.getLastModifiedTime());
                if (file.isReadable()) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream(), Charset.defaultCharset()));
                    assertEquals(TEST_FILE_DATA, reader.readLine());
                    assertNull(reader.readLine());
                }
                Path symlink = base.getRelative("symlink_" + threadId + "_" + 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 18 with TestThread

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

the class InMemoryFileSystemTest method testConcurrentDeletion.

/**
   * Tests concurrent file deletion.
   */
@Test
public void testConcurrentDeletion() throws Exception {
    final int NUM_TO_WRITE = 10000;
    final AtomicInteger baseSelector = new AtomicInteger();
    final Path base = testFS.getPath("/base");
    base.createDirectory();
    // 1) Create a bunch of files.
    for (int i = 0; i < NUM_TO_WRITE; i++) {
        writeToFile(base.getRelative("file" + i), TEST_FILE_DATA);
    }
    // 2) Define our deletion strategy.
    class FileDeleter extends TestThread {

        @Override
        public void runTest() throws Exception {
            for (int i = 0; i < NUM_TO_WRITE / NUM_THREADS_FOR_CONCURRENCY_TESTS; i++) {
                int whichFile = baseSelector.getAndIncrement();
                Path file = base.getRelative("file" + whichFile);
                if (whichFile % 25 != 0) {
                    assertTrue(file.delete());
                } else {
                    // Throw another concurrent access point into the mix.
                    file.setExecutable(whichFile % 2 == 0);
                }
                assertFalse(base.getRelative("doesnotexist" + whichFile).delete());
            }
        }
    }
    // 3) Delete some files.
    Collection<TestThread> threads = Lists.newArrayListWithCapacity(NUM_THREADS_FOR_CONCURRENCY_TESTS);
    for (int i = 0; i < NUM_THREADS_FOR_CONCURRENCY_TESTS; i++) {
        TestThread thread = new FileDeleter();
        thread.start();
        threads.add(thread);
    }
    for (TestThread thread : threads) {
        thread.joinAndAssertState(0);
    }
    // 4) Check the results.
    for (int i = 0; i < NUM_TO_WRITE; i++) {
        Path file = base.getRelative("file" + i);
        if (i % 25 != 0) {
            assertFalse(file.exists());
        } else {
            assertTrue(file.exists());
            assertEquals(i % 2 == 0, file.isExecutable());
        }
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) TestThread(com.google.devtools.build.lib.testutil.TestThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test) ScopeEscapableFileSystemTest(com.google.devtools.build.lib.vfs.ScopeEscapableFileSystemTest)

Example 19 with TestThread

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

the class ResourceManagerTest method testThatCpuAllocationIsNoncommutative.

@Test
public void testThatCpuAllocationIsNoncommutative() throws Exception {
    assertFalse(rm.inUse());
    // Given that CPU has a small initial allocation:
    acquire(0, 0.099, 0, 0);
    // When a request for a large CPU allocation is made,
    // Then the request succeeds:
    TestThread thread1 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            assertThat(acquireNonblocking(0, 0.99, 0, 0)).isNotNull();
            // Cleanup
            release(0, 0.99, 0, 0);
        }
    };
    thread1.start();
    thread1.joinAndAssertState(10000);
    // Cleanup
    release(0, 0.099, 0, 0);
    assertFalse(rm.inUse());
    // Given that CPU has a large initial allocation:
    acquire(0, 0.99, 0, 0);
    // When a request for a small CPU allocation is made,
    // Then the request fails:
    TestThread thread2 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            assertThat(acquireNonblocking(0, 0.099, 0, 0)).isNull();
        }
    };
    thread2.start();
    thread2.joinAndAssertState(10000);
// Note that this behavior is surprising and probably not intended.
}
Also used : TestThread(com.google.devtools.build.lib.testutil.TestThread) Test(org.junit.Test)

Example 20 with TestThread

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

the class ResourceManagerTest method testThatCpuCanBeOverallocated.

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

        @Override
        public void runTest() throws Exception {
            assertThat(acquireNonblocking(0, 0.6, 0, 0)).isNotNull();
        }
    };
    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