|
| 1 | +// Copyright 2022 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 misc |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/google/go-cmp/cmp" |
| 12 | + "golang.org/x/tools/gopls/internal/hooks" |
| 13 | + "golang.org/x/tools/gopls/internal/lsp/cache" |
| 14 | + "golang.org/x/tools/gopls/internal/lsp/debug" |
| 15 | + "golang.org/x/tools/gopls/internal/lsp/fake" |
| 16 | + "golang.org/x/tools/gopls/internal/lsp/lsprpc" |
| 17 | + . "golang.org/x/tools/gopls/internal/lsp/regtest" |
| 18 | + "golang.org/x/tools/internal/jsonrpc2" |
| 19 | + "golang.org/x/tools/internal/jsonrpc2/servertest" |
| 20 | +) |
| 21 | + |
| 22 | +// Test for golang/go#57222. |
| 23 | +func TestCacheLeak(t *testing.T) { |
| 24 | + const files = `-- a.go -- |
| 25 | +package a |
| 26 | +
|
| 27 | +func _() { |
| 28 | + println("1") |
| 29 | +} |
| 30 | +` |
| 31 | + c := cache.New(nil, nil) |
| 32 | + env := setupEnv(t, files, c) |
| 33 | + env.Await(InitialWorkspaceLoad) |
| 34 | + env.OpenFile("a.go") |
| 35 | + |
| 36 | + // Make a couple edits to stabilize cache state. |
| 37 | + // |
| 38 | + // For some reason, after only one edit we're left with two parsed files |
| 39 | + // (perhaps because something had to ParseHeader). If this test proves flaky, |
| 40 | + // we'll need to investigate exactly what is causing various parse modes to |
| 41 | + // be present (or rewrite the test to be more tolerant, for example make ~100 |
| 42 | + // modifications and assert that we're within a few of where we're started). |
| 43 | + env.RegexpReplace("a.go", "1", "2") |
| 44 | + env.RegexpReplace("a.go", "2", "3") |
| 45 | + env.AfterChange() |
| 46 | + |
| 47 | + // Capture cache state, make an arbitrary change, and wait for gopls to do |
| 48 | + // its work. Afterward, we should have the exact same number of parsed |
| 49 | + before := c.MemStats() |
| 50 | + env.RegexpReplace("a.go", "3", "4") |
| 51 | + env.AfterChange() |
| 52 | + after := c.MemStats() |
| 53 | + |
| 54 | + if diff := cmp.Diff(before, after); diff != "" { |
| 55 | + t.Errorf("store objects differ after change (-before +after)\n%s", diff) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// setupEnv creates a new sandbox environment for editing the txtar encoded |
| 60 | +// content of files. It uses a new gopls instance backed by the Cache c. |
| 61 | +func setupEnv(t *testing.T, files string, c *cache.Cache) *Env { |
| 62 | + ctx := debug.WithInstance(context.Background(), "", "off") |
| 63 | + server := lsprpc.NewStreamServer(c, false, hooks.Options) |
| 64 | + ts := servertest.NewPipeServer(server, jsonrpc2.NewRawStream) |
| 65 | + s, err := fake.NewSandbox(&fake.SandboxConfig{ |
| 66 | + Files: fake.UnpackTxt(files), |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + |
| 72 | + a := NewAwaiter(s.Workdir) |
| 73 | + e, err := fake.NewEditor(s, fake.EditorConfig{}).Connect(ctx, ts, a.Hooks()) |
| 74 | + |
| 75 | + return &Env{ |
| 76 | + T: t, |
| 77 | + Ctx: ctx, |
| 78 | + Editor: e, |
| 79 | + Sandbox: s, |
| 80 | + Awaiter: a, |
| 81 | + } |
| 82 | +} |
0 commit comments