| 1 | // Copyright 2020 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package servertest provides utilities for running tests against a remote LSP |
| 6 | // server. |
| 7 | package servertestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/ |
| 8 | |
| 9 | import ( |
| 10 | "contextscip-go gomod github.com/golang/go/src go1.25.0 context/" |
| 11 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 12 | "netscip-go gomod github.com/golang/go/src go1.25.0 net/" |
| 13 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 14 | "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/" |
| 15 | |
| 16 | "golang.org/x/tools/internal/jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/" |
| 17 | ) |
| 18 | |
| 19 | // Connector is the interface used to connect to a server. |
| 20 | type Connectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/Connector# interface { |
| 21 | Connectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/Connector#Connect.(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#) jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.Connscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn# |
| 22 | } |
| 23 | |
| 24 | // TCPServer is a helper for executing tests against a remote jsonrpc2 |
| 25 | // connection. Once initialized, its Addr field may be used to connect a |
| 26 | // jsonrpc2 client. |
| 27 | type TCPServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer# struct { |
| 28 | *connListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#connList. |
| 29 | |
| 30 | Addrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#Addr. string |
| 31 | |
| 32 | lnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#ln. netscip-go gomod github.com/golang/go/src go1.25.0 net/.Listenerscip-go gomod github.com/golang/go/src go1.25.0 net/Listener# |
| 33 | framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#framer. jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.Framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Framer# |
| 34 | } |
| 35 | |
| 36 | // NewTCPServer returns a new test server listening on local tcp port and |
| 37 | // serving incoming jsonrpc2 streams using the provided stream server. It |
| 38 | // panics on any error. |
| 39 | func NewTCPServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/NewTCPServer().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, server jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.StreamServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/StreamServer#, framer jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.Framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Framer#) *TCPServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer# { |
| 40 | ln, err := netscip-go gomod github.com/golang/go/src go1.25.0 net/.Listenscip-go gomod github.com/golang/go/src go1.25.0 net/Listen().("tcp", "127.0.0.1:0") |
| 41 | if err != nil { |
| 42 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("servertest: failed to listen: %v", err)) |
| 43 | } |
| 44 | if framer == nil { |
| 45 | framer = jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.NewHeaderStreamscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/NewHeaderStream(). |
| 46 | } |
| 47 | go jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.Servescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Serve().(ctx, ln, server, 0) |
| 48 | return &TCPServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#{Addrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#Addr.: ln.Addrscip-go gomod github.com/golang/go/src go1.25.0 net/Listener#Addr().().Stringscip-go gomod github.com/golang/go/src go1.25.0 net/Addr#String().(), lnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#ln.: ln, framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#framer.: framer, connListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#connList.: &connListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#{}} |
| 49 | } |
| 50 | |
| 51 | // Connect dials the test server and returns a jsonrpc2 Connection that is |
| 52 | // ready for use. |
| 53 | func (s *TCPServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#) Connectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#Connect().(_ contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#) jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.Connscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn# { |
| 54 | netConn, err := netscip-go gomod github.com/golang/go/src go1.25.0 net/.Dialscip-go gomod github.com/golang/go/src go1.25.0 net/Dial().("tcp", s.Addrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#Addr.) |
| 55 | if err != nil { |
| 56 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("servertest: failed to connect to test instance: %v", err)) |
| 57 | } |
| 58 | conn := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.NewConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/NewConn().(s.framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/TCPServer#framer.(netConn)) |
| 59 | s.addscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#add().(conn) |
| 60 | return conn |
| 61 | } |
| 62 | |
| 63 | // PipeServer is a test server that handles connections over io.Pipes. |
| 64 | type PipeServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer# struct { |
| 65 | *connListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer#connList. |
| 66 | serverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer#server. jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.StreamServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/StreamServer# |
| 67 | framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer#framer. jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.Framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Framer# |
| 68 | } |
| 69 | |
| 70 | // NewPipeServer returns a test server that can be connected to via io.Pipes. |
| 71 | func NewPipeServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/NewPipeServer().(server jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.StreamServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/StreamServer#, framer jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.Framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Framer#) *PipeServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer# { |
| 72 | if framer == nil { |
| 73 | framer = jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.NewRawStreamscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/NewRawStream(). |
| 74 | } |
| 75 | return &PipeServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer#{serverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer#server.: server, framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer#framer.: framer, connListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer#connList.: &connListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#{}} |
| 76 | } |
| 77 | |
| 78 | // Connect creates new io.Pipes and binds them to the underlying StreamServer. |
| 79 | func (s *PipeServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer#) Connectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer#Connect().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#) jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.Connscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn# { |
| 80 | sPipe, cPipe := netscip-go gomod github.com/golang/go/src go1.25.0 net/.Pipescip-go gomod github.com/golang/go/src go1.25.0 net/Pipe().() |
| 81 | serverStream := s.framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer#framer.(sPipe) |
| 82 | serverConn := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.NewConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/NewConn().(serverStream) |
| 83 | s.addscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#add().(serverConn) |
| 84 | go s.serverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer#server..ServeStreamscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/StreamServer#ServeStream.(ctx, serverConn) |
| 85 | |
| 86 | clientStream := s.framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/PipeServer#framer.(cPipe) |
| 87 | clientConn := jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.NewConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/NewConn().(clientStream) |
| 88 | s.addscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#add().(clientConn) |
| 89 | return clientConn |
| 90 | } |
| 91 | |
| 92 | // connList tracks closers to run when a testserver is closed. This is a |
| 93 | // convenience, so that callers don't have to worry about closing each |
| 94 | // connection. |
| 95 | type connListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList# struct { |
| 96 | muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#mu. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex# |
| 97 | connsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#conns. []jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.Connscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn# |
| 98 | } |
| 99 | |
| 100 | func (l *connListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#) addscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#add().(conn jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/.Connscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn#) { |
| 101 | l.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 102 | defer l.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 103 | l.connsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#conns. = append(l.connsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#conns., conn) |
| 104 | } |
| 105 | |
| 106 | func (l *connListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#) Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#Close().() error { |
| 107 | l.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 108 | defer l.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 109 | var errmsgs []string |
| 110 | for _, conn := range l.connsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/servertest`/connList#conns. { |
| 111 | if err := conn.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn#Close.(); err != nil { |
| 112 | errmsgs = append(errmsgs, err.Error()) |
| 113 | } |
| 114 | } |
| 115 | if len(errmsgs) > 0 { |
| 116 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("closing errors:\n%s", stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(errmsgs, "\n")) |
| 117 | } |
| 118 | return nil |
| 119 | } |