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