changeset 36523:e7411fb7ba7f

wireprotoserver: ability to run an SSH server until an event is set It seems useful to be able to start an SSH protocol server that won't run forever and won't call sys.exit() when it stops. This could be used to facilitate intra-process testing of the SSH protocol, for example. We teach the server function to loop until a threading.Event is set and invent a new API to run the server until an event is set. It also won't sys.exit() afterwards. There aren't many callers of serve_forever(). So we could refactor them relatively easily. But I was lazy. threading.Event might be a bit heavyweight. An alternative would be a list whose only elements is changed. We can't use a simple scalar value like a bool or int because those types are immutable. Events are what you use in systems programming for this use case, so the use of threading.Event seems justified. Differential Revision: https://phab.mercurial-scm.org/D2461
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 24 Feb 2018 12:07:21 -0800
parents c3df20906689
children bfe38f787d5b
files mercurial/wireprotoserver.py
diffstat 1 files changed, 8 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/wireprotoserver.py	Thu Mar 01 15:46:21 2018 -0500
+++ b/mercurial/wireprotoserver.py	Sat Feb 24 12:07:21 2018 -0800
@@ -9,6 +9,7 @@
 import contextlib
 import struct
 import sys
+import threading
 
 from .i18n import _
 from . import (
@@ -373,7 +374,7 @@
 class sshv2protocolhandler(sshv1protocolhandler):
     """Protocol handler for version 2 of the SSH protocol."""
 
-def _runsshserver(ui, repo, fin, fout):
+def _runsshserver(ui, repo, fin, fout, ev):
     # This function operates like a state machine of sorts. The following
     # states are defined:
     #
@@ -430,7 +431,7 @@
     proto = sshv1protocolhandler(ui, fin, fout)
     protoswitched = False
 
-    while True:
+    while not ev.is_set():
         if state == 'protov1-serving':
             # Commands are issued on new lines.
             request = fin.readline()[:-1]
@@ -601,5 +602,9 @@
         util.setbinary(self._fout)
 
     def serve_forever(self):
-        _runsshserver(self._ui, self._repo, self._fin, self._fout)
+        self.serveuntil(threading.Event())
         sys.exit(0)
+
+    def serveuntil(self, ev):
+        """Serve until a threading.Event is set."""
+        _runsshserver(self._ui, self._repo, self._fin, self._fout, ev)