diff --git a/_examples/notify/main.go b/_examples/notify/main.go index b9d65b6..dd924d7 100644 --- a/_examples/notify/main.go +++ b/_examples/notify/main.go @@ -24,7 +24,15 @@ func main() { c.String(http.StatusOK, "Welcome Gin Server") }) - if err := router.RunWithContext(ctx); err != nil && err != context.Canceled { + go func() { + if err := router.RunWithContext(context.Background()); err != nil && err != context.Canceled { + panic(err) + } + }() + + <-ctx.Done() + + if err := router.Shutdown(context.Background()); err != nil { panic(err) } } diff --git a/graceful_test.go b/graceful_test.go index 5370cd4..d26ca37 100644 --- a/graceful_test.go +++ b/graceful_test.go @@ -10,8 +10,11 @@ import ( "net" "net/http" "os" + "os/signal" "path/filepath" "runtime" + "sync" + "syscall" "testing" "time" @@ -54,6 +57,85 @@ func TestCycle(t *testing.T) { } } +func TestSimpleSignal(t *testing.T) { + router, err := Default() + assert.NoError(t, err) + assert.NotNil(t, router) + defer router.Close() + + router.GET("/example", func(c *gin.Context) { + time.Sleep(20 * time.Second) + c.String(http.StatusOK, "it worked") + }) + + start := time.Now() + + ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT) + defer cancel() + + var wg sync.WaitGroup + + wg.Add(1) + go func() { + defer wg.Done() + assert.NoError(t, router.RunWithContext(context.Background())) + }() + + wg.Add(1) + go func() { + defer wg.Done() + time.Sleep(5 * time.Second) + assert.NoError(t, syscall.Kill(syscall.Getpid(), syscall.SIGINT)) + }() + + wg.Add(1) + go func() { + defer wg.Done() + testRequest(t, "http://localhost:8080/example") + }() + + <-ctx.Done() + + assert.NoError(t, router.Shutdown(context.Background())) + assert.GreaterOrEqual(t, time.Since(start).Seconds(), 20.0) + + wg.Wait() +} + +func TestSimpleSleep(t *testing.T) { + router, err := Default() + assert.NoError(t, err) + assert.NotNil(t, router) + defer router.Close() + + router.GET("/example", func(c *gin.Context) { + time.Sleep(20 * time.Second) + c.String(http.StatusOK, "it worked") + }) + + start := time.Now() + var wg sync.WaitGroup + + wg.Add(1) + go func() { + defer wg.Done() + assert.NoError(t, router.RunWithContext(context.Background())) + }() + + wg.Add(1) + go func() { + defer wg.Done() + testRequest(t, "http://localhost:8080/example") + }() + + time.Sleep(5 * time.Second) + + assert.NoError(t, router.Shutdown(context.Background())) + assert.GreaterOrEqual(t, time.Since(start).Seconds(), 20.0) + + wg.Wait() +} + func TestSimpleCycle(t *testing.T) { router, err := Default() assert.NoError(t, err)