Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions internal/cmd/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,28 @@ type recordedRequest struct {
Body []byte
}

// recordedMu orders the servers' appends against recordedSoFar. A reader that
// runs while a cancelled request is still inside its handler (a stopped watch)
// has no other happens-before edge to that append.
var recordedMu sync.Mutex

// recordedSoFar is *reqs for a reader that may overlap a handler still running.
// Appends only write past len, so the elements of the returned slice are stable.
func recordedSoFar(reqs *[]recordedRequest) []recordedRequest {
recordedMu.Lock()
defer recordedMu.Unlock()
return *reqs
}

func newRecordingServer(t *testing.T, handler http.HandlerFunc) (*httptest.Server, *[]recordedRequest) {
t.Helper()
var (
reqs []recordedRequest
mu sync.Mutex
)
var reqs []recordedRequest
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer
_, _ = b.ReadFrom(r.Body)
mu.Lock()
recordedMu.Lock()
reqs = append(reqs, recordedRequest{r.Method, r.URL.Path, r.URL.RawQuery, r.Header.Clone(), b.Bytes()})
mu.Unlock()
recordedMu.Unlock()
r.Body = http.NoBody
handler(w, r)
}))
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/logs_watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func logsScripted(t *testing.T, bodies ...string) (string, func() []recordedRequ
i := min(int(n.Add(1))-1, len(bodies)-1)
_, _ = w.Write([]byte(bodies[i]))
})
return srv.URL, func() []recordedRequest { return *reqs }
return srv.URL, func() []recordedRequest { return recordedSoFar(reqs) }
}

var (
Expand Down Expand Up @@ -319,7 +319,7 @@ func logsTUIServer(t *testing.T) (string, func() []recordedRequest) {
}
_, _ = w.Write([]byte(logsList(false, e)))
})
return srv.URL, func() []recordedRequest { return *reqs }
return srv.URL, func() []recordedRequest { return recordedSoFar(reqs) }
}

func TestLogsWatchFullScreenQuitsOnQ(t *testing.T) {
Expand Down
Loading