Skip to content

Commit 3bd4169

Browse files
Fix flaky TestNegativeCases in cometd input (#47698) (#47820) (#47918)
The test was flaky because workerWg.Done() was called before the final log statement completed, allowing input.Stop() to return while the goroutine was still logging to the test's logger. Fix by reordering defers so workerWg.Done() runs last (must be first defer due to LIFO ordering). Also fix goroutine leak in the test itself by removing the unnecessary goroutine and using a select to check for unexpected events. ## How to test this PR locally Difficult to reproduce, run with: ```sh GOMAXPROCS=1 go test -run TestNegativeCases -count=2000 -failfast ./x-pack/filebeat/input/cometd ``` ``` panic: Log in goroutine after TestNegativeCases has completed: 2025-11-28T16:04:01.738+0100 INFO cometd Input worker has stopped. {"pubsub_channel": "channel_name"} goroutine 8352 [running]: testing.(*common).log(0xc0002aa000, {0xc0003e9a40?, 0x65?}) /usr/lib/go/src/testing/testing.go:1030 +0x1ac testing.(*common).Logf(0xc0002aa000, {0x13f435e?, 0x400?}, {0xc00035ddb0?, 0x0?, 0x2?}) /usr/lib/go/src/testing/testing.go:1191 +0x4f go.uber.org/zap/zaptest.TestingWriter.Write({{0x75eedea33200?, 0xc0002aa000?}, 0x40?}, {0xc0005e5c00?, 0x66, 0xc0005e5c00?}) /home/orestis/go/pkg/mod/go.uber.org/zap@v1.27.0/zaptest/logger.go:146 +0xdb go.uber.org/zap/zapcore.(*ioCore).Write(0xc000136d80, {0x0, {0xc2428c586bfdd61c, 0xafdaba1f1, 0x1d14dc0}, {0x13f6038, 0x6}, {0x140a066, 0x19}, {0x0, ...}, ...}, ...) /home/orestis/go/pkg/mod/go.uber.org/zap@v1.27.0/zapcore/core.go:99 +0xb5 go.uber.org/zap/zapcore.(*CheckedEntry).Write(0xc000251790, {0x0, 0x0, 0x0}) /home/orestis/go/pkg/mod/go.uber.org/zap@v1.27.0/zapcore/entry.go:253 +0x119 go.uber.org/zap.(*SugaredLogger).log(0xc000054a98, 0x0, {0x0?, 0x0?}, {0xc0000d5e18?, 0x1?, 0x1?}, {0x0, 0x0, 0x0}) /home/orestis/go/pkg/mod/go.uber.org/zap@v1.27.0/sugar.go:355 +0xec go.uber.org/zap.(*SugaredLogger).Info(...) /home/orestis/go/pkg/mod/go.uber.org/zap@v1.27.0/sugar.go:155 github.com/elastic/elastic-agent-libs/logp.(*Logger).Info(...) /home/orestis/go/pkg/mod/github.com/elastic/elastic-agent-libs@v0.26.2/logp/logger.go:186 github.com/elastic/beats/v7/x-pack/filebeat/input/cometd.(*cometdInput).Run.func1.1() /home/orestis/src/beats/x-pack/filebeat/input/cometd/input.go:77 +0x56d created by github.com/elastic/beats/v7/x-pack/filebeat/input/cometd.(*cometdInput).Run.func1 in goroutine 8343 /home/orestis/src/beats/x-pack/filebeat/input/cometd/input.go:41 +0x87 FAIL github.com/elastic/beats/v7/x-pack/filebeat/input/cometd 47.249s FAIL ``` ## Related issues - Closes #47698 (cherry picked from commit 787ea1e) Co-authored-by: Orestis Floros <orestis.floros@elastic.co>
1 parent 36109da commit 3bd4169

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

x-pack/filebeat/input/cometd/input.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ func (in *cometdInput) Run() {
3939
in.workerOnce.Do(func() {
4040
in.workerWg.Add(1)
4141
go func() {
42-
in.log.Info("Input worker has started.")
43-
defer in.log.Info("Input worker has stopped.")
4442
defer in.workerWg.Done()
4543
defer in.workerCancel()
44+
in.log.Info("Input worker has started.")
45+
defer in.log.Info("Input worker has stopped.")
4646
in.b = bay.Bayeux{}
4747

4848
rt := rate.NewLimiter(rate.Every(retryInterval), 1)

x-pack/filebeat/input/cometd/input_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -581,13 +581,14 @@ func TestNegativeCases(t *testing.T) {
581581
require.NotNil(t, input)
582582

583583
input.Run()
584-
go func() {
585-
<-eventsCh
586-
assert.Error(t, fmt.Errorf("there should be no events"))
587-
}()
588584

589585
// wait for run to return error or event
590-
time.Sleep(100 * time.Millisecond)
586+
select {
587+
case <-eventsCh:
588+
t.Error("expected no events, but received one")
589+
case <-time.After(100 * time.Millisecond):
590+
// Expected: no events received.
591+
}
591592

592593
input.Stop()
593594
}

0 commit comments

Comments
 (0)