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
2 changes: 1 addition & 1 deletion lib/web/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions lib/web/command_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
42 changes: 21 additions & 21 deletions lib/web/command_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -83,7 +84,7 @@ func TestSummaryBuffer(t *testing.T) {
"node2": []byte("bazbarfoo"),
"node3": []byte("bazbazbaz"),
},
expectedOverflow: false,
assertValidity: require.True,
},
{
name: "Multiple nodes overflow",
Expand All @@ -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 {
Expand All @@ -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)
})
}
}