mirror of
https://github.com/golang/go
synced 2024-10-01 18:18:32 -06:00
0fd2d649e6
Add a new Forwarder type to the lsprpc package, which implements the jsonrpc2.StreamServer interface. This will be used to establish some parity in the implementation of shared and singleton gopls servers. Much more testing is needed, as is handling for the many edge cases around forwarding the LSP, but since this is functionally equivalent to TCP forwarding (and the -remote flag was already broken), I went ahead and used the Forwarder to replace the forward method in the serve command. This means that we can now use the combination of -listen and -remote to chain together gopls servers... not that there's any reason to do this. Also, wrap the new regression tests with a focus on expressiveness when testing the happy path, as well as parameterizing them so that they can be run against different client/server execution environments. This started to be sizable enough to warrant moving them to a separate regtest package. The lsprpc package tests will instead focus on unit testing the client-server binding logic. Updates golang/go#36879 Updates golang/go#34111 Change-Id: Ib98131a58aabc69299845d2ecefceccfc1199574 Reviewed-on: https://go-review.googlesource.com/c/tools/+/218698 Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
136 lines
3.1 KiB
Go
136 lines
3.1 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 lsprpc
|
|
|
|
import (
|
|
"context"
|
|
"regexp"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2/servertest"
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/telemetry/log"
|
|
)
|
|
|
|
type fakeClient struct {
|
|
protocol.Client
|
|
|
|
logs chan string
|
|
}
|
|
|
|
func (c fakeClient) LogMessage(ctx context.Context, params *protocol.LogMessageParams) error {
|
|
c.logs <- params.Message
|
|
return nil
|
|
}
|
|
|
|
type pingServer struct{ protocol.Server }
|
|
|
|
func (s pingServer) DidOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error {
|
|
log.Print(ctx, "ping")
|
|
return nil
|
|
}
|
|
|
|
func TestClientLogging(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
server := pingServer{}
|
|
client := fakeClient{logs: make(chan string, 10)}
|
|
|
|
ss := &StreamServer{
|
|
accept: func(c protocol.Client) protocol.Server {
|
|
return server
|
|
},
|
|
}
|
|
ts := servertest.NewServer(ctx, ss)
|
|
cc := ts.Connect(ctx)
|
|
cc.AddHandler(protocol.ClientHandler(client))
|
|
|
|
protocol.ServerDispatcher(cc).DidOpen(ctx, &protocol.DidOpenTextDocumentParams{})
|
|
|
|
select {
|
|
case got := <-client.logs:
|
|
want := "ping"
|
|
matched, err := regexp.MatchString(want, got)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !matched {
|
|
t.Errorf("got log %q, want a log containing %q", got, want)
|
|
}
|
|
case <-time.After(1000 * time.Second):
|
|
t.Error("timeout waiting for client log")
|
|
}
|
|
}
|
|
|
|
type waitableServer struct {
|
|
protocol.Server
|
|
|
|
started chan struct{}
|
|
// finished records whether the request ended with a cancellation or not
|
|
// (true means the request was cancelled).
|
|
finished chan bool
|
|
}
|
|
|
|
func (s waitableServer) CodeLens(ctx context.Context, params *protocol.CodeLensParams) ([]protocol.CodeLens, error) {
|
|
s.started <- struct{}{}
|
|
cancelled := false
|
|
defer func() {
|
|
s.finished <- cancelled
|
|
}()
|
|
select {
|
|
case <-ctx.Done():
|
|
cancelled = true
|
|
return nil, ctx.Err()
|
|
case <-time.After(1 * time.Second):
|
|
cancelled = false
|
|
}
|
|
return []protocol.CodeLens{}, nil
|
|
}
|
|
|
|
func TestRequestCancellation(t *testing.T) {
|
|
server := waitableServer{
|
|
started: make(chan struct{}),
|
|
finished: make(chan bool),
|
|
}
|
|
ss := &StreamServer{
|
|
accept: func(c protocol.Client) protocol.Server {
|
|
return server
|
|
},
|
|
}
|
|
ctx := context.Background()
|
|
tsDirect := servertest.NewServer(ctx, ss)
|
|
|
|
forwarder := NewForwarder(tsDirect.Addr, false)
|
|
tsForwarded := servertest.NewServer(ctx, forwarder)
|
|
|
|
tests := []struct {
|
|
serverType string
|
|
ts *servertest.Server
|
|
}{
|
|
{"direct", tsDirect},
|
|
{"forwarder", tsForwarded},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.serverType, func(t *testing.T) {
|
|
cc := test.ts.Connect(ctx)
|
|
cc.AddHandler(protocol.Canceller{})
|
|
lensCtx, cancelLens := context.WithCancel(context.Background())
|
|
go func() {
|
|
protocol.ServerDispatcher(cc).CodeLens(lensCtx, &protocol.CodeLensParams{})
|
|
}()
|
|
<-server.started
|
|
cancelLens()
|
|
if got, want := <-server.finished, true; got != want {
|
|
t.Errorf("CodeLens was cancelled: %t, want %t", got, want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TODO: add a test for telemetry.
|