diff --git a/receiver/carbonreceiver/transport/server_test.go b/receiver/carbonreceiver/transport/server_test.go index 4b86bc9a5ec1..5e4288f1186d 100644 --- a/receiver/carbonreceiver/transport/server_test.go +++ b/receiver/carbonreceiver/transport/server_test.go @@ -55,7 +55,8 @@ func Test_Server_ListenAndServe(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - addr := testutil.GetAvailableLocalAddress(t) + addr := testutil.GetAvailableLocalNetworkAddress(t, tt.name) + svr, err := tt.buildServerFn(addr) require.NoError(t, err) require.NotNil(t, svr) @@ -89,6 +90,12 @@ func Test_Server_ListenAndServe(t *testing.T) { mr.WaitAllOnMetricsProcessedCalls() + // Keep trying until we're timed out or got a result + assert.Eventually(t, func() bool { + mdd := mc.AllMetrics() + return len(mdd) > 0 + }, 10*time.Second, 500*time.Millisecond) + err = svr.Close() assert.NoError(t, err) diff --git a/receiver/statsdreceiver/transport/server_test.go b/receiver/statsdreceiver/transport/server_test.go index 347ce2151836..6965d5612b66 100644 --- a/receiver/statsdreceiver/transport/server_test.go +++ b/receiver/statsdreceiver/transport/server_test.go @@ -20,6 +20,7 @@ import ( "strconv" "sync" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -31,7 +32,6 @@ import ( ) func Test_Server_ListenAndServe(t *testing.T) { - t.Skip("Test is unstable, see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/1426") tests := []struct { name string @@ -48,7 +48,21 @@ func Test_Server_ListenAndServe(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - addr := testutil.GetAvailableLocalAddress(t) + addr := testutil.GetAvailableLocalNetworkAddress(t, "udp") + + // Endpoint should be free. + ln0, err := net.ListenPacket("udp", addr) + require.NoError(t, err) + require.NotNil(t, ln0) + + // Ensure that the endpoint wasn't something like ":0" by checking that a second listener will fail. + ln1, err := net.ListenPacket("udp", addr) + require.Error(t, err) + require.Nil(t, ln1) + + // Unbind the local address so the mock UDP service can use it + ln0.Close() + srv, err := tt.buildServerFn(addr) require.NoError(t, err) require.NotNil(t, srv) @@ -76,7 +90,6 @@ func Test_Server_ListenAndServe(t *testing.T) { gc, err := tt.buildClientFn(host, port) require.NoError(t, err) require.NotNil(t, gc) - err = gc.SendMetric(client.Metric{ Name: "test.metric", Value: "42", @@ -84,10 +97,15 @@ func Test_Server_ListenAndServe(t *testing.T) { }) assert.NoError(t, err) runtime.Gosched() - err = gc.Disconnect() assert.NoError(t, err) + // Keep trying until we're timed out or got a result + assert.Eventually(t, func() bool { + return len(transferChan) > 0 + }, 10*time.Second, 500*time.Millisecond) + + // Close the server connection, this will cause ListenAndServer to error out and the deferred wgListenAndServe.Done will fire err = srv.Close() assert.NoError(t, err)