From 7eb88908b50508c1bc6ff3830cf1c15f5ecea32c Mon Sep 17 00:00:00 2001 From: Senna46 <29295263+Senna46@users.noreply.github.com> Date: Tue, 19 Mar 2024 22:54:07 +0900 Subject: [PATCH 1/3] feat: add error group --- test/util/testnode/rpc_client.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/test/util/testnode/rpc_client.go b/test/util/testnode/rpc_client.go index 1758d4c4..f9c70f0a 100644 --- a/test/util/testnode/rpc_client.go +++ b/test/util/testnode/rpc_client.go @@ -11,6 +11,7 @@ import ( srvconfig "github.com/cosmos/cosmos-sdk/server/config" srvgrpc "github.com/cosmos/cosmos-sdk/server/grpc" srvtypes "github.com/cosmos/cosmos-sdk/server/types" + "golang.org/x/sync/errgroup" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" ) @@ -54,6 +55,9 @@ func StartNode(tmNode *node.Node, cctx Context) (Context, func() error, error) { // context. The returned function should be used to shutdown the server. func StartGRPCServer(app srvtypes.Application, appCfg *srvconfig.Config, cctx Context) (Context, func() error, error) { emptycleanup := func() error { return nil } + + cctx.Context = cctx.Context.WithClient(cctx.RpcClient) + // Add the tx service in the gRPC router. app.RegisterTxService(cctx.Context) @@ -63,17 +67,20 @@ func StartGRPCServer(app srvtypes.Application, appCfg *srvconfig.Config, cctx Co // Add the node service queries to the grpc router. app.RegisterNodeService(cctx.Context, *appCfg) + ctx := cctx.rootCtx + errGroup, _ := errgroup.WithContext(ctx) + + grpcCfg := appCfg.GRPC + // Run maxSendMsgSize := cfg.MaxSendMsgSize to gogoreflection.Register(grpcSrv) - grpcSrv, err := srvgrpc.NewGRPCServer(cctx.Context, app, appCfg.GRPC) + grpcSrv, err := srvgrpc.NewGRPCServer(cctx.Context, app, grpcCfg) if err != nil { return Context{}, emptycleanup, err } - // Start the gRPC server in a goroutine. Note, the provided ctx will ensure - // that the server is gracefully shut down. - go func() error { - return srvgrpc.StartGRPCServer(cctx.rootCtx, log.NewNopLogger().With("module", "grpc-server"), appCfg.GRPC, grpcSrv) - }() + errGroup.Go(func() error { + return srvgrpc.StartGRPCServer(ctx, log.NewNopLogger().With("module", "grpc-server"), grpcCfg, grpcSrv) + }) nodeGRPCAddr := strings.Replace(appCfg.GRPC.Address, "0.0.0.0", "localhost", 1) conn, err := grpc.Dial(nodeGRPCAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) @@ -84,7 +91,7 @@ func StartGRPCServer(app srvtypes.Application, appCfg *srvconfig.Config, cctx Co cctx.Context = cctx.WithGRPCClient(conn) return cctx, func() error { - grpcSrv.GracefulStop() + grpcSrv.Stop() return nil }, nil } From 49418fbf86570f4f6f8de5ab416c7f71f8726ffd Mon Sep 17 00:00:00 2001 From: Senna46 <29295263+Senna46@users.noreply.github.com> Date: Tue, 19 Mar 2024 23:20:25 +0900 Subject: [PATCH 2/3] fix: missing ctx --- test/util/testnode/rpc_client.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/util/testnode/rpc_client.go b/test/util/testnode/rpc_client.go index f9c70f0a..882d95ca 100644 --- a/test/util/testnode/rpc_client.go +++ b/test/util/testnode/rpc_client.go @@ -1,6 +1,7 @@ package testnode import ( + "context" "os" "path" "strings" @@ -32,7 +33,10 @@ func StartNode(tmNode *node.Node, cctx Context) (Context, func() error, error) { coreClient := local.New(tmNode) cctx.Context = cctx.WithClient(coreClient) + goCtx, cancel := context.WithCancel(context.Background()) + cctx.rootCtx = goCtx cleanup := func() error { + cancel() err := tmNode.Stop() if err != nil { return err @@ -56,8 +60,6 @@ func StartNode(tmNode *node.Node, cctx Context) (Context, func() error, error) { func StartGRPCServer(app srvtypes.Application, appCfg *srvconfig.Config, cctx Context) (Context, func() error, error) { emptycleanup := func() error { return nil } - cctx.Context = cctx.Context.WithClient(cctx.RpcClient) - // Add the tx service in the gRPC router. app.RegisterTxService(cctx.Context) From b14ef57735ab0ef31c0a5d1d0df78e796c98fd57 Mon Sep 17 00:00:00 2001 From: Senna46 <29295263+Senna46@users.noreply.github.com> Date: Fri, 22 Mar 2024 00:50:09 +0900 Subject: [PATCH 3/3] test: set RpcClient --- test/util/testnode/rpc_client.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/util/testnode/rpc_client.go b/test/util/testnode/rpc_client.go index 882d95ca..4de25afd 100644 --- a/test/util/testnode/rpc_client.go +++ b/test/util/testnode/rpc_client.go @@ -33,6 +33,8 @@ func StartNode(tmNode *node.Node, cctx Context) (Context, func() error, error) { coreClient := local.New(tmNode) cctx.Context = cctx.WithClient(coreClient) + // Set the rpc client in the context. + cctx.RpcClient = coreClient goCtx, cancel := context.WithCancel(context.Background()) cctx.rootCtx = goCtx cleanup := func() error {