(t *testing.T)
| 12 | ) |
| 13 | |
| 14 | func TestVersioned(t *testing.T) { |
| 15 | t.Run("happy case: can store, retrieve and delete", func(t *testing.T) { |
| 16 | cache := NewMockCache() |
| 17 | v1 := NewVersioned(cache, 1, log.NewNopLogger()) |
| 18 | data := map[string][]byte{"hit": []byte(`data`)} |
| 19 | v1.SetMultiAsync(data, time.Minute) |
| 20 | res := v1.GetMulti(context.Background(), []string{"hit", "miss"}) |
| 21 | assert.Equal(t, data, res) |
| 22 | err := v1.Delete(context.Background(), "hit") |
| 23 | require.NoError(t, err) |
| 24 | res = v1.GetMulti(context.Background(), []string{"hit", "miss"}) |
| 25 | assert.Equal(t, map[string][]uint8{}, res) |
| 26 | }) |
| 27 | |
| 28 | t.Run("different versions use different datasets", func(t *testing.T) { |
| 29 | cache := NewMockCache() |
| 30 | v1 := NewVersioned(cache, 1, log.NewNopLogger()) |
| 31 | v1Data := map[string][]byte{"hit": []byte(`first version`)} |
| 32 | v1.SetMultiAsync(v1Data, time.Minute) |
| 33 | v2 := NewVersioned(cache, 2, log.NewNopLogger()) |
| 34 | v2Data := map[string][]byte{"hit": []byte(`second version`)} |
| 35 | v2.SetMultiAsync(v2Data, time.Minute) |
| 36 | |
| 37 | resV1 := v1.GetMulti(context.Background(), []string{"hit", "miss"}) |
| 38 | assert.Equal(t, v1Data, resV1) |
| 39 | resV2 := v2.GetMulti(context.Background(), []string{"hit", "miss"}) |
| 40 | assert.Equal(t, v2Data, resV2) |
| 41 | |
| 42 | err := v1.Delete(context.Background(), "hit") |
| 43 | require.NoError(t, err) |
| 44 | resV1 = v1.GetMulti(context.Background(), []string{"hit", "miss"}) |
| 45 | assert.Equal(t, map[string][]uint8{}, resV1) |
| 46 | resV2 = v2.GetMulti(context.Background(), []string{"hit", "miss"}) |
| 47 | assert.Equal(t, v2Data, resV2, "v2 cached data should still retain the data") |
| 48 | err = v2.Delete(context.Background(), "hit") |
| 49 | require.NoError(t, err) |
| 50 | resV2 = v2.GetMulti(context.Background(), []string{"hit", "miss"}) |
| 51 | assert.Equal(t, map[string][]uint8{}, resV2) |
| 52 | }) |
| 53 | |
| 54 | t.Run("GetMultiWithError works correctly", func(t *testing.T) { |
| 55 | cache := NewMockCache() |
| 56 | v1 := NewVersioned(cache, 1, log.NewNopLogger()) |
| 57 | data := map[string][]byte{"hit": []byte(`data`)} |
| 58 | v1.SetMultiAsync(data, time.Minute) |
| 59 | res, err := v1.GetMultiWithError(context.Background(), []string{"hit", "miss"}) |
| 60 | require.NoError(t, err) |
| 61 | assert.Equal(t, data, res) |
| 62 | }) |
| 63 | |
| 64 | t.Run("GetMultiWithError propagates backend errors", func(t *testing.T) { |
| 65 | backendErr := errors.New("backend error") |
| 66 | backend := NewErroringMockCache(backendErr) |
| 67 | v1 := NewVersioned(backend, 1, log.NewNopLogger()) |
| 68 | |
| 69 | result, err := v1.GetMultiWithError(context.Background(), []string{"key"}) |
| 70 | assert.Empty(t, result) |
| 71 | assert.ErrorIs(t, err, backendErr) |
nothing calls this directly
no test coverage detected