1package mox
2
3import (
4 "context"
5 "errors"
6 "net"
7 "os"
8 "testing"
9
10 "github.com/prometheus/client_golang/prometheus"
11)
12
13func TestLifecycle(t *testing.T) {
14 Shutdown, ShutdownCancel = context.WithCancel(context.Background())
15 c := &connections{
16 conns: map[net.Conn]connKind{},
17 gauges: map[connKind]prometheus.GaugeFunc{},
18 active: map[connKind]int64{},
19 }
20 nc0, nc1 := net.Pipe()
21 defer nc0.Close()
22 defer nc1.Close()
23 c.Register(nc0, "proto", "listener")
24 c.Shutdown()
25
26 done := c.Done()
27 select {
28 case <-done:
29 t.Fatalf("already done, but still a connection open")
30 default:
31 }
32
33 _, err := nc0.Read(make([]byte, 1))
34 if err == nil {
35 t.Fatalf("expected i/o deadline exceeded, got no error")
36 }
37 if !errors.Is(err, os.ErrDeadlineExceeded) {
38 t.Fatalf("got %v, expected os.ErrDeadlineExceeded", err)
39 }
40 c.Unregister(nc0)
41 select {
42 case <-done:
43 default:
44 t.Fatalf("unregistered connection, but not yet done")
45 }
46}
47