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) + }) +}