Commits

Yuya Nishihara committed 1e91d5f

thgbackdoor: split controller and process of client test case

Comments (0)

Files changed (1)

tests/thgbackdoor_test.py

         return code
     return lines[0] + '\n' + '\n'.join(l[margin:] for l in lines[1:])
 
-class ThgClient(object):
-    def __init__(self, cmdargs=None, timeout=None, connect=True):
-        self._cmdargs = list(cmdargs or [])
+class ThgController(object):
+    def __init__(self, timeout=None, connect=True):
         self._timeout = timeout
-        self._proc = None
+        self._sock = None
         if connect:
             self.open()
 
     def open(self):
-        if self._proc:
+        if self._sock:
             raise ValueError('already open')
-
-        self._proc = subprocess.Popen([THG, '--nofork'] + self._cmdargs,
-                                      cwd=_tempdir, env=_tempenv)
         self._sock = socket.socket()
         self._sock.settimeout(self._timeout)
         # dirty code to wait server wake up
         self._recvq = ''
 
     def close(self):
-        if not self._proc:
+        if not self._sock:
             return
-        self._sendmsg(('quit',))
-        self.wait()
         self._sock.close()
-        self._proc.wait()
-        self._proc = None
+        self._sock = None
 
     def eval(self, expression):
         self._sendmsg(('eval', _cleancode(expression)))
         self._sendmsg(('exec', _cleancode(code), delay))
         return self.wait()
 
+    def quit(self):
+        self._sendmsg(('quit',))
+        self.wait()
+
     def _sendmsg(self, msg):
-        if not self._proc:
+        if not self._sock:
             raise ValueError('not open')
         self._sock.sendall(pickle.dumps(msg))
 
     def wait(self):
-        if not self._proc:
+        if not self._sock:
             raise ValueError('not open')
         data = self._sock.recv(4096)
         if not data:
         else:
             raise ValueError('unexpected response type: %r' % resp)
 
+class ThgClient(object):
+    def __init__(self, cmdargs=None, timeout=None, connect=True):
+        self._cmdargs = list(cmdargs or [])
+        self._proc = None
+        self._controller = ThgController(timeout=timeout, connect=False)
+        if connect:
+            self.open()
+
+    def open(self):
+        if self._proc:
+            raise ValueError('already open')
+        self._proc = subprocess.Popen([THG, '--nofork'] + self._cmdargs,
+                                      cwd=_tempdir, env=_tempenv)
+        self._controller.open()
+
+    def close(self):
+        if not self._proc:
+            return
+        self._controller.quit()
+        self._controller.close()
+        self._proc.wait()
+        self._proc = None
+
+    def __getattr__(self, name):
+        return getattr(self._controller, name)
+
 def test_run_workbench():
     thg = ThgClient(timeout=5)
     try: