| 1 |
package cache |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"sync" |
| 6 |
|
| 7 |
. "gopkg.in/check.v1" |
| 8 |
) |
| 9 |
|
| 10 |
type BufferSuite struct { |
| 11 |
c map[string]Buffer |
| 12 |
aBuffer []byte |
| 13 |
bBuffer []byte |
| 14 |
cBuffer []byte |
| 15 |
dBuffer []byte |
| 16 |
eBuffer []byte |
| 17 |
} |
| 18 |
|
| 19 |
var _ = Suite(&BufferSuite{}) |
| 20 |
|
| 21 |
func (s *BufferSuite) SetUpTest(c *C) { |
| 22 |
s.aBuffer = []byte("a") |
| 23 |
s.bBuffer = []byte("bbb") |
| 24 |
s.cBuffer = []byte("c") |
| 25 |
s.dBuffer = []byte("d") |
| 26 |
s.eBuffer = []byte("ee") |
| 27 |
|
| 28 |
s.c = make(map[string]Buffer) |
| 29 |
s.c["two_bytes"] = NewBufferLRU(2 * Byte) |
| 30 |
s.c["default_lru"] = NewBufferLRUDefault() |
| 31 |
} |
| 32 |
|
| 33 |
func (s *BufferSuite) TestPutSameBuffer(c *C) { |
| 34 |
for _, o := range s.c { |
| 35 |
o.Put(1, s.aBuffer) |
| 36 |
o.Put(1, s.aBuffer) |
| 37 |
_, ok := o.Get(1) |
| 38 |
c.Assert(ok, Equals, true) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func (s *ObjectSuite) TestPutSameBufferWithDifferentSize(c *C) { |
| 43 |
aBuffer := []byte("a") |
| 44 |
bBuffer := []byte("bbb") |
| 45 |
cBuffer := []byte("ccccc") |
| 46 |
dBuffer := []byte("ddddddd") |
| 47 |
|
| 48 |
cache := NewBufferLRU(7 * Byte) |
| 49 |
cache.Put(1, aBuffer) |
| 50 |
cache.Put(1, bBuffer) |
| 51 |
cache.Put(1, cBuffer) |
| 52 |
cache.Put(1, dBuffer) |
| 53 |
|
| 54 |
c.Assert(cache.MaxSize, Equals, 7*Byte) |
| 55 |
c.Assert(cache.actualSize, Equals, 7*Byte) |
| 56 |
c.Assert(cache.ll.Len(), Equals, 1) |
| 57 |
|
| 58 |
buf, ok := cache.Get(1) |
| 59 |
c.Assert(bytes.Equal(buf, dBuffer), Equals, true) |
| 60 |
c.Assert(FileSize(len(buf)), Equals, 7*Byte) |
| 61 |
c.Assert(ok, Equals, true) |
| 62 |
} |
| 63 |
|
| 64 |
func (s *BufferSuite) TestPutBigBuffer(c *C) { |
| 65 |
for _, o := range s.c { |
| 66 |
o.Put(1, s.bBuffer) |
| 67 |
_, ok := o.Get(2) |
| 68 |
c.Assert(ok, Equals, false) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func (s *BufferSuite) TestPutCacheOverflow(c *C) { |
| 73 |
// this test only works with an specific size |
| 74 |
o := s.c["two_bytes"] |
| 75 |
|
| 76 |
o.Put(1, s.aBuffer) |
| 77 |
o.Put(2, s.cBuffer) |
| 78 |
o.Put(3, s.dBuffer) |
| 79 |
|
| 80 |
obj, ok := o.Get(1) |
| 81 |
c.Assert(ok, Equals, false) |
| 82 |
c.Assert(obj, IsNil) |
| 83 |
obj, ok = o.Get(2) |
| 84 |
c.Assert(ok, Equals, true) |
| 85 |
c.Assert(obj, NotNil) |
| 86 |
obj, ok = o.Get(3) |
| 87 |
c.Assert(ok, Equals, true) |
| 88 |
c.Assert(obj, NotNil) |
| 89 |
} |
| 90 |
|
| 91 |
func (s *BufferSuite) TestEvictMultipleBuffers(c *C) { |
| 92 |
o := s.c["two_bytes"] |
| 93 |
|
| 94 |
o.Put(1, s.cBuffer) |
| 95 |
o.Put(2, s.dBuffer) // now cache is full with two objects |
| 96 |
o.Put(3, s.eBuffer) // this put should evict all previous objects |
| 97 |
|
| 98 |
obj, ok := o.Get(1) |
| 99 |
c.Assert(ok, Equals, false) |
| 100 |
c.Assert(obj, IsNil) |
| 101 |
obj, ok = o.Get(2) |
| 102 |
c.Assert(ok, Equals, false) |
| 103 |
c.Assert(obj, IsNil) |
| 104 |
obj, ok = o.Get(3) |
| 105 |
c.Assert(ok, Equals, true) |
| 106 |
c.Assert(obj, NotNil) |
| 107 |
} |
| 108 |
|
| 109 |
func (s *BufferSuite) TestClear(c *C) { |
| 110 |
for _, o := range s.c { |
| 111 |
o.Put(1, s.aBuffer) |
| 112 |
o.Clear() |
| 113 |
obj, ok := o.Get(1) |
| 114 |
c.Assert(ok, Equals, false) |
| 115 |
c.Assert(obj, IsNil) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func (s *BufferSuite) TestConcurrentAccess(c *C) { |
| 120 |
for _, o := range s.c { |
| 121 |
var wg sync.WaitGroup |
| 122 |
|
| 123 |
for i := 0; i < 1000; i++ { |
| 124 |
wg.Add(3) |
| 125 |
go func(i int) { |
| 126 |
o.Put(int64(i), []byte{00}) |
| 127 |
wg.Done() |
| 128 |
}(i) |
| 129 |
|
| 130 |
go func(i int) { |
| 131 |
if i%30 == 0 { |
| 132 |
o.Clear() |
| 133 |
} |
| 134 |
wg.Done() |
| 135 |
}(i) |
| 136 |
|
| 137 |
go func(i int) { |
| 138 |
o.Get(int64(i)) |
| 139 |
wg.Done() |
| 140 |
}(i) |
| 141 |
} |
| 142 |
|
| 143 |
wg.Wait() |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func (s *BufferSuite) TestDefaultLRU(c *C) { |
| 148 |
defaultLRU := s.c["default_lru"].(*BufferLRU) |
| 149 |
|
| 150 |
c.Assert(defaultLRU.MaxSize, Equals, DefaultMaxSize) |
| 151 |
} |