wireproto: implement basic frame reading and processing
We just implemented support for writing frames. Now let's implement
support for reading them.
The bulk of the new code is for a class that maintains the state of
a server. Essentially, you construct an instance, feed frames to it,
and it tells you what you should do next. The design is inspired by
the "sans I/O" movement and the reactor pattern. We don't want to
perform I/O or any major blocking event during frame ingestion because
this arbitrarily limits ways that server pieces can be implemented.
For example, it makes it much harder to swap in an alternate
implementation based on asyncio or do crazy things like have requests
dispatch to other processes.
We do still implement readframe() which does I/O. But it is decoupled
from the server reactor. And important parsing of frame headers is
a standalone function. So I/O is only needed to obtain frame data.
Because testing server-side ingest is useful and difficult on running
servers, we create a new "debugreflect" endpoint that will echo back
to the client what was received and how it was interpreted. This could
be useful for a server admin, someone implementing a client. But
immediately, it is useful for testing: we're able to demonstrate that
frames are parsed correctly and turned into requests to run commands
without having to implement command dispatch on the server!
In addition, we implement Python level unit tests for the reactor.
This is vastly more efficient than sending requests to the
"debugreflect" endpoint and vastly more powerful for advanced
testing.
Differential Revision: https://phab.mercurial-scm.org/D2852
#require serve
$ hgserve()
> {
> hg serve -a localhost -d --pid-file=hg.pid -E errors.log -v $@ \
> | sed -e "s/:$HGPORT1\\([^0-9]\\)/:HGPORT1\1/g" \
> -e "s/:$HGPORT2\\([^0-9]\\)/:HGPORT2\1/g" \
> -e 's/http:\/\/[^/]*\//http:\/\/localhost\//'
> cat hg.pid >> "$DAEMON_PIDS"
> echo % errors
> cat errors.log
> killdaemons.py hg.pid
> }
$ hg init test
$ cd test
$ echo '[web]' > .hg/hgrc
$ echo 'accesslog = access.log' >> .hg/hgrc
$ echo "port = $HGPORT1" >> .hg/hgrc
Without -v
$ hg serve -a localhost -p $HGPORT -d --pid-file=hg.pid -E errors.log
$ cat hg.pid >> "$DAEMON_PIDS"
$ if [ -f access.log ]; then
> echo 'access log created - .hg/hgrc respected'
> fi
access log created - .hg/hgrc respected
errors
$ cat errors.log
With -v
$ hgserve
listening at http://localhost/ (bound to *$LOCALIP*:HGPORT1) (glob) (?)
% errors
With -v and -p HGPORT2
$ hgserve -p "$HGPORT2"
listening at http://localhost/ (bound to *$LOCALIP*:HGPORT2) (glob) (?)
% errors
With -v and -p daytime (should fail because low port)
#if no-root no-windows
$ KILLQUIETLY=Y
$ hgserve -p daytime
abort: cannot start server at 'localhost:13': Permission denied
abort: child process failed to start
% errors
$ KILLQUIETLY=N
#endif
With --prefix foo
$ hgserve --prefix foo
listening at http://localhost/foo/ (bound to *$LOCALIP*:HGPORT1) (glob) (?)
% errors
With --prefix /foo
$ hgserve --prefix /foo
listening at http://localhost/foo/ (bound to *$LOCALIP*:HGPORT1) (glob) (?)
% errors
With --prefix foo/
$ hgserve --prefix foo/
listening at http://localhost/foo/ (bound to *$LOCALIP*:HGPORT1) (glob) (?)
% errors
With --prefix /foo/
$ hgserve --prefix /foo/
listening at http://localhost/foo/ (bound to *$LOCALIP*:HGPORT1) (glob) (?)
% errors
$ cd ..