bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
1 |
# (C) 2005 Canonical Ltd
|
2 |
||
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
||
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
||
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
from bzrlib.selftest import InTempDir |
|
18 |
||
19 |
||
20 |
||
21 |
def sha1(t): |
|
22 |
import sha |
|
23 |
return sha.new(t).hexdigest() |
|
24 |
||
25 |
||
26 |
def pause(): |
|
27 |
import time |
|
28 |
# allow it to stabilize
|
|
29 |
start = int(time.time()) |
|
30 |
while int(time.time()) == start: |
|
31 |
time.sleep(0.2) |
|
32 |
||
33 |
||
34 |
||
35 |
class TestStatCache(InTempDir): |
|
36 |
"""Functional tests for statcache""" |
|
37 |
def runTest(self): |
|
38 |
from bzrlib.hashcache import HashCache |
|
39 |
import os |
|
40 |
import time |
|
41 |
||
42 |
hc = HashCache('.') |
|
43 |
||
44 |
file('foo', 'wb').write('hello') |
|
45 |
os.mkdir('subdir') |
|
46 |
pause() |
|
47 |
||
48 |
self.assertEquals(hc.get_sha1('foo'), |
|
49 |
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d') |
|
50 |
self.assertEquals(hc.miss_count, 1) |
|
51 |
self.assertEquals(hc.hit_count, 0) |
|
52 |
||
53 |
# check we hit without re-reading
|
|
54 |
self.assertEquals(hc.get_sha1('foo'), |
|
55 |
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d') |
|
56 |
self.assertEquals(hc.miss_count, 1) |
|
57 |
self.assertEquals(hc.hit_count, 1) |
|
58 |
||
59 |
# check again without re-reading
|
|
60 |
self.assertEquals(hc.get_sha1('foo'), |
|
61 |
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d') |
|
62 |
self.assertEquals(hc.miss_count, 1) |
|
63 |
self.assertEquals(hc.hit_count, 2) |
|
64 |
||
65 |
# write new file and make sure it is seen
|
|
66 |
file('foo', 'wb').write('goodbye') |
|
67 |
pause() |
|
68 |
self.assertEquals(hc.get_sha1('foo'), |
|
69 |
'3c8ec4874488f6090a157b014ce3397ca8e06d4f') |
|
70 |
self.assertEquals(hc.miss_count, 2) |
|
71 |
||
72 |
# quickly write new file of same size and make sure it is seen
|
|
73 |
# this may rely on detection of timestamps that are too close
|
|
74 |
# together to be safe
|
|
75 |
file('foo', 'wb').write('g00dbye') |
|
76 |
self.assertEquals(hc.get_sha1('foo'), |
|
77 |
sha1('g00dbye')) |
|
78 |
||
79 |
# this is not quite guaranteed to be true; we might have
|
|
80 |
# crossed a 1s boundary before
|
|
81 |
self.assertEquals(hc.danger_count, 1) |
|
82 |
||
83 |
self.assertEquals(hc.get_sha1('subdir'), None) |
|
84 |
||
85 |
||
86 |
||
87 |
TEST_CLASSES = [ |
|
88 |
TestStatCache, |
|
89 |
]
|