mirror of
https://github.com/golang/go
synced 2024-11-18 22:34:45 -07:00
9ffc0ab4ef
When running gopls against an automatically started remote instance, we want the lifecycle of the remote to be detached from that of its clients, so that it doesn't shut down while clients are still connected. On the other hand, a gopls process can consume significant resources, so we don't want it to remain when there are no more connected clients. The jsonrpc2 package is updated to support the concept of idle timeout: a duration after which the server is shut down when there are no connected clients. This is exposed in the gopls serve command via the -listen.timeout flag. Update golang/go#34111 Change-Id: Id62b3d4a2fa66de2c9306d130ca431717f01d1e5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/220281 Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
60 lines
1.2 KiB
Go
60 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 fakeHandler struct {
|
|
jsonrpc2.EmptyHandler
|
|
}
|
|
|
|
type msg struct {
|
|
Msg string
|
|
}
|
|
|
|
func (fakeHandler) Deliver(ctx context.Context, r *jsonrpc2.Request, delivered bool) bool {
|
|
if err := r.Reply(ctx, &msg{"pong"}, nil); err != nil {
|
|
panic(err)
|
|
}
|
|
return true
|
|
}
|
|
|
|
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)
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|