Skip to content

Remove testutil.WaitFor, use testify helper if needed #2920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2021
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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

## 🛑 Breaking changes 🛑

- Remove testutil.HostPortFromAddr, users can write their own parsing helper (#2919)
- Remove `testutil.HostPortFromAddr`, users can write their own parsing helper (#2919)
- Remove `configparser.DecodeTypeAndName`, use `config.IDFromString` (#2869)
- Remove config.NewViper, users should use config.NewParser (#2917)
- Remove `config.NewViper`, users should use `config.NewParser` (#2917)
- Remove `testutil.WaitFor`, use `testify.Eventually` helper if needed (#2920)

## 💡 Enhancements 💡

Expand Down
17 changes: 9 additions & 8 deletions exporter/opencensusexporter/opencensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package opencensusexporter
import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -66,9 +67,9 @@ func TestSendTraces(t *testing.T) {

td := testdata.GenerateTraceDataOneSpan()
assert.NoError(t, exp.ConsumeTraces(context.Background(), td))
testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return len(sink.AllTraces()) == 1
})
}, 10*time.Second, 5*time.Millisecond)
traces := sink.AllTraces()
require.Len(t, traces, 1)
assert.Equal(t, td, traces[0])
Expand All @@ -77,9 +78,9 @@ func TestSendTraces(t *testing.T) {
// Sending data no Node.
td.ResourceSpans().At(0).Resource().Attributes().InitEmptyWithCapacity(0)
assert.NoError(t, exp.ConsumeTraces(context.Background(), td))
testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return len(sink.AllTraces()) == 1
})
}, 10*time.Second, 5*time.Millisecond)
traces = sink.AllTraces()
require.Len(t, traces, 1)
assert.Equal(t, td, traces[0])
Expand Down Expand Up @@ -163,9 +164,9 @@ func TestSendMetrics(t *testing.T) {

md := testdata.GenerateMetricsOneMetric()
assert.NoError(t, exp.ConsumeMetrics(context.Background(), md))
testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return len(sink.AllMetrics()) == 1
})
}, 10*time.Second, 5*time.Millisecond)
metrics := sink.AllMetrics()
require.Len(t, metrics, 1)
assert.Equal(t, md, metrics[0])
Expand All @@ -174,9 +175,9 @@ func TestSendMetrics(t *testing.T) {
sink.Reset()
md.ResourceMetrics().At(0).Resource().Attributes().InitEmptyWithCapacity(0)
assert.NoError(t, exp.ConsumeMetrics(context.Background(), md))
testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return len(sink.AllMetrics()) == 1
})
}, 10*time.Second, 5*time.Millisecond)
metrics = sink.AllMetrics()
require.Len(t, metrics, 1)
assert.Equal(t, md, metrics[0])
Expand Down
29 changes: 14 additions & 15 deletions exporter/otlpexporter/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
otlptraces "go.opentelemetry.io/collector/internal/data/protogen/collector/trace/v1"
"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/obsreport"
"go.opentelemetry.io/collector/testutil"
)

type mockReceiver struct {
Expand Down Expand Up @@ -229,9 +228,9 @@ func TestSendTraces(t *testing.T) {
assert.NoError(t, exp.ConsumeTraces(context.Background(), td))

// Wait until it is received.
testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return atomic.LoadInt32(&rcv.requestCount) > 0
}, "receive a request")
}, 10*time.Second, 5*time.Millisecond)

// Ensure it was received empty.
assert.EqualValues(t, 0, atomic.LoadInt32(&rcv.totalItems))
Expand All @@ -245,9 +244,9 @@ func TestSendTraces(t *testing.T) {
assert.NoError(t, err)

// Wait until it is received.
testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return atomic.LoadInt32(&rcv.requestCount) > 1
}, "receive a request")
}, 10*time.Second, 5*time.Millisecond)

expectedHeader := []string{"header-value"}

Expand Down Expand Up @@ -299,9 +298,9 @@ func TestSendMetrics(t *testing.T) {
assert.NoError(t, exp.ConsumeMetrics(context.Background(), md))

// Wait until it is received.
testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return atomic.LoadInt32(&rcv.requestCount) > 0
}, "receive a request")
}, 10*time.Second, 5*time.Millisecond)

// Ensure it was received empty.
assert.EqualValues(t, 0, atomic.LoadInt32(&rcv.totalItems))
Expand All @@ -315,9 +314,9 @@ func TestSendMetrics(t *testing.T) {
assert.NoError(t, err)

// Wait until it is received.
testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return atomic.LoadInt32(&rcv.requestCount) > 1
}, "receive a request")
}, 10*time.Second, 5*time.Millisecond)

expectedHeader := []string{"header-value"}

