diff --git a/lib/web/command.go b/lib/web/command.go index 2bcdd223f8382..e1095d79b71b6 100644 --- a/lib/web/command.go +++ b/lib/web/command.go @@ -287,7 +287,7 @@ func (h *Handler) executeCommand( runCommands(hosts, runCmd, h.log) // Optionally, try to compute the command summary. - if output, overflow := buffer.Export(); !overflow || len(output) != 0 { + if output, valid := buffer.Export(); valid { summaryReq := summaryRequest{ hosts: hosts, output: output, diff --git a/lib/web/command_utils.go b/lib/web/command_utils.go index 48b4712fb74eb..49154e47dbca7 100644 --- a/lib/web/command_utils.go +++ b/lib/web/command_utils.go @@ -189,13 +189,14 @@ func (b *summaryBuffer) Write(node string, data []byte) { b.remainingCapacity -= len(data) } -// Export returns the buffer content and a whether the buffer overflowed. +// Export returns the buffer content and a whether the Export is valid. +// Exporting the buffer can only happen once. func (b *summaryBuffer) Export() (map[string][]byte, bool) { b.mutex.Lock() defer b.mutex.Unlock() if b.invalid { - return nil, true + return nil, false } b.invalid = true - return b.buffer, false + return b.buffer, len(b.buffer) != 0 } diff --git a/lib/web/command_utils_test.go b/lib/web/command_utils_test.go index 01528bff29c1b..1366c1e6cae3d 100644 --- a/lib/web/command_utils_test.go +++ b/lib/web/command_utils_test.go @@ -23,12 +23,13 @@ import ( ) func TestSummaryBuffer(t *testing.T) { + t.Parallel() tests := []struct { - name string - outputs map[string][][]byte - capacity int - expectedOutput map[string][]byte - expectedOverflow bool + name string + outputs map[string][][]byte + capacity int + expectedOutput map[string][]byte + assertValidity require.BoolAssertionFunc }{ { name: "Single node", @@ -43,7 +44,7 @@ func TestSummaryBuffer(t *testing.T) { expectedOutput: map[string][]byte{ "node": []byte("foobarbaz"), }, - expectedOverflow: false, + assertValidity: require.True, }, { name: "Single node overflow", @@ -54,9 +55,9 @@ func TestSummaryBuffer(t *testing.T) { []byte("baz"), }, }, - capacity: 8, - expectedOutput: nil, - expectedOverflow: true, + capacity: 8, + expectedOutput: nil, + assertValidity: require.False, }, { name: "Multiple nodes", @@ -83,7 +84,7 @@ func TestSummaryBuffer(t *testing.T) { "node2": []byte("bazbarfoo"), "node3": []byte("bazbazbaz"), }, - expectedOverflow: false, + assertValidity: require.True, }, { name: "Multiple nodes overflow", @@ -104,16 +105,16 @@ func TestSummaryBuffer(t *testing.T) { []byte("baz"), }, }, - capacity: 25, - expectedOutput: nil, - expectedOverflow: true, + capacity: 25, + expectedOutput: nil, + assertValidity: require.False, }, { - name: "No output", - outputs: nil, - capacity: 10, - expectedOutput: map[string][]byte{}, - expectedOverflow: false, + name: "No output", + outputs: nil, + capacity: 10, + expectedOutput: map[string][]byte{}, + assertValidity: require.False, }, } for _, tc := range tests { @@ -133,10 +134,9 @@ func TestSummaryBuffer(t *testing.T) { }() } wg.Wait() - output, overflow := buffer.Export() + output, isValid := buffer.Export() require.Equal(t, tc.expectedOutput, output) - require.Equal(t, tc.expectedOverflow, overflow) - + tc.assertValidity(t, isValid) }) } }