make tests pass with "go test -count n" with n > 1

by closing initialized resources during tests.
This commit is contained in:
Mechiel Lukkien
2024-06-10 18:18:20 +02:00
parent dde2258f69
commit f56b04805b
9 changed files with 50 additions and 40 deletions

View File

@ -6,24 +6,17 @@ import (
"net"
"os"
"testing"
"github.com/prometheus/client_golang/prometheus"
)
func TestLifecycle(t *testing.T) {
Shutdown, ShutdownCancel = context.WithCancel(context.Background())
c := &connections{
conns: map[net.Conn]connKind{},
gauges: map[connKind]prometheus.GaugeFunc{},
active: map[connKind]int64{},
}
nc0, nc1 := net.Pipe()
defer nc0.Close()
defer nc1.Close()
c.Register(nc0, "proto", "listener")
c.Shutdown()
Connections.Register(nc0, "proto", "listener")
Connections.Shutdown()
done := c.Done()
done := Connections.Done()
select {
case <-done:
t.Fatalf("already done, but still a connection open")
@ -37,7 +30,7 @@ func TestLifecycle(t *testing.T) {
if !errors.Is(err, os.ErrDeadlineExceeded) {
t.Fatalf("got %v, expected os.ErrDeadlineExceeded", err)
}
c.Unregister(nc0)
Connections.Unregister(nc0)
select {
case <-done:
default: