Skip to content

Commit 672a036

Browse files
committed
gopls/internal/regtest: simplify OnceMet expressions with an env helper
Simplify env.Await(OnceMet(...)) to env.OnceMet(...). Aside from avoiding boilerplate, this makes it easier to identify where we're still using Await. Updates golang/go#39384 Change-Id: I57a18242ce6b48e371e5ce4876ef01a6774fe15c Reviewed-on: https://go-review.googlesource.com/c/tools/+/461917 Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent ab7b5b2 commit 672a036

20 files changed

+263
-457
lines changed

gopls/internal/lsp/regtest/env.go

+12
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,25 @@ func checkExpectations(s State, expectations []Expectation) (Verdict, string) {
302302
return finalVerdict, summary.String()
303303
}
304304

305+
// Await blocks until the given expectations are all simultaneously met.
306+
//
307+
// Generally speaking Await should be avoided because it can block indefinitely
308+
// if gopls ends up in a state where the expectations are never going to be
309+
// met. Use AfterChange or OnceMet instead, so that the runner knows when to
310+
// stop waiting.
305311
func (e *Env) Await(expectations ...Expectation) {
306312
e.T.Helper()
307313
if err := e.Awaiter.Await(e.Ctx, expectations...); err != nil {
308314
e.T.Fatal(err)
309315
}
310316
}
311317

318+
// OnceMet blocks until precondition is met or unmeetable; if the precondition
319+
// is met, it atomically checks that all expectations in mustMeets are met.
320+
func (e *Env) OnceMet(precondition Expectation, mustMeets ...Expectation) {
321+
e.Await(OnceMet(precondition, mustMeets...))
322+
}
323+
312324
// Await waits for all expectations to simultaneously be met. It should only be
313325
// called from the main test goroutine.
314326
func (a *Awaiter) Await(ctx context.Context, expectations ...Expectation) error {

gopls/internal/lsp/regtest/expectation.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,9 @@ func (e *Env) DoneDiagnosingChanges() Expectation {
291291
// expectations.
292292
func (e *Env) AfterChange(expectations ...Expectation) {
293293
e.T.Helper()
294-
e.Await(
295-
OnceMet(
296-
e.DoneDiagnosingChanges(),
297-
expectations...,
298-
),
294+
e.OnceMet(
295+
e.DoneDiagnosingChanges(),
296+
expectations...,
299297
)
300298
}
301299

gopls/internal/regtest/codelens/codelens_test.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,14 @@ require golang.org/x/hello v1.2.3
208208
env.OpenFile("b/go.mod")
209209
env.ExecuteCodeLensCommand("a/go.mod", command.CheckUpgrades, nil)
210210
d := &protocol.PublishDiagnosticsParams{}
211-
env.Await(
212-
OnceMet(
213-
env.DiagnosticAtRegexpWithMessage("a/go.mod", `require`, "can be upgraded"),
214-
ReadDiagnostics("a/go.mod", d),
215-
// We do not want there to be a diagnostic for b/go.mod,
216-
// but there may be some subtlety in timing here, where this
217-
// should always succeed, but may not actually test the correct
218-
// behavior.
219-
NoMatchingDiagnostics(env.AtRegexp("b/go.mod", `require`)),
220-
),
211+
env.OnceMet(
212+
env.DiagnosticAtRegexpWithMessage("a/go.mod", `require`, "can be upgraded"),
213+
ReadDiagnostics("a/go.mod", d),
214+
// We do not want there to be a diagnostic for b/go.mod,
215+
// but there may be some subtlety in timing here, where this
216+
// should always succeed, but may not actually test the correct
217+
// behavior.
218+
NoMatchingDiagnostics(env.AtRegexp("b/go.mod", `require`)),
221219
)
222220
// Check for upgrades in b/go.mod and then clear them.
223221
env.ExecuteCodeLensCommand("b/go.mod", command.CheckUpgrades, nil)

gopls/internal/regtest/codelens/gcdetails_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,9 @@ func main() {
4545
env.OpenFile("main.go")
4646
env.ExecuteCodeLensCommand("main.go", command.GCDetails, nil)
4747
d := &protocol.PublishDiagnosticsParams{}
48-
env.Await(
49-
OnceMet(
50-
DiagnosticAt("main.go", 5, 13),
51-
ReadDiagnostics("main.go", d),
52-
),
48+
env.OnceMet(
49+
DiagnosticAt("main.go", 5, 13),
50+
ReadDiagnostics("main.go", d),
5351
)
5452
// Confirm that the diagnostics come from the gc details code lens.
5553
var found bool

gopls/internal/regtest/diagnostics/builtin_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ const (
3030
if !strings.HasSuffix(name, "builtin.go") {
3131
t.Fatalf("jumped to %q, want builtin.go", name)
3232
}
33-
env.Await(OnceMet(
34-
env.DoneWithOpen(),
35-
NoDiagnostics("builtin.go"),
36-
))
33+
env.AfterChange(NoDiagnostics("builtin.go"))
3734
})
3835
}

0 commit comments

Comments
 (0)