Debian Codegraph @

gopls/internal/util/lru/lru_test.go

raw symbols SCIP
1 // Copyright 2023 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 lru_test
6
7 import (
8 "bytes"
9 cryptorand "crypto/rand"
10 "fmt"
11 "log"
12 mathrand "math/rand"
13 "strings"
14 "testing"
15
16 "golang.org/x/sync/errgroup"
17 "golang.org/x/tools/gopls/internal/util/lru"
18 )
19
20 func TestCache(t *testing.T) {
21 type get struct {
22 key string
23 want string
24 }
25 type set struct {
26 key, value string
27 }
28
29 tests := []struct {
30 label string
31 steps []any
32 }{
33 {"empty cache", []any{
34 get{"a", ""},
35 get{"b", ""},
36 }},
37 {"zero-length string", []any{
38 set{"a", ""},
39 get{"a", ""},
40 }},
41 {"under capacity", []any{
42 set{"a", "123"},
43 set{"b", "456"},
44 get{"a", "123"},
45 get{"b", "456"},
46 }},
47 {"over capacity", []any{
48 set{"a", "123"},
49 set{"b", "456"},
50 set{"c", "78901"},
51 get{"a", ""},
52 get{"b", "456"},
53 get{"c", "78901"},
54 }},
55 {"access ordering", []any{
56 set{"a", "123"},
57 set{"b", "456"},
58 get{"a", "123"},
59 set{"c", "78901"},
60 get{"a", "123"},
61 get{"b", ""},
62 get{"c", "78901"},
63 }},
64 }
65
66 for _, test := range tests {
67 t.Run(test.label, func(t *testing.T) {
68 c := lru.New[string, string](10)
69 for i, step := range test.steps {
70 switch step := step.(type) {
71 case get:
72 if got, _ := c.Get(step.key); got != step.want {
73 t.Errorf("#%d: c.Get(%q) = %q, want %q", i, step.key, got, step.want)
74 }
75 case set:
76 c.Set(step.key, step.value, len(step.value))
77 }
78 }
79 })
80 }
81 }
82
83 // TestConcurrency exercises concurrent access to the same entry.
84 //
85 // It is a copy of TestConcurrency from the filecache package.
86 func TestConcurrency(t *testing.T) {
87 key := uniqueKey()
88 const N = 100 // concurrency level
89
90 // Construct N distinct values, each larger
91 // than a typical 4KB OS file buffer page.
92 var values [N][8192]byte
93 for i := range values {
94 if _, err := mathrand.Read(values[i][:]); err != nil {
95 t.Fatalf("rand: %v", err)
96 }
97 }
98
99 cache := lru.New[[32]byte, []byte](100 * 1e6) // 100MB cache
100
101 // get calls Get and verifies that the cache entry
102 // matches one of the values passed to Set.
103 get := func(mustBeFound bool) error {
104 got, ok := cache.Get(key)
105 if !ok {
106 if !mustBeFound {
107 return nil
108 }
109 return fmt.Errorf("Get did not return a value")
110 }
111 for _, want := range values {
112 if bytes.Equal(want[:], got) {
113 return nil // a match
114 }
115 }
116 return fmt.Errorf("Get returned a value that was never Set")
117 }
118
119 // Perform N concurrent calls to Set and Get.
120 // All sets must succeed.
121 // All gets must return nothing, or one of the Set values;
122 // there is no third possibility.
123 var group errgroup.Group
124 for i := range values {
125 v := values[i][:]
126 group.Go(func() error {
127 cache.Set(key, v, len(v))
128 return nil
129 })
130 group.Go(func() error { return get(false) })
131 }
132 if err := group.Wait(); err != nil {
133 if strings.Contains(err.Error(), "operation not supported") ||
134 strings.Contains(err.Error(), "not implemented") {
135 t.Skipf("skipping: %v", err)
136 }
137 t.Fatal(err)
138 }
139
140 // A final Get must report one of the values that was Set.
141 if err := get(true); err != nil {
142 t.Fatalf("final Get failed: %v", err)
143 }
144 }
145
146 // uniqueKey returns a key that has never been used before.
147 func uniqueKey() (key [32]byte) {
148 if _, err := cryptorand.Read(key[:]); err != nil {
149 log.Fatalf("rand: %v", err)
150 }
151 return
152 }