Debian Codegraph @

tests/test-filecache.py

raw
1 import os
2 import stat
3 import subprocess
4 import sys
5
6 if subprocess.call(
7 [sys.executable, '%s/hghave' % os.environ['TESTDIR'], 'cacheable']
8 ):
9 sys.exit(80)
10
11 print_ = print
12
13
14 def print(*args, **kwargs):
15 """print() wrapper that flushes stdout buffers to avoid py3 buffer issues
16
17 We could also just write directly to sys.stdout.buffer the way the
18 ui object will, but this was easier for porting the test.
19 """
20 print_(*args, **kwargs)
21 sys.stdout.flush()
22
23
24 from mercurial import (
25 extensions,
26 localrepo,
27 pycompat,
28 ui as uimod,
29 util,
30 vfs as vfsmod,
31 )
32 from mercurial.repo import factory
33
34
35 class fakerepo:
36 def __init__(self):
37 self._filecache = {}
38
39 class fakevfs:
40 def join(self, p):
41 return p
42
43 vfs = fakevfs()
44
45 def unfiltered(self):
46 return self
47
48 def sjoin(self, p):
49 return p
50
51 @localrepo.repofilecache(b'x', b'y')
52 def cached(self):
53 print('creating')
54 return 'string from function'
55
56 def invalidate(self):
57 for k in self._filecache:
58 try:
59 delattr(self, pycompat.sysstr(k))
60 except AttributeError:
61 pass
62
63
64 def basic(repo):
65 print("* neither file exists")
66 # calls function
67 repo.cached
68
69 repo.invalidate()
70 print("* neither file still exists")
71 # uses cache
72 repo.cached
73
74 # create empty file
75 f = open('x', 'wb')
76 f.close()
77 repo.invalidate()
78 print("* empty file x created")
79 # should recreate the object
80 repo.cached
81
82 f = open('x', 'wb')
83 f.write(b'a')
84 f.close()
85 repo.invalidate()
86 print("* file x changed size")
87 # should recreate the object
88 repo.cached
89
90 repo.invalidate()
91 print("* nothing changed with either file")
92 # stats file again, reuses object
93 repo.cached
94
95 # atomic replace file, size doesn't change
96 # hopefully st_mtime doesn't change as well so this doesn't use the cache
97 # because of inode change
98 f = vfsmod.vfs(b'.')(b'x', b'w', atomictemp=True)
99 f.write(b'b')
100 f.close()
101
102 repo.invalidate()
103 print("* file x changed inode")
104 repo.cached
105
106 # create empty file y
107 f = open('y', 'wb')
108 f.close()
109 repo.invalidate()
110 print("* empty file y created")
111 # should recreate the object
112 repo.cached
113
114 f = open('y', 'wb')
115 f.write(b'A')
116 f.close()
117 repo.invalidate()
118 print("* file y changed size")
119 # should recreate the object
120 repo.cached
121
122 f = vfsmod.vfs(b'.')(b'y', b'w', atomictemp=True)
123 f.write(b'B')
124 f.close()
125
126 repo.invalidate()
127 print("* file y changed inode")
128 repo.cached
129
130 f = vfsmod.vfs(b'.')(b'x', b'w', atomictemp=True)
131 f.write(b'c')
132 f.close()
133 f = vfsmod.vfs(b'.')(b'y', b'w', atomictemp=True)
134 f.write(b'C')
135 f.close()
136
137 repo.invalidate()
138 print("* both files changed inode")
139 repo.cached
140
141
142 def fakeuncacheable():
143 def wrapcacheable(orig, *args, **kwargs):
144 return False
145
146 def wrapinit(orig, *args, **kwargs):
147 pass
148
149 originit = extensions.wrapfunction(util.cachestat, '__init__', wrapinit)
150 origcacheable = extensions.wrapfunction(
151 util.cachestat, 'cacheable', wrapcacheable
152 )
153
154 for fn in [b'x', b'y']:
155 try:
156 os.remove(fn)
157 except OSError:
158 pass
159
160 basic(fakerepo())
161
162 util.cachestat.cacheable = origcacheable
163 util.cachestat.__init__ = originit
164
165
166 def test_filecache_synced():
167 # test old behavior that caused filecached properties to go out of sync
168 os.system('hg init && echo a >> a && hg add a && hg ci -qm.')
169 repo = factory.repository(uimod.ui.load())
170 # first rollback clears the filecache, but changelog to stays in __dict__
171 repo.rollback()
172 repo.commit(b'.')
173 # second rollback comes along and touches the changelog externally
174 # (file is moved)
175 repo.rollback()
176 # but since changelog isn't under the filecache control anymore, we don't
177 # see that it changed, and return the old changelog without reconstructing
178 # it
179 repo.commit(b'.')
180
181
182 def setbeforeget(repo):
183 os.remove(b'x')
184 os.remove(b'y')
185 repo.__class__.cached.set(repo, 'string set externally')
186 repo.invalidate()
187 print("* neither file exists")
188 print(repo.cached)
189 repo.invalidate()
190 f = open('x', 'wb')
191 f.write(b'a')
192 f.close()
193 print("* file x created")
194 print(repo.cached)
195
196 repo.__class__.cached.set(repo, 'string 2 set externally')
197 repo.invalidate()
198 print("* string set externally again")
199 print(repo.cached)
200
201 repo.invalidate()
202 f = open('y', 'wb')
203 f.write(b'b')
204 f.close()
205 print("* file y created")
206 print(repo.cached)
207
208
209 def antiambiguity():
210 filename = 'ambigcheck'
211
212 # try some times, because reproduction of ambiguity depends on
213 # "filesystem time"
214 for i in range(5):
215 fp = open(filename, 'wb')
216 fp.write(b'FOO')
217 fp.close()
218
219 oldstat = os.stat(filename)
220 if oldstat[stat.ST_CTIME] != oldstat[stat.ST_MTIME]:
221 # subsequent changing never causes ambiguity
222 continue
223
224 repetition = 3
225
226 # repeat changing via checkambigatclosing, to examine whether
227 # st_mtime is advanced multiple times as expected
228 for i in range(repetition):
229 # explicit closing
230 fp = vfsmod.checkambigatclosing(open(filename, 'ab'))
231 fp.write(b'FOO')
232 fp.close()
233
234 # implicit closing by "with" statement
235 with vfsmod.checkambigatclosing(open(filename, 'ab')) as fp:
236 fp.write(b'BAR')
237
238 newstat = os.stat(filename)
239 if oldstat[stat.ST_CTIME] != newstat[stat.ST_CTIME]:
240 # timestamp ambiguity was naturally avoided while repetition
241 continue
242
243 # st_mtime should be advanced "repetition * 2" times, because
244 # all changes occurred at same time (in sec)
245 expected = (oldstat[stat.ST_MTIME] + repetition * 2) & 0x7FFFFFFF
246 if newstat[stat.ST_MTIME] != expected:
247 print(
248 "'newstat[stat.ST_MTIME] %s is not %s (as %s + %s * 2)"
249 % (
250 newstat[stat.ST_MTIME],
251 expected,
252 oldstat[stat.ST_MTIME],
253 repetition,
254 )
255 )
256
257 # no more examination is needed regardless of result
258 break
259 else:
260 # This platform seems too slow to examine anti-ambiguity
261 # of file timestamp (or test happened to be executed at
262 # bad timing). Exit silently in this case, because running
263 # on other faster platforms can detect problems
264 pass
265
266
267 print('basic:')
268 print()
269 basic(fakerepo())
270 print()
271 print('fakeuncacheable:')
272 print()
273 fakeuncacheable()
274 test_filecache_synced()
275 print()
276 print('setbeforeget:')
277 print()
278 setbeforeget(fakerepo())
279 print()
280 print('antiambiguity:')
281 print()
282 antiambiguity()