Mercurial > hg
annotate tests/wireprotosimplecache.py @ 40023:10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
This is pretty straightforward.
Redirect targets will require an extension to support. So we've added
a function that can be wrapped to define redirect targets.
To test this, we teach our simple cache test extension to read
redirect targets from a file. It's a bit hacky. But it gets the
job done.
Differential Revision: https://phab.mercurial-scm.org/D4775
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 26 Sep 2018 17:46:48 -0700 |
parents | c537144fdbef |
children | b099e6032f38 |
rev | line source |
---|---|
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
1 # wireprotosimplecache.py - Extension providing in-memory wire protocol cache |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
2 # |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
3 # Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com> |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
4 # |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
7 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
8 from __future__ import absolute_import |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
9 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
10 from mercurial import ( |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
11 extensions, |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
12 registrar, |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
13 repository, |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
14 util, |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
15 wireprototypes, |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
16 wireprotov2server, |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
17 ) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
18 from mercurial.utils import ( |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
19 interfaceutil, |
40023
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
20 stringutil, |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
21 ) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
22 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
23 CACHE = None |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
24 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
25 configtable = {} |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
26 configitem = registrar.configitem(configtable) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
27 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
28 configitem('simplecache', 'cacheobjects', |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
29 default=False) |
40023
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
30 configitem('simplecache', 'redirectsfile', |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
31 default=None) |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
32 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
33 @interfaceutil.implementer(repository.iwireprotocolcommandcacher) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
34 class memorycacher(object): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
35 def __init__(self, ui, command, encodefn): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
36 self.ui = ui |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
37 self.encodefn = encodefn |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
38 self.key = None |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
39 self.cacheobjects = ui.configbool('simplecache', 'cacheobjects') |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
40 self.buffered = [] |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
41 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
42 ui.log('simplecache', 'cacher constructed for %s\n', command) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
43 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
44 def __enter__(self): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
45 return self |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
46 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
47 def __exit__(self, exctype, excvalue, exctb): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
48 if exctype: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
49 self.ui.log('simplecache', 'cacher exiting due to error\n') |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
50 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
51 def adjustcachekeystate(self, state): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
52 # Needed in order to make tests deterministic. Don't copy this |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
53 # pattern for production caches! |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
54 del state[b'repo'] |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
55 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
56 def setcachekey(self, key): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
57 self.key = key |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
58 return True |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
59 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
60 def lookup(self): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
61 if self.key not in CACHE: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
62 self.ui.log('simplecache', 'cache miss for %s\n', self.key) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
63 return None |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
64 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
65 entry = CACHE[self.key] |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
66 self.ui.log('simplecache', 'cache hit for %s\n', self.key) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
67 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
68 if self.cacheobjects: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
69 return { |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
70 'objs': entry, |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
71 } |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
72 else: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
73 return { |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
74 'objs': [wireprototypes.encodedresponse(entry)], |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
75 } |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
76 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
77 def onobject(self, obj): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
78 if self.cacheobjects: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
79 self.buffered.append(obj) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
80 else: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
81 self.buffered.extend(self.encodefn(obj)) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
82 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
83 yield obj |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
84 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
85 def onfinished(self): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
86 self.ui.log('simplecache', 'storing cache entry for %s\n', self.key) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
87 if self.cacheobjects: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
88 CACHE[self.key] = self.buffered |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
89 else: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
90 CACHE[self.key] = b''.join(self.buffered) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
91 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
92 return [] |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
93 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
94 def makeresponsecacher(orig, repo, proto, command, args, objencoderfn): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
95 return memorycacher(repo.ui, command, objencoderfn) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
96 |
40023
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
97 def loadredirecttargets(ui): |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
98 path = ui.config('simplecache', 'redirectsfile') |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
99 if not path: |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
100 return [] |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
101 |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
102 with open(path, 'rb') as fh: |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
103 s = fh.read() |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
104 |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
105 return stringutil.evalpythonliteral(s) |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
106 |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
107 def getadvertisedredirecttargets(orig, repo, proto): |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
108 return loadredirecttargets(repo.ui) |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
109 |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
110 def extsetup(ui): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
111 global CACHE |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
112 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
113 CACHE = util.lrucachedict(10000) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
114 |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
115 extensions.wrapfunction(wireprotov2server, 'makeresponsecacher', |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
116 makeresponsecacher) |
40023
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
117 extensions.wrapfunction(wireprotov2server, 'getadvertisedredirecttargets', |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
118 getadvertisedredirecttargets) |