| index 8b45961fd..9f8b96b9e 100644 |
| --- a/internal/activator/exectee_test.go |
| +++ b/internal/activator/exectee_test.go |
| @@ -2,6 +2,7 @@ package activator |
| |
| import ( |
| "bufio" |
| + "fmt" |
| "io" |
| "os" |
| "os/exec" |
| @@ -120,9 +121,11 @@ func TestExecTeeStalledJournalBlocksNoDrainNoTenantNoWait(t *testing.T) { |
| if err := <-waited; err != nil { |
| t.Fatalf("flooding child: %v", err) |
| } |
| + // The journal's writer runs on its own: give it a bounded time to |
| + // reach the stalled write, which holds it from then on. |
| select { |
| case <-journal.entered: |
| - default: |
| + case <-time.After(5 * time.Second): |
| t.Fatal("nothing reached the journal: the stall was never in the way") |
| } |
| |
| @@ -152,10 +155,13 @@ func TestExecTeeStalledJournalBlocksNoDrainNoTenantNoWait(t *testing.T) { |
| func TestExecTeeJournalCopyIsCappedLinesPerChild(t *testing.T) { |
| out := &stallWriter{release: make(chan struct{}), entered: make(chan struct{}, 1)} |
| out.unstall() |
| - agent := agentJournal |
| + agent, now := agentJournal, execTeeNow |
| j := newLogJournal(out, 64<<20) |
| agentJournal = func() *logJournal { return j } |
| - t.Cleanup(func() { agentJournal = agent; j.stop() }) |
| + // A fixed clock: each child's cap gets its burst and no refill. |
| + fixed := time.Unix(1_700_000_000, 0) |
| + execTeeNow = func() time.Time { return fixed } |
| + t.Cleanup(func() { agentJournal, execTeeNow = agent, now; j.stop() }) |
| sink := &teeSink{} |
| b := &ExecBackend{Logs: sink} |
| |
| @@ -166,22 +172,41 @@ func TestExecTeeJournalCopyIsCappedLinesPerChild(t *testing.T) { |
| t.Fatalf("%s child: %v", mode, err) |
| } |
| } |
| - waitFor(t, "the journal copy", func() bool { |
| - return strings.Contains(out.String(), "no newline at exit\n") && strings.Contains(out.String(), "flood.example.com: dropped") |
| + // Run returns once both streams are copied, so every line and notice |
| + // is queued; the journal is drained once its writer has written them |
| + // all out (logJournal.size counts what it has not finished writing). |
| + waitFor(t, "the journal drained", func() bool { |
| + j.mu.Lock() |
| + defer j.mu.Unlock() |
| + return j.head == nil && j.size == 0 |
| }) |
| lines := strings.Split(strings.TrimSuffix(out.String(), "\n"), "\n") |
| - floods := 0 |
| + // The flooding child's lines (stdout's and its one stderr line share |
| + // the cap) that reached the journal, and their bytes. |
| + floods, kept := 0, 0 |
| for _, line := range lines { |
| if strings.HasPrefix(line, "flood") && !strings.Contains(line, "dropped") { |
| floods++ |
| + kept += len(line) + 1 |
| } |
| } |
| - // The burst, and what the cap refilled while the flood ran. |
| - if floods < logJournalLineBurst || floods >= execTeeFloodLines/2 { |
| - t.Errorf("journal got %d flood lines, want the per-child burst %d and little more", floods, logJournalLineBurst) |
| + if floods != logJournalLineBurst { |
| + t.Errorf("journal got %d flood lines, want the per-child burst %d", floods, logJournalLineBurst) |
| } |
| - if !strings.Contains(out.String(), "flood.example.com: dropped ") || !strings.Contains(out.String(), " byte(s) of app output (journal line cap)\n") { |
| - t.Errorf("no journal line cap notice for the flooding child in %d lines", len(lines)) |
| + if !strings.Contains(out.String(), "no newline at exit\n") { |
| + t.Error("the journal copy's last line, with no newline, was not flushed at the stream's end") |
| + } |
| + // The two streams share the cap; each reports what it took from it at |
| + // its end, so the notices add up to every flood line the cap dropped. |
| + var dropped int |
| + for _, line := range lines { |
| + var n int |
| + if _, err := fmt.Sscanf(line, "flood.example.com: dropped %d byte(s) of app output (journal line cap)", &n); err == nil { |
| + dropped += n |
| + } |
| + } |
| + if want := execTeeFloodLines*1024 + len("flood-err\n") - kept; dropped != want { |
| + t.Errorf("journal line cap notices count %d bytes, want %d (all the child wrote less what the journal kept)", dropped, want) |
| } |
| if got := sink.of("partial.example.com"); len(got) != 1 || got[0] != "whole" { |
| t.Errorf("tenant lines %q: the tenant sink keeps only complete lines, as before", got) |