Ejemplo n.º 1
0
func BenchmarkJSONRPC2_pipe(b *testing.B) {
	cli, srv := net.Pipe()
	go jsonrpc2.ServeConn(srv)
	client := jsonrpc2.NewClient(cli)
	defer client.Close()
	benchmarkRPC(b, client)
}
Ejemplo n.º 2
0
// - for each of these servers:
//   * TCP server without context
//   * TCP server with context
//   * HTTP server with context
// - call these methods:
//   * Sum()
//   * Name()
//   * NameCtx()
//   * TODO batch call all
func TestContext(t *testing.T) {
	// Server provide a TCP transport without context.
	serverTCPNoCtx, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	defer serverTCPNoCtx.Close()
	go func() {
		for {
			conn, err := serverTCPNoCtx.Accept()
			if err != nil {
				return
			}
			go jsonrpc2.ServeConn(conn)
		}
	}()

	// Server provide a TCP transport with context.
	serverTCP, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	defer serverTCP.Close()
	go func() {
		for {
			conn, err := serverTCP.Accept()
			if err != nil {
				return
			}
			ctx := context.WithValue(context.Background(), remoteAddrContextKey, conn.RemoteAddr())
			go jsonrpc2.ServeConnContext(ctx, conn)
		}
	}()

	// Server provide a HTTP transport with context.
	http.Handle("/", jsonrpc2.HTTPHandler(nil))
	serverHTTP, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	defer serverHTTP.Close()
	go http.Serve(serverHTTP, nil)

	// Client use TCP transport without context.
	clientTCPNoCtx, err := jsonrpc2.Dial("tcp", serverTCPNoCtx.Addr().String())
	if err != nil {
		t.Fatal(err)
	}
	defer clientTCPNoCtx.Close()

	// Client use TCP transport with context.
	clientTCP, err := jsonrpc2.Dial("tcp", serverTCP.Addr().String())
	if err != nil {
		t.Fatal(err)
	}
	defer clientTCP.Close()

	// Client use HTTP transport with context.
	clientHTTP := jsonrpc2.NewHTTPClient("http://" + serverHTTP.Addr().String() + "/")
	defer clientHTTP.Close()

	cases := []struct {
		client     *jsonrpc2.Client
		clientName string
		method     string
		arg        interface{}
		want       interface{}
	}{
		{clientTCPNoCtx, "clientTCPNoCtx",
			"CtxSvc.Sum", [2]int{3, 5}, 8.0},
		{clientTCP, "clientTCP",
			"CtxSvc.Sum", [2]int{3, 5}, 8.0},
		{clientHTTP, "clientHTTP",
			"CtxSvc.Sum", [2]int{3, 5}, 8.0},
		{clientTCPNoCtx, "clientTCPNoCtx",
			"CtxSvc.Name", NameArg{"First", "Last"}, map[string]interface{}{
				"Name": "First Last",
			}},
		{clientTCP, "clientTCP",
			"CtxSvc.Name", NameArg{"First", "Last"}, map[string]interface{}{
				"Name": "First Last",
			}},
		{clientHTTP, "clientHTTP",
			"CtxSvc.Name", NameArg{"First", "Last"}, map[string]interface{}{
				"Name": "First Last",
			}},
		{clientTCPNoCtx, "clientTCPNoCtx",
			"CtxSvc.NameCtx", NameArg{"First", "Last"}, map[string]interface{}{
				"Name":           "First Last",
				"TCPRemoteAddr":  "",
				"HTTPRemoteAddr": "",
			}},
		{clientTCP, "clientTCP",
			"CtxSvc.NameCtx", NameArg{"First", "Last"}, map[string]interface{}{
				"Name":           "First Last",
				"TCPRemoteAddr":  "127.0.0.1",
				"HTTPRemoteAddr": "",
			}},
		{clientHTTP, "clientHTTP",
			"CtxSvc.NameCtx", NameArg{"First", "Last"}, map[string]interface{}{
				"Name":           "First Last",
				"TCPRemoteAddr":  "",
				"HTTPRemoteAddr": "127.0.0.1",
			}},
	}
	for _, v := range cases {
		var res interface{}
		err := v.client.Call(v.method, v.arg, &res)
		if err != nil {
			t.Errorf("%s.Call(%q) = %v", v.clientName, v.method, err)
		}
		if !reflect.DeepEqual(v.want, res) {
			t.Errorf("%s.Call(%q):\n\n\texp: %#v\n\n\tgot: %#v\n\n", v.clientName, v.method, v.want, res)
		}
	}
}