Debian Codegraph @

gopls/internal/cache/future_test.go

raw symbols SCIP
1 // Copyright 2024 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 cache
6
7 import (
8 "context"
9 "fmt"
10 "sync/atomic"
11 "testing"
12 "time"
13
14 "golang.org/x/sync/errgroup"
15 )
16
17 func TestFutureCache_Persistent(t *testing.T) {
18 c := newFutureCache[int, int](true)
19 ctx := context.Background()
20
21 var computed atomic.Int32
22 compute := func(i int) cacheFunc[int] {
23 return func(context.Context) (int, error) {
24 computed.Add(1)
25 return i, ctx.Err()
26 }
27 }
28
29 testFutureCache(t, ctx, c, compute)
30
31 // Since this cache is persistent, we should get exactly 10 computations,
32 // since there are 10 distinct keys in [testFutureCache].
33 if got := computed.Load(); got != 10 {
34 t.Errorf("computed %d times, want 10", got)
35 }
36 }
37
38 func TestFutureCache_Ephemeral(t *testing.T) {
39 c := newFutureCache[int, int](false)
40 ctx := context.Background()
41
42 var computed atomic.Int32
43 compute := func(i int) cacheFunc[int] {
44 return func(context.Context) (int, error) {
45 time.Sleep(1 * time.Millisecond)
46 computed.Add(1)
47 return i, ctx.Err()
48 }
49 }
50
51 testFutureCache(t, ctx, c, compute)
52
53 // Since this cache is ephemeral, we should get at least 30 computations,
54 // since there are 10 distinct keys and three synchronous passes in
55 // [testFutureCache].
56 if got := computed.Load(); got < 30 {
57 t.Errorf("computed %d times, want at least 30", got)
58 } else {
59 t.Logf("compute ran %d times", got)
60 }
61 }
62
63 // testFutureCache starts 100 goroutines concurrently, indexed by j, each
64 // getting key j%10 from the cache. It repeats this three times, synchronizing
65 // after each.
66 //
67 // This is designed to exercise both concurrent and synchronous access to the
68 // cache.
69 func testFutureCache(t *testing.T, ctx context.Context, c *futureCache[int, int], compute func(int) cacheFunc[int]) {
70 for range 3 {
71 var g errgroup.Group
72 for j := range 100 {
73 mod := j % 10
74 compute := compute(mod)
75 g.Go(func() error {
76 got, err := c.get(ctx, mod, compute)
77 if err == nil && got != mod {
78 t.Errorf("get() = %d, want %d", got, mod)
79 }
80 return err
81 })
82 }
83 if err := g.Wait(); err != nil {
84 t.Fatal(err)
85 }
86 }
87 }
88
89 func TestFutureCache_Retrying(t *testing.T) {
90 // This test verifies the retry behavior of cache entries,
91 // by checking that cancelled work is handed off to the next awaiter.
92 //
93 // The setup is a little tricky: 10 goroutines are started, and the first 9
94 // are cancelled whereas the 10th is allowed to finish. As a result, the
95 // computation should always succeed with value 9.
96
97 ctx := context.Background()
98
99 for _, persistent := range []bool{true, false} {
100 t.Run(fmt.Sprintf("persistent=%t", persistent), func(t *testing.T) {
101 c := newFutureCache[int, int](persistent)
102
103 var started atomic.Int32
104
105 // compute returns a new cacheFunc that produces the value i, after the
106 // provided done channel is closed.
107 compute := func(i int, done <-chan struct{}) cacheFunc[int] {
108 return func(ctx context.Context) (int, error) {
109 started.Add(1)
110 select {
111 case <-ctx.Done():
112 return 0, ctx.Err()
113 case <-done:
114 return i, nil
115 }
116 }
117 }
118
119 // goroutines are either cancelled, or allowed to complete,
120 // as controlled by cancels and dones.
121 var (
122 cancels = make([]func(), 10)
123 dones = make([]chan struct{}, 10)
124 )
125
126 var g errgroup.Group
127 var lastValue atomic.Int32 // keep track of the last successfully computed value
128 for i := range 10 {
129 ctx, cancel := context.WithCancel(ctx)
130 done := make(chan struct{})
131 cancels[i] = cancel
132 dones[i] = done
133 compute := compute(i, done)
134 g.Go(func() error {
135 v, err := c.get(ctx, 0, compute)
136 if err == nil {
137 lastValue.Store(int32(v))
138 }
139 return nil
140 })
141 }
142 for _, cancel := range cancels[:9] {
143 cancel()
144 }
145 defer cancels[9]()
146
147 dones[9] <- struct{}{}
148 _ = g.Wait() // can't fail
149
150 t.Logf("started %d computations", started.Load())
151 if got := lastValue.Load(); got != 9 {
152 t.Errorf("after cancelling computation 0-8, got %d, want 9", got)
153 }
154 })
155 }
156 }