(t *testing.T)
| 203 | } |
| 204 | |
| 205 | func TestGetWithExpiration(t *testing.T) { |
| 206 | cache := NewCache(1024) |
| 207 | key := []byte("abcd") |
| 208 | val := []byte("efgh") |
| 209 | err := cache.Set(key, val, 2) |
| 210 | if err != nil { |
| 211 | t.Error("err should be nil", err.Error()) |
| 212 | } |
| 213 | |
| 214 | res, expiry, err := cache.GetWithExpiration(key) |
| 215 | var expireTime time.Time |
| 216 | startTime := time.Now() |
| 217 | for { |
| 218 | _, _, err := cache.GetWithExpiration(key) |
| 219 | expireTime = time.Now() |
| 220 | if err != nil { |
| 221 | break |
| 222 | } |
| 223 | if time.Now().Unix() > int64(expiry+1) { |
| 224 | break |
| 225 | } |
| 226 | time.Sleep(1 * time.Millisecond) |
| 227 | } |
| 228 | if time.Second > expireTime.Sub(startTime) || 3*time.Second < expireTime.Sub(startTime) { |
| 229 | t.Error("Cache should expire within a second of the expire time") |
| 230 | } |
| 231 | |
| 232 | if err != nil { |
| 233 | t.Error("err should be nil", err.Error()) |
| 234 | } |
| 235 | if !bytes.Equal(val, res) { |
| 236 | t.Fatalf("%s should be the same as %s but isn't", res, val) |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | func TestPeekWithExpiration(t *testing.T) { |
| 241 | now := uint32(1000) |
nothing calls this directly
no test coverage detected
searching dependent graphs…