Skip to content

Commit fa3ebdb

Browse files
committed
test: add goleak to detect goroutine leaks
1 parent d22fec5 commit fa3ebdb

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/coder/quartz
22

33
go 1.23.9
4+
5+
require go.uber.org/goleak v1.3.0

mock.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,11 @@ func (t *Trap) matches(c *apiCall) bool {
761761
func (t *Trap) Close() {
762762
t.mock.mu.Lock()
763763
defer t.mock.mu.Unlock()
764+
select {
765+
case <-t.done:
766+
return // already closed
767+
default:
768+
}
764769
if t.unreleasedCalls != 0 {
765770
t.mock.tb.Helper()
766771
t.mock.tb.Errorf("trap Closed() with %d unreleased calls", t.unreleasedCalls)

mock_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/coder/quartz"
11+
"go.uber.org/goleak"
1112
)
1213

1314
func TestTimer_NegativeDuration(t *testing.T) {
@@ -403,7 +404,10 @@ func Test_UnreleasedCalls(t *testing.T) {
403404
_ = mClock.Now()
404405
}()
405406

406-
trap.MustWait(testCtx) // missing release
407+
c := trap.MustWait(testCtx) // missing release
408+
trap.Close() // detect unreleased call and fail
409+
410+
c.Release(testCtx) // clean up goroutine
407411
})
408412
}
409413

@@ -573,3 +577,7 @@ func TestTickerStop_Go123(t *testing.T) {
573577
// OK!
574578
}
575579
}
580+
581+
func TestMain(m *testing.M) {
582+
goleak.VerifyTestMain(m)
583+
}

0 commit comments

Comments
 (0)