mirror of
https://github.com/golang/go
synced 2024-11-18 23:34:45 -07:00
5fb17a1e7b
For tests (and perhaps later, for daemon discovery), unix domain sockets offer advantages over TCP: we can know the exact socket address that will be used when starting a server subprocess. They also offer performance and security advantages over TCP, and were specifically requested on golang.org/issues/34111. This CL adds support for listening on UDS, and uses this to implement an additional regtest environment mode that starts up an external process. This mode is disabled by default, but may be enabled by the -enable_gopls_subprocess_tests. The regtest TestMain may be hijacked to instead run as gopls, if a special environment variable is set. This allows the the test runner to start a separate process by using os.Argv[0]. The -gopls_test_binary flag may be used to point tests at a separate gopls binary. Updates golang/go#36879 Updates golang/go#34111 Change-Id: I1cfdf55040e81ffa69a6726878a96529e5522e82 Reviewed-on: https://go-review.googlesource.com/c/tools/+/218839 Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
127 lines
3.3 KiB
Go
127 lines
3.3 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 provides utilities for running tests against a remote LSP
|
|
// server.
|
|
package servertest
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"sync"
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
)
|
|
|
|
// Connector is the interface used to connect to a server.
|
|
type Connector interface {
|
|
Connect(context.Context) *jsonrpc2.Conn
|
|
}
|
|
|
|
// TCPServer is a helper for executing tests against a remote jsonrpc2
|
|
// connection. Once initialized, its Addr field may be used to connect a
|
|
// jsonrpc2 client.
|
|
type TCPServer struct {
|
|
Addr string
|
|
|
|
ln net.Listener
|
|
cls *closerList
|
|
}
|
|
|
|
// NewTCPServer returns a new test server listening on local tcp port and
|
|
// serving incoming jsonrpc2 streams using the provided stream server. It
|
|
// panics on any error.
|
|
func NewTCPServer(ctx context.Context, server jsonrpc2.StreamServer) *TCPServer {
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
panic(fmt.Sprintf("servertest: failed to listen: %v", err))
|
|
}
|
|
go jsonrpc2.Serve(ctx, ln, server)
|
|
return &TCPServer{Addr: ln.Addr().String(), ln: ln, cls: &closerList{}}
|
|
}
|
|
|
|
// Connect dials the test server and returns a jsonrpc2 Connection that is
|
|
// ready for use.
|
|
func (s *TCPServer) Connect(ctx context.Context) *jsonrpc2.Conn {
|
|
netConn, err := net.Dial("tcp", s.Addr)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("servertest: failed to connect to test instance: %v", err))
|
|
}
|
|
s.cls.add(func() {
|
|
netConn.Close()
|
|
})
|
|
conn := jsonrpc2.NewConn(jsonrpc2.NewHeaderStream(netConn, netConn))
|
|
go conn.Run(ctx)
|
|
return conn
|
|
}
|
|
|
|
// Close closes all connected pipes.
|
|
func (s *TCPServer) Close() error {
|
|
s.cls.closeAll()
|
|
return nil
|
|
}
|
|
|
|
// PipeServer is a test server that handles connections over io.Pipes.
|
|
type PipeServer struct {
|
|
server jsonrpc2.StreamServer
|
|
cls *closerList
|
|
}
|
|
|
|
// NewPipeServer returns a test server that can be connected to via io.Pipes.
|
|
func NewPipeServer(ctx context.Context, server jsonrpc2.StreamServer) *PipeServer {
|
|
return &PipeServer{server: server, cls: &closerList{}}
|
|
}
|
|
|
|
// Connect creates new io.Pipes and binds them to the underlying StreamServer.
|
|
func (s *PipeServer) Connect(ctx context.Context) *jsonrpc2.Conn {
|
|
// Pipes connect like this:
|
|
// Client🡒(sWriter)🡒(sReader)🡒Server
|
|
// 🡔(cReader)🡐(cWriter)🡗
|
|
sReader, sWriter := io.Pipe()
|
|
cReader, cWriter := io.Pipe()
|
|
s.cls.add(func() {
|
|
sReader.Close()
|
|
sWriter.Close()
|
|
cReader.Close()
|
|
cWriter.Close()
|
|
})
|
|
serverStream := jsonrpc2.NewStream(sReader, cWriter)
|
|
go s.server.ServeStream(ctx, serverStream)
|
|
|
|
clientStream := jsonrpc2.NewStream(cReader, sWriter)
|
|
clientConn := jsonrpc2.NewConn(clientStream)
|
|
go clientConn.Run(ctx)
|
|
return clientConn
|
|
}
|
|
|
|
// Close closes all connected pipes.
|
|
func (s *PipeServer) Close() error {
|
|
s.cls.closeAll()
|
|
return nil
|
|
}
|
|
|
|
// closerList tracks closers to run when a testserver is closed. This is a
|
|
// convenience, so that callers don't have to worry about closing each
|
|
// connection.
|
|
type closerList struct {
|
|
mu sync.Mutex
|
|
closers []func()
|
|
}
|
|
|
|
func (l *closerList) add(closer func()) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
l.closers = append(l.closers, closer)
|
|
}
|
|
|
|
func (l *closerList) closeAll() {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
for _, closer := range l.closers {
|
|
closer()
|
|
}
|
|
}
|