2023-01-30 16:27:06 +03:00
|
|
|
package mox
|
|
|
|
|
|
|
|
import (
|
2023-02-16 11:57:27 +03:00
|
|
|
"context"
|
2023-01-30 16:27:06 +03:00
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLifecycle(t *testing.T) {
|
2023-02-16 11:57:27 +03:00
|
|
|
Shutdown, ShutdownCancel = context.WithCancel(context.Background())
|
2023-01-30 16:27:06 +03:00
|
|
|
nc0, nc1 := net.Pipe()
|
|
|
|
defer nc0.Close()
|
|
|
|
defer nc1.Close()
|
2024-06-10 19:18:20 +03:00
|
|
|
Connections.Register(nc0, "proto", "listener")
|
|
|
|
Connections.Shutdown()
|
2023-01-30 16:27:06 +03:00
|
|
|
|
2024-06-10 19:18:20 +03:00
|
|
|
done := Connections.Done()
|
2023-01-30 16:27:06 +03:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
t.Fatalf("already done, but still a connection open")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := nc0.Read(make([]byte, 1))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected i/o deadline exceeded, got no error")
|
|
|
|
}
|
|
|
|
if !errors.Is(err, os.ErrDeadlineExceeded) {
|
|
|
|
t.Fatalf("got %v, expected os.ErrDeadlineExceeded", err)
|
|
|
|
}
|
2024-06-10 19:18:20 +03:00
|
|
|
Connections.Unregister(nc0)
|
2023-01-30 16:27:06 +03:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unregistered connection, but not yet done")
|
|
|
|
}
|
|
|
|
}
|