Repository navigation
Expand file tree
/
Copy pathrunlog_test.go
More file actions
100 lines (92 loc) · 2.25 KB
/
Copy pathrunlog_test.go
File metadata and controls
100 lines (92 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"bufio"
"bytes"
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestRunLogTruncates(t *testing.T) {
run := newRunLog()
half := bytes.Repeat([]byte("x"), maxRunLogSize/2+10)
for range 3 {
if n, err := run.Write(half); err != nil || n != len(half) {
t.Fatalf("write = %d, %v", n, err)
}
}
if got := len(run.String()); got != maxRunLogSize {
t.Fatalf("kept %d bytes, want %d", got, maxRunLogSize)
}
run.finish(nil)
if !strings.Contains(run.summary(), "not shown") {
t.Fatalf("summary %q doesn't mention truncation", run.summary())
}
}
// readUntil reads lines until one contains want, failing after a timeout.
func readUntil(t *testing.T, r *bufio.Reader, want string) string {
t.Helper()
var got strings.Builder
done := make(chan error, 1)
go func() {
for {
line, err := r.ReadString('\n')
got.WriteString(line)
if strings.Contains(line, want) || err != nil {
done <- err
return
}
}
}()
select {
case err := <-done:
if err != nil && err != io.EOF {
t.Fatal(err)
}
case <-time.After(5 * time.Second):
t.Fatalf("timed out waiting for %q", want)
}
return got.String()
}
func TestRunLogFollowStreams(t *testing.T) {
run := newRunLog()
_, _ = run.Write([]byte("first line\n"))
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = run.follow(r.Context(), w)
}))
defer srv.Close()
resp, err := http.Get(srv.URL)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body := bufio.NewReader(resp.Body)
// output arrives while the run is still going
readUntil(t, body, "first line")
_, _ = run.Write([]byte("second line\n"))
readUntil(t, body, "second line")
run.finish(errors.New("exit status 1"))
rest := readUntil(t, body, "Failed: exit status 1")
if strings.Contains(rest, "first line") {
t.Fatalf("output repeated: %q", rest)
}
}
func TestRunLogFollowStopsWhenClientLeaves(t *testing.T) {
run := newRunLog()
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
_ = run.follow(ctx, httptest.NewRecorder())
close(done)
}()
cancel()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("follow kept running after the client left")
}
}