Skip to content

Commit bb8896f

Browse files
committed
fix: resolve race condition in prometheus.receive_http test
Add thread-safe getter methods for `server` and `args` fields to prevent race condition in `TestServerRestarts`. The test was accessing these fields without proper synchronization while `Update()` modified them under lock, causing intermittent failures.
1 parent e1322d9 commit bb8896f

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

internal/component/prometheus/receive_http/receive_http.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,15 @@ func (c *Component) shutdownServer() {
167167
c.server = nil
168168
}
169169
}
170+
171+
func (c *Component) getServer() *fnet.TargetServer {
172+
c.updateMut.RLock()
173+
defer c.updateMut.RUnlock()
174+
return c.server
175+
}
176+
177+
func (c *Component) getArgs() Arguments {
178+
c.updateMut.RLock()
179+
defer c.updateMut.RUnlock()
180+
return c.args
181+
}

internal/component/prometheus/receive_http/receive_http_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,18 +440,19 @@ func TestServerRestarts(t *testing.T) {
440440
serverExit <- comp.Run(ctx)
441441
}()
442442

443-
waitForServerToBeReady(t, comp.args)
443+
waitForServerToBeReady(t, comp.getArgs())
444444

445-
initialServer := comp.server
445+
initialServer := comp.getServer()
446446
require.NotNil(t, initialServer)
447447

448448
err = comp.Update(tc.newArgs)
449449
require.NoError(t, err)
450450

451-
waitForServerToBeReady(t, comp.args)
451+
waitForServerToBeReady(t, comp.getArgs())
452452

453-
require.NotNil(t, comp.server)
454-
restarted := initialServer != comp.server
453+
currentServer := comp.getServer()
454+
require.NotNil(t, currentServer)
455+
restarted := initialServer != currentServer
455456

456457
require.Equal(t, tc.shouldRestart, restarted)
457458

@@ -564,7 +565,6 @@ func testAppendable(actualSamples chan testSample) []storage.Appendable {
564565
val float64,
565566
next storage.Appender,
566567
) (storage.SeriesRef, error) {
567-
568568
actualSamples <- testSample{ts: ts, val: val, l: l}
569569
return ref, nil
570570
}

0 commit comments

Comments
 (0)