aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/cache/buffer_test.go
blob: 3e3adc25e9a0829e16d8c85b93be6aa8534b9c3f (plain) (blame)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cache

import (
	"bytes"
	"sync"

	. "gopkg.in/check.v1"
)

type BufferSuite struct {
	c       map[string]Buffer
	aBuffer []byte
	bBuffer []byte
	cBuffer []byte
	dBuffer []byte
	eBuffer []byte
}

var _ = Suite(&BufferSuite{})

func (s *BufferSuite) SetUpTest(c *C) {
	s.aBuffer = []byte("a")
	s.bBuffer = []byte("bbb")
	s.cBuffer = []byte("c")
	s.dBuffer = []byte("d")
	s.eBuffer = []byte("ee")

	s.c = make(map[string]Buffer)
	s.c["two_bytes"] = NewBufferLRU(2 * Byte)
	s.c["default_lru"] = NewBufferLRUDefault()
}

func (s *BufferSuite) TestPutSameBuffer(c *C) {
	for _, o := range s.c {
		o.Put(1, s.aBuffer)
		o.Put(1, s.aBuffer)
		_, ok := o.Get(1)
		c.Assert(ok, Equals, true)
	}
}

func (s *ObjectSuite) TestPutSameBufferWithDifferentSize(c *C) {
	aBuffer := []byte("a")
	bBuffer := []byte("bbb")
	cBuffer := []byte("ccccc")
	dBuffer := []byte("ddddddd")

	cache := NewBufferLRU(7 * Byte)
	cache.Put(1, aBuffer)
	cache.Put(1, bBuffer)
	cache.Put(1, cBuffer)
	cache.Put(1, dBuffer)

	c.Assert(cache.MaxSize, Equals, 7*Byte)
	c.Assert(cache.actualSize, Equals, 7*Byte)
	c.Assert(cache.ll.Len(), Equals, 1)

	buf, ok := cache.Get(1)
	c.Assert(bytes.Equal(buf, dBuffer), Equals, true)
	c.Assert(FileSize(len(buf)), Equals, 7*Byte)
	c.Assert(ok, Equals, true)
}

func (s *BufferSuite) TestPutBigBuffer(c *C) {
	for _, o := range s.c {
		o.Put(1, s.bBuffer)
		_, ok := o.Get(2)
		c.Assert(ok, Equals, false)
	}
}

func (s *BufferSuite) TestPutCacheOverflow(c *C) {
	// this test only works with an specific size
	o := s.c["two_bytes"]

	o.Put(1, s.aBuffer)
	o.Put(2, s.cBuffer)
	o.Put(3, s.dBuffer)

	obj, ok := o.Get(1)
	c.Assert(ok, Equals, false)
	c.Assert(obj, IsNil)
	obj, ok = o.Get(2)
	c.Assert(ok, Equals, true)
	c.Assert(obj, NotNil)
	obj, ok = o.Get(3)
	c.Assert(ok, Equals, true)
	c.Assert(obj, NotNil)
}

func (s *BufferSuite) TestEvictMultipleBuffers(c *C) {
	o := s.c["two_bytes"]

	o.Put(1, s.cBuffer)
	o.Put(2, s.dBuffer) // now cache is full with two objects
	o.Put(3, s.eBuffer) // this put should evict all previous objects

	obj, ok := o.Get(1)
	c.Assert(ok, Equals, false)
	c.Assert(obj, IsNil)
	obj, ok = o.Get(2)
	c.Assert(ok, Equals, false)
	c.Assert(obj, IsNil)
	obj, ok = o.Get(3)
	c.Assert(ok, Equals, true)
	c.Assert(obj, NotNil)
}

func (s *BufferSuite) TestClear(c *C) {
	for _, o := range s.c {
		o.Put(1, s.aBuffer)
		o.Clear()
		obj, ok := o.Get(1)
		c.Assert(ok, Equals, false)
		c.Assert(obj, IsNil)
	}
}

func (s *BufferSuite) TestConcurrentAccess(c *C) {
	for _, o := range s.c {
		var wg sync.WaitGroup

		for i := 0; i < 1000; i++ {
			wg.Add(3)
			go func(i int) {
				o.Put(int64(i), []byte{00})
				wg.Done()
			}(i)

			go func(i int) {
				if i%30 == 0 {
					o.Clear()
				}
				wg.Done()
			}(i)

			go func(i int) {
				o.Get(int64(i))
				wg.Done()
			}(i)
		}

		wg.Wait()
	}
}

func (s *BufferSuite) TestDefaultLRU(c *C) {
	defaultLRU := s.c["default_lru"].(*BufferLRU)

	c.Assert(defaultLRU.MaxSize, Equals, DefaultMaxSize)
}