Skip to content

Commit fd0fb38

Browse files
committed
remove dependency
1 parent f5c86b9 commit fd0fb38

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

go.mod

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

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

mock_test.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package quartz_test
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
67
"fmt"
8+
"os"
9+
"runtime/pprof"
10+
"strings"
711
"testing"
812
"time"
913

1014
"github.com/coder/quartz"
11-
"go.uber.org/goleak"
1215
)
1316

1417
func TestTimer_NegativeDuration(t *testing.T) {
@@ -579,5 +582,53 @@ func TestTickerStop_Go123(t *testing.T) {
579582
}
580583

581584
func TestMain(m *testing.M) {
582-
goleak.VerifyTestMain(m)
585+
verifyNoLeakTestMain(m)
586+
}
587+
588+
func verifyNoLeakTestMain(m *testing.M) {
589+
before := snapshot()
590+
code := m.Run()
591+
now := time.Now()
592+
for {
593+
after := snapshot()
594+
if len(after) > len(before) {
595+
// Allow test cleanup to settle.
596+
if time.Since(now) < 200*time.Millisecond {
597+
time.Sleep(50 * time.Millisecond)
598+
continue
599+
}
600+
fmt.Fprintln(os.Stderr, "Possible goroutine leak(s):")
601+
fmt.Fprintln(os.Stderr, diff(before, after))
602+
os.Exit(1)
603+
}
604+
os.Exit(code)
605+
}
606+
}
607+
608+
func snapshot() []string {
609+
var buf bytes.Buffer
610+
_ = pprof.Lookup("goroutine").WriteTo(&buf, 2)
611+
var clean []string
612+
for _, s := range strings.Split(buf.String(), "\n\n") {
613+
if !strings.Contains(s, "runtime/pprof") {
614+
clean = append(clean, s)
615+
}
616+
}
617+
return clean
618+
}
619+
620+
func diff(a, b []string) string {
621+
m := make(map[string]int)
622+
for _, s := range a {
623+
m[s]++
624+
}
625+
var leaks []string
626+
for _, s := range b {
627+
if m[s] > 0 {
628+
m[s]--
629+
continue
630+
}
631+
leaks = append(leaks, s)
632+
}
633+
return strings.Join(leaks, "\n\n")
583634
}

0 commit comments

Comments
 (0)