1
0
mirror of https://github.com/golang/go synced 2024-11-05 18:26:10 -07:00
go/internal/jsonrpc2/servertest/servertest_test.go
Ian Cottrell ae9902aceb internal/lsp: remove the CallCanceller
This required changing the jsonrpc.Conn.Call signature to also return the
request ID so it can be cancelled.
The protocol package now declares the Call function which wrapps up
Conn.Call and then sends a cancel message if the context was
cancelled during the call.
There is a small chance that a context can be cancelled on a
request that has already completed, but it is safe to do so.

Change-Id: Ic8040c193e1dd4ef376ad21194b1d0ea82145976
Reviewed-on: https://go-review.googlesource.com/c/tools/+/227558
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2020-04-10 13:26:12 +00:00

54 lines
1.2 KiB
Go

// Copyright 2020 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 servertest
import (
"context"
"testing"
"time"
"golang.org/x/tools/internal/jsonrpc2"
)
type msg struct {
Msg string
}
func fakeHandler(ctx context.Context, r *jsonrpc2.Request) error {
return r.Reply(ctx, &msg{"pong"}, nil)
}
func TestTestServer(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
server := jsonrpc2.HandlerServer(fakeHandler)
tcpTS := NewTCPServer(ctx, server)
defer tcpTS.Close()
pipeTS := NewPipeServer(ctx, server)
defer pipeTS.Close()
tests := []struct {
name string
connector Connector
}{
{"tcp", tcpTS},
{"pipe", pipeTS},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
conn := test.connector.Connect(ctx)
go conn.Run(ctx, jsonrpc2.MethodNotFound)
var got msg
if _, err := conn.Call(ctx, "ping", &msg{"ping"}, &got); err != nil {
t.Fatal(err)
}
if want := "pong"; got.Msg != want {
t.Errorf("conn.Call(...): returned %q, want %q", got, want)
}
})
}
}