| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "os" |
| 10 | "strings" |
| 11 | "sync" |
| 12 | "sync/atomic" |
| 13 | "testing" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | func refOf(b []byte) string { |
| 18 | s := sha256.Sum256(b) |
| 19 | return hex.EncodeToString(s[:]) |
| 20 | } |
| 21 | |
| 22 | // reader is a load that cannot seek, counting how often it is asked. |
| 23 | func reader(b []byte, loads *atomic.Int32) func() (string, io.Reader, func(), error) { |
| 24 | return func() (string, io.Reader, func(), error) { |
| 25 | loads.Add(1) |
| 26 | return "video/webm", io.NopCloser(bytes.NewReader(b)), func() {}, nil |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func getAll(t *testing.T, c *clipCache, key string, verify bool, load func() (string, io.Reader, func(), error)) []byte { |
| 31 | t.Helper() |
| 32 | body, kind, done, err := c.get(key, verify, load) |
| 33 | if err != nil { |
| 34 | t.Fatalf("get %s: %v", key[:6], err) |
| 35 | } |
| 36 | defer done() |
| 37 | if kind != "video/webm" { |
| 38 | t.Errorf("kind %q", kind) |
| 39 | } |
| 40 | got, _ := io.ReadAll(body) |
| 41 | return got |
| 42 | } |
| 43 | |
| 44 | // TestTheClipCacheFillsOnceAndReusesIt: one entry per key, filled by |
| 45 | // one load whoever asks and however many at once; asked again, the |
| 46 | // completed entry is used and nothing is loaded. |
| 47 | func TestTheClipCacheFillsOnceAndReusesIt(t *testing.T) { |
| 48 | c := newClipCache("", 1<<20) |
| 49 | defer c.close() |
| 50 | v := bytes.Repeat([]byte("v"), 5000) |
| 51 | var loads atomic.Int32 |
| 52 | slow := func() (string, io.Reader, func(), error) { |
| 53 | time.Sleep(50 * time.Millisecond) |
| 54 | return reader(v, &loads)() |
| 55 | } |
| 56 | var wg sync.WaitGroup |
| 57 | for i := 0; i < 8; i++ { |
| 58 | wg.Add(1) |
| 59 | go func() { |
| 60 | defer wg.Done() |
| 61 | if got := getAll(t, c, refOf(v), true, slow); !bytes.Equal(got, v) { |
| 62 | t.Error("a waiter read the wrong bytes") |
| 63 | } |
| 64 | }() |
| 65 | } |
| 66 | wg.Wait() |
| 67 | for i := 0; i < 3; i++ { |
| 68 | if got := getAll(t, c, refOf(v), true, reader(v, &loads)); !bytes.Equal(got, v) { |
| 69 | t.Fatal("the cached entry is not the clip") |
| 70 | } |
| 71 | } |
| 72 | if n := loads.Load(); n != 1 { |
| 73 | t.Errorf("loaded %d times, want once", n) |
| 74 | } |
| 75 | // A staged key is not a hash of its bytes, and reused just the same. |
| 76 | var staged atomic.Int32 |
| 77 | for i := 0; i < 2; i++ { |
| 78 | if got := getAll(t, c, "staged-abc", false, reader(v, &staged)); !bytes.Equal(got, v) { |
| 79 | t.Fatal("the staged entry is not the clip") |
| 80 | } |
| 81 | } |
| 82 | if n := staged.Load(); n != 1 { |
| 83 | t.Errorf("the staged clip loaded %d times, want once", n) |
| 84 | } |
| 85 | // Bytes that are not their ref are refused and never kept. |
| 86 | var wrong atomic.Int32 |
| 87 | if _, _, _, err := c.get(strings.Repeat("ab", 32), true, reader(v, &wrong)); !errors.Is(err, errClipChanged) { |
| 88 | t.Errorf("the wrong bytes: %v", err) |
| 89 | } |
| 90 | if _, ok := c.entries[strings.Repeat("ab", 32)]; ok { |
| 91 | t.Error("the wrong bytes were kept") |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // TestTheClipCacheEvictsWithoutBreakingWhatIsBeingServed: past the cap |
| 96 | // the least recently served goes, and a response already reading it |
| 97 | // reads it to the end. |
| 98 | func TestTheClipCacheEvictsWithoutBreakingWhatIsBeingServed(t *testing.T) { |
| 99 | c := newClipCache("", 100) |
| 100 | var loads atomic.Int32 |
| 101 | a, b, d := bytes.Repeat([]byte("a"), 40), bytes.Repeat([]byte("b"), 40), bytes.Repeat([]byte("d"), 40) |
| 102 | held, _, done, err := c.get(refOf(a), true, reader(a, &loads)) |
| 103 | if err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | first := make([]byte, 10) |
| 107 | if _, err := io.ReadFull(held, first); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | getAll(t, c, refOf(b), true, reader(b, &loads)) |
| 111 | getAll(t, c, refOf(d), true, reader(d, &loads)) // a is the least recently served: gone |
| 112 | if _, ok := c.entries[refOf(a)]; ok { |
| 113 | t.Error("a was kept past the cap") |
| 114 | } |
| 115 | rest, err := io.ReadAll(held) |
| 116 | done() |
| 117 | if err != nil || !bytes.Equal(append(first, rest...), a) { |
| 118 | t.Errorf("the response serving a read %q, %v", append(first, rest...), err) |
| 119 | } |
| 120 | for _, v := range [][]byte{b, d} { |
| 121 | if got := getAll(t, c, refOf(v), true, reader(v, &loads)); !bytes.Equal(got, v) { |
| 122 | t.Errorf("%q was lost", v[:1]) |
| 123 | } |
| 124 | } |
| 125 | if loads.Load() != 3 || c.total != 80 { |
| 126 | t.Errorf("loads %d, total %d; want 3 and 80", loads.Load(), c.total) |
| 127 | } |
| 128 | dir := c.dir |
| 129 | c.close() |
| 130 | if _, err := os.Stat(dir); !os.IsNotExist(err) { |
| 131 | t.Errorf("the cache's directory outlived it: %v", err) |
| 132 | } |
| 133 | newClipCache("", 100).close() |
| 134 | } |
| 135 | |
| 136 | // noSeek is a store reader that says it seeks and cannot: every Seek |
| 137 | // fails. |
| 138 | type noSeek struct{ *bytes.Reader } |
| 139 | |
| 140 | func (noSeek) Seek(int64, int) (int64, error) { return 0, errors.New("no seeking here") } |
| 141 | func (noSeek) Close() error { return nil } |
| 142 | |
| 143 | // TestASniffedClipKeepsItsHeadWhenItCannotRewind: the head read to |
| 144 | // sniff a clip is part of its bytes. When the reader cannot be rewound |
| 145 | // it is put back in front of the rest, so a registered clip still |
| 146 | // matches its ref and a staged one comes back whole. |
| 147 | func TestASniffedClipKeepsItsHeadWhenItCannotRewind(t *testing.T) { |
| 148 | v := append([]byte{0, 0, 0, 0x18, 'f', 't', 'y', 'p', 'i', 's', 'o', 'm', 0, 0, 0, 0, 'i', 's', 'o', 'm', 'm', 'p', '4', '1'}, |
| 149 | bytes.Repeat([]byte("v"), 5000)...) |
| 150 | load := func() (string, io.Reader, func(), error) { return sniffedVideo(noSeek{bytes.NewReader(v)}) } |
| 151 | kind, r, done, err := load() |
| 152 | if err != nil || kind != "video/mp4" { |
| 153 | t.Fatalf("sniffed %q, %v; want video/mp4", kind, err) |
| 154 | } |
| 155 | got, _ := io.ReadAll(r) |
| 156 | done() |
| 157 | if !bytes.Equal(got, v) { |
| 158 | t.Errorf("read %d bytes back, want the whole %d", len(got), len(v)) |
| 159 | } |
| 160 | c := newClipCache("", 1<<20) |
| 161 | defer c.close() |
| 162 | for _, key := range []string{refOf(v), "staged-abc"} { |
| 163 | body, _, release, err := c.get(key, key == refOf(v), load) |
| 164 | if err != nil { |
| 165 | t.Fatalf("get %s: %v", key[:6], err) |
| 166 | } |
| 167 | got, _ := io.ReadAll(body) |
| 168 | release() |
| 169 | if !bytes.Equal(got, v) { |
| 170 | t.Errorf("%s: the cache holds %d bytes, want the whole %d", key[:6], len(got), len(v)) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |