| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cache |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"go/token" |
| 11 |
"math/bits" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 16 |
"golang.org/x/tools/gopls/internal/file" |
| 17 |
"golang.org/x/tools/gopls/internal/protocol" |
| 18 |
) |
| 19 |
|
| 20 |
func skipIfNoParseCache(t *testing.T) { |
| 21 |
if bits.UintSize == 32 { |
| 22 |
t.Skip("the parse cache is not supported on 32-bit systems") |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
func TestParseCache(t *testing.T) { |
| 27 |
skipIfNoParseCache(t) |
| 28 |
|
| 29 |
ctx := context.Background() |
| 30 |
uri := protocol.DocumentURI("file:///myfile") |
| 31 |
fh := makeFakeFileHandle(uri, []byte("package p\n\nconst _ = \"foo\"")) |
| 32 |
fset := token.NewFileSet() |
| 33 |
|
| 34 |
cache := newParseCache(0) |
| 35 |
pgfs1, err := cache.parseFiles(ctx, fset, parsego.Full, false, fh) |
| 36 |
if err != nil { |
| 37 |
t.Fatal(err) |
| 38 |
} |
| 39 |
pgf1 := pgfs1[0] |
| 40 |
pgfs2, err := cache.parseFiles(ctx, fset, parsego.Full, false, fh) |
| 41 |
pgf2 := pgfs2[0] |
| 42 |
if err != nil { |
| 43 |
t.Fatal(err) |
| 44 |
} |
| 45 |
if pgf1 != pgf2 { |
| 46 |
t.Errorf("parseFiles(%q): unexpected cache miss on repeated call", uri) |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
cache.gcOnce() |
| 51 |
files := []file.Handle{fh} |
| 52 |
files = append(files, dummyFileHandles(parseCacheMinFiles-1)...) |
| 53 |
|
| 54 |
pgfs3, err := cache.parseFiles(ctx, fset, parsego.Full, false, files...) |
| 55 |
if err != nil { |
| 56 |
t.Fatal(err) |
| 57 |
} |
| 58 |
pgf3 := pgfs3[0] |
| 59 |
if pgf3 != pgf1 { |
| 60 |
t.Errorf("parseFiles(%q, ...): unexpected cache miss", uri) |
| 61 |
} |
| 62 |
if pgf3.Tok.Base() != pgf1.Tok.Base() || pgf3.Tok.Size() != pgf1.Tok.Size() { |
| 63 |
t.Errorf("parseFiles(%q, ...): result.Tok has base: %d, size: %d, want (%d, %d)", uri, pgf3.Tok.Base(), pgf3.Tok.Size(), pgf1.Tok.Base(), pgf1.Tok.Size()) |
| 64 |
} |
| 65 |
if tok := fset.File(token.Pos(pgf3.Tok.Base())); tok != pgf3.Tok { |
| 66 |
t.Errorf("parseFiles(%q, ...): result.Tok not contained in FileSet", uri) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
cache.gcOnce() |
| 71 |
files = dummyFileHandles(parseCacheMinFiles) |
| 72 |
_, err = cache.parseFiles(ctx, fset, parsego.Full, false, files...) |
| 73 |
if err != nil { |
| 74 |
t.Fatal(err) |
| 75 |
} |
| 76 |
|
| 77 |
cache.gcOnce() |
| 78 |
pgfs4, err := cache.parseFiles(ctx, fset, parsego.Full, false, fh) |
| 79 |
if err != nil { |
| 80 |
t.Fatal(err) |
| 81 |
} |
| 82 |
if pgfs4[0] == pgf1 { |
| 83 |
t.Errorf("parseFiles(%q): unexpected cache hit after overwriting cache", uri) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func TestParseCache_Reparsing(t *testing.T) { |
| 88 |
skipIfNoParseCache(t) |
| 89 |
|
| 90 |
defer func(padding int) { |
| 91 |
parsePadding = padding |
| 92 |
}(parsePadding) |
| 93 |
parsePadding = 0 |
| 94 |
|
| 95 |
files := dummyFileHandles(parseCacheMinFiles) |
| 96 |
danglingSelector := []byte("package p\nfunc _() {\n\tx.\n}") |
| 97 |
files = append(files, makeFakeFileHandle("file:///bad1", danglingSelector)) |
| 98 |
files = append(files, makeFakeFileHandle("file:///bad2", danglingSelector)) |
| 99 |
|
| 100 |
|
| 101 |
cache := newParseCache(0) |
| 102 |
_, err := cache.parseFiles(context.Background(), token.NewFileSet(), parsego.Full, false, files...) |
| 103 |
if err != nil { |
| 104 |
t.Fatal(err) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func TestParseCache_Issue59097(t *testing.T) { |
| 110 |
skipIfNoParseCache(t) |
| 111 |
|
| 112 |
defer func(padding int) { |
| 113 |
parsePadding = padding |
| 114 |
}(parsePadding) |
| 115 |
parsePadding = 0 |
| 116 |
|
| 117 |
danglingSelector := []byte("package p\nfunc _() {\n\tx.\n}") |
| 118 |
files := []file.Handle{makeFakeFileHandle("file:///bad", danglingSelector)} |
| 119 |
|
| 120 |
|
| 121 |
cache := newParseCache(0) |
| 122 |
_, err := cache.parseFiles(context.Background(), token.NewFileSet(), parsego.Full, false, files...) |
| 123 |
if err != nil { |
| 124 |
t.Fatal(err) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
func TestParseCache_TimeEviction(t *testing.T) { |
| 129 |
skipIfNoParseCache(t) |
| 130 |
|
| 131 |
ctx := context.Background() |
| 132 |
fset := token.NewFileSet() |
| 133 |
uri := protocol.DocumentURI("file:///myfile") |
| 134 |
fh := makeFakeFileHandle(uri, []byte("package p\n\nconst _ = \"foo\"")) |
| 135 |
|
| 136 |
const gcDuration = 10 * time.Millisecond |
| 137 |
cache := newParseCache(gcDuration) |
| 138 |
cache.stop() |
| 139 |
|
| 140 |
pgfs0, err := cache.parseFiles(ctx, fset, parsego.Full, false, fh, fh) |
| 141 |
if err != nil { |
| 142 |
t.Fatal(err) |
| 143 |
} |
| 144 |
|
| 145 |
files := dummyFileHandles(parseCacheMinFiles) |
| 146 |
_, err = cache.parseFiles(ctx, fset, parsego.Full, false, files...) |
| 147 |
if err != nil { |
| 148 |
t.Fatal(err) |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
pgfs1, err := cache.parseFiles(ctx, fset, parsego.Full, false, fh, fh) |
| 153 |
if err != nil { |
| 154 |
t.Fatal(err) |
| 155 |
} |
| 156 |
|
| 157 |
if pgfs0[0] != pgfs1[0] { |
| 158 |
t.Errorf("before GC, got unexpected cache miss") |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
_, err = cache.parseFiles(ctx, fset, parsego.Full, false, files...) |
| 163 |
if err != nil { |
| 164 |
t.Fatal(err) |
| 165 |
} |
| 166 |
time.Sleep(gcDuration) |
| 167 |
cache.gcOnce() |
| 168 |
|
| 169 |
pgfs2, err := cache.parseFiles(ctx, fset, parsego.Full, false, fh, fh) |
| 170 |
if err != nil { |
| 171 |
t.Fatal(err) |
| 172 |
} |
| 173 |
|
| 174 |
if pgfs0[0] == pgfs2[0] { |
| 175 |
t.Errorf("after GC, got unexpected cache hit for %s", pgfs0[0].URI) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func TestParseCache_Duplicates(t *testing.T) { |
| 180 |
skipIfNoParseCache(t) |
| 181 |
|
| 182 |
ctx := context.Background() |
| 183 |
uri := protocol.DocumentURI("file:///myfile") |
| 184 |
fh := makeFakeFileHandle(uri, []byte("package p\n\nconst _ = \"foo\"")) |
| 185 |
|
| 186 |
cache := newParseCache(0) |
| 187 |
pgfs, err := cache.parseFiles(ctx, token.NewFileSet(), parsego.Full, false, fh, fh) |
| 188 |
if err != nil { |
| 189 |
t.Fatal(err) |
| 190 |
} |
| 191 |
if pgfs[0] != pgfs[1] { |
| 192 |
t.Errorf("parseFiles(fh, fh): = [%p, %p], want duplicate files", pgfs[0].File, pgfs[1].File) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
func dummyFileHandles(n int) []file.Handle { |
| 197 |
var fhs []file.Handle |
| 198 |
for i := range n { |
| 199 |
uri := protocol.DocumentURI(fmt.Sprintf("file:///_%d", i)) |
| 200 |
src := fmt.Appendf(nil, "package p\nvar _ = %d", i) |
| 201 |
fhs = append(fhs, makeFakeFileHandle(uri, src)) |
| 202 |
} |
| 203 |
return fhs |
| 204 |
} |
| 205 |
|
| 206 |
func makeFakeFileHandle(uri protocol.DocumentURI, src []byte) fakeFileHandle { |
| 207 |
return fakeFileHandle{ |
| 208 |
uri: uri, |
| 209 |
data: src, |
| 210 |
hash: file.HashOf(src), |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
type fakeFileHandle struct { |
| 215 |
file.Handle |
| 216 |
uri protocol.DocumentURI |
| 217 |
data []byte |
| 218 |
hash file.Hash |
| 219 |
} |
| 220 |
|
| 221 |
func (h fakeFileHandle) String() string { |
| 222 |
return h.uri.Path() |
| 223 |
} |
| 224 |
|
| 225 |
func (h fakeFileHandle) URI() protocol.DocumentURI { |
| 226 |
return h.uri |
| 227 |
} |
| 228 |
|
| 229 |
func (h fakeFileHandle) Content() ([]byte, error) { |
| 230 |
return h.data, nil |
| 231 |
} |
| 232 |
|
| 233 |
func (h fakeFileHandle) Identity() file.Identity { |
| 234 |
return file.Identity{ |
| 235 |
URI: h.uri, |
| 236 |
Hash: h.hash, |
| 237 |
} |
| 238 |
} |