mirror of
https://github.com/golang/go
synced 2024-11-18 16:04:44 -07:00
5fb17a1e7b
For tests (and perhaps later, for daemon discovery), unix domain sockets offer advantages over TCP: we can know the exact socket address that will be used when starting a server subprocess. They also offer performance and security advantages over TCP, and were specifically requested on golang.org/issues/34111. This CL adds support for listening on UDS, and uses this to implement an additional regtest environment mode that starts up an external process. This mode is disabled by default, but may be enabled by the -enable_gopls_subprocess_tests. The regtest TestMain may be hijacked to instead run as gopls, if a special environment variable is set. This allows the the test runner to start a separate process by using os.Argv[0]. The -gopls_test_binary flag may be used to point tests at a separate gopls binary. Updates golang/go#36879 Updates golang/go#34111 Change-Id: I1cfdf55040e81ffa69a6726878a96529e5522e82 Reviewed-on: https://go-review.googlesource.com/c/tools/+/218839 Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
// Copyright 2020 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package regtest
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"golang.org/x/tools/internal/lsp/fake"
|
|
)
|
|
|
|
const sharedProgram = `
|
|
-- go.mod --
|
|
module mod
|
|
|
|
go 1.12
|
|
-- main.go --
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
fmt.Println("Hello World.")
|
|
}`
|
|
|
|
func runShared(t *testing.T, program string, testFunc func(ctx context.Context, t *testing.T, env1 *Env, env2 *Env)) {
|
|
// Only run these tests in forwarded modes.
|
|
modes := runner.Modes() & (Forwarded | SeparateProcess)
|
|
runner.RunInMode(modes, t, sharedProgram, func(ctx context.Context, t *testing.T, env1 *Env) {
|
|
// Create a second test session connected to the same workspace and server
|
|
// as the first.
|
|
env2 := NewEnv(ctx, t, env1.W, env1.Server)
|
|
testFunc(ctx, t, env1, env2)
|
|
})
|
|
}
|
|
|
|
func TestSimultaneousEdits(t *testing.T) {
|
|
t.Parallel()
|
|
runShared(t, exampleProgram, func(ctx context.Context, t *testing.T, env1 *Env, env2 *Env) {
|
|
// In editor #1, break fmt.Println as before.
|
|
edit1 := fake.NewEdit(5, 11, 5, 12, "")
|
|
env1.OpenFile("main.go")
|
|
env1.EditBuffer("main.go", edit1)
|
|
// In editor #2 remove the closing brace.
|
|
edit2 := fake.NewEdit(6, 0, 6, 1, "")
|
|
env2.OpenFile("main.go")
|
|
env2.EditBuffer("main.go", edit2)
|
|
|
|
// Now check that we got different diagnostics in each environment.
|
|
env1.Await(DiagnosticAt("main.go", 5, 5))
|
|
env2.Await(DiagnosticAt("main.go", 7, 0))
|
|
})
|
|
}
|
|
|
|
func TestShutdown(t *testing.T) {
|
|
t.Parallel()
|
|
runShared(t, sharedProgram, func(ctx context.Context, t *testing.T, env1 *Env, env2 *Env) {
|
|
env1.CloseEditor()
|
|
// Now make an edit in editor #2 to trigger diagnostics.
|
|
edit2 := fake.NewEdit(6, 0, 6, 1, "")
|
|
env2.OpenFile("main.go")
|
|
env2.EditBuffer("main.go", edit2)
|
|
env2.Await(DiagnosticAt("main.go", 7, 0))
|
|
})
|
|
}
|