Expand Down Expand Up @@ -456,9 +455,9 @@ func startServerAndMakeRequest(t *testing.T, exp component.TracesExporter, td pd
cancel()

// Wait until it is received.
testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return atomic.LoadInt32(&rcv.requestCount) > 0
}, "receive a request")
}, 10*time.Second, 5*time.Millisecond)

// Verify received span.
assert.EqualValues(t, 2, atomic.LoadInt32(&rcv.totalItems))
Expand Down Expand Up @@ -502,9 +501,9 @@ func TestSendLogData(t *testing.T) {
assert.NoError(t, exp.ConsumeLogs(context.Background(), td))

// Wait until it is received.
testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return atomic.LoadInt32(&rcv.requestCount) > 0
}, "receive a request")
}, 10*time.Second, 5*time.Millisecond)

// Ensure it was received empty.
assert.EqualValues(t, 0, atomic.LoadInt32(&rcv.totalItems))
Expand All @@ -517,9 +516,9 @@ func TestSendLogData(t *testing.T) {
assert.NoError(t, err)

// Wait until it is received.
testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return atomic.LoadInt32(&rcv.requestCount) > 1
}, "receive a request")
}, 10*time.Second, 5*time.Millisecond)

// Verify received logs.
assert.EqualValues(t, 2, atomic.LoadInt32(&rcv.requestCount))
Expand Down
5 changes: 3 additions & 2 deletions receiver/jaegerreceiver/jaeger_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net"
"net/http"
"testing"
"time"

"github.com/apache/thrift/lib/go/thrift"
"github.com/jaegertracing/jaeger/cmd/agent/app/servers/thriftudp"
Expand Down Expand Up @@ -198,9 +199,9 @@ func testJaegerAgent(t *testing.T, agentEndpoint string, receiverConfig *configu
require.NoError(t, jexp.EmitBatch(context.Background(), modelToThrift(batch)))
}

testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return sink.SpansCount() > 0
})
}, 10*time.Second, 5*time.Millisecond)

gotTraces := sink.AllTraces()
require.Equal(t, 1, len(gotTraces))
Expand Down
5 changes: 2 additions & 3 deletions receiver/opencensusreceiver/ocmetrics/opencensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
"go.opentelemetry.io/collector/exporter/opencensusexporter"
"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/obsreport"
"go.opentelemetry.io/collector/testutil"
"go.opentelemetry.io/collector/translator/internaldata"
)

Expand All @@ -70,9 +69,9 @@ func TestReceiver_endToEnd(t *testing.T) {
md := testdata.GenerateMetricsOneMetric()
assert.NoError(t, oce.ConsumeMetrics(context.Background(), md))

testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return len(metricSink.AllMetrics()) != 0
})
}, 10*time.Second, 5*time.Millisecond)
gotMetrics := metricSink.AllMetrics()
require.Len(t, gotMetrics, 1)
assert.Equal(t, md, gotMetrics[0])
Expand Down
5 changes: 2 additions & 3 deletions receiver/opencensusreceiver/octrace/opencensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"go.opentelemetry.io/collector/exporter/opencensusexporter"
"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/obsreport"
"go.opentelemetry.io/collector/testutil"
"go.opentelemetry.io/collector/translator/internaldata"
)

Expand All @@ -67,9 +66,9 @@ func TestReceiver_endToEnd(t *testing.T) {
td := testdata.GenerateTraceDataOneSpan()
assert.NoError(t, oce.ConsumeTraces(context.Background(), td))

testutil.WaitFor(t, func() bool {
assert.Eventually(t, func() bool {
return len(spanSink.AllTraces()) != 0
})
}, 10*time.Second, 5*time.Millisecond)
gotTraces := spanSink.AllTraces()
require.Len(t, gotTraces, 1)
assert.Equal(t, td, gotTraces[0])
Expand Down
30 changes: 0 additions & 30 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,36 +172,6 @@ func WaitForPort(t *testing.T, port uint16) error {
}
}

// WaitFor the specific condition for up to 10 seconds. Records a test error
// if condition does not become true.
func WaitFor(t *testing.T, cond func() bool, errMsg ...interface{}) bool {
t.Helper()

startTime := time.Now()

// Start with 5 ms waiting interval between condition re-evaluation.
waitInterval := time.Millisecond * 5

for {
time.Sleep(waitInterval)

// Increase waiting interval exponentially up to 500 ms.
if waitInterval < time.Millisecond*500 {
waitInterval *= 2
}

if cond() {
return true
}

if time.Since(startTime) > time.Second*10 {
// Waited too long
t.Error("Time out waiting for", errMsg)
return false
}
}
}

// LimitedWriter is an io.Writer that will return an EOF error after MaxLen has
// been reached. If MaxLen is 0, Writes will always succeed.
type LimitedWriter struct {
Expand Down