diff --git a/checks/process.go b/checks/process.go index 5d2207003..4bcf37ac0 100644 --- a/checks/process.go +++ b/checks/process.go @@ -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 + 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 diff --git a/checks/process_test.go b/checks/process_test.go index e6b88ee84..7108bdf49 100644 --- a/checks/process_test.go +++ b/checks/process_test.go @@ -1,6 +1,7 @@ package checks import ( + "github.com/stretchr/testify/require" "regexp" "runtime" "strings" @@ -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