prints = append(prints, printTest)
}
- // Order runs for coordinating start JSON prints.
+ // Order runs for coordinating start JSON prints via two mechanisms:
+ // 1. Channel locking forces runTest actions to start in-order.
+ // 2. Barrier tasks force runTest actions to be scheduled in-order.
+ // We need both for performant behavior, as channel locking without the barrier tasks starves the worker pool,
+ // and barrier tasks without channel locking doesn't guarantee start in-order behavior alone.
+ var prevBarrier *work.Action
ch := make(chan struct{})
close(ch)
for _, a := range runs {
if r, ok := a.Actor.(*runTestActor); ok {
+ // Inject a barrier task between the run action and its dependencies.
+ // This barrier task wil also depend on the previous barrier task.
+ // This prevents the run task from being scheduled until all previous run dependencies have finished.
+ // The build graph will be augmented to look roughly like this:
+ // build("a") build("b") build("c")
+ // | | |
+ // barrier("a.test") -> barrier("b.test") -> barrier("c.test")
+ // | | |
+ // run("a.test") run("b.test") run("c.test")
+
+ barrier := &work.Action{
+ Mode: "test barrier",
+ Deps: slices.Clip(a.Deps),
+ }
+ if prevBarrier != nil {
+ barrier.Deps = append(barrier.Deps, prevBarrier)
+ }
+ a.Deps = []*work.Action{barrier}
+ prevBarrier = barrier
+
r.prev = ch
ch = make(chan struct{})
r.next = ch
func (r *runTestActor) Act(b *work.Builder, ctx context.Context, a *work.Action) error {
sh := b.Shell(a)
+ barrierAction := a.Deps[0]
+ buildAction := barrierAction.Deps[0]
// Wait for previous test to get started and print its first json line.
select {
// we have different link inputs but the same final binary,
// we still reuse the cached test result.
// c.saveOutput will store the result under both IDs.
- r.c.tryCacheWithID(b, a, a.Deps[0].BuildContentID())
+ r.c.tryCacheWithID(b, a, buildAction.BuildContentID())
}
if r.c.buf != nil {
if stdout != &buf {
// fresh copies of tools to test as part of the testing.
addToEnv = "GOCOVERDIR=" + gcd
}
- args := str.StringList(execCmd, a.Deps[0].BuiltTarget(), testlogArg, panicArg, fuzzArg, coverdirArg, testArgs)
+ args := str.StringList(execCmd, buildAction.BuiltTarget(), testlogArg, panicArg, fuzzArg, coverdirArg, testArgs)
if testCoverProfile != "" {
// Write coverage to temporary profile, for merging later.
// tryCache is called just before the link attempt,
// to see if the test result is cached and therefore the link is unneeded.
// It reports whether the result can be satisfied from cache.
-func (c *runCache) tryCache(b *work.Builder, a *work.Action) bool {
- return c.tryCacheWithID(b, a, a.Deps[0].BuildActionID())
+func (c *runCache) tryCache(b *work.Builder, a *work.Action, linkAction *work.Action) bool {
+ return c.tryCacheWithID(b, a, linkAction.BuildActionID())
}
func (c *runCache) tryCacheWithID(b *work.Builder, a *work.Action, id string) bool {
stdlibRecompiledIncOnce = sync.OnceFunc(stdlibRecompiled.Inc)
)
+// testRunAction returns the run action for a test given the link action
+// for the test binary, if the only (non-test-barrier) action that depend
+// on the link action is the run action.
+func testRunAction(a *Action) *Action {
+ if len(a.triggers) != 1 || a.triggers[0].Mode != "test barrier" {
+ return nil
+ }
+ var runAction *Action
+ for _, t := range a.triggers[0].triggers {
+ if t.Mode == "test run" {
+ if runAction != nil {
+ return nil
+ }
+ runAction = t
+ }
+ }
+ return runAction
+}
+
// useCache tries to satisfy the action a, which has action ID actionHash,
// by using a cached result from an earlier build.
// If useCache decides that the cache can be used, it sets a.buildID
// then to avoid the link step, report the link as up-to-date.
// We avoid the nested build ID problem in the previous special case
// by recording the test results in the cache under the action ID half.
- if len(a.triggers) == 1 && a.triggers[0].TryCache != nil && a.triggers[0].TryCache(b, a.triggers[0]) {
+ if ra := testRunAction(a); ra != nil && ra.TryCache != nil && ra.TryCache(b, ra, a) {
// Best effort attempt to display output from the compile and link steps.
// If it doesn't work, it doesn't work: reusing the test result is more
// important than reprinting diagnostic information.
for _, a0 := range a.triggers {
if a.Failed != nil {
- a0.Failed = a.Failed
+ if a0.Mode == "test barrier" {
+ // If this action was triggered by a test, there
+ // will be a test barrier action in between the test
+ // and the true trigger. But there will be other
+ // triggers that are other barriers that are waiting
+ // for this one. Propagate the failure to the true
+ // trigger, but not to the other barriers.
+ for _, bt := range a0.triggers {
+ if bt.Mode != "test barrier" {
+ bt.Failed = a.Failed
+ }
+ }
+ } else {
+ a0.Failed = a.Failed
+ }
}
if a0.pending--; a0.pending == 0 {
b.ready.push(a0)