From 0c998a8e17a5dccec9385084777f57319248d6aa Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Mon, 27 Apr 2020 07:19:14 -0700 Subject: [PATCH] Add test for signal handling --- server_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/server_test.go b/server_test.go index fe2d976..5cb88b1 100644 --- a/server_test.go +++ b/server_test.go @@ -6,6 +6,7 @@ package asynq import ( "context" + "syscall" "testing" "time" @@ -49,6 +50,35 @@ func TestServer(t *testing.T) { srv.Stop() } +func TestServerRun(t *testing.T) { + // https://github.com/go-redis/redis/issues/1029 + ignoreOpt := goleak.IgnoreTopFunction("github.com/go-redis/redis/v7/internal/pool.(*ConnPool).reaper") + defer goleak.VerifyNoLeaks(t, ignoreOpt) + + srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{}) + + done := make(chan struct{}) + // Make sure server exits when receiving TERM signal. + go func() { + time.Sleep(2 * time.Second) + syscall.Kill(syscall.Getpid(), syscall.SIGTERM) + done <- struct{}{} + }() + + go func() { + select { + case <-time.After(10 * time.Second): + t.Fatal("server did not stop after receiving TERM signal") + case <-done: + } + }() + + mux := NewServeMux() + if err := srv.Run(mux); err != nil { + t.Fatal(err) + } +} + func TestServerErrServerStopped(t *testing.T) { srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{}) handler := NewServeMux()