From 758c455c7e0e033ddc2c1ea43f3f08a31650272c Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 13 Apr 2023 11:48:18 +0400 Subject: [PATCH] services/rpcsrv: Test `Server` shutdown with failed precondition There is an existing problem with RPC server shutdown freeze after start failure due to some init actions (at least HTTP listen) described in #2896. Add dedicated unit test which checks that `Shutdown` returns within 5s after `Start` method encounters internal problems. Signed-off-by: Leonard Lyubich --- pkg/services/rpcsrv/server_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/services/rpcsrv/server_test.go b/pkg/services/rpcsrv/server_test.go index 08d636d07..98d8324ef 100644 --- a/pkg/services/rpcsrv/server_test.go +++ b/pkg/services/rpcsrv/server_test.go @@ -50,6 +50,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.uber.org/atomic" "go.uber.org/zap/zapcore" ) @@ -3331,3 +3332,21 @@ func BenchmarkHandleIn(b *testing.B) { {"type": "Integer", "value": "42"}, {"type": "Boolean", "value": false}]]}`)) }) } + +func TestFailedPreconditionShutdown(t *testing.T) { + _, srv, _ := initClearServerWithCustomConfig(t, func(c *config.Config) { + c.ApplicationConfiguration.RPC.Addresses = []string{"not an address"} + }) + + srv.Start() + require.Positive(t, len(srv.errChan)) // this is how Start reports internal failures + + var stopped atomic.Bool + + go func() { + srv.Shutdown() + stopped.Store(true) + }() + + require.Eventually(t, stopped.Load, 5*time.Second, 100*time.Millisecond, "Shutdown should return") +}