Merge pull request #3368 from nspcc-dev/test-dial

rpcsrv: fix TestRPC failing
This commit is contained in:
Anna Shaleva 2024-03-22 16:53:09 +03:00 committed by GitHub
commit d5a5e4c125
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 5 deletions

View file

@ -2,6 +2,7 @@ package rpcsrv
import ( import (
"bytes" "bytes"
"context"
"encoding/base64" "encoding/base64"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
@ -9,6 +10,7 @@ import (
gio "io" gio "io"
"math" "math"
"math/big" "math/big"
"net"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"reflect" "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 { 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") url = "ws" + strings.TrimPrefix(url, "http")
c, r, err := dialer.Dial(url+"/ws", nil) c, r, err := dialer.Dial(url+"/ws", nil)
require.NoError(t, err) 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 { 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)) resp, err := cl.Post(url, "application/json", strings.NewReader(rpcCall))
require.NoErrorf(t, err, "could not make a POST request") require.NoErrorf(t, err, "could not make a POST request")
body, err := gio.ReadAll(resp.Body) body, err := gio.ReadAll(resp.Body)
@ -4160,7 +4181,12 @@ func TestErrorResponseContentType(t *testing.T) {
req = `{"jsonrpc":"2.0", "method":"unknown","params":[]}` 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)) resp, err := cl.Post(httpSrv.URL, "application/json", strings.NewReader(req))
require.NoErrorf(t, err, "could not make a POST request") require.NoErrorf(t, err, "could not make a POST request")
resp.Body.Close() resp.Body.Close()

View file

@ -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) { func initCleanServerAndWSClient(t *testing.T) (*core.Blockchain, *Server, *websocket.Conn, chan []byte, *atomic.Bool) {
chain, rpcSrv, httpSrv := initClearServerWithInMemoryChain(t) 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" url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws"
ws, r, err := dialer.Dial(url, nil) ws, r, err := dialer.Dial(url, nil)
require.NoError(t, err) require.NoError(t, err)
@ -586,7 +586,7 @@ func TestWSClientsLimit(t *testing.T) {
cfg.ApplicationConfiguration.RPC.MaxWebSocketClients = limit 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" url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws"
wss := make([]*websocket.Conn, effectiveClients) wss := make([]*websocket.Conn, effectiveClients)