Skip to content

Commit

Permalink
test: TestHostNetwork Wait for results until go test times out
Browse files Browse the repository at this point in the history
  • Loading branch information
laojianzi committed Sep 29, 2021
1 parent 08b8025 commit f7764b4
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions instrumentation/host/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/contrib/instrumentation/host"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/metrictest"

"go.opentelemetry.io/contrib/instrumentation/host"
)

func getMetric(impl *metrictest.MeterImpl, name string, lbl attribute.KeyValue) float64 {
Expand Down Expand Up @@ -220,20 +221,18 @@ func TestHostNetwork(t *testing.T) {
require.NoError(t, err)

// As we are going to read the /proc file system for this info, sleep a while:
time.Sleep(time.Second)

impl.RunAsyncInstruments()
require.Eventually(t, func() bool {
hostAfter, err := net.IOCountersWithContext(ctx, false)
require.NoError(t, err)

hostAfter, err := net.IOCountersWithContext(ctx, false)
require.NoError(t, err)
return uint64(howMuch) <= hostAfter[0].BytesSent-hostBefore[0].BytesSent &&
uint64(howMuch) <= hostAfter[0].BytesRecv-hostBefore[0].BytesRecv
}, 30*time.Second, time.Second/2)

impl.RunAsyncInstruments()
hostTransmit := getMetric(impl, "system.network.io", host.AttributeNetworkTransmit[0])
hostReceive := getMetric(impl, "system.network.io", host.AttributeNetworkReceive[0])

// Check that the network transmit/receive used is greater than before:
require.LessOrEqual(t, uint64(howMuch), hostAfter[0].BytesSent-hostBefore[0].BytesSent)
require.LessOrEqual(t, uint64(howMuch), hostAfter[0].BytesRecv-hostBefore[0].BytesRecv)

// Check that the recorded measurements reflect the same change:
require.LessOrEqual(t, uint64(howMuch), uint64(hostTransmit)-hostBefore[0].BytesSent)
require.LessOrEqual(t, uint64(howMuch), uint64(hostReceive)-hostBefore[0].BytesRecv)
Expand Down

0 comments on commit f7764b4

Please sign in to comment.