| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package lsprpc |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"errors" |
| 11 |
"regexp" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"golang.org/x/tools/gopls/internal/cache" |
| 17 |
"golang.org/x/tools/gopls/internal/debug" |
| 18 |
"golang.org/x/tools/gopls/internal/protocol" |
| 19 |
"golang.org/x/tools/gopls/internal/test/integration/fake" |
| 20 |
"golang.org/x/tools/internal/event" |
| 21 |
"golang.org/x/tools/internal/jsonrpc2" |
| 22 |
"golang.org/x/tools/internal/jsonrpc2/servertest" |
| 23 |
"golang.org/x/tools/internal/testenv" |
| 24 |
) |
| 25 |
|
| 26 |
type FakeClient struct { |
| 27 |
protocol.Client |
| 28 |
|
| 29 |
Logs chan string |
| 30 |
} |
| 31 |
|
| 32 |
func (c FakeClient) LogMessage(ctx context.Context, params *protocol.LogMessageParams) error { |
| 33 |
c.Logs <- params.Message |
| 34 |
return nil |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
type fakeServer struct { |
| 40 |
protocol.Server |
| 41 |
} |
| 42 |
|
| 43 |
func (fakeServer) Shutdown(ctx context.Context) error { |
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
type PingServer struct{ fakeServer } |
| 48 |
|
| 49 |
func (s PingServer) DidOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error { |
| 50 |
event.Log(ctx, "ping") |
| 51 |
return nil |
| 52 |
} |
| 53 |
|
| 54 |
func TestClientLogging(t *testing.T) { |
| 55 |
ctx := t.Context() |
| 56 |
|
| 57 |
server := PingServer{} |
| 58 |
client := FakeClient{Logs: make(chan string, 10)} |
| 59 |
|
| 60 |
ctx = debug.WithInstance(ctx, "") |
| 61 |
ss := NewStreamServer(cache.New(nil), false, nil) |
| 62 |
ss.serverForTest = server |
| 63 |
ts := servertest.NewPipeServer(ss, nil) |
| 64 |
defer checkClose(t, ts.Close) |
| 65 |
cc := ts.Connect(ctx) |
| 66 |
cc.Go(ctx, protocol.ClientHandler(client, jsonrpc2.MethodNotFound)) |
| 67 |
|
| 68 |
if err := protocol.ServerDispatcher(cc).DidOpen(ctx, &protocol.DidOpenTextDocumentParams{}); err != nil { |
| 69 |
t.Errorf("DidOpen: %v", err) |
| 70 |
} |
| 71 |
|
| 72 |
select { |
| 73 |
case got := <-client.Logs: |
| 74 |
want := "ping" |
| 75 |
matched, err := regexp.MatchString(want, got) |
| 76 |
if err != nil { |
| 77 |
t.Fatal(err) |
| 78 |
} |
| 79 |
if !matched { |
| 80 |
t.Errorf("got log %q, want a log containing %q", got, want) |
| 81 |
} |
| 82 |
|
| 83 |
case <-time.After(10 * time.Second): |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
t.Error("timeout waiting for client log") |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
type WaitableServer struct { |
| 95 |
fakeServer |
| 96 |
|
| 97 |
Started chan struct{} |
| 98 |
Completed chan error |
| 99 |
} |
| 100 |
|
| 101 |
func (s WaitableServer) Hover(ctx context.Context, _ *protocol.HoverParams) (_ *protocol.Hover, err error) { |
| 102 |
s.Started <- struct{}{} |
| 103 |
defer func() { |
| 104 |
s.Completed <- err |
| 105 |
}() |
| 106 |
select { |
| 107 |
case <-ctx.Done(): |
| 108 |
return nil, errors.New("cancelled hover") |
| 109 |
case <-time.After(10 * time.Second): |
| 110 |
} |
| 111 |
return &protocol.Hover{}, nil |
| 112 |
} |
| 113 |
|
| 114 |
func (s WaitableServer) ResolveCompletionItem(_ context.Context, item *protocol.CompletionItem) (*protocol.CompletionItem, error) { |
| 115 |
return item, nil |
| 116 |
} |
| 117 |
|
| 118 |
func checkClose(t *testing.T, closer func() error) { |
| 119 |
t.Helper() |
| 120 |
if err := closer(); err != nil { |
| 121 |
t.Errorf("closing: %v", err) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func setupForwarding(ctx context.Context, t *testing.T, s protocol.Server) (direct, forwarded servertest.Connector, cleanup func()) { |
| 126 |
t.Helper() |
| 127 |
serveCtx := debug.WithInstance(ctx, "") |
| 128 |
ss := NewStreamServer(cache.New(nil), false, nil) |
| 129 |
ss.serverForTest = s |
| 130 |
tsDirect := servertest.NewTCPServer(serveCtx, ss, nil) |
| 131 |
|
| 132 |
forwarder, err := NewForwarder("tcp;"+tsDirect.Addr, nil) |
| 133 |
if err != nil { |
| 134 |
t.Fatal(err) |
| 135 |
} |
| 136 |
tsForwarded := servertest.NewPipeServer(forwarder, nil) |
| 137 |
return tsDirect, tsForwarded, func() { |
| 138 |
checkClose(t, tsDirect.Close) |
| 139 |
checkClose(t, tsForwarded.Close) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestRequestCancellation(t *testing.T) { |
| 144 |
ctx := context.Background() |
| 145 |
server := WaitableServer{ |
| 146 |
Started: make(chan struct{}), |
| 147 |
Completed: make(chan error), |
| 148 |
} |
| 149 |
tsDirect, tsForwarded, cleanup := setupForwarding(ctx, t, server) |
| 150 |
defer cleanup() |
| 151 |
tests := []struct { |
| 152 |
serverType string |
| 153 |
ts servertest.Connector |
| 154 |
}{ |
| 155 |
{"direct", tsDirect}, |
| 156 |
{"forwarder", tsForwarded}, |
| 157 |
} |
| 158 |
|
| 159 |
for _, test := range tests { |
| 160 |
t.Run(test.serverType, func(t *testing.T) { |
| 161 |
cc := test.ts.Connect(ctx) |
| 162 |
sd := protocol.ServerDispatcher(cc) |
| 163 |
cc.Go(ctx, |
| 164 |
protocol.Handlers( |
| 165 |
jsonrpc2.MethodNotFound)) |
| 166 |
|
| 167 |
ctx := context.Background() |
| 168 |
ctx, cancel := context.WithCancel(ctx) |
| 169 |
|
| 170 |
result := make(chan error) |
| 171 |
go func() { |
| 172 |
_, err := sd.Hover(ctx, &protocol.HoverParams{}) |
| 173 |
result <- err |
| 174 |
}() |
| 175 |
|
| 176 |
<-server.Started |
| 177 |
cancel() |
| 178 |
if err := <-result; err == nil { |
| 179 |
t.Error("nil error for cancelled Hover(), want non-nil") |
| 180 |
} |
| 181 |
if err := <-server.Completed; err == nil || !strings.Contains(err.Error(), "cancelled hover") { |
| 182 |
t.Errorf("Hover(): unexpected server-side error %v", err) |
| 183 |
} |
| 184 |
}) |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
const exampleProgram = ` |
| 189 |
-- go.mod -- |
| 190 |
module mod |
| 191 |
|
| 192 |
go 1.12 |
| 193 |
-- main.go -- |
| 194 |
package main |
| 195 |
|
| 196 |
import "fmt" |
| 197 |
|
| 198 |
func main() { |
| 199 |
fmt.Println("Hello World.") |
| 200 |
}` |
| 201 |
|
| 202 |
func TestDebugInfoLifecycle(t *testing.T) { |
| 203 |
sb, err := fake.NewSandbox(&fake.SandboxConfig{Files: fake.UnpackTxt(exampleProgram)}) |
| 204 |
if err != nil { |
| 205 |
t.Fatal(err) |
| 206 |
} |
| 207 |
defer func() { |
| 208 |
if err := sb.Close(); err != nil { |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
t.Logf("closing workspace failed: %v", err) |
| 215 |
} |
| 216 |
}() |
| 217 |
|
| 218 |
baseCtx := t.Context() |
| 219 |
clientCtx := debug.WithInstance(baseCtx, "") |
| 220 |
serverCtx := debug.WithInstance(baseCtx, "") |
| 221 |
|
| 222 |
ss := NewStreamServer(cache.New(nil), false, nil) |
| 223 |
tsBackend := servertest.NewTCPServer(serverCtx, ss, nil) |
| 224 |
|
| 225 |
forwarder, err := NewForwarder("tcp;"+tsBackend.Addr, nil) |
| 226 |
if err != nil { |
| 227 |
t.Fatal(err) |
| 228 |
} |
| 229 |
tsForwarder := servertest.NewPipeServer(forwarder, nil) |
| 230 |
|
| 231 |
ed1, err := fake.NewEditor(sb, fake.EditorConfig{}).Connect(clientCtx, tsForwarder, fake.ClientHooks{}) |
| 232 |
if err != nil { |
| 233 |
t.Fatal(err) |
| 234 |
} |
| 235 |
defer ed1.Close(clientCtx) |
| 236 |
ed2, err := fake.NewEditor(sb, fake.EditorConfig{}).Connect(baseCtx, tsBackend, fake.ClientHooks{}) |
| 237 |
if err != nil { |
| 238 |
t.Fatal(err) |
| 239 |
} |
| 240 |
defer ed2.Close(baseCtx) |
| 241 |
|
| 242 |
serverDebug := debug.GetInstance(serverCtx) |
| 243 |
if got, want := len(serverDebug.State.Clients()), 2; got != want { |
| 244 |
t.Errorf("len(server:Clients) = %d, want %d", got, want) |
| 245 |
} |
| 246 |
if got, want := len(serverDebug.State.Sessions()), 2; got != want { |
| 247 |
t.Errorf("len(server:Sessions) = %d, want %d", got, want) |
| 248 |
} |
| 249 |
clientDebug := debug.GetInstance(clientCtx) |
| 250 |
if got, want := len(clientDebug.State.Servers()), 1; got != want { |
| 251 |
t.Errorf("len(client:Servers) = %d, want %d", got, want) |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
if err := ed1.Close(clientCtx); err != nil { |
| 256 |
t.Fatal(err) |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
start := time.Now() |
| 265 |
delay := time.Millisecond |
| 266 |
const maxWait = time.Second |
| 267 |
for len(serverDebug.State.Clients()) > 1 { |
| 268 |
if time.Since(start) > maxWait { |
| 269 |
break |
| 270 |
} |
| 271 |
time.Sleep(delay) |
| 272 |
delay *= 2 |
| 273 |
} |
| 274 |
if got, want := len(serverDebug.State.Clients()), 1; got != want { |
| 275 |
t.Errorf("len(server:Clients) = %d, want %d", got, want) |
| 276 |
} |
| 277 |
if got, want := len(serverDebug.State.Sessions()), 1; got != want { |
| 278 |
t.Errorf("len(server:Sessions()) = %d, want %d", got, want) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
type initServer struct { |
| 283 |
fakeServer |
| 284 |
|
| 285 |
params *protocol.ParamInitialize |
| 286 |
} |
| 287 |
|
| 288 |
func (s *initServer) Initialize(ctx context.Context, params *protocol.ParamInitialize) (*protocol.InitializeResult, error) { |
| 289 |
s.params = params |
| 290 |
return &protocol.InitializeResult{}, nil |
| 291 |
} |
| 292 |
|
| 293 |
func TestEnvForwarding(t *testing.T) { |
| 294 |
testenv.NeedsTool(t, "go") |
| 295 |
|
| 296 |
ctx := context.Background() |
| 297 |
|
| 298 |
server := &initServer{} |
| 299 |
_, tsForwarded, cleanup := setupForwarding(ctx, t, server) |
| 300 |
defer cleanup() |
| 301 |
|
| 302 |
conn := tsForwarded.Connect(ctx) |
| 303 |
conn.Go(ctx, jsonrpc2.MethodNotFound) |
| 304 |
dispatch := protocol.ServerDispatcher(conn) |
| 305 |
initParams := &protocol.ParamInitialize{} |
| 306 |
initParams.InitializationOptions = map[string]any{ |
| 307 |
"env": map[string]any{ |
| 308 |
"GONOPROXY": "example.com", |
| 309 |
}, |
| 310 |
} |
| 311 |
_, err := dispatch.Initialize(ctx, initParams) |
| 312 |
if err != nil { |
| 313 |
t.Fatal(err) |
| 314 |
} |
| 315 |
if server.params == nil { |
| 316 |
t.Fatalf("initialize params are unset") |
| 317 |
} |
| 318 |
env := server.params.InitializationOptions.(map[string]any)["env"].(map[string]any) |
| 319 |
|
| 320 |
|
| 321 |
if _, ok := env["GOPRIVATE"]; !ok { |
| 322 |
t.Errorf("Go environment variable GOPRIVATE unset in initialization options") |
| 323 |
} |
| 324 |
|
| 325 |
if v := env["GONOPROXY"]; v != "example.com" { |
| 326 |
t.Errorf("GONOPROXY environment variable was overwritten") |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
func TestListenParsing(t *testing.T) { |
| 331 |
tests := []struct { |
| 332 |
input, wantNetwork, wantAddr string |
| 333 |
}{ |
| 334 |
{"127.0.0.1:0", "tcp", "127.0.0.1:0"}, |
| 335 |
{"unix;/tmp/sock", "unix", "/tmp/sock"}, |
| 336 |
{"auto", "auto", ""}, |
| 337 |
{"auto;foo", "auto", "foo"}, |
| 338 |
} |
| 339 |
|
| 340 |
for _, test := range tests { |
| 341 |
gotNetwork, gotAddr := ParseAddr(test.input) |
| 342 |
if gotNetwork != test.wantNetwork { |
| 343 |
t.Errorf("network = %q, want %q", gotNetwork, test.wantNetwork) |
| 344 |
} |
| 345 |
if gotAddr != test.wantAddr { |
| 346 |
t.Errorf("addr = %q, want %q", gotAddr, test.wantAddr) |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
func TestEmptySlices(t *testing.T) { |
| 353 |
|
| 354 |
const bad = `{"a":null}` |
| 355 |
const good = `{"a":[]}` |
| 356 |
var x struct { |
| 357 |
A []string `json:"a"` |
| 358 |
} |
| 359 |
buf, _ := json.Marshal(x) |
| 360 |
if string(buf) != bad { |
| 361 |
|
| 362 |
t.Errorf("unexpectedly got %s, want %s", buf, bad) |
| 363 |
} |
| 364 |
x.A = make([]string, 0) |
| 365 |
buf, _ = json.Marshal(x) |
| 366 |
if string(buf) != good { |
| 367 |
|
| 368 |
t.Errorf("unexpectedly got %s, want %s", buf, good) |
| 369 |
} |
| 370 |
x.A = []string{} |
| 371 |
buf, _ = json.Marshal(x) |
| 372 |
if string(buf) != good { |
| 373 |
|
| 374 |
t.Errorf("unexpectedly got %s, want %s", buf, good) |
| 375 |
} |
| 376 |
} |