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 <ekt@morphbits.io>
This commit is contained in:
Ekaterina Pavlova 2024-03-20 13:36:53 +03:00
parent b028c772f2
commit f409fc36e2
2 changed files with 31 additions and 5 deletions

View file

@ -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()

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) {
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)