Skip to content

Commit 09919ab

Browse files
authored
[chore] Reduce port contention in service tests (#12616)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description I noticed the service tests were a little flaky due to using the default Prometheus port. This should at least reduce the frequency where this happens if not eliminate it. I did two things to help elimination contention for the port: 1. Disable the Prometheus server in tests where it isn't needed. 2. Ensure the port is randomized in tests where it is needed.
1 parent d3c5895 commit 09919ab

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

service/service_test.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -349,19 +349,31 @@ func testCollectorStartHelperWithReaders(t *testing.T, tc ownMetricsTestCase, ne
349349

350350
// TestServiceTelemetryRestart tests that the service correctly restarts the telemetry server.
351351
func TestServiceTelemetryRestart(t *testing.T) {
352+
metricsAddr := promtest.GetAvailableLocalAddressPrometheus(t)
353+
cfg := newNopConfig()
354+
cfg.Telemetry.Metrics.Readers = []config.MetricReader{
355+
{
356+
Pull: &config.PullMetricReader{
357+
Exporter: config.PullMetricExporter{
358+
Prometheus: metricsAddr,
359+
},
360+
},
361+
},
362+
}
352363
// Create a service
353-
srvOne, err := New(context.Background(), newNopSettings(), newNopConfig())
364+
srvOne, err := New(context.Background(), newNopSettings(), cfg)
354365
require.NoError(t, err)
355366

356367
// URL of the telemetry service metrics endpoint
357-
telemetryURL := "http://localhost:8888/metrics"
368+
telemetryURL := fmt.Sprintf("http://%s:%d/metrics", *metricsAddr.Host, *metricsAddr.Port)
358369

359370
// Start the service
360371
require.NoError(t, srvOne.Start(context.Background()))
361372

362373
// check telemetry server to ensure we get a response
363374
var resp *http.Response
364375

376+
//nolint:gosec
365377
resp, err = http.Get(telemetryURL)
366378
assert.NoError(t, err)
367379
assert.NoError(t, resp.Body.Close())
@@ -375,7 +387,7 @@ func TestServiceTelemetryRestart(t *testing.T) {
375387
require.NoError(t, srvOne.Shutdown(context.Background()))
376388

377389
// Create a new service with the same telemetry
378-
srvTwo, err := New(context.Background(), newNopSettings(), newNopConfig())
390+
srvTwo, err := New(context.Background(), newNopSettings(), cfg)
379391
require.NoError(t, err)
380392

381393
// Start the new service
@@ -384,6 +396,7 @@ func TestServiceTelemetryRestart(t *testing.T) {
384396
// check telemetry server to ensure we get a response
385397
require.Eventually(t,
386398
func() bool {
399+
//nolint:gosec
387400
resp, err = http.Get(telemetryURL)
388401
assert.NoError(t, resp.Body.Close())
389402
return err == nil
@@ -693,14 +706,6 @@ func newNopConfigPipelineConfigs(pipelineCfgs pipelines.Config) Config {
693706
},
694707
Metrics: telemetry.MetricsConfig{
695708
Level: configtelemetry.LevelBasic,
696-
Readers: []config.MetricReader{
697-
{
698-
Pull: &config.PullMetricReader{Exporter: config.PullMetricExporter{Prometheus: &config.Prometheus{
699-
Host: newPtr("localhost"),
700-
Port: newPtr(8888),
701-
}}},
702-
},
703-
},
704709
},
705710
},
706711
}

0 commit comments

Comments
 (0)