Search in sources :

Example 46 with Artifact

use of com.google.devtools.build.lib.actions.Artifact in project bazel by bazelbuild.

the class TreeArtifactBuildTest method testMTimeForTreeArtifactsDoesNotMatter.

/** TreeArtifacts don't care about mtime, even when the file is empty. */
@Test
public void testMTimeForTreeArtifactsDoesNotMatter() throws Exception {
    // For this test, we only touch the input file.
    Artifact in = createSourceArtifact("touchable_input");
    touchFile(in);
    WriteInputToFilesAction actionOne = new WriteInputToFilesAction(buttonOne, in, outOneFileOne, outOneFileTwo);
    registerAction(actionOne);
    CopyTreeAction actionTwo = new CopyTreeAction(buttonTwo, ImmutableList.of(outOneFileOne, outOneFileTwo), ImmutableList.of(outTwoFileOne, outTwoFileTwo));
    registerAction(actionTwo);
    buttonOne.pressed = buttonTwo.pressed = false;
    buildArtifact(outTwo);
    // built
    assertTrue(buttonOne.pressed);
    // built
    assertTrue(buttonTwo.pressed);
    buttonOne.pressed = buttonTwo.pressed = false;
    touchFile(in);
    buildArtifact(outTwo);
    // mtime does not matter.
    assertFalse(buttonOne.pressed);
    assertFalse(buttonTwo.pressed);
    // None of the below following should result in anything being built.
    buttonOne.pressed = buttonTwo.pressed = false;
    touchFile(outOneFileOne);
    buildArtifact(outTwo);
    // Nothing should be built.
    assertFalse(buttonOne.pressed);
    assertFalse(buttonTwo.pressed);
    buttonOne.pressed = buttonTwo.pressed = false;
    touchFile(outOneFileTwo);
    buildArtifact(outTwo);
    // Nothing should be built.
    assertFalse(buttonOne.pressed);
    assertFalse(buttonTwo.pressed);
}
Also used : SpecialArtifact(com.google.devtools.build.lib.actions.Artifact.SpecialArtifact) Artifact(com.google.devtools.build.lib.actions.Artifact) ActionInputHelper.treeFileArtifact(com.google.devtools.build.lib.actions.ActionInputHelper.treeFileArtifact) TreeFileArtifact(com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact) Test(org.junit.Test)

Example 47 with Artifact

use of com.google.devtools.build.lib.actions.Artifact in project bazel by bazelbuild.

the class TreeArtifactMetadataTest method createTreeArtifact.

private Artifact createTreeArtifact(String path) throws IOException {
    PathFragment execPath = new PathFragment("out").getRelative(path);
    Path fullPath = root.getRelative(execPath);
    Artifact output = new SpecialArtifact(fullPath, Root.asDerivedRoot(root, root.getRelative("out")), execPath, ALL_OWNER, SpecialArtifactType.TREE);
    actions.add(new DummyAction(ImmutableList.<Artifact>of(), output));
    FileSystemUtils.createDirectoryAndParents(fullPath);
    return output;
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) SpecialArtifact(com.google.devtools.build.lib.actions.Artifact.SpecialArtifact) DummyAction(com.google.devtools.build.lib.actions.util.TestAction.DummyAction) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) SpecialArtifact(com.google.devtools.build.lib.actions.Artifact.SpecialArtifact) Artifact(com.google.devtools.build.lib.actions.Artifact) TreeFileArtifact(com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)

Example 48 with Artifact

use of com.google.devtools.build.lib.actions.Artifact in project bazel by bazelbuild.

the class ParallelBuilderTest method testWaitsForSubprocesses.

