|
|
@ -19,6 +19,7 @@ package rpc |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"context" |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
|
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"math/rand" |
|
|
|
"math/rand" |
|
|
|
"net" |
|
|
|
"net" |
|
|
@ -144,6 +145,53 @@ func TestClientBatchRequest(t *testing.T) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestClientBatchRequest_len(t *testing.T) { |
|
|
|
|
|
|
|
b, err := json.Marshal([]jsonrpcMessage{ |
|
|
|
|
|
|
|
{Version: "2.0", ID: json.RawMessage("1"), Method: "foo", Result: json.RawMessage(`"0x1"`)}, |
|
|
|
|
|
|
|
{Version: "2.0", ID: json.RawMessage("2"), Method: "bar", Result: json.RawMessage(`"0x2"`)}, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal("failed to encode jsonrpc message:", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
_, err := rw.Write(b) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Error("failed to write response:", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
t.Cleanup(s.Close) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client, err := Dial(s.URL) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal("failed to dial test server:", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
defer client.Close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("too-few", func(t *testing.T) { |
|
|
|
|
|
|
|
batch := []BatchElem{ |
|
|
|
|
|
|
|
{Method: "foo"}, |
|
|
|
|
|
|
|
{Method: "bar"}, |
|
|
|
|
|
|
|
{Method: "baz"}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), time.Second) |
|
|
|
|
|
|
|
defer cancelFn() |
|
|
|
|
|
|
|
if err := client.BatchCallContext(ctx, batch); !errors.Is(err, ErrBadResult) { |
|
|
|
|
|
|
|
t.Errorf("expected %q but got: %v", ErrBadResult, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("too-many", func(t *testing.T) { |
|
|
|
|
|
|
|
batch := []BatchElem{ |
|
|
|
|
|
|
|
{Method: "foo"}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), time.Second) |
|
|
|
|
|
|
|
defer cancelFn() |
|
|
|
|
|
|
|
if err := client.BatchCallContext(ctx, batch); !errors.Is(err, ErrBadResult) { |
|
|
|
|
|
|
|
t.Errorf("expected %q but got: %v", ErrBadResult, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestClientNotify(t *testing.T) { |
|
|
|
func TestClientNotify(t *testing.T) { |
|
|
|
server := newTestServer() |
|
|
|
server := newTestServer() |
|
|
|
defer server.Stop() |
|
|
|
defer server.Stop() |
|
|
|