Debian Codegraph @

tests/test-wireproto.py

raw
1 import sys
2
3 from mercurial import (
4 error,
5 pycompat,
6 ui as uimod,
7 util,
8 wireprototypes,
9 wireprotov1peer,
10 wireprotov1server,
11 )
12 from mercurial.utils import stringutil
13
14 stringio = util.stringio
15
16
17 class proto:
18 def __init__(self, args):
19 self.args = args
20 self.name = 'dummyproto'
21
22 def getargs(self, spec):
23 args = self.args
24 args.setdefault(b'*', {})
25 names = spec.split()
26 return [args[n] for n in names]
27
28 def checkperm(self, perm):
29 pass
30
31
32 wireprototypes.TRANSPORTS['dummyproto'] = {
33 'transport': 'dummy',
34 'version': 1,
35 }
36
37
38 class clientpeer(wireprotov1peer.wirepeer):
39 def __init__(self, serverrepo, ui):
40 self.serverrepo = serverrepo
41 self.ui = ui
42
43 def url(self):
44 return b'test'
45
46 def local(self):
47 return None
48
49 def peer(self):
50 return self
51
52 def canpush(self):
53 return True
54
55 def close(self):
56 pass
57
58 def capabilities(self) -> set[bytes]:
59 return {b'batch'}
60
61 def _call(self, cmd, **args):
62 args = pycompat.byteskwargs(args)
63 res = wireprotov1server.dispatch(self.serverrepo, proto(args), cmd)
64 if isinstance(res, wireprototypes.bytesresponse):
65 return res.data
66 elif isinstance(res, bytes):
67 return res
68 else:
69 raise error.Abort(b'dummy client does not support response type')
70
71 def _callstream(self, cmd, **args):
72 return stringio(self._call(cmd, **args))
73
74 @wireprotov1peer.batchable
75 def greet(self, name):
76 return {b'name': mangle(name)}, unmangle
77
78
79 class serverrepo:
80 def __init__(self, ui):
81 self.ui = ui
82
83 def greet(self, name):
84 return b"Hello, " + name
85
86 def filtered(self, name):
87 return self
88
89
90 def mangle(s):
91 return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s))
92
93
94 def unmangle(s):
95 return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s))
96
97
98 def greet(repo, proto, name):
99 return mangle(repo.greet(unmangle(name)))
100
101
102 wireprotov1server.commands[b'greet'] = (greet, b'name')
103
104 srv = serverrepo(uimod.ui())
105 clt = clientpeer(srv, uimod.ui())
106
107
108 def printb(data, end=b'\n'):
109 out = getattr(sys.stdout, 'buffer', sys.stdout)
110 out.write(data + end)
111 out.flush()
112
113
114 printb(clt.greet(b"Foobar"))
115
116 with clt.commandexecutor() as e:
117 fgreet1 = e.callcommand(b'greet', {b'name': b'Fo, =;:<o'})
118 fgreet2 = e.callcommand(b'greet', {b'name': b'Bar'})
119
120 printb(
121 stringutil.pprint([f.result() for f in (fgreet1, fgreet2)], bprefix=True)
122 )