Skip to content
This repository was archived by the owner on Jul 27, 2023. It is now read-only.

Ensure that process.IOStat is non-nil #202

Merged
merged 1 commit into from
Nov 6, 2018
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 checks/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func formatIO(fp *process.FilledProcess, lastIO *process.IOCountersStat, before

diff := time.Now().Unix() - before.Unix()
if before.IsZero() || diff <= 0 {
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change the signature to return a non-pointer struct if we never return nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right or wrong, the current approach is consistent with formatMemory and formatCommand.

Whats the benefit on returning a non pointer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only benefit is that the compiler will force us to return a struct with zero-ed out fields instead of nil. Since we never return nil (and none of the callers handle nil returns) it feels off to allow it to be returned in the function signature.

However, going by https://stackoverflow.com/questions/23542989/pointers-vs-values-in-parameters-and-return-values it looks like pointers are recommended for "big structs" which IOStat is.

return &model.IOStat{}
}
// Reading 0 as a counter means the file could not be opened due to permissions. We distinguish this from a real 0 in rates.
var readRate float32
Expand Down
34 changes: 34 additions & 0 deletions checks/process_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package checks

import (
"github.com/stretchr/testify/require"
"regexp"
"runtime"
"strings"
Expand Down Expand Up @@ -140,6 +141,39 @@ func TestRateCalculation(t *testing.T) {
assert.True(t, floatEquals(calculateRate(0, 1, prev), 0))
}

func TestFormatIO(t *testing.T) {
fp := &process.FilledProcess{
IOStat: &process.IOCountersStat{
ReadCount: 6,
WriteCount: 8,
ReadBytes: 10,
WriteBytes: 12,
},
}

last := &process.IOCountersStat{
ReadCount: 1,
WriteCount: 2,
ReadBytes: 3,
WriteBytes: 4,
}

// fp.IoStat is nil
assert.NotNil(t, formatIO(&process.FilledProcess{}, last, time.Now().Add(-2*time.Second)))

// Elapsed time < 1s
assert.NotNil(t, formatIO(fp, last, time.Now()))

result := formatIO(fp, last, time.Now().Add(-1*time.Second))
require.NotNil(t, result)

assert.Equal(t, float32(5), result.ReadRate)
assert.Equal(t, float32(6), result.WriteRate)
assert.Equal(t, float32(7), result.ReadBytesRate)
assert.Equal(t, float32(8), result.WriteBytesRate)

}

func floatEquals(a, b float32) bool {
var e float32 = 0.00000001 // Difference less than some epsilon
return a-b < e && b-a < e
Expand Down