annotate tests/test-wireproto-command-lookup.t @ 39559:07b58266bce3

wireprotov2: implement commands as a generator of objects Previously, wire protocol version 2 inherited version 1's model of having separate types to represent the results of different wire protocol commands. As I implemented more powerful commands in future commits, I found I was using a common pattern of returning a special type to hold a generator. This meant the command function required a closure to do most of the work. That made logic flow more difficult to follow. I also noticed that many commands were effectively a sequence of objects to be CBOR encoded. I think it makes sense to define version 2 commands as generators. This way, commands can simply emit the data structures they wish to send to the client. This eliminates the need for a closure in command functions and removes encoding from the bodies of commands. As part of this commit, the handling of response objects has been moved into the serverreactor class. This puts the reactor in the driver's seat with regards to CBOR encoding and error handling. Having error handling in the function that emits frames is particularly important because exceptions in that function can lead to things getting in a bad state: I'm fairly certain that uncaught exceptions in the frame generator were causing deadlocks. I also introduced a dedicated error type for explicit error reporting in command handlers. This will be used in subsequent commits. There's still a bit of work to be done here, especially around formalizing the error handling "protocol." I've added yet another TODO to track this so we don't forget. Test output changed because we're using generators and no longer know we are at the end of the data until we hit the end of the generator. This means we can't emit the end-of-stream flag until we've exhausted the generator. Hence the introduction of 0-sized end-of-stream frames. Differential Revision: https://phab.mercurial-scm.org/D4472
author Gregory Szorc <gregory.szorc@gmail.com>
date Wed, 05 Sep 2018 09:06:40 -0700
parents 3ea8323d6f95
children 393e44324037
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37538
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
1 $ . $TESTDIR/wireprotohelpers.sh
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
2
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
3 $ hg init server
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
4 $ enablehttpv2 server
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
5 $ cd server
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
6 $ cat >> .hg/hgrc << EOF
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
7 > [web]
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
8 > push_ssl = false
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
9 > allow-push = *
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
10 > EOF
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
11 $ hg debugdrawdag << EOF
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
12 > C D
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
13 > |/
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
14 > B
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
15 > |
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
16 > A
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
17 > EOF
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
18
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
19 $ hg serve -p $HGPORT -d --pid-file hg.pid -E error.log
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
20 $ cat hg.pid > $DAEMON_PIDS
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
21
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
22 lookup for known node works
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
23
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
24 $ sendhttpv2peer << EOF
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
25 > command lookup
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
26 > key 426bada5c67598ca65036d57d9e4b64b0c1ce7a0
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
27 > EOF
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
28 creating http peer for wire protocol version 2
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
29 sending lookup command
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
30 s> *\r\n (glob)
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
31 s> Accept-Encoding: identity\r\n
37725
3ea8323d6f95 wireprotov2: change command response protocol to include a leading map
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
32 s> accept: application/mercurial-exp-framing-0005\r\n
3ea8323d6f95 wireprotov2: change command response protocol to include a leading map
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
33 s> content-type: application/mercurial-exp-framing-0005\r\n
37538
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
34 s> content-length: 73\r\n
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
35 s> host: $LOCALIP:$HGPORT\r\n (glob)
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
36 s> user-agent: Mercurial debugwireproto\r\n
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
37 s> \r\n
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
38 s> A\x00\x00\x01\x00\x01\x01\x11\xa2Dargs\xa1CkeyX(426bada5c67598ca65036d57d9e4b64b0c1ce7a0DnameFlookup
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
39 s> makefile('rb', None)
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
40 s> HTTP/1.1 200 OK\r\n
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
41 s> Server: testing stub value\r\n
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
42 s> Date: $HTTP_DATE$\r\n
37725
3ea8323d6f95 wireprotov2: change command response protocol to include a leading map
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37724
diff changeset
43 s> Content-Type: application/mercurial-exp-framing-0005\r\n
37538
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
44 s> Transfer-Encoding: chunked\r\n
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
45 s> \r\n
39559
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
46 s> 13\r\n
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
47 s> \x0b\x00\x00\x01\x00\x02\x011
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
48 s> \xa1FstatusBok
37538
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
49 s> \r\n
39559
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
50 received frame(size=11; request=1; stream=2; streamflags=stream-begin; type=command-response; flags=continuation)
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
51 s> 1d\r\n
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
52 s> \x15\x00\x00\x01\x00\x02\x001
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
53 s> TBk\xad\xa5\xc6u\x98\xcae\x03mW\xd9\xe4\xb6K\x0c\x1c\xe7\xa0
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
54 s> \r\n
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
55 received frame(size=21; request=1; stream=2; streamflags=; type=command-response; flags=continuation)
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
56 s> 8\r\n
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
57 s> \x00\x00\x00\x01\x00\x02\x002
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
58 s> \r\n
37538
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
59 s> 0\r\n
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
60 s> \r\n
39559
07b58266bce3 wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37725
diff changeset
61 received frame(size=0; request=1; stream=2; streamflags=; type=command-response; flags=eos)
37721
f7673845b167 wireprotov2: decode responses to their expected types
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37718
diff changeset
62 response: b'Bk\xad\xa5\xc6u\x98\xcae\x03mW\xd9\xe4\xb6K\x0c\x1c\xe7\xa0'
37538
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
63
89fed81bbb6c wireproto: port lookup to wire protocol v2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
64 $ cat error.log