Skip to content

Commit a3f208a

Browse files
authored
Replace flaky host instrumentation test (#426)
Instead of checking if the underlying system memory does not change over a second, an assumption heavily dependant on the external system, test that the reported memory is within expected bounds.
1 parent 1d9c921 commit a3f208a

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

instrumentation/host/host_test.go

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package host_test
1717
import (
1818
"context"
1919
"os"
20-
"runtime"
2120
"testing"
2221
"time"
2322

@@ -142,45 +141,32 @@ func TestHostMemory(t *testing.T) {
142141
assert.NoError(t, err)
143142

144143
ctx := context.Background()
145-
hostBefore, err := mem.VirtualMemoryWithContext(ctx)
144+
vMem, err := mem.VirtualMemoryWithContext(ctx)
146145
require.NoError(t, err)
147146

148-
slice := make([]byte, 100*1024*1024)
149-
defer runtime.KeepAlive(slice)
150-
for i := range slice {
151-
slice[i] = byte(i)
152-
}
153-
154-
// As we are going to read the /proc file system for this info, sleep a while:
155-
time.Sleep(time.Second)
156-
157147
impl.RunAsyncInstruments()
158148

159-
hostAfter, err := mem.VirtualMemoryWithContext(ctx)
160-
require.NoError(t, err)
161-
162149
hostUsed := getMetric(impl, "system.memory.usage", host.LabelMemoryUsed[0])
150+
assert.Greater(t, hostUsed, 0.0)
151+
assert.LessOrEqual(t, hostUsed, float64(vMem.Total))
152+
163153
hostAvailable := getMetric(impl, "system.memory.usage", host.LabelMemoryAvailable[0])
154+
assert.GreaterOrEqual(t, hostAvailable, 0.0)
155+
assert.Less(t, hostAvailable, float64(vMem.Total))
164156

165157
hostUsedUtil := getMetric(impl, "system.memory.utilization", host.LabelMemoryUsed[0])
166-
hostAvailableUtil := getMetric(impl, "system.memory.utilization", host.LabelMemoryAvailable[0])
167-
168-
beforeTotal := hostBefore.Available + hostBefore.Used
169-
afterTotal := hostAfter.Available + hostAfter.Used
170-
measureTotal := hostUsed + hostAvailable
171-
172-
// Tolerance is 5%
173-
const tolerance = 0.05
158+
assert.Greater(t, hostUsedUtil, 0.0)
159+
assert.LessOrEqual(t, hostUsedUtil, 1.0)
174160

175-
// Check that the sum of used and available doesn't change:
176-
require.InEpsilon(t, float64(beforeTotal), measureTotal, tolerance)
177-
require.InEpsilon(t, float64(afterTotal), measureTotal, tolerance)
178-
179-
// Check that the implied total is equal from both Used and Available metrics:
180-
require.InEpsilon(t, hostUsed/hostUsedUtil, hostAvailable/hostAvailableUtil, tolerance)
161+
hostAvailableUtil := getMetric(impl, "system.memory.utilization", host.LabelMemoryAvailable[0])
162+
assert.GreaterOrEqual(t, hostAvailableUtil, 0.0)
163+
assert.Less(t, hostAvailableUtil, 1.0)
181164

182-
// Check that utilization sums to 1.0:
183-
require.InEpsilon(t, 1.0, hostUsedUtil+hostAvailableUtil, tolerance)
165+
if hostUsed > hostAvailable {
166+
assert.Greater(t, hostUsedUtil, hostAvailableUtil)
167+
} else {
168+
assert.Less(t, hostUsedUtil, hostAvailableUtil)
169+
}
184170
}
185171

186172
func sendBytes(t *testing.T, count int) error {

0 commit comments

Comments
 (0)