forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_suite.go
94 lines (85 loc) · 2.2 KB
/
cache_suite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2017 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package storagetest
import (
"sort"
"time"
"github.com/tsuru/tsuru/types/cache"
check "gopkg.in/check.v1"
)
type CacheSuite struct {
SuiteHooks
CacheStorage cache.CacheStorage
}
func (s *CacheSuite) TestCachePut(c *check.C) {
err := s.CacheStorage.Put(cache.CacheEntry{
Key: "k1",
Value: "v1",
})
c.Assert(err, check.IsNil)
entry, err := s.CacheStorage.Get("k1")
c.Assert(err, check.IsNil)
c.Assert(entry, check.DeepEquals, cache.CacheEntry{
Key: "k1",
Value: "v1",
})
}
func (s *CacheSuite) TestCacheGetNotFound(c *check.C) {
entry, err := s.CacheStorage.Get("k1")
c.Assert(err, check.Equals, cache.ErrEntryNotFound)
c.Assert(entry, check.DeepEquals, cache.CacheEntry{})
}
func (s *CacheSuite) TestCacheGetAll(c *check.C) {
err := s.CacheStorage.Put(cache.CacheEntry{
Key: "k1",
Value: "v1",
})
c.Assert(err, check.IsNil)
err = s.CacheStorage.Put(cache.CacheEntry{
Key: "k2",
Value: "v2",
})
c.Assert(err, check.IsNil)
err = s.CacheStorage.Put(cache.CacheEntry{
Key: "k3",
Value: "v3",
})
c.Assert(err, check.IsNil)
entries, err := s.CacheStorage.GetAll("k1", "k3")
c.Assert(err, check.IsNil)
sort.Slice(entries, func(i, j int) bool {
return entries[i].Key < entries[j].Key
})
c.Assert(entries, check.DeepEquals, []cache.CacheEntry{
{Key: "k1", Value: "v1"},
{Key: "k3", Value: "v3"},
})
entries, err = s.CacheStorage.GetAll("kx")
c.Assert(err, check.IsNil)
c.Assert(entries, check.HasLen, 0)
}
func (s *CacheSuite) TestCacheExpiration(c *check.C) {
err := s.CacheStorage.Put(cache.CacheEntry{
Key: "k1",
Value: "v1",
ExpireAt: time.Now().Add(time.Second),
})
c.Assert(err, check.IsNil)
entry, err := s.CacheStorage.Get("k1")
c.Assert(err, check.IsNil)
c.Assert(entry.Value, check.Equals, "v1")
timeout := time.After(70 * time.Second)
for {
_, err = s.CacheStorage.Get("k1")
if err != nil {
c.Assert(err, check.Equals, cache.ErrEntryNotFound)
break
}
select {
case <-time.After(500 * time.Millisecond):
case <-timeout:
c.Fatal("timeout waiting for key to expire")
}
}
}