Skip to content

Commit

Permalink
[chore] fix flaky test for datadogreceiver server binding (#24895)
Browse files Browse the repository at this point in the history
Use localhost:0 to get a free assignable port.

Fixes #24894
  • Loading branch information
atoulme authored Aug 4, 2023
1 parent 0c2d590 commit b149984
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions receiver/datadogreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func TestCreateReceiver(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
cfg.(*Config).Endpoint = "http://localhost:0"

tReceiver, err := factory.CreateTracesReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, consumertest.NewNop())
assert.NoError(t, err)
Expand Down
3 changes: 3 additions & 0 deletions receiver/datadogreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

type datadogReceiver struct {
address string
config *Config
params receiver.CreateSettings
nextConsumer consumer.Traces
Expand Down Expand Up @@ -66,6 +67,8 @@ func (ddr *datadogReceiver) Start(_ context.Context, host component.Host) error
return fmt.Errorf("failed to create datadog listener: %w", err)
}

ddr.address = hln.Addr().String()

go func() {
if err := ddr.server.Serve(hln); err != nil && !errors.Is(err, http.ErrServerClosed) {
host.ReportFatalError(fmt.Errorf("error starting datadog receiver: %w", err))
Expand Down
7 changes: 5 additions & 2 deletions receiver/datadogreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
func TestDatadogReceiver_Lifecycle(t *testing.T) {

factory := NewFactory()
ddr, err := factory.CreateTracesReceiver(context.Background(), receivertest.NewNopCreateSettings(), factory.CreateDefaultConfig(), consumertest.NewNop())
cfg := factory.CreateDefaultConfig()
cfg.(*Config).Endpoint = "localhost:0"
ddr, err := factory.CreateTracesReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, consumertest.NewNop())
assert.NoError(t, err, "Receiver should be created")

err = ddr.Start(context.Background(), componenttest.NewNopHost())
Expand All @@ -34,6 +36,7 @@ func TestDatadogReceiver_Lifecycle(t *testing.T) {

func TestDatadogServer(t *testing.T) {
cfg := createDefaultConfig().(*Config)
cfg.Endpoint = "localhost:0" // Using a randomly assigned address
dd, err := newDataDogReceiver(
cfg,
consumertest.NewNop(),
Expand Down Expand Up @@ -69,7 +72,7 @@ func TestDatadogServer(t *testing.T) {

req, err := http.NewRequest(
http.MethodPost,
fmt.Sprintf("http://%s/v0.7/traces", cfg.Endpoint),
fmt.Sprintf("http://%s/v0.7/traces", dd.(*datadogReceiver).address),
tc.op,
)
require.NoError(t, err, "Must not error when creating request")
Expand Down

0 comments on commit b149984

Please sign in to comment.