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 5package regtest 6 7import ( 8 "context" 9 "testing" 10 11 "golang.org/x/tools/internal/lsp/fake" 12) 13 14const sharedProgram = ` 15-- go.mod -- 16module mod 17 18go 1.12 19-- main.go -- 20package main 21 22import "fmt" 23 24func main() { 25 fmt.Println("Hello World.") 26}` 27 28func runShared(t *testing.T, program string, testFunc func(ctx context.Context, t *testing.T, env1 *Env, env2 *Env)) { 29 // Only run these tests in forwarded modes. 30 modes := runner.Modes() & (Forwarded | SeparateProcess) 31 runner.RunInMode(modes, t, sharedProgram, func(ctx context.Context, t *testing.T, env1 *Env) { 32 // Create a second test session connected to the same workspace and server 33 // as the first. 34 env2 := NewEnv(ctx, t, env1.W, env1.Server) 35 testFunc(ctx, t, env1, env2) 36 }) 37} 38 39func TestSimultaneousEdits(t *testing.T) { 40 t.Parallel() 41 runShared(t, exampleProgram, func(ctx context.Context, t *testing.T, env1 *Env, env2 *Env) { 42 // In editor #1, break fmt.Println as before. 43 edit1 := fake.NewEdit(5, 11, 5, 12, "") 44 env1.OpenFile("main.go") 45 env1.EditBuffer("main.go", edit1) 46 // In editor #2 remove the closing brace. 47 edit2 := fake.NewEdit(6, 0, 6, 1, "") 48 env2.OpenFile("main.go") 49 env2.EditBuffer("main.go", edit2) 50 51 // Now check that we got different diagnostics in each environment. 52 env1.Await(DiagnosticAt("main.go", 5, 5)) 53 env2.Await(DiagnosticAt("main.go", 7, 0)) 54 }) 55} 56 57func TestShutdown(t *testing.T) { 58 t.Parallel() 59 runShared(t, sharedProgram, func(ctx context.Context, t *testing.T, env1 *Env, env2 *Env) { 60 env1.CloseEditor() 61 // Now make an edit in editor #2 to trigger diagnostics. 62 edit2 := fake.NewEdit(6, 0, 6, 1, "") 63 env2.OpenFile("main.go") 64 env2.EditBuffer("main.go", edit2) 65 env2.Await(DiagnosticAt("main.go", 7, 0)) 66 }) 67} 68