From d72997da0482c9ea39470fbaae03c47132f11037 Mon Sep 17 00:00:00 2001 From: xyqweb <12839346+xyqweb@users.noreply.github.com> Date: Wed, 19 Jul 2023 20:06:06 +0800 Subject: [PATCH] fix g.Wait not support OS Signal #2752 (#2768) --- net/ghttp/ghttp_server.go | 3 +++ net/ghttp/ghttp_z_unit_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/net/ghttp/ghttp_server.go b/net/ghttp/ghttp_server.go index 51a3ff3df..782781693 100644 --- a/net/ghttp/ghttp_server.go +++ b/net/ghttp/ghttp_server.go @@ -447,6 +447,9 @@ func (s *Server) Run() { func Wait() { var ctx = context.TODO() + // Signal handler in asynchronous way. + go handleProcessSignal() + <-allShutdownChan // Remove plugins. serverMapping.Iterator(func(k string, v interface{}) bool { diff --git a/net/ghttp/ghttp_z_unit_test.go b/net/ghttp/ghttp_z_unit_test.go index e85d44f28..5f62d4afb 100644 --- a/net/ghttp/ghttp_z_unit_test.go +++ b/net/ghttp/ghttp_z_unit_test.go @@ -168,3 +168,16 @@ func Test_BuildParams(t *testing.T) { } }) } + +func Test_ServerSignal(t *testing.T) { + s := g.Server(guid.S()) + s.BindHandler("/", func(r *ghttp.Request) { + r.Response.Write("hello world") + }) + gtest.Assert(s.Start(), nil) + g.Wait() + time.Sleep(100 * time.Millisecond) + gtest.C(t, func(t *gtest.T) { + t.AssertEQ(s.Shutdown(), nil) + }) +}