| 1 | //go:build browser |
| 2 | |
| 3 | package harness |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // bootBudget is how long a failed boot may take to fail. The real |
| 14 | // failure takes milliseconds; the hang this guards against lasted |
| 15 | // until go test's own ten-minute alarm. |
| 16 | const bootBudget = 30 * time.Second |
| 17 | |
| 18 | // TestAFailedBootFailsPromptly pins the boot path in New. With a |
| 19 | // browser that cannot start, chromedp's graceful Cancel waits forever |
| 20 | // on a process that never existed (the context watcher takes the one |
| 21 | // token Cancel would have read), so New must tear the contexts down |
| 22 | // itself and only register Cancel once a browser is up. Measured in a |
| 23 | // subprocess, because the hang holds the whole test binary: this |
| 24 | // binary runs TestScreenPassesACleanPage again with FIESTA_CHROME |
| 25 | // pointing at nothing, and the run has to fail, by the boot's own |
| 26 | // name, inside the budget. |
| 27 | // |
| 28 | // ChromePath first: the subprocess needs no browser, but the policy |
| 29 | // does. With none, this fails as every drive does, or skips when |
| 30 | // FIESTA_BROWSER_OPTIONAL says so. |
| 31 | func TestAFailedBootFailsPromptly(t *testing.T) { |
| 32 | ChromePath(t) |
| 33 | cmd := exec.Command(os.Args[0], "-test.run=^TestScreenPassesACleanPage$", "-test.count=1", "-test.timeout="+bootBudget.String()) |
| 34 | cmd.Env = append(os.Environ(), "FIESTA_CHROME=/nonexistent", "FIESTA_BROWSER_OPTIONAL=") |
| 35 | start := time.Now() |
| 36 | out, err := cmd.CombinedOutput() |
| 37 | took := time.Since(start) |
| 38 | if err == nil { |
| 39 | t.Fatalf("a boot with FIESTA_CHROME=/nonexistent passed:\n%s", out) |
| 40 | } |
| 41 | if took > bootBudget { |
| 42 | t.Errorf("the failed boot took %s to fail, over the %s budget: the hang is back", took, bootBudget) |
| 43 | } |
| 44 | for _, want := range []string{"harness: browser boot:", "/nonexistent"} { |
| 45 | if !strings.Contains(string(out), want) { |
| 46 | t.Errorf("the failure does not say %q; the boot did not fail as itself:\n%s", want, out) |
| 47 | } |
| 48 | } |
| 49 | if strings.Contains(string(out), "test timed out") { |
| 50 | t.Errorf("the subprocess hit go test's alarm instead of failing:\n%s", out) |
| 51 | } |
| 52 | } |
| 53 | |