~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_hashcache.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-07-12 10:09:27 UTC
  • mfrom: (1845.1.3 bzr.mbp.hashcache)
  • Revision ID: pqm@pqm.ubuntu.com-20060712100927-6af9cbe9c9e6af3b
(mbp) hashcache fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
import os
18
18
import sha
 
19
import stat
19
20
import sys
20
21
import time
21
22
 
22
23
from bzrlib.errors import BzrError
23
24
from bzrlib.hashcache import HashCache
24
 
from bzrlib.tests import TestCaseInTempDir, TestSkipped
 
25
from bzrlib.tests import TestCaseInTempDir, TestSkipped, TestCase
25
26
 
26
27
 
27
28
def sha1(t):
29
30
 
30
31
 
31
32
def pause():
32
 
    if False:
33
 
        return
34
 
    if sys.platform in ('win32', 'cygwin'):
35
 
        time.sleep(3)
36
 
        return
37
 
    # allow it to stabilize
38
 
    start = int(time.time())
39
 
    while int(time.time()) == start:
40
 
        time.sleep(0.2)
41
 
 
42
 
 
43
 
class FixThisError(Exception):
44
 
    pass
45
 
    
 
33
    time.sleep(5.0)
 
34
 
46
35
 
47
36
class TestHashCache(TestCaseInTempDir):
48
 
 
49
 
    def test_hashcache(self):
50
 
        """Functional tests for hashcache"""
51
 
 
 
37
    """Test the hashcache against a real directory"""
 
38
 
 
39
    def make_hashcache(self):
52
40
        # make a dummy bzr directory just to hold the cache
53
41
        os.mkdir('.bzr')
54
42
        hc = HashCache('.', '.bzr/stat-cache')
55
 
 
56
 
        file('foo', 'wb').write('hello')
57
 
        os.mkdir('subdir')
58
 
        pause()
59
 
 
 
43
        return hc
 
44
 
 
45
    def reopen_hashcache(self):
 
46
        hc = HashCache('.', '.bzr/stat-cache')
 
47
        hc.read()
 
48
        return hc
 
49
 
 
50
    def test_hashcache_initial_miss(self):
 
51
        """Get correct hash from an empty hashcache"""
 
52
        hc = self.make_hashcache()
 
53
        self.build_tree_contents([('foo', 'hello')])
