gopls/test/integration/completion: Fix flaky test

TestUnimportedCompletion() was reported flaky, as the module
cache index was not yet ready when it was needed.

In tests the module cache index needs to be built, but gopls builds
it using a background goroutine, which might not have finished
by the time the index is needed. This CL contains code that
guarantees that it will be ready.

Fixes: golang/go#78835
Change-Id: Ibd95b65fe9508b5534478c96d2b59376bd818831
Reviewed-on: https://go-review.googlesource.com/c/tools/+/802340
Reviewed-by: Madeline Kalil <mkalil@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/gopls/internal/test/integration/completion/completion_test.go b/gopls/internal/test/integration/completion/completion_test.go
index cedb5f2..6139395 100644
--- a/gopls/internal/test/integration/completion/completion_test.go
+++ b/gopls/internal/test/integration/completion/completion_test.go
@@ -377,6 +377,12 @@
 		env.RunGoCommand("mod", "tidy")
 		env.Await(env.DoneWithChangeWatchedFiles())
 
+		// assure that the module cache index exists
+		// (rather than hoping the background goroutine finishes)
+		if _, err := modindex.Update(filepath.Join(env.Sandbox.GOPATH(), "pkg", "mod")); err != nil {
+			t.Fatal(err)
+		}
+
 		// Trigger unimported completions for the example.com/blah package.
 		env.OpenFile("main.go")
 		env.Await(env.DoneWithOpen())