Skip to content

Commit 763f65c

Browse files
committed
gopls/internal/regtest/misc: simplify shared edit tests
In order to avoid shutdown races we must always close the second editor created for simultaneous edit tests. Rather than introduce additional indirection, just merge the two tests into one. For golang/go#54241 Change-Id: I6604141baa77d07f6281d3a90efa13c02b94079a Reviewed-on: https://go-review.googlesource.com/c/tools/+/421258 gopls-CI: kokoro <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Suzy Mueller <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent fc3b24a commit 763f65c

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

gopls/internal/regtest/misc/shared_test.go

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
. "golang.org/x/tools/internal/lsp/regtest"
1212
)
1313

14-
const sharedProgram = `
14+
// Smoke test that simultaneous editing sessions in the same workspace works.
15+
func TestSimultaneousEdits(t *testing.T) {
16+
const sharedProgram = `
1517
-- go.mod --
1618
module mod
1719
@@ -25,13 +27,9 @@ func main() {
2527
fmt.Println("Hello World.")
2628
}`
2729

28-
// runShared is a helper to run a test in the same directory using both the
29-
// original env, and an additional other environment connected to the same
30-
// server.
31-
func runShared(t *testing.T, testFunc func(origEnv *Env, otherEnv *Env)) {
32-
// Only run these tests in forwarded modes.
33-
modes := DefaultModes() & (Forwarded | SeparateProcess)
34-
WithOptions(Modes(modes)).Run(t, sharedProgram, func(t *testing.T, env1 *Env) {
30+
WithOptions(
31+
Modes(DefaultModes()&(Forwarded|SeparateProcess)),
32+
).Run(t, sharedProgram, func(t *testing.T, env1 *Env) {
3533
// Create a second test session connected to the same workspace and server
3634
// as the first.
3735
awaiter := NewAwaiter(env1.Sandbox.Workdir)
@@ -48,37 +46,29 @@ func runShared(t *testing.T, testFunc func(origEnv *Env, otherEnv *Env)) {
4846
Awaiter: awaiter,
4947
}
5048
env2.Await(InitialWorkspaceLoad)
51-
testFunc(env1, env2)
52-
})
53-
}
54-
55-
func TestSimultaneousEdits(t *testing.T) {
56-
runShared(t, func(origEnv *Env, otherEnv *Env) {
5749
// In editor #1, break fmt.Println as before.
58-
origEnv.OpenFile("main.go")
59-
origEnv.RegexpReplace("main.go", "Printl(n)", "")
50+
env1.OpenFile("main.go")
51+
env1.RegexpReplace("main.go", "Printl(n)", "")
6052
// In editor #2 remove the closing brace.
61-
otherEnv.OpenFile("main.go")
62-
otherEnv.RegexpReplace("main.go", "\\)\n(})", "")
53+
env2.OpenFile("main.go")
54+
env2.RegexpReplace("main.go", "\\)\n(})", "")
6355

6456
// Now check that we got different diagnostics in each environment.
65-
origEnv.Await(origEnv.DiagnosticAtRegexp("main.go", "Printl"))
66-
otherEnv.Await(otherEnv.DiagnosticAtRegexp("main.go", "$"))
67-
})
68-
}
57+
env1.Await(env1.DiagnosticAtRegexp("main.go", "Printl"))
58+
env2.Await(env2.DiagnosticAtRegexp("main.go", "$"))
6959

70-
func TestShutdown(t *testing.T) {
71-
runShared(t, func(origEnv *Env, otherEnv *Env) {
72-
// Close otherEnv, and verify that operation in the original environment is
73-
// unaffected. Note: 'otherEnv' must be the environment being closed here.
74-
// If we were to instead close 'env' here, we'd run into a duplicate
75-
// shutdown when the test runner closes the original env.
76-
if err := otherEnv.Editor.Close(otherEnv.Ctx); err != nil {
77-
t.Errorf("closing first editor: %v", err)
60+
// Now close editor #2, and verify that operation in editor #1 is
61+
// unaffected.
62+
if err := env2.Editor.Close(env2.Ctx); err != nil {
63+
t.Errorf("closing second editor: %v", err)
7864
}
79-
// Now make an edit in editor #2 to trigger diagnostics.
80-
origEnv.OpenFile("main.go")
81-
origEnv.RegexpReplace("main.go", "\\)\n(})", "")
82-
origEnv.Await(origEnv.DiagnosticAtRegexp("main.go", "$"))
65+
66+
env1.RegexpReplace("main.go", "Printl", "Println")
67+
env1.Await(
68+
OnceMet(
69+
env1.DoneWithChange(),
70+
EmptyDiagnostics("main.go"),
71+
),
72+
)
8373
})
8474
}

0 commit comments

Comments
 (0)