60
54
        self.assertEquals(hc.get_sha1('foo'),
61
55
                          'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
62
56
        self.assertEquals(hc.miss_count, 1)
63
57
        self.assertEquals(hc.hit_count, 0)
64
58
 
65
 
        # check we hit without re-reading
66
 
        self.assertEquals(hc.get_sha1('foo'),
67
 
                          'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
68
 
        self.assertEquals(hc.miss_count, 1)
69
 
        self.assertEquals(hc.hit_count, 1)
70
 
 
71
 
        # check again without re-reading
72
 
        self.assertEquals(hc.get_sha1('foo'),
73
 
                          'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
74
 
        self.assertEquals(hc.miss_count, 1)
75
 
        self.assertEquals(hc.hit_count, 2)
76
 
 
77
 
        # write new file and make sure it is seen
78
 
        file('foo', 'wb').write('goodbye')
79
 
        pause()
80
 
        self.assertEquals(hc.get_sha1('foo'),
81
 
                          '3c8ec4874488f6090a157b014ce3397ca8e06d4f')
82
 
        self.assertEquals(hc.miss_count, 2)
83
 
 
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
86
 
        # together to be safe
87
 
        file('foo', 'wb').write('g00dbye')
88
 
        self.assertEquals(hc.get_sha1('foo'),
89
 
                          sha1('g00dbye'))
90
 
 
91
 
        file('foo2', 'wb').write('other file')
92
 
        self.assertEquals(hc.get_sha1('foo2'), sha1('other file'))
93
 
 
94
 
        os.remove('foo2')
95
 
        self.assertEquals(hc.get_sha1('foo2'), None)
96
 
 
97
 
        file('foo2', 'wb').write('new content')
98
 
        self.assertEquals(hc.get_sha1('foo2'), sha1('new content'))
99
 
 
 
59
    def test_hashcache_new_file(self):
 
60
        hc = self.make_hashcache()
 
61
        self.build_tree_contents([('foo', 'goodbye')])
 
62
        # now read without pausing; it may not be possible to cache it as its
 
63
        # so new
 
64
        self.assertEquals(hc.get_sha1('foo'), sha1('goodbye'))
 
65
 
 
66
    def test_hashcache_nonexistent_file(self):
 
67
        hc = self.make_hashcache()
 
68
        self.assertEquals(hc.get_sha1('no-name-yet'), None)
 
69
 
 
70
    def test_hashcache_replaced_file(self):
 
71
        hc = self.make_hashcache()
 
72
        self.build_tree_contents([('foo', 'goodbye')])
 
73
        self.assertEquals(hc.get_sha1('foo'), sha1('goodbye'))
 
74
        os.remove('foo')
 
75
        self.assertEquals(hc.get_sha1('foo'), None)
 
76
        self.build_tree_contents([('foo', 'new content')])
 
77
        self.assertEquals(hc.get_sha1('foo'), sha1('new content'))
 
78
 
 
79
    def test_hashcache_not_file(self):
 
80
        hc = self.make_hashcache()
 
81
        self.build_tree(['subdir/'])
100
82
        self.assertEquals(hc.get_sha1('subdir'), None)
101
83
 
102
 
        # pause briefly to make sure they're not treated as new uncacheable
103
 
        # files
 
84
    def test_hashcache_load(self):
 
85
        hc = self.make_hashcache()
 
86
        self.build_tree_contents([('foo', 'contents')])
104
87
        pause()
105
 
 
106
 
        self.assertEquals(hc.get_sha1('foo'), sha1('g00dbye'))
107
 
        self.assertEquals(hc.get_sha1('foo2'), sha1('new content'))
108
 
 
109
 
        # write out, read back in and check that we don't need to
110
 
        # re-read any files
 
88
        self.assertEquals(hc.get_sha1('foo'), sha1('contents'))
111
89
        hc.write()
112
 
        del hc
113
 
 
114
 
        hc = HashCache('.', '.bzr/stat-cache')
115
 
        hc.read()
116
 
 
117
 
        self.assertEquals(len(hc._cache), 2)
118
 
        self.assertEquals(hc.get_sha1('foo'), sha1('g00dbye'))
 
90
        hc = self.reopen_hashcache()
 
91
        self.assertEquals(hc.get_sha1('foo'), sha1('contents'))
119
92
        self.assertEquals(hc.hit_count, 1)
120
 
        self.assertEquals(hc.miss_count, 0)
121
 
        self.assertEquals(hc.get_sha1('foo2'), sha1('new content'))
 
93
 
 
94
    def test_hammer_hashcache(self):
 
95
        hc = self.make_hashcache()
 
96
        for i in xrange(10000):
 
97
            self.log('start writing at %s', time.time())
 
98
            f = file('foo', 'w')
 
99
            try:
 
100
                last_content = '%08x' % i
 
101
                f.write(last_content)
 
102
            finally:
 
103
                f.close()
 
104
            last_sha1 = sha1(last_content)
 
105
            self.log("iteration %d: %r -> %r",
 
106
                     i, last_content, last_sha1)
 
107
            got_sha1 = hc.get_sha1('foo')
 
108
            self.assertEquals(got_sha1, last_sha1)
 
109
            hc.write()
 
110
            hc = self.reopen_hashcache()
122
111
 
123
112
    def test_hashcache_raise(self):
124
113
        """check that hashcache can raise BzrError"""
125
 
 
126
 
        os.mkdir('.bzr')
127
 
        hc = HashCache('.', '.bzr/stat-cache')
128
 
        ok = False
129
 
 
130
 
        # make a best effort to create a weird kind of file
131
 
        funcs = (getattr(os, 'mkfifo', None), getattr(os, 'mknod', None))
132
 
        for func in funcs:
133
 
            if func is None:
134
 
                continue
135
 
            try:
136
 
                func('a')
137
 
                ok = True
138
 
                break
139
 
            except FixThisError:
140
 
                pass
141
 
 
142
 
        if ok:
143
 
            self.assertRaises(BzrError, hc.get_sha1, 'a')
 
114
        hc = self.make_hashcache()
 
115
        if not hasattr(os, 'mkfifo'):
 
116
            raise TestSkipped('filesystem fifos not supported on this system')
 
117
        os.mkfifo('a')
 
118
        # It's possible that the system supports fifos but the filesystem
 
119
        # can't.  In that case we should skip at this point.  But in fact
 
120
        # such combinations don't usually occur for the filesystem where
 
121
        # people test bzr.
 
122
        self.assertRaises(BzrError, hc.get_sha1, 'a')
 
123
 
 
124
 
 
125
class FakeHashCache(HashCache):
 
126
    """Hashcache that consults a fake clock rather than the real one.
 
127
 
 
128
    This lets us examine how old or new files would be handled, without
 
129
    actually having to wait for time to pass.
 
130
    """
 
131
    def __init__(self):
 
132
        # set root and cache file name to none to make sure we won't touch the
 
133
        # real filesystem
 
134
        HashCache.__init__(self, '.', 'hashcache')
 
135
        self._files = {}
 
136
        # simulated clock running forward as operations happen
 
137
        self._clock = 0
 
138
 
 
139
    def put_file(self, filename, file_contents):
 
140
        abspath = './' + filename
 
141
        self._files[abspath] = (file_contents, self._clock)
 
142
 
 
143
    def _fingerprint(self, abspath):
 
144
        entry = self._files[abspath]
 
145
        return (len(entry[0]),
 
146
                entry[1], entry[1],
 
147
                10, 20,
 
148
                stat.S_IFREG | 0600)
 
149
 
 
150
    def _really_sha1_file(self, abspath):
 
151
        if abspath in self._files:
 
152
            return sha1(self._files[abspath][0])
144
153
        else:
145
 
            raise TestSkipped('No weird file type could be created')
 
154
            return None
 
155
 
 
156
    def _cutoff_time(self):
 
157
        return self._clock - 2
 
158
 
 
159
    def pretend_to_sleep(self, secs):
 
160
        self._clock += secs
 
161
 
 
162
    
 
163
class TestHashCacheFakeFilesystem(TestCaseInTempDir):
 
164
    """Tests the hashcache using a simulated OS.
 
165
    """
 
166
 
 
167
    def make_hashcache(self):
 
168
        return FakeHashCache()
 
169
 
 
170
    def test_hashcache_miss_new_file(self):
 
171
        """A new file gives the right sha1 but misses"""
 
172
        hc = self.make_hashcache()
 
173
        hc.put_file('foo', 'hello')
 
174
        self.assertEquals(hc.get_sha1('foo'), sha1('hello'))
 
175
        self.assertEquals(hc.miss_count, 1)
 
176
        self.assertEquals(hc.hit_count, 0)
 
177
        # if we try again it's still too new; 
 
178
        self.assertEquals(hc.get_sha1('foo'), sha1('hello'))
 
179
        self.assertEquals(hc.miss_count, 2)
 
180
        self.assertEquals(hc.hit_count, 0)
 
181
 
 
182
    def test_hashcache_old_file(self):
 
183
        """An old file gives the right sha1 and hits"""
 
184
        hc = self.make_hashcache()
 
185
        hc.put_file('foo', 'hello')
 
186
        hc.pretend_to_sleep(20)
 
187
        # file is new; should get the correct hash but miss
 
188
        self.assertEquals(hc.get_sha1('foo'), sha1('hello'))
 
189
        self.assertEquals(hc.miss_count, 1)
 
190
        self.assertEquals(hc.hit_count, 0)
 
191
        # and can now be hit
 
192
        self.assertEquals(hc.get_sha1('foo'), sha1('hello'))
 
193
        self.assertEquals(hc.miss_count, 1)
 
194
        self.assertEquals(hc.hit_count, 1)
 
195
        hc.pretend_to_sleep(3)
 
196
        # and again
 
197
        self.assertEquals(hc.get_sha1('foo'), sha1('hello'))
 
198
        self.assertEquals(hc.miss_count, 1)
 
199
        self.assertEquals(hc.hit_count, 2)
 
200
 
 
201
    def test_hashcache_invalidates(self):
 
202
        hc = self.make_hashcache()
 
203
        hc.put_file('foo', 'hello')
 
204
        hc.pretend_to_sleep(20)
 
205
        hc.get_sha1('foo')
 
206
        hc.put_file('foo', 'h1llo')
 
207
        self.assertEquals(hc.get_sha1('foo'), sha1('h1llo'))
 
208
        self.assertEquals(hc.miss_count, 2)
 
209
        self.assertEquals(hc.hit_count, 0)