Skip to content

Commit

Permalink
internal/lsp/regtest: rename and document some symbols
Browse files Browse the repository at this point in the history
Minor cleanup for the regtest package:
 - EnvMode is renamed to Mode, because it's really a server mode and not
   directly related to the Env type.
 - Modes are better documented.

Change-Id: Ia3aedfc70b665ea75a66731a72e4e87ae79db298
Reviewed-on: https://go-review.googlesource.com/c/tools/+/229781
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
  • Loading branch information
findleyr committed Apr 28, 2020
1 parent 317da45 commit 12a1c85
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
45 changes: 20 additions & 25 deletions internal/lsp/regtest/runner.go
Expand Up @@ -28,20 +28,21 @@ import (
"golang.org/x/tools/internal/lsp/protocol"
)

// EnvMode is a bitmask that defines in which execution environments a test
// should run.
type EnvMode int
// Mode is a bitmask that defines for which execution modes a test should run.
type Mode int

const (
// Singleton mode uses a separate cache for each test.
Singleton EnvMode = 1 << iota
// Singleton mode uses a separate in-process gopls instance for each test,
// and communicates over pipes to mimic the gopls sidecar execution mode,
// which communicates over stdin/stderr.
Singleton Mode = 1 << iota

// Forwarded forwards connections to an in-process gopls instance.
// Forwarded forwards connections to a shared in-process gopls instance.
Forwarded
// SeparateProcess runs a separate gopls process, and forwards connections to
// it.
// SeparateProcess forwards connection to a shared separate gopls process.
SeparateProcess
// NormalModes runs tests in all modes.
// NormalModes are the global default execution modes, when unmodified by
// test flags or by individual test options.
NormalModes = Singleton | Forwarded
)

Expand All @@ -50,7 +51,7 @@ const (
// remote), any tests that execute on the same Runner will share the same
// state.
type Runner struct {
DefaultModes EnvMode
DefaultModes Mode
Timeout time.Duration
GoplsPath string
AlwaysPrintLogs bool
Expand All @@ -64,12 +65,6 @@ type Runner struct {
closers []io.Closer
}

// Modes returns the bitmask of environment modes this runner is configured to
// test.
func (r *Runner) Modes() EnvMode {
return r.DefaultModes
}

// getTestServer gets the test server instance to connect to, or creates one if
// it doesn't exist.
func (r *Runner) getTestServer() *servertest.TCPServer {
Expand Down Expand Up @@ -129,7 +124,7 @@ func (r *Runner) AddCloser(closer io.Closer) {
}

type runConfig struct {
modes EnvMode
modes Mode
proxyTxt string
timeout time.Duration
env []string
Expand Down Expand Up @@ -168,7 +163,7 @@ func WithProxy(txt string) RunOption {
}

// WithModes configures the execution modes that the test should run in.
func WithModes(modes EnvMode) RunOption {
func WithModes(modes Mode) RunOption {
return optionSetter(func(opts *runConfig) {
opts.modes = modes
})
Expand All @@ -194,12 +189,12 @@ func (r *Runner) Run(t *testing.T, filedata string, test func(t *testing.T, e *E

tests := []struct {
name string
mode EnvMode
mode Mode
getServer func(context.Context, *testing.T) jsonrpc2.StreamServer
}{
{"singleton", Singleton, singletonEnv},
{"forwarded", Forwarded, r.forwardedEnv},
{"separate_process", SeparateProcess, r.separateProcessEnv},
{"singleton", Singleton, singletonServer},
{"forwarded", Forwarded, r.forwardedServer},
{"separate_process", SeparateProcess, r.separateProcessServer},
}

for _, tc := range tests {
Expand Down Expand Up @@ -274,16 +269,16 @@ func (s *loggingServer) printBuffers(testname string, w io.Writer) {
}
}

func singletonEnv(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
func singletonServer(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
return lsprpc.NewStreamServer(cache.New(ctx, nil))
}

func (r *Runner) forwardedEnv(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
func (r *Runner) forwardedServer(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
ts := r.getTestServer()
return lsprpc.NewForwarder("tcp", ts.Addr)
}

func (r *Runner) separateProcessEnv(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
func (r *Runner) separateProcessServer(ctx context.Context, t *testing.T) jsonrpc2.StreamServer {
// TODO(rfindley): can we use the autostart behavior here, instead of
// pre-starting the remote?
socket := r.getRemoteSocket(t)
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/regtest/shared_test.go
Expand Up @@ -24,7 +24,7 @@ func main() {

func runShared(t *testing.T, program string, testFunc func(env1 *Env, env2 *Env)) {
// Only run these tests in forwarded modes.
modes := runner.Modes() & (Forwarded | SeparateProcess)
modes := runner.DefaultModes & (Forwarded | SeparateProcess)
runner.Run(t, sharedProgram, func(t *testing.T, env1 *Env) {
// Create a second test session connected to the same workspace and server
// as the first.
Expand Down

0 comments on commit 12a1c85

Please sign in to comment.