2 |
2 |
3 import unittest |
3 import unittest |
4 |
4 |
5 from mercurial import ( |
5 from mercurial import ( |
6 error, |
6 error, |
|
7 ui as uimod, |
7 wireprotoframing as framing, |
8 wireprotoframing as framing, |
8 ) |
9 ) |
9 from mercurial.utils import ( |
10 from mercurial.utils import ( |
10 cborutil, |
11 cborutil, |
11 ) |
12 ) |
12 |
13 |
13 ffs = framing.makeframefromhumanstring |
14 ffs = framing.makeframefromhumanstring |
|
15 |
|
16 globalui = uimod.ui() |
14 |
17 |
15 def sendframe(reactor, frame): |
18 def sendframe(reactor, frame): |
16 """Send a frame bytearray to a reactor.""" |
19 """Send a frame bytearray to a reactor.""" |
17 header = framing.parseheader(frame) |
20 header = framing.parseheader(frame) |
18 payload = frame[framing.FRAME_HEADER_SIZE:] |
21 payload = frame[framing.FRAME_HEADER_SIZE:] |
33 # the regex version. |
36 # the regex version. |
34 assertRaisesRegex = (# camelcase-required |
37 assertRaisesRegex = (# camelcase-required |
35 unittest.TestCase.assertRaisesRegexp) |
38 unittest.TestCase.assertRaisesRegexp) |
36 |
39 |
37 def testbasic(self): |
40 def testbasic(self): |
38 reactor = framing.clientreactor(hasmultiplesend=False, buffersends=True) |
41 reactor = framing.clientreactor(globalui, |
|
42 hasmultiplesend=False, |
|
43 buffersends=True) |
39 |
44 |
40 request, action, meta = reactor.callcommand(b'foo', {}) |
45 request, action, meta = reactor.callcommand(b'foo', {}) |
41 self.assertEqual(request.state, b'pending') |
46 self.assertEqual(request.state, b'pending') |
42 self.assertEqual(action, b'noop') |
47 self.assertEqual(action, b'noop') |
43 |
48 |
58 reactor.callcommand(b'foo', {}) |
63 reactor.callcommand(b'foo', {}) |
59 |
64 |
60 class NoBufferTests(unittest.TestCase): |
65 class NoBufferTests(unittest.TestCase): |
61 """A reactor without send buffering sends requests immediately.""" |
66 """A reactor without send buffering sends requests immediately.""" |
62 def testbasic(self): |
67 def testbasic(self): |
63 reactor = framing.clientreactor(hasmultiplesend=True, buffersends=False) |
68 reactor = framing.clientreactor(globalui, |
|
69 hasmultiplesend=True, |
|
70 buffersends=False) |
64 |
71 |
65 request, action, meta = reactor.callcommand(b'command1', {}) |
72 request, action, meta = reactor.callcommand(b'command1', {}) |
66 self.assertEqual(request.requestid, 1) |
73 self.assertEqual(request.requestid, 1) |
67 self.assertEqual(action, b'sendframes') |
74 self.assertEqual(action, b'sendframes') |
68 |
75 |
92 # the regex version. |
99 # the regex version. |
93 assertRaisesRegex = (# camelcase-required |
100 assertRaisesRegex = (# camelcase-required |
94 unittest.TestCase.assertRaisesRegexp) |
101 unittest.TestCase.assertRaisesRegexp) |
95 |
102 |
96 def testoddstream(self): |
103 def testoddstream(self): |
97 reactor = framing.clientreactor() |
104 reactor = framing.clientreactor(globalui) |
98 |
105 |
99 action, meta = sendframe(reactor, ffs(b'1 1 0 1 0 foo')) |
106 action, meta = sendframe(reactor, ffs(b'1 1 0 1 0 foo')) |
100 self.assertEqual(action, b'error') |
107 self.assertEqual(action, b'error') |
101 self.assertEqual(meta[b'message'], |
108 self.assertEqual(meta[b'message'], |
102 b'received frame with odd numbered stream ID: 1') |
109 b'received frame with odd numbered stream ID: 1') |
103 |
110 |
104 def testunknownstream(self): |
111 def testunknownstream(self): |
105 reactor = framing.clientreactor() |
112 reactor = framing.clientreactor(globalui) |
106 |
113 |
107 action, meta = sendframe(reactor, ffs(b'1 0 0 1 0 foo')) |
114 action, meta = sendframe(reactor, ffs(b'1 0 0 1 0 foo')) |
108 self.assertEqual(action, b'error') |
115 self.assertEqual(action, b'error') |
109 self.assertEqual(meta[b'message'], |
116 self.assertEqual(meta[b'message'], |
110 b'received frame on unknown stream without beginning ' |
117 b'received frame on unknown stream without beginning ' |
111 b'of stream flag set') |
118 b'of stream flag set') |
112 |
119 |
113 def testunhandledframetype(self): |
120 def testunhandledframetype(self): |
114 reactor = framing.clientreactor(buffersends=False) |
121 reactor = framing.clientreactor(globalui, buffersends=False) |
115 |
122 |
116 request, action, meta = reactor.callcommand(b'foo', {}) |
123 request, action, meta = reactor.callcommand(b'foo', {}) |
117 for frame in meta[b'framegen']: |
124 for frame in meta[b'framegen']: |
118 pass |
125 pass |
119 |
126 |
121 'unhandled frame type'): |
128 'unhandled frame type'): |
122 sendframe(reactor, ffs(b'1 0 stream-begin text-output 0 foo')) |
129 sendframe(reactor, ffs(b'1 0 stream-begin text-output 0 foo')) |
123 |
130 |
124 class StreamTests(unittest.TestCase): |
131 class StreamTests(unittest.TestCase): |
125 def testmultipleresponseframes(self): |
132 def testmultipleresponseframes(self): |
126 reactor = framing.clientreactor(buffersends=False) |
133 reactor = framing.clientreactor(globalui, buffersends=False) |
127 |
134 |
128 request, action, meta = reactor.callcommand(b'foo', {}) |
135 request, action, meta = reactor.callcommand(b'foo', {}) |
129 |
136 |
130 self.assertEqual(action, b'sendframes') |
137 self.assertEqual(action, b'sendframes') |
131 for f in meta[b'framegen']: |
138 for f in meta[b'framegen']: |
142 ffs(b'%d 0 0 command-response eos bar' % request.requestid)) |
149 ffs(b'%d 0 0 command-response eos bar' % request.requestid)) |
143 self.assertEqual(action, b'responsedata') |
150 self.assertEqual(action, b'responsedata') |
144 |
151 |
145 class RedirectTests(unittest.TestCase): |
152 class RedirectTests(unittest.TestCase): |
146 def testredirect(self): |
153 def testredirect(self): |
147 reactor = framing.clientreactor(buffersends=False) |
154 reactor = framing.clientreactor(globalui, buffersends=False) |
148 |
155 |
149 redirect = { |
156 redirect = { |
150 b'targets': [b'a', b'b'], |
157 b'targets': [b'a', b'b'], |
151 b'hashes': [b'sha256'], |
158 b'hashes': [b'sha256'], |
152 } |
159 } |
165 b"b'redirect': {b'targets': [b'a', b'b'], " |
172 b"b'redirect': {b'targets': [b'a', b'b'], " |
166 b"b'hashes': [b'sha256']}}")) |
173 b"b'hashes': [b'sha256']}}")) |
167 |
174 |
168 class StreamSettingsTests(unittest.TestCase): |
175 class StreamSettingsTests(unittest.TestCase): |
169 def testnoflags(self): |
176 def testnoflags(self): |
170 reactor = framing.clientreactor(buffersends=False) |
177 reactor = framing.clientreactor(globalui, buffersends=False) |
171 |
178 |
172 request, action, meta = reactor.callcommand(b'foo', {}) |
179 request, action, meta = reactor.callcommand(b'foo', {}) |
173 for f in meta[b'framegen']: |
180 for f in meta[b'framegen']: |
174 pass |
181 pass |
175 |
182 |
181 b'message': b'stream encoding settings frame must have ' |
188 b'message': b'stream encoding settings frame must have ' |
182 b'continuation or end of stream flag set', |
189 b'continuation or end of stream flag set', |
183 }) |
190 }) |
184 |
191 |
185 def testconflictflags(self): |
192 def testconflictflags(self): |
186 reactor = framing.clientreactor(buffersends=False) |
193 reactor = framing.clientreactor(globalui, buffersends=False) |
187 |
194 |
188 request, action, meta = reactor.callcommand(b'foo', {}) |
195 request, action, meta = reactor.callcommand(b'foo', {}) |
189 for f in meta[b'framegen']: |
196 for f in meta[b'framegen']: |
190 pass |
197 pass |
191 |
198 |
197 b'message': b'stream encoding settings frame cannot have both ' |
204 b'message': b'stream encoding settings frame cannot have both ' |
198 b'continuation and end of stream flags set', |
205 b'continuation and end of stream flags set', |
199 }) |
206 }) |
200 |
207 |
201 def testemptypayload(self): |
208 def testemptypayload(self): |
202 reactor = framing.clientreactor(buffersends=False) |
209 reactor = framing.clientreactor(globalui, buffersends=False) |
203 |
210 |
204 request, action, meta = reactor.callcommand(b'foo', {}) |
211 request, action, meta = reactor.callcommand(b'foo', {}) |
205 for f in meta[b'framegen']: |
212 for f in meta[b'framegen']: |
206 pass |
213 pass |
207 |
214 |
213 b'message': b'stream encoding settings frame did not contain ' |
220 b'message': b'stream encoding settings frame did not contain ' |
214 b'CBOR data' |
221 b'CBOR data' |
215 }) |
222 }) |
216 |
223 |
217 def testbadcbor(self): |
224 def testbadcbor(self): |
218 reactor = framing.clientreactor(buffersends=False) |
225 reactor = framing.clientreactor(globalui, buffersends=False) |
219 |
226 |
220 request, action, meta = reactor.callcommand(b'foo', {}) |
227 request, action, meta = reactor.callcommand(b'foo', {}) |
221 for f in meta[b'framegen']: |
228 for f in meta[b'framegen']: |
222 pass |
229 pass |
223 |
230 |
225 ffs(b'1 2 stream-begin stream-settings eos badvalue')) |
232 ffs(b'1 2 stream-begin stream-settings eos badvalue')) |
226 |
233 |
227 self.assertEqual(action, b'error') |
234 self.assertEqual(action, b'error') |
228 |
235 |
229 def testsingleobject(self): |
236 def testsingleobject(self): |
230 reactor = framing.clientreactor(buffersends=False) |
237 reactor = framing.clientreactor(globalui, buffersends=False) |
231 |
238 |
232 request, action, meta = reactor.callcommand(b'foo', {}) |
239 request, action, meta = reactor.callcommand(b'foo', {}) |
233 for f in meta[b'framegen']: |
240 for f in meta[b'framegen']: |
234 pass |
241 pass |
235 |
242 |
238 |
245 |
239 self.assertEqual(action, b'noop') |
246 self.assertEqual(action, b'noop') |
240 self.assertEqual(meta, {}) |
247 self.assertEqual(meta, {}) |
241 |
248 |
242 def testmultipleobjects(self): |
249 def testmultipleobjects(self): |
243 reactor = framing.clientreactor(buffersends=False) |
250 reactor = framing.clientreactor(globalui, buffersends=False) |
244 |
251 |
245 request, action, meta = reactor.callcommand(b'foo', {}) |
252 request, action, meta = reactor.callcommand(b'foo', {}) |
246 for f in meta[b'framegen']: |
253 for f in meta[b'framegen']: |
247 pass |
254 pass |
248 |
255 |
256 |
263 |
257 self.assertEqual(action, b'noop') |
264 self.assertEqual(action, b'noop') |
258 self.assertEqual(meta, {}) |
265 self.assertEqual(meta, {}) |
259 |
266 |
260 def testmultipleframes(self): |
267 def testmultipleframes(self): |
261 reactor = framing.clientreactor(buffersends=False) |
268 reactor = framing.clientreactor(globalui, buffersends=False) |
262 |
269 |
263 request, action, meta = reactor.callcommand(b'foo', {}) |
270 request, action, meta = reactor.callcommand(b'foo', {}) |
264 for f in meta[b'framegen']: |
271 for f in meta[b'framegen']: |
265 pass |
272 pass |
266 |
273 |