~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_hashcache.py

  • Committer: mbp at sourcefrog
  • Date: 2006-07-09 03:43:16 UTC
  • mto: This revision was merged to the branch mainline in revision 1856.
  • Revision ID: mbp@sourcefrog.net-20060709034316-fa45900c76d1df39
Refactor and improve hashcache tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
 
47
47
class TestHashCache(TestCaseInTempDir):
48
48
 
49
 
    def test_hashcache(self):
50
 
        """Functional tests for hashcache"""
51
 
 
 
49
    def make_hashcache(self):
52
50
        # make a dummy bzr directory just to hold the cache
53
51
        os.mkdir('.bzr')
54
52
        hc = HashCache('.', '.bzr/stat-cache')
55
 
 
56
 
        file('foo', 'wb').write('hello')
57
 
        os.mkdir('subdir')
58
 
        pause()
59
 
 
60
 
        self.assertEquals(hc.get_sha1('foo'),
61
 
                          'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
62
 
        self.assertEquals(hc.miss_count, 1)
63
 
        self.assertEquals(hc.hit_count, 0)
64
 
 
65
 
        # check we hit without re-reading
 
53
        return hc
 
54
 
 
55
    def reopen_hashcache(self):
 
56
        hc = HashCache('.', '.bzr/stat-cache')
 
57
        hc.read()
 
58
        return hc
 
59
 
 
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)
 
68
 
 
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)
70
 
 
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)
76
88
 
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
 
 
 
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
 
93
        # so new
 
94
        self.assertEquals(hc.get_sha1('foo'), sha1('goodbye'))
 
95
 
 
96
    def test_hashcache_nonexistent_file(self):
 
97
        hc = self.make_hashcache()
 
98
        self.assertEquals(hc.get_sha1('no-name-yet'), None)
 
99
 
 
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'))
 
104
        os.remove('foo')
 
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'))
 
108
 
 
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)
101
113
 
102
 
        # pause briefly to make sure they're not treated as new uncacheable
103
 
        # files
 
114
    def test_hashcache_load(self):
 
115
        hc = self.make_hashcache()
 
116
        self.build_tree_contents([('foo', 'contents')])
104
117
        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
 
118
        self.assertEquals(hc.get_sha1('foo'), sha1('contents'))
111
119
        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'))
 
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'))
 
124
 
 
125
    def test_hammer_hashcache(self):
 
126
        hc = self.make_hashcache()
 
127
        for i in xrange(50000):
 
128
            f = file('foo', 'w')
 
129
            try:
 
130
                last_content = '%08x' % i
 
131
                f.write(last_content)
 
132
            finally:
 
133
                f.close()
 
134
            self.assertEquals(hc.get_sha1('foo'), sha1(last_content))
 
135
            hc.write()
 
136
            hc = self.reopen_hashcache()
122
137
 
123
138
    def test_hashcache_raise(self):
124
139
        """check that hashcache can raise BzrError"""
125
 
 
126
 
        os.mkdir('.bzr')
127
 
        hc = HashCache('.', '.bzr/stat-cache')
 
140
        hc = self.make_hashcache()
128
141
        ok = False
129
142
 
130
143
        # make a best effort to create a weird kind of file