Skip to content

Commit

Permalink
rpc: run tests in parallel (#30384)
Browse files Browse the repository at this point in the history
Continuation of #30381
  • Loading branch information
estensen authored and holiman committed Nov 19, 2024
1 parent 71b32b4 commit f3c19b1
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
38 changes: 38 additions & 0 deletions rpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
)

func TestClientRequest(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()
client := DialInProc(server)
Expand All @@ -53,6 +55,8 @@ func TestClientRequest(t *testing.T) {
}

func TestClientResponseType(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()
client := DialInProc(server)
Expand All @@ -71,6 +75,8 @@ func TestClientResponseType(t *testing.T) {

// This test checks calling a method that returns 'null'.
func TestClientNullResponse(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()

Expand All @@ -91,6 +97,8 @@ func TestClientNullResponse(t *testing.T) {

// This test checks that server-returned errors with code and data come out of Client.Call.
func TestClientErrorData(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()
client := DialInProc(server)
Expand Down Expand Up @@ -121,6 +129,8 @@ func TestClientErrorData(t *testing.T) {
}

func TestClientBatchRequest(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()
client := DialInProc(server)
Expand Down Expand Up @@ -172,6 +182,8 @@ func TestClientBatchRequest(t *testing.T) {
// This checks that, for HTTP connections, the length of batch responses is validated to
// match the request exactly.
func TestClientBatchRequest_len(t *testing.T) {
t.Parallel()

b, err := json.Marshal([]jsonrpcMessage{
{Version: "2.0", ID: json.RawMessage("1"), Result: json.RawMessage(`"0x1"`)},
{Version: "2.0", ID: json.RawMessage("2"), Result: json.RawMessage(`"0x2"`)},
Expand All @@ -188,6 +200,8 @@ func TestClientBatchRequest_len(t *testing.T) {
t.Cleanup(s.Close)

t.Run("too-few", func(t *testing.T) {
t.Parallel()

client, err := Dial(s.URL)
if err != nil {
t.Fatal("failed to dial test server:", err)
Expand Down Expand Up @@ -218,6 +232,8 @@ func TestClientBatchRequest_len(t *testing.T) {
})

t.Run("too-many", func(t *testing.T) {
t.Parallel()

client, err := Dial(s.URL)
if err != nil {
t.Fatal("failed to dial test server:", err)
Expand Down Expand Up @@ -249,6 +265,8 @@ func TestClientBatchRequest_len(t *testing.T) {
// This checks that the client can handle the case where the server doesn't
// respond to all requests in a batch.
func TestClientBatchRequestLimit(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()
server.SetBatchLimits(2, 100000)
Expand Down Expand Up @@ -285,6 +303,8 @@ func TestClientBatchRequestLimit(t *testing.T) {
}

func TestClientNotify(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()
client := DialInProc(server)
Expand Down Expand Up @@ -392,6 +412,8 @@ func testClientCancel(transport string, t *testing.T) {
}

func TestClientSubscribeInvalidArg(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()
client := DialInProc(server)
Expand Down Expand Up @@ -422,6 +444,8 @@ func TestClientSubscribeInvalidArg(t *testing.T) {
}

func TestClientSubscribe(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()
client := DialInProc(server)
Expand Down Expand Up @@ -454,6 +478,8 @@ func TestClientSubscribe(t *testing.T) {

// In this test, the connection drops while Subscribe is waiting for a response.
func TestClientSubscribeClose(t *testing.T) {
t.Parallel()

server := newTestServer()
service := &notificationTestService{
gotHangSubscriptionReq: make(chan struct{}),
Expand Down Expand Up @@ -498,6 +524,8 @@ func TestClientSubscribeClose(t *testing.T) {
// This test reproduces https://github.com/ethereum/go-ethereum/issues/17837 where the
// client hangs during shutdown when Unsubscribe races with Client.Close.
func TestClientCloseUnsubscribeRace(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()

Expand Down Expand Up @@ -540,6 +568,8 @@ func (b *unsubscribeBlocker) readBatch() ([]*jsonrpcMessage, bool, error) {
// not respond.
// It reproducers the issue https://github.com/ethereum/go-ethereum/issues/30156
func TestUnsubscribeTimeout(t *testing.T) {
t.Parallel()

srv := NewServer()
srv.RegisterName("nftest", new(notificationTestService))

Expand Down Expand Up @@ -674,6 +704,8 @@ func TestClientSubscriptionChannelClose(t *testing.T) {
// This test checks that Client doesn't lock up when a single subscriber
// doesn't read subscription events.
func TestClientNotificationStorm(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()

Expand Down Expand Up @@ -726,6 +758,8 @@ func TestClientNotificationStorm(t *testing.T) {
}

func TestClientSetHeader(t *testing.T) {
t.Parallel()

var gotHeader bool
srv := newTestServer()
httpsrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -762,6 +796,8 @@ func TestClientSetHeader(t *testing.T) {
}

func TestClientHTTP(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()

Expand Down Expand Up @@ -804,6 +840,8 @@ func TestClientHTTP(t *testing.T) {
}

func TestClientReconnect(t *testing.T) {
t.Parallel()

startServer := func(addr string) (*Server, net.Listener) {
srv := newTestServer()
l, err := net.Listen("tcp", addr)
Expand Down
20 changes: 20 additions & 0 deletions rpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,34 @@ func confirmRequestValidationCode(t *testing.T, method, contentType, body string
}

func TestHTTPErrorResponseWithDelete(t *testing.T) {
t.Parallel()

confirmRequestValidationCode(t, http.MethodDelete, contentType, "", http.StatusMethodNotAllowed)
}

func TestHTTPErrorResponseWithPut(t *testing.T) {
t.Parallel()

confirmRequestValidationCode(t, http.MethodPut, contentType, "", http.StatusMethodNotAllowed)
}

func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) {
t.Parallel()

body := make([]rune, defaultBodyLimit+1)
confirmRequestValidationCode(t,
http.MethodPost, contentType, string(body), http.StatusRequestEntityTooLarge)
}

func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) {
t.Parallel()

confirmRequestValidationCode(t, http.MethodPost, "", "", http.StatusUnsupportedMediaType)
}

func TestHTTPErrorResponseWithValidRequest(t *testing.T) {
t.Parallel()

confirmRequestValidationCode(t, http.MethodPost, contentType, "", 0)
}

Expand All @@ -101,11 +111,15 @@ func confirmHTTPRequestYieldsStatusCode(t *testing.T, method, contentType, body
}

func TestHTTPResponseWithEmptyGet(t *testing.T) {
t.Parallel()

confirmHTTPRequestYieldsStatusCode(t, http.MethodGet, "", "", http.StatusOK)
}

// This checks that maxRequestContentLength is not applied to the response of a request.
func TestHTTPRespBodyUnlimited(t *testing.T) {
t.Parallel()

const respLength = defaultBodyLimit * 3

s := NewServer()
Expand All @@ -132,6 +146,8 @@ func TestHTTPRespBodyUnlimited(t *testing.T) {
// Tests that an HTTP error results in an HTTPError instance
// being returned with the expected attributes.
func TestHTTPErrorResponse(t *testing.T) {
t.Parallel()

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "error has occurred!", http.StatusTeapot)
}))
Expand Down Expand Up @@ -169,6 +185,8 @@ func TestHTTPErrorResponse(t *testing.T) {
}

func TestHTTPPeerInfo(t *testing.T) {
t.Parallel()

s := newTestServer()
defer s.Stop()
ts := httptest.NewServer(s)
Expand Down Expand Up @@ -205,6 +223,8 @@ func TestHTTPPeerInfo(t *testing.T) {
}

func TestNewContextWithHeaders(t *testing.T) {
t.Parallel()

expectedHeaders := 0
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
for i := 0; i < expectedHeaders; i++ {
Expand Down
10 changes: 10 additions & 0 deletions rpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
)

func TestServerRegisterName(t *testing.T) {
t.Parallel()

server := NewServer()
service := new(testService)

Expand All @@ -53,6 +55,8 @@ func TestServerRegisterName(t *testing.T) {
}

func TestServer(t *testing.T) {
t.Parallel()

files, err := os.ReadDir("testdata")
if err != nil {
t.Fatal("where'd my testdata go?")
Expand All @@ -64,6 +68,8 @@ func TestServer(t *testing.T) {
path := filepath.Join("testdata", f.Name())
name := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
t.Run(name, func(t *testing.T) {
t.Parallel()

runTestScript(t, path)
})
}
Expand Down Expand Up @@ -116,6 +122,8 @@ func runTestScript(t *testing.T, file string) {
// This test checks that responses are delivered for very short-lived connections that
// only carry a single request.
func TestServerShortLivedConn(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()

Expand Down Expand Up @@ -156,6 +164,8 @@ func TestServerShortLivedConn(t *testing.T) {
}

func TestServerBatchResponseSizeLimit(t *testing.T) {
t.Parallel()

server := newTestServer()
defer server.Stop()
server.SetBatchLimits(100, 60)
Expand Down
8 changes: 8 additions & 0 deletions rpc/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
)

func TestNewID(t *testing.T) {
t.Parallel()

hexchars := "0123456789ABCDEFabcdef"
for i := 0; i < 100; i++ {
id := string(NewID())
Expand All @@ -54,6 +56,8 @@ func TestNewID(t *testing.T) {
}

func TestSubscriptions(t *testing.T) {
t.Parallel()

var (
namespaces = []string{"eth", "bzz"}
service = &notificationTestService{}
Expand Down Expand Up @@ -132,6 +136,8 @@ func TestSubscriptions(t *testing.T) {

// This test checks that unsubscribing works.
func TestServerUnsubscribe(t *testing.T) {
t.Parallel()

p1, p2 := net.Pipe()
defer p2.Close()

Expand Down Expand Up @@ -260,6 +266,8 @@ func BenchmarkNotify(b *testing.B) {
}

func TestNotify(t *testing.T) {
t.Parallel()

out := new(bytes.Buffer)
id := ID("test")
notifier := &Notifier{
Expand Down
10 changes: 10 additions & 0 deletions rpc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
)

func TestBlockNumberJSONUnmarshal(t *testing.T) {
t.Parallel()

tests := []struct {
input string
mustFail bool
Expand Down Expand Up @@ -70,6 +72,8 @@ func TestBlockNumberJSONUnmarshal(t *testing.T) {
}

func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) {
t.Parallel()

tests := []struct {
input string
mustFail bool
Expand Down Expand Up @@ -131,6 +135,8 @@ func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) {
}

func TestBlockNumberOrHash_WithNumber_MarshalAndUnmarshal(t *testing.T) {
t.Parallel()

tests := []struct {
name string
number int64
Expand All @@ -144,6 +150,8 @@ func TestBlockNumberOrHash_WithNumber_MarshalAndUnmarshal(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

bnh := BlockNumberOrHashWithNumber(BlockNumber(test.number))
marshalled, err := json.Marshal(bnh)
if err != nil {
Expand All @@ -162,6 +170,8 @@ func TestBlockNumberOrHash_WithNumber_MarshalAndUnmarshal(t *testing.T) {
}

func TestBlockNumberOrHash_StringAndUnmarshal(t *testing.T) {
t.Parallel()

tests := []BlockNumberOrHash{
BlockNumberOrHashWithNumber(math.MaxInt64),
BlockNumberOrHashWithNumber(PendingBlockNumber),
Expand Down
4 changes: 4 additions & 0 deletions rpc/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ func TestWebsocketLargeRead(t *testing.T) {
}

func TestWebsocketPeerInfo(t *testing.T) {
t.Parallel()

var (
s = newTestServer()
ts = httptest.NewServer(s.WebsocketHandler([]string{"origin.example.com"}))
Expand Down Expand Up @@ -259,6 +261,8 @@ func TestClientWebsocketPing(t *testing.T) {

// This checks that the websocket transport can deal with large messages.
func TestClientWebsocketLargeMessage(t *testing.T) {
t.Parallel()

var (
srv = NewServer()
httpsrv = httptest.NewServer(srv.WebsocketHandler(nil))
Expand Down

0 comments on commit f3c19b1

Please sign in to comment.