// Regression test for bug fixed in CL 3548332: builder was not waiting for
// all its subprocesses to terminate.
@Test
public void testWaitsForSubprocesses() throws Exception {
    final Semaphore semaphore = new Semaphore(1);
    final boolean[] finished = { false };
    // t=0: semaphore acquired
    semaphore.acquireUninterruptibly();
    // This arrangement ensures that the "bar" action tries to run for about
    // 100ms after the "foo" action has completed (failed).
    // [action] -> foo
    Artifact foo = createDerivedArtifact("foo");
    Callable<Void> makeFoo = new Callable<Void>() {

        @Override
        public Void call() throws IOException {
            // t=2: semaphore re-acquired
            semaphore.acquireUninterruptibly();
            throw new IOException("foo action failed");
        }
    };
    registerAction(new TestAction(makeFoo, Artifact.NO_ARTIFACTS, ImmutableList.of(foo)));
    // [action] -> bar
    Artifact bar = createDerivedArtifact("bar");
    Runnable makeBar = new Runnable() {

        @Override
        public void run() {
            // t=1: semaphore released
            semaphore.release();
            try {
                // 100ms
                Thread.sleep(100);
            } catch (InterruptedException e) {
            // This might happen (though not necessarily).  The
            // ParallelBuilder interrupts all its workers at the first sign
            // of trouble.
            }
            finished[0] = true;
        }
    };
    registerAction(new TestAction(makeBar, emptySet, asSet(bar)));
    // Don't fail fast when we encounter the error
    reporter.removeHandler(failFastHandler);
    try {
        buildArtifacts(foo, bar);
        fail();
    } catch (BuildFailedException e) {
        assertThat(e.getMessage()).contains("TestAction failed due to exception: foo action failed");
        assertContainsEvent("TestAction failed due to exception: foo action failed");
    }
    assertTrue("bar action not finished, yet buildArtifacts has completed.", finished[0]);
}
Also used : BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) Artifact(com.google.devtools.build.lib.actions.Artifact) Callable(java.util.concurrent.Callable) TestAction(com.google.devtools.build.lib.actions.util.TestAction) Test(org.junit.Test)

Example 49 with Artifact

use of com.google.devtools.build.lib.actions.Artifact in project bazel by bazelbuild.

the class ParallelBuilderTest method runsInParallelWithBuilder.

/**
   * Test that independent actions are run in parallel threads
   * that are scheduled concurrently.
   */
public void runsInParallelWithBuilder(Builder builder) throws Exception {
    // We create two actions, each of which waits (spinning) until the
    // other action has started.  If the two actions are not run
    // in parallel, the test will deadlock and time out.
    // This specifies how many iterations to run before timing out.
    // This should be large enough to ensure that that there is at
    // least one context switch, otherwise the test may spuriously fail.
    final long maxIterations = 100000000;
    // This specifies how often to print out progress messages.
    // Uncomment this for debugging.
    //final long PRINT_FREQUENCY = maxIterations / 10;
    runningFooAction = false;
    runningBarAction = false;
    // [action] -> foo
    Artifact foo = createDerivedArtifact("foo");
    Runnable makeFoo = new Runnable() {

        @Override
        public void run() {
            runningFooAction = true;
            for (long i = 0; i < maxIterations; i++) {
                Thread.yield();
                if (runningBarAction) {
                    return;
                }
            // Uncomment this for debugging.
            //if (i % PRINT_FREQUENCY == 0) {
            //  String msg = "ParallelBuilderTest: foo: waiting for bar";
            //  System.out.println(bar);
            //}
            }
            fail("ParallelBuilderTest: foo: waiting for bar: timed out");
        }
    };
    registerAction(new TestAction(makeFoo, Artifact.NO_ARTIFACTS, ImmutableList.of(foo)));
    // [action] -> bar
    Artifact bar = createDerivedArtifact("bar");
    Runnable makeBar = new Runnable() {

        @Override
        public void run() {
            runningBarAction = true;
            for (long i = 0; i < maxIterations; i++) {
                Thread.yield();
                if (runningFooAction) {
                    return;
                }
            // Uncomment this for debugging.
            //if (i % PRINT_FREQUENCY == 0) {
            //  String msg = "ParallelBuilderTest: bar: waiting for foo";
            //  System.out.println(msg);
            //}
            }
            fail("ParallelBuilderTest: bar: waiting for foo: timed out");
        }
    };
    registerAction(new TestAction(makeBar, Artifact.NO_ARTIFACTS, ImmutableList.of(bar)));
    buildArtifacts(builder, foo, bar);
}
Also used : Artifact(com.google.devtools.build.lib.actions.Artifact) TestAction(com.google.devtools.build.lib.actions.util.TestAction)

