tests/test-filecache.py
changeset 20045 b3684fd2ff1a
parent 20041 42deff43460a
child 26098 ce26928cbe41
equal deleted inserted replaced
20044:d38de18d187a 20045:b3684fd2ff1a
    16         return p
    16         return p
    17 
    17 
    18     def sjoin(self, p):
    18     def sjoin(self, p):
    19         return p
    19         return p
    20 
    20 
    21     @filecache('x')
    21     @filecache('x', 'y')
    22     def cached(self):
    22     def cached(self):
    23         print 'creating'
    23         print 'creating'
    24         return 'string from function'
    24         return 'string from function'
    25 
    25 
    26     def invalidate(self):
    26     def invalidate(self):
    29                 delattr(self, k)
    29                 delattr(self, k)
    30             except AttributeError:
    30             except AttributeError:
    31                 pass
    31                 pass
    32 
    32 
    33 def basic(repo):
    33 def basic(repo):
    34     print "* file doesn't exist"
    34     print "* neither file exists"
    35     # calls function
    35     # calls function
    36     repo.cached
    36     repo.cached
    37 
    37 
    38     repo.invalidate()
    38     repo.invalidate()
    39     print "* file still doesn't exist"
    39     print "* neither file still exists"
    40     # uses cache
    40     # uses cache
    41     repo.cached
    41     repo.cached
    42 
    42 
    43     # create empty file
    43     # create empty file
    44     f = open('x', 'w')
    44     f = open('x', 'w')
    55     print "* file x changed size"
    55     print "* file x changed size"
    56     # should recreate the object
    56     # should recreate the object
    57     repo.cached
    57     repo.cached
    58 
    58 
    59     repo.invalidate()
    59     repo.invalidate()
    60     print "* nothing changed with file x"
    60     print "* nothing changed with either file"
    61     # stats file again, reuses object
    61     # stats file again, reuses object
    62     repo.cached
    62     repo.cached
    63 
    63 
    64     # atomic replace file, size doesn't change
    64     # atomic replace file, size doesn't change
    65     # hopefully st_mtime doesn't change as well so this doesn't use the cache
    65     # hopefully st_mtime doesn't change as well so this doesn't use the cache
    70 
    70 
    71     repo.invalidate()
    71     repo.invalidate()
    72     print "* file x changed inode"
    72     print "* file x changed inode"
    73     repo.cached
    73     repo.cached
    74 
    74 
       
    75     # create empty file y
       
    76     f = open('y', 'w')
       
    77     f.close()
       
    78     repo.invalidate()
       
    79     print "* empty file y created"
       
    80     # should recreate the object
       
    81     repo.cached
       
    82 
       
    83     f = open('y', 'w')
       
    84     f.write('A')
       
    85     f.close()
       
    86     repo.invalidate()
       
    87     print "* file y changed size"
       
    88     # should recreate the object
       
    89     repo.cached
       
    90 
       
    91     f = scmutil.opener('.')('y', 'w', atomictemp=True)
       
    92     f.write('B')
       
    93     f.close()
       
    94 
       
    95     repo.invalidate()
       
    96     print "* file y changed inode"
       
    97     repo.cached
       
    98 
       
    99     f = scmutil.opener('.')('x', 'w', atomictemp=True)
       
   100     f.write('c')
       
   101     f.close()
       
   102     f = scmutil.opener('.')('y', 'w', atomictemp=True)
       
   103     f.write('C')
       
   104     f.close()
       
   105 
       
   106     repo.invalidate()
       
   107     print "* both files changed inode"
       
   108     repo.cached
       
   109 
    75 def fakeuncacheable():
   110 def fakeuncacheable():
    76     def wrapcacheable(orig, *args, **kwargs):
   111     def wrapcacheable(orig, *args, **kwargs):
    77         return False
   112         return False
    78 
   113 
    79     def wrapinit(orig, *args, **kwargs):
   114     def wrapinit(orig, *args, **kwargs):
    81 
   116 
    82     originit = extensions.wrapfunction(util.cachestat, '__init__', wrapinit)
   117     originit = extensions.wrapfunction(util.cachestat, '__init__', wrapinit)
    83     origcacheable = extensions.wrapfunction(util.cachestat, 'cacheable',
   118     origcacheable = extensions.wrapfunction(util.cachestat, 'cacheable',
    84                                             wrapcacheable)
   119                                             wrapcacheable)
    85 
   120 
    86     try:
   121     for fn in ['x', 'y']:
    87         os.remove('x')
   122         try:
    88     except OSError:
   123             os.remove(fn)
    89         pass
   124         except OSError:
       
   125             pass
    90 
   126 
    91     basic(fakerepo())
   127     basic(fakerepo())
    92 
   128 
    93     util.cachestat.cacheable = origcacheable
   129     util.cachestat.cacheable = origcacheable
    94     util.cachestat.__init__ = originit
   130     util.cachestat.__init__ = originit
   108     # it
   144     # it
   109     repo.commit('.')
   145     repo.commit('.')
   110 
   146 
   111 def setbeforeget(repo):
   147 def setbeforeget(repo):
   112     os.remove('x')
   148     os.remove('x')
       
   149     os.remove('y')
   113     repo.cached = 'string set externally'
   150     repo.cached = 'string set externally'
   114     repo.invalidate()
   151     repo.invalidate()
   115     print "* file x doesn't exist"
   152     print "* neither file exists"
   116     print repo.cached
   153     print repo.cached
   117     repo.invalidate()
   154     repo.invalidate()
   118     f = open('x', 'w')
   155     f = open('x', 'w')
   119     f.write('a')
   156     f.write('a')
   120     f.close()
   157     f.close()
   121     print "* file x created"
   158     print "* file x created"
       
   159     print repo.cached
       
   160 
       
   161     repo.cached = 'string 2 set externally'
       
   162     repo.invalidate()
       
   163     print "* string set externally again"
       
   164     print repo.cached
       
   165 
       
   166     repo.invalidate()
       
   167     f = open('y', 'w')
       
   168     f.write('b')
       
   169     f.close()
       
   170     print "* file y created"
   122     print repo.cached
   171     print repo.cached
   123 
   172 
   124 print 'basic:'
   173 print 'basic:'
   125 print
   174 print
   126 basic(fakerepo())
   175 basic(fakerepo())