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 misc
6
7import (
8	"testing"
9
10	. "golang.org/x/tools/internal/lsp/regtest"
11)
12
13const sharedProgram = `
14-- go.mod --
15module mod
16
17go 1.12
18-- main.go --
19package main
20
21import "fmt"
22
23func main() {
24	fmt.Println("Hello World.")
25}`
26
27func runShared(t *testing.T, testFunc func(env1 *Env, env2 *Env)) {
28	// Only run these tests in forwarded modes.
29	modes := DefaultModes() & (Forwarded | SeparateProcess)
30	WithOptions(Modes(modes)).Run(t, sharedProgram, func(t *testing.T, env1 *Env) {
31		// Create a second test session connected to the same workspace and server
32		// as the first.
33		env2 := NewEnv(env1.Ctx, t, env1.Sandbox, env1.Server, env1.Editor.Config, true)
34		env2.Await(InitialWorkspaceLoad)
35		testFunc(env1, env2)
36	})
37}
38
39func TestSimultaneousEdits(t *testing.T) {
40	runShared(t, func(env1 *Env, env2 *Env) {
41		// In editor #1, break fmt.Println as before.
42		env1.OpenFile("main.go")
43		env1.RegexpReplace("main.go", "Printl(n)", "")
44		// In editor #2 remove the closing brace.
45		env2.OpenFile("main.go")
46		env2.RegexpReplace("main.go", "\\)\n(})", "")
47
48		// Now check that we got different diagnostics in each environment.
49		env1.Await(env1.DiagnosticAtRegexp("main.go", "Printl"))
50		env2.Await(env2.DiagnosticAtRegexp("main.go", "$"))
51	})
52}
53
54func TestShutdown(t *testing.T) {
55	runShared(t, func(env1 *Env, env2 *Env) {
56		if err := env1.Editor.Close(env1.Ctx); err != nil {
57			t.Errorf("closing first editor: %v", err)
58		}
59		// Now make an edit in editor #2 to trigger diagnostics.
60		env2.OpenFile("main.go")
61		env2.RegexpReplace("main.go", "\\)\n(})", "")
62		env2.Await(env2.DiagnosticAtRegexp("main.go", "$"))
63	})
64}
65