Skip to content

Commit f6effae

Browse files
mergify[bot]faec
andauthored
Fix race in add_host_metadata unit test (#47192) (#47203)
Fix the logic used in the add_host_metadata TestDataReload test to guarantee at least one asynchronous call to Run after a feature flag change, which was buggy because its starting counter was measured before the feature flag change instead of after. (cherry picked from commit 3dd2c38) Co-authored-by: Fae Charlton <[email protected]>
1 parent 2453bb6 commit f6effae

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

libbeat/processors/add_host_metadata/add_host_metadata_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,6 @@ func TestDataReload(t *testing.T) {
582582
assert.Equal(t, int64(1), info.HostInfoRequestCount.Load())
583583
assert.Equal(t, int64(0), info.FQDNRequestCount.Load())
584584

585-
var previousEventCount = eventCount.Load()
586585
// update
587586
err = features.UpdateFromConfig(conf.MustNewConfigFrom(map[string]interface{}{
588587
"features.fqdn.enabled": true,
@@ -593,7 +592,13 @@ func TestDataReload(t *testing.T) {
593592

594593
// we should have reloaded the data once
595594
// note that with fqdn enabled, we still fetch the host info
595+
var previousEventCount = eventCount.Load()
596596
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
597+
// Causality: there can be up to processingGoroutineCount pending
598+
// increments of eventCount from Run calls that already finished.
599+
// To guarantee that at least one run has happened since the
600+
// feature flag change, our event count must go up by _more_
601+
// than that.
597602
assert.Greater(collect, eventCount.Load(), previousEventCount+processingGoroutineCount)
598603
}, time.Second*5, time.Millisecond)
599604

@@ -603,13 +608,13 @@ func TestDataReload(t *testing.T) {
603608
assert.Equal(t, int64(1), info.FQDNRequestCount.Load())
604609

605610
// update back to the original value
606-
previousEventCount = eventCount.Load()
607611
err = features.UpdateFromConfig(conf.MustNewConfigFrom(map[string]interface{}{
608612
"features.fqdn.enabled": false,
609613
}))
610614
require.NoError(t, err)
611615

612616
// we should have reloaded the data once more
617+
previousEventCount = eventCount.Load()
613618
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
614619
assert.Greater(collect, eventCount.Load(), previousEventCount+processingGoroutineCount)
615620
}, time.Second*5, time.Millisecond)

0 commit comments

Comments
 (0)