2018-09-21 09:17:43 -06:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package jsonrpc2_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-04-13 10:32:10 -06:00
|
|
|
"errors"
|
2018-09-21 09:17:43 -06:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
2020-04-13 10:32:10 -06:00
|
|
|
"sync"
|
2018-09-21 09:17:43 -06:00
|
|
|
"testing"
|
|
|
|
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event/export/eventtest"
|
2018-09-21 09:17:43 -06:00
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var logRPC = flag.Bool("logrpc", false, "Enable jsonrpc2 communication logging")
|
|
|
|
|
|
|
|
type callTest struct {
|
|
|
|
method string
|
|
|
|
params interface{}
|
|
|
|
expect interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var callTests = []callTest{
|
|
|
|
{"no_args", nil, true},
|
|
|
|
{"one_string", "fish", "got:fish"},
|
|
|
|
{"one_number", 10, "got:10"},
|
|
|
|
{"join", []string{"a", "b", "c"}, "a/b/c"},
|
|
|
|
//TODO: expand the test cases
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *callTest) newResults() interface{} {
|
|
|
|
switch e := test.expect.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
var r []interface{}
|
|
|
|
for _, v := range e {
|
|
|
|
r = append(r, reflect.New(reflect.TypeOf(v)).Interface())
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return reflect.New(reflect.TypeOf(test.expect)).Interface()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *callTest) verifyResults(t *testing.T, results interface{}) {
|
|
|
|
if results == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
val := reflect.Indirect(reflect.ValueOf(results)).Interface()
|
|
|
|
if !reflect.DeepEqual(val, test.expect) {
|
|
|
|
t.Errorf("%v:Results are incorrect, got %+v expect %+v", test.method, val, test.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:32:10 -06:00
|
|
|
func TestCall(t *testing.T) {
|
|
|
|
ctx := eventtest.NewContext(context.Background(), t)
|
|
|
|
for _, headers := range []bool{false, true} {
|
|
|
|
name := "Plain"
|
|
|
|
if headers {
|
|
|
|
name = "Headers"
|
2018-09-21 09:17:43 -06:00
|
|
|
}
|
2020-04-13 10:32:10 -06:00
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
ctx := eventtest.NewContext(ctx, t)
|
|
|
|
a, b, done := prepare(ctx, t, headers)
|
|
|
|
defer done()
|
|
|
|
for _, test := range callTests {
|
|
|
|
t.Run(test.method, func(t *testing.T) {
|
|
|
|
ctx := eventtest.NewContext(ctx, t)
|
|
|
|
results := test.newResults()
|
|
|
|
if _, err := a.Call(ctx, test.method, test.params, results); err != nil {
|
|
|
|
t.Fatalf("%v:Call failed: %v", test.method, err)
|
|
|
|
}
|
|
|
|
test.verifyResults(t, results)
|
|
|
|
if _, err := b.Call(ctx, test.method, test.params, results); err != nil {
|
|
|
|
t.Fatalf("%v:Call failed: %v", test.method, err)
|
|
|
|
}
|
|
|
|
test.verifyResults(t, results)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2018-09-21 09:17:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:32:10 -06:00
|
|
|
func prepare(ctx context.Context, t *testing.T, withHeaders bool) (*jsonrpc2.Conn, *jsonrpc2.Conn, func()) {
|
|
|
|
// make a wait group that can be used to wait for the system to shut down
|
|
|
|
wg := &sync.WaitGroup{}
|
2019-03-28 19:06:01 -06:00
|
|
|
aR, bW := io.Pipe()
|
|
|
|
bR, aW := io.Pipe()
|
2020-04-13 10:32:10 -06:00
|
|
|
a := run(ctx, t, withHeaders, aR, aW, wg)
|
|
|
|
b := run(ctx, t, withHeaders, bR, bW, wg)
|
|
|
|
return a, b, func() {
|
|
|
|
// we close the main writer, this should cascade through the server and
|
|
|
|
// cause normal shutdown of the entire chain
|
|
|
|
aW.Close()
|
|
|
|
// this should then wait for that entire cascade,
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2018-09-21 09:17:43 -06:00
|
|
|
}
|
|
|
|
|
2020-04-13 10:32:10 -06:00
|
|
|
func run(ctx context.Context, t *testing.T, withHeaders bool, r io.ReadCloser, w io.WriteCloser, wg *sync.WaitGroup) *jsonrpc2.Conn {
|
2019-03-28 19:06:01 -06:00
|
|
|
var stream jsonrpc2.Stream
|
|
|
|
if withHeaders {
|
|
|
|
stream = jsonrpc2.NewHeaderStream(r, w)
|
|
|
|
} else {
|
2020-04-29 07:17:00 -06:00
|
|
|
stream = jsonrpc2.NewRawStream(r, w)
|
2019-03-28 19:06:01 -06:00
|
|
|
}
|
|
|
|
conn := jsonrpc2.NewConn(stream)
|
2020-04-13 10:32:10 -06:00
|
|
|
wg.Add(1)
|
2019-03-28 19:06:01 -06:00
|
|
|
go func() {
|
|
|
|
defer func() {
|
2020-04-13 10:32:10 -06:00
|
|
|
// this will happen when Run returns, which means at least one of the
|
|
|
|
// streams has already been closed
|
|
|
|
// we close both streams anyway, this may be redundant but is safe
|
2019-03-28 19:06:01 -06:00
|
|
|
r.Close()
|
|
|
|
w.Close()
|
2020-04-13 10:32:10 -06:00
|
|
|
// and then signal that this connection is done
|
|
|
|
wg.Done()
|
2019-03-28 19:06:01 -06:00
|
|
|
}()
|
2020-04-13 10:32:10 -06:00
|
|
|
err := conn.Run(ctx, testHandler(*logRPC))
|
2020-04-27 10:22:50 -06:00
|
|
|
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrClosedPipe) {
|
2020-02-02 07:34:18 -07:00
|
|
|
t.Errorf("Stream failed: %v", err)
|
2019-03-28 19:06:01 -06:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return conn
|
2018-09-21 09:17:43 -06:00
|
|
|
}
|
|
|
|
|
2020-03-30 15:09:42 -06:00
|
|
|
func testHandler(log bool) jsonrpc2.Handler {
|
2020-04-12 20:47:10 -06:00
|
|
|
return func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
|
|
|
|
switch req.Method() {
|
2020-03-30 15:09:42 -06:00
|
|
|
case "no_args":
|
2020-04-12 20:47:10 -06:00
|
|
|
if len(req.Params()) > 0 {
|
2020-04-09 21:54:23 -06:00
|
|
|
return reply(ctx, nil, fmt.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams))
|
2020-03-30 15:09:42 -06:00
|
|
|
}
|
2020-04-09 21:54:23 -06:00
|
|
|
return reply(ctx, true, nil)
|
2020-03-30 15:09:42 -06:00
|
|
|
case "one_string":
|
|
|
|
var v string
|
2020-04-12 20:47:10 -06:00
|
|
|
if err := json.Unmarshal(req.Params(), &v); err != nil {
|
2020-04-09 21:54:23 -06:00
|
|
|
return reply(ctx, nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err))
|
2020-03-30 15:09:42 -06:00
|
|
|
}
|
2020-04-09 21:54:23 -06:00
|
|
|
return reply(ctx, "got:"+v, nil)
|
2020-03-30 15:09:42 -06:00
|
|
|
case "one_number":
|
|
|
|
var v int
|
2020-04-12 20:47:10 -06:00
|
|
|
if err := json.Unmarshal(req.Params(), &v); err != nil {
|
2020-04-09 21:54:23 -06:00
|
|
|
return reply(ctx, nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err))
|
2020-03-30 15:09:42 -06:00
|
|
|
}
|
2020-04-09 21:54:23 -06:00
|
|
|
return reply(ctx, fmt.Sprintf("got:%d", v), nil)
|
2020-03-30 15:09:42 -06:00
|
|
|
case "join":
|
|
|
|
var v []string
|
2020-04-12 20:47:10 -06:00
|
|
|
if err := json.Unmarshal(req.Params(), &v); err != nil {
|
2020-04-09 21:54:23 -06:00
|
|
|
return reply(ctx, nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err))
|
2020-03-30 15:09:42 -06:00
|
|
|
}
|
2020-04-09 21:54:23 -06:00
|
|
|
return reply(ctx, path.Join(v...), nil)
|
2020-03-30 15:09:42 -06:00
|
|
|
default:
|
2020-04-09 21:54:23 -06:00
|
|
|
return jsonrpc2.MethodNotFound(ctx, reply, req)
|
2019-07-11 22:43:12 -06:00
|
|
|
}
|
2019-07-11 10:57:35 -06:00
|
|
|
}
|
2018-09-21 09:17:43 -06:00
|
|
|
}
|