1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package filecache_test
// This file defines tests of the API of the filecache package.
//
// Some properties (e.g. garbage collection) cannot be exercised
// through the API, so this test does not attempt to do so.
import (
"bytes"
cryptorand "crypto/rand"
"fmt"
"log"
mathrand "math/rand"
"os"
"os/exec"
"strconv"
"testing"
"golang.org/x/sync/errgroup"
"golang.org/x/tools/gopls/internal/lsp/filecache"
)
func TestBasics(t *testing.T) {
const kind = "TestBasics"
key := uniqueKey() // never used before
value := []byte("hello")
// Get of a never-seen key returns not found.
if _, err := filecache.Get(kind, key); err != filecache.ErrNotFound {
t.Errorf("Get of random key returned err=%q, want not found", err)
}
// Set of a never-seen key and a small value succeeds.
if err := filecache.Set(kind, key, value); err != nil {
t.Errorf("Set failed: %v", err)
}
// Get of the key returns a copy of the value.
if got, err := filecache.Get(kind, key); err != nil {
t.Errorf("Get after Set failed: %v", err)
} else if string(got) != string(value) {
t.Errorf("Get after Set returned different value: got %q, want %q", got, value)
}
// The kind is effectively part of the key.
if _, err := filecache.Get("different-kind", key); err != filecache.ErrNotFound {
t.Errorf("Get with wrong kind returned err=%q, want not found", err)
}
}
// TestConcurrency exercises concurrent access to the same entry.
func TestConcurrency(t *testing.T) {
const kind = "TestConcurrency"
key := uniqueKey()
const N = 100 // concurrency level
// Construct N distinct values, each larger
// than a typical 4KB OS file buffer page.
var values [N][8192]byte
for i := range values {
if _, err := mathrand.Read(values[i][:]); err != nil {
t.Fatalf("rand: %v", err)
}
}
// get calls Get and verifies that the cache entry
// matches one of the values passed to Set.
get := func(mustBeFound bool) error {
got, err := filecache.Get(kind, key)
if err != nil {
if err == filecache.ErrNotFound && !mustBeFound {
return nil // not found
}
return err
}
for _, want := range values {
if bytes.Equal(want[:], got) {
return nil // a match
}
}
return fmt.Errorf("Get returned a value that was never Set")
}
// Perform N concurrent calls to Set and Get.
// All sets must succeed.
// All gets must return nothing, or one of the Set values;
// there is no third possibility.
var group errgroup.Group
for i := range values {
i := i
group.Go(func() error { return filecache.Set(kind, key, values[i][:]) })
group.Go(func() error { return get(false) })
}
if err := group.Wait(); err != nil {
t.Fatal(err)
}
// A final Get must report one of the values that was Set.
if err := get(true); err != nil {
t.Fatalf("final Get failed: %v", err)
}
}
const (
testIPCKind = "TestIPC"
testIPCValueA = "hello"
testIPCValueB = "world"
)
// TestIPC exercises interprocess communication through the cache.
// It calls Set(A) in the parent, { Get(A); Set(B) } in the child
// process, then Get(B) in the parent.
func TestIPC(t *testing.T) {
keyA := uniqueKey()
keyB := uniqueKey()
value := []byte(testIPCValueA)
// Set keyA.
if err := filecache.Set(testIPCKind, keyA, value); err != nil {
t.Fatalf("Set: %v", err)
}
// Call ipcChild in a child process,
// passing it the keys in the environment
// (quoted, to avoid NUL termination of C strings).
// It will Get(A) then Set(B).
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Env = append(os.Environ(),
"ENTRYPOINT=ipcChild",
fmt.Sprintf("KEYA=%q", keyA),
fmt.Sprintf("KEYB=%q", keyB))
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
// Verify keyB.
got, err := filecache.Get(testIPCKind, keyB)
if err != nil {
t.Fatal(err)
}
if string(got) != "world" {
t.Fatalf("Get(keyB) = %q, want %q", got, "world")
}
}
// We define our own main function so that portions of
// some tests can run in a separate (child) process.
func TestMain(m *testing.M) {
switch os.Getenv("ENTRYPOINT") {
case "ipcChild":
ipcChild()
default:
os.Exit(m.Run())
}
}
// ipcChild is the portion of TestIPC that runs in a child process.
func ipcChild() {
getenv := func(name string) (key [32]byte) {
s, _ := strconv.Unquote(os.Getenv(name))
copy(key[:], []byte(s))
return
}
// Verify key A.
got, err := filecache.Get(testIPCKind, getenv("KEYA"))
if err != nil || string(got) != testIPCValueA {
log.Fatalf("child: Get(key) = %q, %v; want %q", got, err, testIPCValueA)
}
// Set key B.
if err := filecache.Set(testIPCKind, getenv("KEYB"), []byte(testIPCValueB)); err != nil {
log.Fatalf("child: Set(keyB) failed: %v", err)
}
}
// uniqueKey returns a key that has never been used before.
func uniqueKey() (key [32]byte) {
if _, err := cryptorand.Read(key[:]); err != nil {
log.Fatalf("rand: %v", err)
}
return
}
|