golang.org/x/tools/gopls@v0.15.3/internal/test/integration/misc/shared_test.go (about) 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 misc 6 7 import ( 8 "testing" 9 10 . "golang.org/x/tools/gopls/internal/test/integration" 11 "golang.org/x/tools/gopls/internal/test/integration/fake" 12 ) 13 14 // Smoke test that simultaneous editing sessions in the same workspace works. 15 func TestSimultaneousEdits(t *testing.T) { 16 const sharedProgram = ` 17 -- go.mod -- 18 module mod 19 20 go 1.12 21 -- main.go -- 22 package main 23 24 import "fmt" 25 26 func main() { 27 fmt.Println("Hello World.") 28 }` 29 30 WithOptions( 31 Modes(DefaultModes()&(Forwarded|SeparateProcess)), 32 ).Run(t, sharedProgram, func(t *testing.T, env1 *Env) { 33 // Create a second test session connected to the same workspace and server 34 // as the first. 35 awaiter := NewAwaiter(env1.Sandbox.Workdir) 36 const skipApplyEdits = false 37 editor, err := fake.NewEditor(env1.Sandbox, env1.Editor.Config()).Connect(env1.Ctx, env1.Server, awaiter.Hooks(), skipApplyEdits) 38 if err != nil { 39 t.Fatal(err) 40 } 41 env2 := &Env{ 42 T: t, 43 Ctx: env1.Ctx, 44 Sandbox: env1.Sandbox, 45 Server: env1.Server, 46 Editor: editor, 47 Awaiter: awaiter, 48 } 49 env2.Await(InitialWorkspaceLoad) 50 // In editor #1, break fmt.Println as before. 51 env1.OpenFile("main.go") 52 env1.RegexpReplace("main.go", "Printl(n)", "") 53 // In editor #2 remove the closing brace. 54 env2.OpenFile("main.go") 55 env2.RegexpReplace("main.go", "\\)\n(})", "") 56 57 // Now check that we got different diagnostics in each environment. 58 env1.AfterChange(Diagnostics(env1.AtRegexp("main.go", "Printl"))) 59 env2.AfterChange(Diagnostics(env2.AtRegexp("main.go", "$"))) 60 61 // Now close editor #2, and verify that operation in editor #1 is 62 // unaffected. 63 if err := env2.Editor.Close(env2.Ctx); err != nil { 64 t.Errorf("closing second editor: %v", err) 65 } 66 67 env1.RegexpReplace("main.go", "Printl", "Println") 68 env1.AfterChange( 69 NoDiagnostics(ForFile("main.go")), 70 ) 71 }) 72 }