|
| 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 | + |
| 5 | +package regtest |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "golang.org/x/tools/internal/lsp/fake" |
| 12 | +) |
| 13 | + |
| 14 | +const sharedProgram = ` |
| 15 | +-- go.mod -- |
| 16 | +module mod |
| 17 | +
|
| 18 | +go 1.12 |
| 19 | +-- main.go -- |
| 20 | +package main |
| 21 | +
|
| 22 | +import "fmt" |
| 23 | +
|
| 24 | +func main() { |
| 25 | + fmt.Println("Hello World.") |
| 26 | +}` |
| 27 | + |
| 28 | +func runShared(t *testing.T, program string, testFunc func(ctx context.Context, t *testing.T, env1 *Env, env2 *Env)) { |
| 29 | + runner.RunInMode(Forwarded, t, sharedProgram, func(ctx context.Context, t *testing.T, env1 *Env) { |
| 30 | + // Create a second test session connected to the same workspace and server |
| 31 | + // as the first. |
| 32 | + env2 := NewEnv(ctx, t, env1.W, env1.Server) |
| 33 | + testFunc(ctx, t, env1, env2) |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +func TestSimultaneousEdits(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + runner.Run(t, exampleProgram, func(ctx context.Context, t *testing.T, env1 *Env) { |
| 40 | + // Create a second test session connected to the same workspace and server |
| 41 | + // as the first. |
| 42 | + env2 := NewEnv(ctx, t, env1.W, env1.Server) |
| 43 | + |
| 44 | + // In editor #1, break fmt.Println as before. |
| 45 | + edit1 := fake.NewEdit(5, 11, 5, 12, "") |
| 46 | + env1.OpenFile("main.go") |
| 47 | + env1.EditBuffer("main.go", edit1) |
| 48 | + // In editor #2 remove the closing brace. |
| 49 | + edit2 := fake.NewEdit(6, 0, 6, 1, "") |
| 50 | + env2.OpenFile("main.go") |
| 51 | + env2.EditBuffer("main.go", edit2) |
| 52 | + |
| 53 | + // Now check that we got different diagnostics in each environment. |
| 54 | + env1.Await(DiagnosticAt("main.go", 5, 5)) |
| 55 | + env2.Await(DiagnosticAt("main.go", 7, 0)) |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func TestShutdown(t *testing.T) { |
| 60 | + t.Parallel() |
| 61 | + runShared(t, sharedProgram, func(ctx context.Context, t *testing.T, env1 *Env, env2 *Env) { |
| 62 | + env1.CloseEditor() |
| 63 | + // Now make an edit in editor #2 to trigger diagnostics. |
| 64 | + edit2 := fake.NewEdit(6, 0, 6, 1, "") |
| 65 | + env2.OpenFile("main.go") |
| 66 | + env2.EditBuffer("main.go", edit2) |
| 67 | + env2.Await(DiagnosticAt("main.go", 7, 0)) |
| 68 | + }) |
| 69 | +} |
0 commit comments