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 <noreply+kokoro@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/gopls/internal/regtest/misc/shared_test.go b/gopls/internal/regtest/misc/shared_test.go
index e433f4b..64e0720 100644
--- a/gopls/internal/regtest/misc/shared_test.go
+++ b/gopls/internal/regtest/misc/shared_test.go
@@ -11,7 +11,9 @@
 	. "golang.org/x/tools/internal/lsp/regtest"
 )
 
-const sharedProgram = `
+// Smoke test that simultaneous editing sessions in the same workspace works.
+func TestSimultaneousEdits(t *testing.T) {
+	const sharedProgram = `
 -- go.mod --
 module mod
 
@@ -25,13 +27,9 @@
 	fmt.Println("Hello World.")
 }`
 
-// runShared is a helper to run a test in the same directory using both the
-// original env, and an additional other environment connected to the same
-// server.
-func runShared(t *testing.T, testFunc func(origEnv *Env, otherEnv *Env)) {
-	// Only run these tests in forwarded modes.
-	modes := DefaultModes() & (Forwarded | SeparateProcess)
-	WithOptions(Modes(modes)).Run(t, sharedProgram, func(t *testing.T, env1 *Env) {
+	WithOptions(
+		Modes(DefaultModes()&(Forwarded|SeparateProcess)),
+	).Run(t, sharedProgram, func(t *testing.T, env1 *Env) {
 		// Create a second test session connected to the same workspace and server
 		// as the first.
 		awaiter := NewAwaiter(env1.Sandbox.Workdir)
@@ -48,37 +46,29 @@
 			Awaiter: awaiter,
 		}
 		env2.Await(InitialWorkspaceLoad)
-		testFunc(env1, env2)
-	})
-}
-
-func TestSimultaneousEdits(t *testing.T) {
-	runShared(t, func(origEnv *Env, otherEnv *Env) {
 		// In editor #1, break fmt.Println as before.
-		origEnv.OpenFile("main.go")
-		origEnv.RegexpReplace("main.go", "Printl(n)", "")
+		env1.OpenFile("main.go")
+		env1.RegexpReplace("main.go", "Printl(n)", "")
 		// In editor #2 remove the closing brace.
-		otherEnv.OpenFile("main.go")
-		otherEnv.RegexpReplace("main.go", "\\)\n(})", "")
+		env2.OpenFile("main.go")
+		env2.RegexpReplace("main.go", "\\)\n(})", "")
 
 		// Now check that we got different diagnostics in each environment.
-		origEnv.Await(origEnv.DiagnosticAtRegexp("main.go", "Printl"))
-		otherEnv.Await(otherEnv.DiagnosticAtRegexp("main.go", "$"))
-	})
-}
+		env1.Await(env1.DiagnosticAtRegexp("main.go", "Printl"))
+		env2.Await(env2.DiagnosticAtRegexp("main.go", "$"))
 
-func TestShutdown(t *testing.T) {
-	runShared(t, func(origEnv *Env, otherEnv *Env) {
-		// Close otherEnv, and verify that operation in the original environment is
-		// unaffected. Note: 'otherEnv' must be the environment being closed here.
-		// If we were to instead close 'env' here, we'd run into a duplicate
-		// shutdown when the test runner closes the original env.
-		if err := otherEnv.Editor.Close(otherEnv.Ctx); err != nil {
-			t.Errorf("closing first editor: %v", err)
+		// Now close editor #2, and verify that operation in editor #1 is
+		// unaffected.
+		if err := env2.Editor.Close(env2.Ctx); err != nil {
+			t.Errorf("closing second editor: %v", err)
 		}
-		// Now make an edit in editor #2 to trigger diagnostics.
-		origEnv.OpenFile("main.go")
-		origEnv.RegexpReplace("main.go", "\\)\n(})", "")
-		origEnv.Await(origEnv.DiagnosticAtRegexp("main.go", "$"))
+
+		env1.RegexpReplace("main.go", "Printl", "Println")
+		env1.Await(
+			OnceMet(
+				env1.DoneWithChange(),
+				EmptyDiagnostics("main.go"),
+			),
+		)
 	})
 }