~bzr-pqm/bzr/bzr.dev

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
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
17
import os
1185.38.8 by John Arbash Meinel
[patch] testhashcache delay for win32 and cygwin
18
import sys
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
19
import time
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
20
from bzrlib.tests import TestCaseInTempDir
846 by Martin Pool
- start adding refactored/simplified hash cache
21
22
23
24
def sha1(t):
25
    import sha
26
    return sha.new(t).hexdigest()
27
28
29
def pause():
1275 by Martin Pool
- remove pauses from hashcache tests
30
    if False:
1390 by Robert Collins
pair programming worx... merge integration and weave
31
        return
1185.38.8 by John Arbash Meinel
[patch] testhashcache delay for win32 and cygwin
32
    if sys.platform in ('win32', 'cygwin'):
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
33
        time.sleep(3)
34
        return
846 by Martin Pool
- start adding refactored/simplified hash cache
35
    # allow it to stabilize
36
    start = int(time.time())
37
    while int(time.time()) == start:
38
        time.sleep(0.2)
1185.33.109 by Denys Duchier
test_hashcache: catch no exceptions
39
40
41
class FixThisError(Exception):
42
    pass
846 by Martin Pool
- start adding refactored/simplified hash cache
43
    
44
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
45
class TestHashCache(TestCaseInTempDir):
1102 by Martin Pool
- merge test refactoring from robertc
46
47
    def test_hashcache(self):
48
        """Functional tests for hashcache"""
846 by Martin Pool
- start adding refactored/simplified hash cache
49
        from bzrlib.hashcache import HashCache
50
        import os
51
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
52
        # make a dummy bzr directory just to hold the cache
53
        os.mkdir('.bzr')
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
54
        hc = HashCache(u'.')
846 by Martin Pool
- start adding refactored/simplified hash 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
66
        self.assertEquals(hc.get_sha1('foo'),
67
                          'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
1185.60.5 by Aaron Bentley
Reactivated hashcache tests
68
        self.assertEquals(hc.miss_count, 1)
69
        self.assertEquals(hc.hit_count, 1)
846 by Martin Pool
- start adding refactored/simplified hash cache
70
71
        # check again without re-reading
72
        self.assertEquals(hc.get_sha1('foo'),
73
                          'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
1185.60.5 by Aaron Bentley
Reactivated hashcache tests
74
        self.assertEquals(hc.miss_count, 1)
75
        self.assertEquals(hc.hit_count, 2)
846 by Martin Pool
- start adding refactored/simplified hash cache
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')
1185.60.5 by Aaron Bentley
Reactivated hashcache tests
82
        self.assertEquals(hc.miss_count, 2)
846 by Martin Pool
- start adding refactored/simplified hash cache
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'))
863 by Martin Pool
- more hashcache tests
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'))
846 by Martin Pool
- start adding refactored/simplified hash cache
99
100
        self.assertEquals(hc.get_sha1('subdir'), None)
848 by Martin Pool
doc
101
863 by Martin Pool
- more hashcache tests
102
        # it's likely neither are cached at the moment because they 
103
        # changed recently, but we can't be sure
861 by Martin Pool
- more hash-cache tests
104
        pause()
105
863 by Martin Pool
- more hashcache tests
106
        # should now be safe to cache it if we reread them
107
        self.assertEquals(hc.get_sha1('foo'), sha1('g00dbye'))
1185.60.5 by Aaron Bentley
Reactivated hashcache tests
108
        self.assertEquals(len(hc._cache), 1)
863 by Martin Pool
- more hashcache tests
109
        self.assertEquals(hc.get_sha1('foo2'), sha1('new content'))
1185.60.5 by Aaron Bentley
Reactivated hashcache tests
110
        self.assertEquals(len(hc._cache), 2)
861 by Martin Pool
- more hash-cache tests
111
112
        # write out, read back in and check that we don't need to
113
        # re-read any files
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
114
        hc.write()
861 by Martin Pool
- more hash-cache tests
115
        del hc
116
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
117
        hc = HashCache(u'.')
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
118
        hc.read()
862 by Martin Pool
- code to re-read hashcache from file
119
1185.60.5 by Aaron Bentley
Reactivated hashcache tests
120
        self.assertEquals(len(hc._cache), 2)
862 by Martin Pool
- code to re-read hashcache from file
121
        self.assertEquals(hc.get_sha1('foo'), sha1('g00dbye'))
1185.60.5 by Aaron Bentley
Reactivated hashcache tests
122
        self.assertEquals(hc.hit_count, 1)
123
        self.assertEquals(hc.miss_count, 0)
863 by Martin Pool
- more hashcache tests
124
        self.assertEquals(hc.get_sha1('foo2'), sha1('new content'))
1185.33.106 by Denys Duchier
check that hashcache can raise BzrError
125
126
    def test_hashcache_raise(self):
127
        """check that hashcache can raise BzrError"""
128
        from bzrlib.hashcache import HashCache
129
        import os
130
131
        os.mkdir('.bzr')
132
        hc = HashCache(u'.')
133
        ok = False
1185.33.109 by Denys Duchier
test_hashcache: catch no exceptions
134
1185.33.108 by Denys Duchier
test_hashcache_raise: complain if no weird file type can be created
135
        # make a best effort to create a weird kind of file
1185.33.109 by Denys Duchier
test_hashcache: catch no exceptions
136
        funcs = (os.mkfifo, os.mknod)
137
        for func in funcs:
1185.33.106 by Denys Duchier
check that hashcache can raise BzrError
138
            try:
1185.33.109 by Denys Duchier
test_hashcache: catch no exceptions
139
                func('a')
1185.33.110 by Denys Duchier
test_hashcache_raise: minor space fix
140
                ok = True
1185.33.109 by Denys Duchier
test_hashcache: catch no exceptions
141
                break
142
            except FixThisError:
143
                pass
144
1185.33.108 by Denys Duchier
test_hashcache_raise: complain if no weird file type can be created
145
        from bzrlib.errors import BzrError
1185.33.106 by Denys Duchier
check that hashcache can raise BzrError
146
        if ok:
1185.33.107 by Denys Duchier
simplified test_hashcache_raise as advised by jam
147
            self.assertRaises(BzrError, hc.get_sha1, 'a')
1185.33.108 by Denys Duchier
test_hashcache_raise: complain if no weird file type can be created
148
        else:
149
            raise BzrError("no weird file type could be created: extend this test case for your os")