tests/test-filecache.py
author Siddharth Agarwal <sid0@fb.com>
Sat, 16 Nov 2013 13:57:35 -0800
changeset 20041 42deff43460a
parent 20040 ed80cecdfc57
child 20045 b3684fd2ff1a
permissions -rw-r--r--
test-filecache.py: add markers to the output for each event Previously it was possible that a different, incorrect set of events might print out 'creating' the same number of times.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     1
import sys, os, subprocess
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     2
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 15518
diff changeset
     3
if subprocess.call(['python', '%s/hghave' % os.environ['TESTDIR'],
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 15518
diff changeset
     4
                    'cacheable']):
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     5
    sys.exit(80)
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     6
18313
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
     7
from mercurial import util, scmutil, extensions, hg, ui
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     8
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     9
filecache = scmutil.filecache
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    10
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    11
class fakerepo(object):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    12
    def __init__(self):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    13
        self._filecache = {}
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    14
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    15
    def join(self, p):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    16
        return p
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    17
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    18
    def sjoin(self, p):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    19
        return p
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    20
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    21
    @filecache('x')
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    22
    def cached(self):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    23
        print 'creating'
20040
ed80cecdfc57 test-filecache.py: make setbeforeget test clearer
Siddharth Agarwal <sid0@fb.com>
parents: 18316
diff changeset
    24
        return 'string from function'
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    25
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    26
    def invalidate(self):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    27
        for k in self._filecache:
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    28
            try:
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    29
                delattr(self, k)
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    30
            except AttributeError:
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    31
                pass
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    32
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    33
def basic(repo):
20041
42deff43460a test-filecache.py: add markers to the output for each event
Siddharth Agarwal <sid0@fb.com>
parents: 20040
diff changeset
    34
    print "* file doesn't exist"
42deff43460a test-filecache.py: add markers to the output for each event
Siddharth Agarwal <sid0@fb.com>
parents: 20040
diff changeset
    35
    # calls function
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    36
    repo.cached
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    37
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    38
    repo.invalidate()
20041
42deff43460a test-filecache.py: add markers to the output for each event
Siddharth Agarwal <sid0@fb.com>
parents: 20040
diff changeset
    39
    print "* file still doesn't exist"
42deff43460a test-filecache.py: add markers to the output for each event
Siddharth Agarwal <sid0@fb.com>
parents: 20040
diff changeset
    40
    # uses cache
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    41
    repo.cached
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    42
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    43
    # create empty file
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    44
    f = open('x', 'w')
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    45
    f.close()
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    46
    repo.invalidate()
20041
42deff43460a test-filecache.py: add markers to the output for each event
Siddharth Agarwal <sid0@fb.com>
parents: 20040
diff changeset
    47
    print "* empty file x created"
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    48
    # should recreate the object
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    49
    repo.cached
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    50
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    51
    f = open('x', 'w')
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    52
    f.write('a')
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    53
    f.close()
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    54
    repo.invalidate()
20041
42deff43460a test-filecache.py: add markers to the output for each event
Siddharth Agarwal <sid0@fb.com>
parents: 20040
diff changeset
    55
    print "* file x changed size"
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    56
    # should recreate the object
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    57
    repo.cached
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    58
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    59
    repo.invalidate()
20041
42deff43460a test-filecache.py: add markers to the output for each event
Siddharth Agarwal <sid0@fb.com>
parents: 20040
diff changeset
    60
    print "* nothing changed with file x"
42deff43460a test-filecache.py: add markers to the output for each event
Siddharth Agarwal <sid0@fb.com>
parents: 20040
diff changeset
    61
    # stats file again, reuses object
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    62
    repo.cached
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    63
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    64
    # atomic replace file, size doesn't change
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    65
    # hopefully st_mtime doesn't change as well so this doesn't use the cache
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    66
    # because of inode change
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    67
    f = scmutil.opener('.')('x', 'w', atomictemp=True)
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    68
    f.write('b')
15057
774da7121fc9 atomictempfile: make close() consistent with other file-like objects.
Greg Ward <greg@gerg.ca>
parents: 14982
diff changeset
    69
    f.close()
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    70
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    71
    repo.invalidate()
20041
42deff43460a test-filecache.py: add markers to the output for each event
Siddharth Agarwal <sid0@fb.com>
parents: 20040
diff changeset
    72
    print "* file x changed inode"
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    73
    repo.cached
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    74
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    75
def fakeuncacheable():
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    76
    def wrapcacheable(orig, *args, **kwargs):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    77
        return False
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    78
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    79
    def wrapinit(orig, *args, **kwargs):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    80
        pass
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    81
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    82
    originit = extensions.wrapfunction(util.cachestat, '__init__', wrapinit)
14937
0b3e57c1b8c0 filecache: fix check-code complaint
Matt Mackall <mpm@selenic.com>
parents: 14928
diff changeset
    83
    origcacheable = extensions.wrapfunction(util.cachestat, 'cacheable',
0b3e57c1b8c0 filecache: fix check-code complaint
Matt Mackall <mpm@selenic.com>
parents: 14928
diff changeset
    84
                                            wrapcacheable)
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    85
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    86
    try:
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    87
        os.remove('x')
16688
cfb6682961b8 cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 16683
diff changeset
    88
    except OSError:
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    89
        pass
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    90
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    91
    basic(fakerepo())
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    92
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    93
    util.cachestat.cacheable = origcacheable
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    94
    util.cachestat.__init__ = originit
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    95
18313
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
    96
def test_filecache_synced():
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
    97
    # test old behaviour that caused filecached properties to go out of sync
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
    98
    os.system('hg init && echo a >> a && hg ci -qAm.')
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
    99
    repo = hg.repository(ui.ui())
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
   100
    # first rollback clears the filecache, but changelog to stays in __dict__
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
   101
    repo.rollback()
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
   102
    repo.commit('.')
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
   103
    # second rollback comes along and touches the changelog externally
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
   104
    # (file is moved)
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
   105
    repo.rollback()
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
   106
    # but since changelog isn't under the filecache control anymore, we don't
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
   107
    # see that it changed, and return the old changelog without reconstructing
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
   108
    # it
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
   109
    repo.commit('.')
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
   110
18316
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   111
def setbeforeget(repo):
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   112
    os.remove('x')
20040
ed80cecdfc57 test-filecache.py: make setbeforeget test clearer
Siddharth Agarwal <sid0@fb.com>
parents: 18316
diff changeset
   113
    repo.cached = 'string set externally'
18316
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   114
    repo.invalidate()
20041
42deff43460a test-filecache.py: add markers to the output for each event
Siddharth Agarwal <sid0@fb.com>
parents: 20040
diff changeset
   115
    print "* file x doesn't exist"
18316
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   116
    print repo.cached
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   117
    repo.invalidate()
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   118
    f = open('x', 'w')
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   119
    f.write('a')
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   120
    f.close()
20041
42deff43460a test-filecache.py: add markers to the output for each event
Siddharth Agarwal <sid0@fb.com>
parents: 20040
diff changeset
   121
    print "* file x created"
18316
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   122
    print repo.cached
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   123
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
   124
print 'basic:'
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
   125
print
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
   126
basic(fakerepo())
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
   127
print
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
   128
print 'fakeuncacheable:'
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
   129
print
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
   130
fakeuncacheable()
18313
3e4a944c0d04 destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents: 16688
diff changeset
   131
test_filecache_synced()
18316
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   132
print
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   133
print 'setbeforeget:'
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   134
print
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18313
diff changeset
   135
setbeforeget(fakerepo())