Commit 3bd4169
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
2 files changed
+8
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
44 | 42 | | |
45 | 43 | | |
| 44 | + | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
581 | 581 | | |
582 | 582 | | |
583 | 583 | | |
584 | | - | |
585 | | - | |
586 | | - | |
587 | | - | |
588 | 584 | | |
589 | 585 | | |
590 | | - | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
591 | 592 | | |
592 | 593 | | |
593 | 594 | | |
| |||
0 commit comments