Example 50 with Artifact

use of com.google.devtools.build.lib.actions.Artifact in project bazel by bazelbuild.

the class ParallelBuilderTest method testProgressReporting.

@Test
public void testProgressReporting() throws Exception {
    // Build three artifacts in 3 separate actions (baz depends on bar and bar
    // depends on foo.  Make sure progress is reported at the beginning of all
    // three actions.
    List<Artifact> sourceFiles = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        sourceFiles.add(createInputFile("file" + i));
    }
    Artifact foo = createDerivedArtifact("foo");
    Artifact bar = createDerivedArtifact("bar");
    Artifact baz = createDerivedArtifact("baz");
    bar.getPath().delete();
    baz.getPath().delete();
    final List<String> messages = new ArrayList<>();
    EventHandler handler = new EventHandler() {

        @Override
        public void handle(Event event) {
            EventKind k = event.getKind();
            if (k == EventKind.START || k == EventKind.FINISH) {
                // Remove the tmpDir as this is user specific and the assert would
                // fail below.
                messages.add(event.getMessage().replaceFirst(TestUtils.tmpDir(), "") + " " + event.getKind());
            }
        }
    };
    reporter.addHandler(handler);
    reporter.addHandler(new PrintingEventHandler(EventKind.ALL_EVENTS));
    registerAction(new TestAction(TestAction.NO_EFFECT, sourceFiles, asSet(foo)));
    registerAction(new TestAction(TestAction.NO_EFFECT, asSet(foo), asSet(bar)));
    registerAction(new TestAction(TestAction.NO_EFFECT, asSet(bar), asSet(baz)));
    buildArtifacts(baz);
    // Check that the percentages increase non-linearly, because foo has 10 input files
    List<String> expectedMessages = Lists.newArrayList("Test foo START", "Test foo FINISH", "Test bar START", "Test bar FINISH", "Test baz START", "Test baz FINISH");
    assertThat(messages).containsAllIn(expectedMessages);
    // Now do an incremental rebuild of bar and baz,
    // and check the incremental progress percentages.
    messages.clear();
    bar.getPath().delete();
    baz.getPath().delete();
    // This uses a new builder instance so that we refetch timestamps from
    // (in-memory) file system, rather than using cached entries.
    buildArtifacts(baz);
    expectedMessages = Lists.newArrayList("Test bar START", "Test bar FINISH", "Test baz START", "Test baz FINISH");
    assertThat(messages).containsAllIn(expectedMessages);
}
Also used : EventKind(com.google.devtools.build.lib.events.EventKind) ArrayList(java.util.ArrayList) EventHandler(com.google.devtools.build.lib.events.EventHandler) PrintingEventHandler(com.google.devtools.build.lib.events.PrintingEventHandler) ActionExecutedEvent(com.google.devtools.build.lib.actions.ActionExecutedEvent) Event(com.google.devtools.build.lib.events.Event) Artifact(com.google.devtools.build.lib.actions.Artifact) PrintingEventHandler(com.google.devtools.build.lib.events.PrintingEventHandler) TestAction(com.google.devtools.build.lib.actions.util.TestAction) Test(org.junit.Test)

Aggregations

Artifact (com.google.devtools.build.lib.actions.Artifact)659 Test (org.junit.Test)242 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)184 TreeFileArtifact (com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)83 SpecialArtifact (com.google.devtools.build.lib.actions.Artifact.SpecialArtifact)65 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)64 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)51 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)50 ImmutableList (com.google.common.collect.ImmutableList)46 Action (com.google.devtools.build.lib.actions.Action)42 IOException (java.io.IOException)42 ArrayList (java.util.ArrayList)40 SpawnAction (com.google.devtools.build.lib.analysis.actions.SpawnAction)38 Path (com.google.devtools.build.lib.vfs.Path)37 HashMap (java.util.HashMap)37 Map (java.util.Map)36 Root (com.google.devtools.build.lib.actions.Root)31 LinkedHashMap (java.util.LinkedHashMap)31 ImmutableMap (com.google.common.collect.ImmutableMap)28 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)28