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