| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 32 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 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 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 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 |
|
| 106 |
|
| 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 |
|
| 120 |
|
| 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 |
| 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() |
| 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 |
} |