| 1 | package plugin |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // TestWaitWithBudgetReturnsWithinBudget pins that the budgeted reap helper |
| 11 | // returns even when the underlying wait never completes. withStderr calls this |
| 12 | // while holding callMu, and a surviving grandchild can keep cmd.Wait blocked |
| 13 | // forever — without the budget every future call on the transport would wedge. |
| 14 | func TestWaitWithBudgetReturnsWithinBudget(t *testing.T) { |
| 15 | blocked := make(chan struct{}) |
| 16 | t.Cleanup(func() { close(blocked) }) // let the abandoned goroutine exit |
| 17 | |
| 18 | done := make(chan struct{}) |
| 19 | go func() { |
| 20 | waitWithBudget(func() { <-blocked }, 100*time.Millisecond) |
| 21 | close(done) |
| 22 | }() |
| 23 | |
| 24 | select { |
| 25 | case <-done: |
| 26 | case <-time.After(2 * time.Second): |
| 27 | t.Fatal("waitWithBudget did not return within its budget — callMu would wedge") |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // TestWaitWithBudgetReturnsEarlyWhenWaitCompletes pins that a fast wait returns |
| 32 | // promptly rather than always paying the full budget. |
| 33 | func TestWaitWithBudgetReturnsEarlyWhenWaitCompletes(t *testing.T) { |
| 34 | done := make(chan struct{}) |
| 35 | go func() { |
| 36 | waitWithBudget(func() {}, 10*time.Second) |
| 37 | close(done) |
| 38 | }() |
| 39 | |
| 40 | select { |
| 41 | case <-done: |
| 42 | case <-time.After(2 * time.Second): |
| 43 | t.Fatal("waitWithBudget blocked on a wait that already completed") |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestWaitFinishedWithinBudgetReportsGracefulExit(t *testing.T) { |
| 48 | if !waitFinishedWithinBudget(func() {}, time.Second) { |
| 49 | t.Fatal("completed wait should be reported as graceful") |
| 50 | } |
| 51 | blocked := make(chan struct{}) |
| 52 | t.Cleanup(func() { close(blocked) }) |
| 53 | if waitFinishedWithinBudget(func() { <-blocked }, 10*time.Millisecond) { |
| 54 | t.Fatal("blocked wait should require forced process cleanup") |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestWithStderrRedactsCredentials(t *testing.T) { |
| 59 | tr := &stdioTransport{stderr: &tailBuffer{limit: 1024}} |
| 60 | _, _ = tr.stderr.Write([]byte("Authorization: Bearer transport-secret-value")) |
| 61 | got := tr.withStderr(errors.New("child exited")).Error() |
| 62 | if strings.Contains(got, "transport-secret-value") || !strings.Contains(got, "Bearer [redacted]") { |
| 63 | t.Fatalf("stderr error was not redacted: %q", got) |
| 64 | } |
| 65 | } |
| 66 |