From f409fc36e2173cda48c11fc898fc495ea1f7035d Mon Sep 17 00:00:00 2001 From: Ekaterina Pavlova Date: Wed, 20 Mar 2024 13:36:53 +0300 Subject: [PATCH] rpcsrv: fix TestRPC failing Default http.Client and http.Transport can be really slow. Also on windows Timeout: time.Second was not enough. Probably network related issue. As mentioned in https://github .com/nspcc-dev/neo-go/issues/2975#issuecomment-1750523651 forcely use only tcp4 and FallbackDelay: -1. This made TestRPC little bit faster so gh windows runner can manage it without timeout or POST request fails. Close #2975 Close #3314 Signed-off-by: Ekaterina Pavlova --- pkg/services/rpcsrv/server_test.go | 32 +++++++++++++++++++++--- pkg/services/rpcsrv/subscription_test.go | 4 +-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/pkg/services/rpcsrv/server_test.go b/pkg/services/rpcsrv/server_test.go index e374ae181..e2befee00 100644 --- a/pkg/services/rpcsrv/server_test.go +++ b/pkg/services/rpcsrv/server_test.go @@ -2,6 +2,7 @@ package rpcsrv import ( "bytes" + "context" "encoding/base64" "encoding/binary" "encoding/json" @@ -9,6 +10,7 @@ import ( gio "io" "math" "math/big" + "net" "net/http" "net/http/httptest" "reflect" @@ -3458,7 +3460,15 @@ func checkErrGetBatchResult(t *testing.T, body []byte, expectingFail bool, expec } func doRPCCallOverWS(rpcCall string, url string, t *testing.T) []byte { - dialer := websocket.Dialer{HandshakeTimeout: time.Second} + dialer := websocket.Dialer{ + HandshakeTimeout: 5 * time.Second, + NetDialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + dialer := net.Dialer{Timeout: 3 * time.Second, + KeepAlive: 30 * time.Second, + FallbackDelay: -1} + return dialer.DialContext(ctx, "tcp4", addr) + }, + } url = "ws" + strings.TrimPrefix(url, "http") c, r, err := dialer.Dial(url+"/ws", nil) require.NoError(t, err) @@ -3475,7 +3485,18 @@ func doRPCCallOverWS(rpcCall string, url string, t *testing.T) []byte { } func doRPCCallOverHTTP(rpcCall string, url string, t *testing.T) []byte { - cl := http.Client{Timeout: time.Second} + cl := http.Client{Timeout: 3 * time.Second, Transport: &http.Transport{ + MaxIdleConns: 50, + MaxConnsPerHost: 50, + MaxIdleConnsPerHost: 50, + IdleConnTimeout: 3 * time.Second, + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + dialer := net.Dialer{Timeout: 3 * time.Second, + KeepAlive: 30 * time.Second, + FallbackDelay: -1} + return dialer.DialContext(ctx, "tcp4", addr) + }, + }} resp, err := cl.Post(url, "application/json", strings.NewReader(rpcCall)) require.NoErrorf(t, err, "could not make a POST request") body, err := gio.ReadAll(resp.Body) @@ -4160,7 +4181,12 @@ func TestErrorResponseContentType(t *testing.T) { req = `{"jsonrpc":"2.0", "method":"unknown","params":[]}` ) - cl := http.Client{Timeout: time.Second} + cl := http.Client{Timeout: 5 * time.Second, Transport: &http.Transport{MaxIdleConns: 50, MaxIdleConnsPerHost: 50, IdleConnTimeout: 5 * time.Second, + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + dialer := net.Dialer{Timeout: 5 * time.Second} + return dialer.DialContext(ctx, "tcp4", addr) + }, + }} resp, err := cl.Post(httpSrv.URL, "application/json", strings.NewReader(req)) require.NoErrorf(t, err, "could not make a POST request") resp.Body.Close() diff --git a/pkg/services/rpcsrv/subscription_test.go b/pkg/services/rpcsrv/subscription_test.go index 316699c23..eed19a3ad 100644 --- a/pkg/services/rpcsrv/subscription_test.go +++ b/pkg/services/rpcsrv/subscription_test.go @@ -59,7 +59,7 @@ func getNotification(t *testing.T, respCh <-chan []byte) *neorpc.Notification { func initCleanServerAndWSClient(t *testing.T) (*core.Blockchain, *Server, *websocket.Conn, chan []byte, *atomic.Bool) { chain, rpcSrv, httpSrv := initClearServerWithInMemoryChain(t) - dialer := websocket.Dialer{HandshakeTimeout: time.Second} + dialer := websocket.Dialer{HandshakeTimeout: 5 * time.Second} url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws" ws, r, err := dialer.Dial(url, nil) require.NoError(t, err) @@ -586,7 +586,7 @@ func TestWSClientsLimit(t *testing.T) { cfg.ApplicationConfiguration.RPC.MaxWebSocketClients = limit }) - dialer := websocket.Dialer{HandshakeTimeout: time.Second} + dialer := websocket.Dialer{HandshakeTimeout: 5 * time.Second} url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws" wss := make([]*websocket.Conn, effectiveClients)