(t *testing.T)
| 76 | } |
| 77 | |
| 78 | func TestValkeyDataCache(t *testing.T) { |
| 79 | cache := getTestValkey(t) |
| 80 | ctx := context.Background() |
| 81 | |
| 82 | testKey1 := "test-key-1" |
| 83 | testValue1 := []byte("test-value") |
| 84 | |
| 85 | t.Run("cache miss", func(t *testing.T) { |
| 86 | result, err := cache.Get(ctx, testKey1) |
| 87 | if !errors.Is(err, cachetypes.ErrCachedDataNotFound) { |
| 88 | t.Errorf("invalid error %v", err) |
| 89 | } |
| 90 | if result != nil { |
| 91 | t.Error("expected null result") |
| 92 | } |
| 93 | }) |
| 94 | |
| 95 | t.Run("cache hit", func(t *testing.T) { |
| 96 | // Store result |
| 97 | err := cache.Cache(ctx, testKey1, testValue1) |
| 98 | if !errors.Is(err, nil) { |
| 99 | t.Errorf("invalid error storing value %v", err) |
| 100 | } |
| 101 | |
| 102 | // Get result. |
| 103 | result, err := cache.Get(ctx, testKey1) |
| 104 | if !errors.Is(err, nil) { |
| 105 | t.Errorf("invalid error getting value %v", err) |
| 106 | } |
| 107 | if !reflect.DeepEqual(result, testValue1) { |
| 108 | t.Error("expected result") |
| 109 | } |
| 110 | |
| 111 | // Wait for TTL |
| 112 | time.Sleep(getDefatulTTL() * 2) |
| 113 | result, err = cache.Get(ctx, testKey1) |
| 114 | if !errors.Is(err, cachetypes.ErrCachedDataNotFound) { |
| 115 | t.Errorf("invalid error getting expired result %v", err) |
| 116 | } |
| 117 | if result != nil { |
| 118 | t.Error("expected null result") |
| 119 | } |
| 120 | |
| 121 | }) |
| 122 | |
| 123 | t.Run("cache hit with custom ttl", func(t *testing.T) { |
| 124 | // Store result with custom ttl |
| 125 | err := cache.Cache(ctx, testKey1, testValue1, cachetypes.WithTTL(getDefatulTTL()*4)) |
| 126 | if !errors.Is(err, nil) { |
| 127 | t.Errorf("invalid error storing value %v", err) |
| 128 | } |
| 129 | |
| 130 | // Get result. |
| 131 | result, err := cache.Get(ctx, testKey1) |
| 132 | if !errors.Is(err, nil) { |
| 133 | t.Errorf("invalid error getting value %v", err) |
| 134 | } |
| 135 | if !reflect.DeepEqual(result, testValue1) { |
nothing calls this directly
no test coverage detected