test-filecache.py
248 lines
| 6.2 KiB
| text/x-python
|
PythonLexer
/ tests / test-filecache.py
|
r28742 | from __future__ import absolute_import, print_function | ||
|
r28741 | import os | ||
import subprocess | ||||
import sys | ||||
|
r14928 | |||
|
r16683 | if subprocess.call(['python', '%s/hghave' % os.environ['TESTDIR'], | ||
'cacheable']): | ||||
|
r14928 | sys.exit(80) | ||
|
r28802 | from mercurial import ( | ||
extensions, | ||||
hg, | ||||
scmutil, | ||||
|
r28803 | ui as uimod, | ||
|
r28802 | util, | ||
|
r31251 | vfs as vfsmod, | ||
|
r28802 | ) | ||
|
r14928 | |||
filecache = scmutil.filecache | ||||
class fakerepo(object): | ||||
def __init__(self): | ||||
self._filecache = {} | ||||
def join(self, p): | ||||
return p | ||||
def sjoin(self, p): | ||||
return p | ||||
|
r20045 | @filecache('x', 'y') | ||
|
r14928 | def cached(self): | ||
|
r28742 | print('creating') | ||
|
r20040 | return 'string from function' | ||
|
r14928 | |||
def invalidate(self): | ||||
for k in self._filecache: | ||||
try: | ||||
delattr(self, k) | ||||
except AttributeError: | ||||
pass | ||||
def basic(repo): | ||||
|
r28742 | print("* neither file exists") | ||
|
r20041 | # calls function | ||
|
r14928 | repo.cached | ||
repo.invalidate() | ||||
|
r28742 | print("* neither file still exists") | ||
|
r20041 | # uses cache | ||
|
r14928 | repo.cached | ||
# create empty file | ||||
f = open('x', 'w') | ||||
f.close() | ||||
repo.invalidate() | ||||
|
r28742 | print("* empty file x created") | ||
|
r14928 | # should recreate the object | ||
repo.cached | ||||
f = open('x', 'w') | ||||
f.write('a') | ||||
f.close() | ||||
repo.invalidate() | ||||
|
r28742 | print("* file x changed size") | ||
|
r14928 | # should recreate the object | ||
repo.cached | ||||
repo.invalidate() | ||||
|
r28742 | print("* nothing changed with either file") | ||
|
r20041 | # stats file again, reuses object | ||
|
r14928 | repo.cached | ||
# atomic replace file, size doesn't change | ||||
# hopefully st_mtime doesn't change as well so this doesn't use the cache | ||||
# because of inode change | ||||
|
r31251 | f = vfsmod.vfs('.')('x', 'w', atomictemp=True) | ||
|
r14928 | f.write('b') | ||
|
r15057 | f.close() | ||
|
r14928 | |||
repo.invalidate() | ||||
|
r28742 | print("* file x changed inode") | ||
|
r14928 | repo.cached | ||
|
r20045 | # create empty file y | ||
f = open('y', 'w') | ||||
f.close() | ||||
repo.invalidate() | ||||
|
r28742 | print("* empty file y created") | ||
|
r20045 | # should recreate the object | ||
repo.cached | ||||
f = open('y', 'w') | ||||
f.write('A') | ||||
f.close() | ||||
repo.invalidate() | ||||
|
r28742 | print("* file y changed size") | ||
|
r20045 | # should recreate the object | ||
repo.cached | ||||
|
r31251 | f = vfsmod.vfs('.')('y', 'w', atomictemp=True) | ||
|
r20045 | f.write('B') | ||
f.close() | ||||
repo.invalidate() | ||||
|
r28742 | print("* file y changed inode") | ||
|
r20045 | repo.cached | ||
|
r31251 | f = vfsmod.vfs('.')('x', 'w', atomictemp=True) | ||
|
r20045 | f.write('c') | ||
f.close() | ||||
|
r31251 | f = vfsmod.vfs('.')('y', 'w', atomictemp=True) | ||
|
r20045 | f.write('C') | ||
f.close() | ||||
repo.invalidate() | ||||
|
r28742 | print("* both files changed inode") | ||
|
r20045 | repo.cached | ||
|
r14928 | def fakeuncacheable(): | ||
def wrapcacheable(orig, *args, **kwargs): | ||||
return False | ||||
def wrapinit(orig, *args, **kwargs): | ||||
pass | ||||
originit = extensions.wrapfunction(util.cachestat, '__init__', wrapinit) | ||||
|
r14937 | origcacheable = extensions.wrapfunction(util.cachestat, 'cacheable', | ||
wrapcacheable) | ||||
|
r14928 | |||
|
r20045 | for fn in ['x', 'y']: | ||
try: | ||||
os.remove(fn) | ||||
except OSError: | ||||
pass | ||||
|
r14928 | |||
basic(fakerepo()) | ||||
util.cachestat.cacheable = origcacheable | ||||
util.cachestat.__init__ = originit | ||||
|
r18313 | def test_filecache_synced(): | ||
|
r26098 | # test old behavior that caused filecached properties to go out of sync | ||
|
r18313 | os.system('hg init && echo a >> a && hg ci -qAm.') | ||
|
r30559 | repo = hg.repository(uimod.ui.load()) | ||
|
r18313 | # first rollback clears the filecache, but changelog to stays in __dict__ | ||
repo.rollback() | ||||
repo.commit('.') | ||||
# second rollback comes along and touches the changelog externally | ||||
# (file is moved) | ||||
repo.rollback() | ||||
# but since changelog isn't under the filecache control anymore, we don't | ||||
# see that it changed, and return the old changelog without reconstructing | ||||
# it | ||||
repo.commit('.') | ||||
|
r18316 | def setbeforeget(repo): | ||
os.remove('x') | ||||
|
r20045 | os.remove('y') | ||
|
r20040 | repo.cached = 'string set externally' | ||
|
r18316 | repo.invalidate() | ||
|
r28742 | print("* neither file exists") | ||
print(repo.cached) | ||||
|
r18316 | repo.invalidate() | ||
f = open('x', 'w') | ||||
f.write('a') | ||||
f.close() | ||||
|
r28742 | print("* file x created") | ||
print(repo.cached) | ||||
|
r18316 | |||
|
r20045 | repo.cached = 'string 2 set externally' | ||
repo.invalidate() | ||||
|
r28742 | print("* string set externally again") | ||
print(repo.cached) | ||||
|
r20045 | |||
repo.invalidate() | ||||
f = open('y', 'w') | ||||
f.write('b') | ||||
f.close() | ||||
|
r28742 | print("* file y created") | ||
print(repo.cached) | ||||
|
r20045 | |||
|
r29995 | def antiambiguity(): | ||
filename = 'ambigcheck' | ||||
# try some times, because reproduction of ambiguity depends on | ||||
# "filesystem time" | ||||
for i in xrange(5): | ||||
fp = open(filename, 'w') | ||||
fp.write('FOO') | ||||
fp.close() | ||||
oldstat = os.stat(filename) | ||||
if oldstat.st_ctime != oldstat.st_mtime: | ||||
# subsequent changing never causes ambiguity | ||||
continue | ||||
repetition = 3 | ||||
# repeat changing via checkambigatclosing, to examine whether | ||||
|
r30332 | # st_mtime is advanced multiple times as expected | ||
|
r29995 | for i in xrange(repetition): | ||
# explicit closing | ||||
|
r31251 | fp = vfsmod.checkambigatclosing(open(filename, 'a')) | ||
|
r29995 | fp.write('FOO') | ||
fp.close() | ||||
# implicit closing by "with" statement | ||||
|
r31251 | with vfsmod.checkambigatclosing(open(filename, 'a')) as fp: | ||
|
r29995 | fp.write('BAR') | ||
newstat = os.stat(filename) | ||||
if oldstat.st_ctime != newstat.st_ctime: | ||||
# timestamp ambiguity was naturally avoided while repetition | ||||
continue | ||||
# st_mtime should be advanced "repetition * 2" times, because | ||||
|
r30332 | # all changes occurred at same time (in sec) | ||
|
r29995 | expected = (oldstat.st_mtime + repetition * 2) & 0x7fffffff | ||
if newstat.st_mtime != expected: | ||||
print("'newstat.st_mtime %s is not %s (as %s + %s * 2)" % | ||||
(newstat.st_mtime, expected, oldstat.st_mtime, repetition)) | ||||
# no more examination is needed regardless of result | ||||
break | ||||
else: | ||||
# This platform seems too slow to examine anti-ambiguity | ||||
# of file timestamp (or test happened to be executed at | ||||
# bad timing). Exit silently in this case, because running | ||||
# on other faster platforms can detect problems | ||||
pass | ||||
|
r28742 | print('basic:') | ||
print() | ||||
|
r14928 | basic(fakerepo()) | ||
|
r28742 | print() | ||
print('fakeuncacheable:') | ||||
print() | ||||
|
r14928 | fakeuncacheable() | ||
|
r18313 | test_filecache_synced() | ||
|
r28742 | print() | ||
print('setbeforeget:') | ||||
print() | ||||
|
r18316 | setbeforeget(fakerepo()) | ||
|
r29995 | print() | ||
print('antiambiguity:') | ||||
print() | ||||
antiambiguity() | ||||