47
47
class TestHashCache(TestCaseInTempDir):
49
def test_hashcache(self):
50
"""Functional tests for hashcache"""
49
def make_hashcache(self):
52
50
# make a dummy bzr directory just to hold the cache
54
52
hc = HashCache('.', '.bzr/stat-cache')
56
file('foo', 'wb').write('hello')
60
self.assertEquals(hc.get_sha1('foo'),
61
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
62
self.assertEquals(hc.miss_count, 1)
63
self.assertEquals(hc.hit_count, 0)
65
# check we hit without re-reading
55
def reopen_hashcache(self):
56
hc = HashCache('.', '.bzr/stat-cache')
60
def test_hashcache_initial_miss(self):
61
"""Get correct hash from an empty hashcache"""
62
hc = self.make_hashcache()
63
self.build_tree_contents([('foo', 'hello')])
64
self.assertEquals(hc.get_sha1('foo'),
65
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
66
self.assertEquals(hc.miss_count, 1)
67
self.assertEquals(hc.hit_count, 0)
69
def test_hashcache_hit_old_file(self):
70
"""An old file gives a cache hit"""
71
hc = self.make_hashcache()
72
self.build_tree_contents([('foo', 'hello')])
73
pause() # make sure file's old enough to cache
74
self.assertEquals(hc.get_sha1('foo'),
75
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
76
self.assertEquals(hc.miss_count, 1)
77
self.assertEquals(hc.hit_count, 0)
78
# now should hit on second try
66
79
self.assertEquals(hc.get_sha1('foo'),
67
80
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
68
81
self.assertEquals(hc.miss_count, 1)
69
82
self.assertEquals(hc.hit_count, 1)
71
# check again without re-reading
83
# and hit again on third try
72
84
self.assertEquals(hc.get_sha1('foo'),
73
85
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
74
86
self.assertEquals(hc.miss_count, 1)
75
87
self.assertEquals(hc.hit_count, 2)
77
# write new file and make sure it is seen
78
file('foo', 'wb').write('goodbye')
80
self.assertEquals(hc.get_sha1('foo'),
81
'3c8ec4874488f6090a157b014ce3397ca8e06d4f')
82
self.assertEquals(hc.miss_count, 2)
84
# quickly write new file of same size and make sure it is seen
85
# this may rely on detection of timestamps that are too close
87
file('foo', 'wb').write('g00dbye')
88
self.assertEquals(hc.get_sha1('foo'),
91
file('foo2', 'wb').write('other file')
92
self.assertEquals(hc.get_sha1('foo2'), sha1('other file'))
95
self.assertEquals(hc.get_sha1('foo2'), None)
97
file('foo2', 'wb').write('new content')
98
self.assertEquals(hc.get_sha1('foo2'), sha1('new content'))
89
def test_hashcache_new_file(self):
90
hc = self.make_hashcache()
91
self.build_tree_contents([('foo', 'goodbye')])
92
# now read without pausing; it may not be possible to cache it as its
94
self.assertEquals(hc.get_sha1('foo'), sha1('goodbye'))
96
def test_hashcache_nonexistent_file(self):
97
hc = self.make_hashcache()
98
self.assertEquals(hc.get_sha1('no-name-yet'), None)
100
def test_hashcache_replaced_file(self):
101
hc = self.make_hashcache()
102
self.build_tree_contents([('foo', 'goodbye')])
103
self.assertEquals(hc.get_sha1('foo'), sha1('goodbye'))
105
self.assertEquals(hc.get_sha1('foo'), None)
106
self.build_tree_contents([('foo', 'new content')])
107
self.assertEquals(hc.get_sha1('foo'), sha1('new content'))
109
def test_hashcache_not_file(self):
110
hc = self.make_hashcache()
111
self.build_tree(['subdir/'])
100
112
self.assertEquals(hc.get_sha1('subdir'), None)
102
# pause briefly to make sure they're not treated as new uncacheable
114
def test_hashcache_load(self):
115
hc = self.make_hashcache()
116
self.build_tree_contents([('foo', 'contents')])
106
self.assertEquals(hc.get_sha1('foo'), sha1('g00dbye'))
107
self.assertEquals(hc.get_sha1('foo2'), sha1('new content'))
109
# write out, read back in and check that we don't need to
118
self.assertEquals(hc.get_sha1('foo'), sha1('contents'))
114
hc = HashCache('.', '.bzr/stat-cache')
117
self.assertEquals(len(hc._cache), 2)
118
self.assertEquals(hc.get_sha1('foo'), sha1('g00dbye'))
120
hc = self.reopen_hashcache()
121
self.assertEquals(len(hc._cache), 1)
122
self.assertEquals(hc.get_sha1('foo'), sha1('contents'))
119
123
self.assertEquals(hc.hit_count, 1)
120
self.assertEquals(hc.miss_count, 0)
121
self.assertEquals(hc.get_sha1('foo2'), sha1('new content'))
125
def test_hammer_hashcache(self):
126
hc = self.make_hashcache()
127
for i in xrange(50000):
130
last_content = '%08x' % i
131
f.write(last_content)
134
self.assertEquals(hc.get_sha1('foo'), sha1(last_content))
136
hc = self.reopen_hashcache()
123
138
def test_hashcache_raise(self):
124
139
"""check that hashcache can raise BzrError"""
127
hc = HashCache('.', '.bzr/stat-cache')
140
hc = self.make_hashcache()
130
143
# make a best effort to create a weird kind of file