-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
104 lines (84 loc) · 2.17 KB
/
cache_test.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
95
96
97
98
99
100
101
102
103
104
package lru
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"testing"
"github.com/deciduosity/grip"
"github.com/satori/go.uuid"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type CacheSuite struct {
tempDir string
cache *Cache
require *require.Assertions
suite.Suite
}
func TestCacheSuite(t *testing.T) {
suite.Run(t, new(CacheSuite))
}
func (s *CacheSuite) SetupSuite() {
s.require = s.Require()
dir, err := ioutil.TempDir("", uuid.NewV4().String())
s.require.NoError(err)
s.tempDir = dir
}
func (s *CacheSuite) TearDownSuite() {
grip.CatchError(os.RemoveAll(s.tempDir))
}
func (s *CacheSuite) SetupTest() {
s.cache = NewCache()
s.require.Len(s.cache.table, 0)
}
func (s *CacheSuite) TestInitialStateOfCacheObjectIsEmpty() {
s.Equal(0, s.cache.size)
s.Len(s.cache.table, 0)
s.Equal(0, s.cache.heap.Len())
s.Equal(0, s.cache.Count())
s.Equal(0, s.cache.Size())
}
func (s *CacheSuite) TestAddFileThatDoeNotExistResultsInError() {
s.Error(s.cache.AddFile(filepath.Join(s.tempDir, "DOES-NOT-EXIST")))
}
func (s *CacheSuite) TestAddDirectoryThatExistsSucceeds() {
s.NoError(s.cache.AddFile(s.tempDir))
}
func (s *CacheSuite) TestAddRejectsFilesThatAlreadyExist() {
s.NoError(s.cache.AddFile(s.tempDir))
for i := 0; i < 40; i++ {
s.Error(s.cache.AddFile(s.tempDir))
}
}
func (s *CacheSuite) TestMutlithreadedFileAdds() {
s.Equal(0, s.cache.Count())
s.Equal(0, s.cache.Size())
wg := &sync.WaitGroup{}
for i := 0; i < 40; i++ {
fn := filepath.Join(s.tempDir, uuid.NewV4().String())
s.NoError(ioutil.WriteFile(fn, []byte(fmt.Sprintf("in %s is it %d", fn, i)), 0644))
wg.Add(1)
go func(f string) {
s.NoError(s.cache.AddFile(f))
wg.Done()
}(fn)
}
wg.Wait()
s.Equal(40, s.cache.Count())
s.Equal(5710, s.cache.Size())
}
func (s *CacheSuite) TestAddStatRejectsFilesWithNilStats() {
s.Error(s.cache.AddStat("foo", nil))
}
func (s *CacheSuite) TestUpdateRejectsRecordsThatAreNotLocal() {
fn := filepath.Join(s.tempDir, "foo")
fobj := &FileObject{
Path: fn,
}
s.NoError(ioutil.WriteFile(fn, []byte("foo"), 0644))
s.Error(s.cache.Update(fobj))
s.NoError(s.cache.Add(fobj))
s.NoError(s.cache.Update(fobj))
}