package callstest import ( "fmt" "os" "runtime" "strings" "testing" "time" "amadan.net/oficina/calls/internal/calls/keymailtest" ) // TestMain keeps Keymail classification inside the process: every POST // /signin now classifies its address (auth.go's gateBegin), and without // this each one would ask the real network whether example.com runs // Keymail. Every DNS query is answered NXDOMAIN; no fixture is // registered here, so every address is plain email. func TestMain(m *testing.M) { restore, err := keymailtest.Install() if err != nil { fmt.Fprintln(os.Stderr, "keymailtest.Install:", err) os.Exit(1) } code := m.Run() // No test may leave a sign-in mail worker running: every app a test // builds has its queue closed in cleanup (Runtime.Close, a // TestHandle's stop, or buildForTest). if code == 0 { deadline := time.Now().Add(5 * time.Second) for mailWorkers() > 0 && time.Now().Before(deadline) { time.Sleep(10 * time.Millisecond) } if n := mailWorkers(); n > 0 { fmt.Fprintf(os.Stderr, "FAIL: %d sign-in mail queue goroutines still running after the tests\n", n) code = 1 } } restore() os.Exit(code) } // mailWorkers counts goroutines running a sign-in mail queue's worker // or log pump (internal/calls/signin_mail.go). func mailWorkers() int { buf := make([]byte, 1<<22) stacks := string(buf[:runtime.Stack(buf, true)]) return strings.Count(stacks, "(*mailQueue).run(") + strings.Count(stacks, "(*mailQueue).pump(") }