ya2 · news · projects · code · about

xmlrpc
authorFlavio Calva <f.calva@gmail.com>
Mon, 13 Jun 2022 18:25:50 +0000 (19:25 +0100)
committerFlavio Calva <f.calva@gmail.com>
Mon, 13 Jun 2022 18:25:50 +0000 (19:25 +0100)
gui/menu.py
prj.org
tests/functional_test.py
ya2/utils/functional.py

index 8f21d5bdb1404f5417fc258d2cbbb41f72f0b1fe..54929d986f73db30473e41163f659476867602f0 100644 (file)
@@ -5,6 +5,7 @@ from glob import glob
 from importlib import import_module
 from inspect import isclass
 from webbrowser import open_new_tab
+from xmlrpc.client import ServerProxy
 from panda3d.core import Texture, TextNode, WindowProperties, LVector2i, \
     TextProperties, TextPropertiesManager
 from direct.gui.DirectGui import DirectButton, DirectCheckButton, \
@@ -85,8 +86,12 @@ class Menu(DirectObject):
         self._widgets += [DirectButton(
             text=_('Credits'), pos=(0, 1, -.2), command=self.on_credits,
             **self._common_btn)]
+        def btn_exit():
+            if self._fun_test:
+                ServerProxy('http://localhost:6000').destroy()
+            exit()
         self._widgets += [DirectButton(
-            text=_('Exit'), pos=(0, 1, -.6), command=lambda: exit(),
+            text=_('Exit'), pos=(0, 1, -.6), command=lambda: btn_exit(),
             **self._common_btn)]
         self._rearrange_width()
         self.accept('enforce_resolution', self.enforce_res)
diff --git a/prj.org b/prj.org
index 924def2d316756de6cfb428d3c7dabcbe9e76224..409095735424a1400535999cb08a6832a756fd2f 100644 (file)
--- a/prj.org
+++ b/prj.org
@@ -3,8 +3,7 @@
 #+CATEGORY: pmachines
 #+TAGS: bug(b) calendar(c) waiting(w)
 
-* RED implement functional tests with xmlrpc
-* READY retrieve the coordinates of testing elements
+* RED retrieve the coordinates of testing elements
 * READY functional tests for performance (frame rate)
 * READY functional tests for "cleaning" i.e. at the end of the states verify:
 - [ ] active threads
index fa2b5b2373b271d373e36aa83dca5faab9e7dadf..9ac9b29e56b76e08138695c07c741ae0439f7efb 100644 (file)
@@ -9,7 +9,7 @@ load_prc_file_data('', 'window-type none')
 import datetime
 from time import sleep
 from os import getcwd, system
-from multiprocessing.connection import Client
+from xmlrpc.client import ServerProxy
 from logging import debug, info
 from pathlib import Path
 from shutil import rmtree
@@ -36,19 +36,18 @@ class FunctionalTest(GameObject):
         info('test idx: %s' % idx)
         self._offset = offset
         sleep(5)
-        address = ('localhost', 6000)
-        self._conn = Client(address)
+        self._proxy = ServerProxy('http://localhost:6000')
         self._curr_time = 0
         self._tasks = []
         self._prev_time = 0
         taskMgr.add(self.on_frame_unpausable, 'on-frame-unpausable')
-        self._conn.send(['set_idx', idx])
+        self._proxy.set_idx(idx)
         self._do_screenshots(idx)
 
     def _do_screenshot(self, name):
         time = datetime.datetime.now().strftime('%y%m%d%H%M%S')
         name = name + '.png'
-        self._conn.send(['screenshot', name])
+        self._proxy.screenshot(name)
         info('screenshot %s' % name)
 
     def _screenshot(self, time, name):
@@ -95,7 +94,7 @@ class FunctionalTest(GameObject):
 
     def _enforce_res(self, time, res):
         def cback():
-            self._conn.send(['enforce_res', res])
+            self._proxy.enforce_res(res)
             info('enforce_res %s' % res)
         self._tasks += [(
             self._curr_time + time,
@@ -105,7 +104,7 @@ class FunctionalTest(GameObject):
 
     def _enforce_resolution(self, time, res):
         def cback():
-            self._conn.send(['enforce_resolution', res])
+            self._proxy.enforce_resolution(res)
             info('enforce_resolution %s (send)' % res)
         self._tasks += [(
             self._curr_time + time,
@@ -115,7 +114,7 @@ class FunctionalTest(GameObject):
 
     def _verify(self):
         def __verify():
-            self._conn.send(['verify'])
+            self._proxy.verify()
             info('verify')
         self._tasks += [(
             self._curr_time + 3,
@@ -125,7 +124,7 @@ class FunctionalTest(GameObject):
 
     def _exit(self):
         def do_exit():
-            self._conn.close()
+            self._proxy.close()
             exit()
         self._tasks += [(
             self._curr_time + 3,
@@ -144,8 +143,8 @@ class FunctionalTest(GameObject):
     def _do_screenshots_1(self):
         info('_do_screenshots_1')
         self._screenshot(FunctionalTest.start_time, 'main_menu')
-        self._do_screenshots_credits()
-        self._do_screenshots_options()
+        #self._do_screenshots_credits()
+        #self._do_screenshots_options()
         self._do_screenshots_exit()
 
     def _do_screenshots_credits(self):
index 3ac3995c1164ee96fb9eb490382e4178ec7ef0d8..a3459ca0fa27a672351cb2ba420faa4f9a17db03 100644 (file)
@@ -7,7 +7,7 @@ from os import makedirs
 from os.path import join, exists
 from glob import glob
 from sys import exit
-from multiprocessing.connection import Listener
+from xmlrpc.server import SimpleXMLRPCServer
 from threading import Thread
 from panda3d.core import Filename
 from direct.gui.OnscreenText import OnscreenText
@@ -15,44 +15,54 @@ from ya2.patterns.gameobject import GameObject
 from ya2.build.build import _branch
 
 
-class ListenerThread(Thread):
+class RPCServer(SimpleXMLRPCServer):
+
+    def __init__(self, callbacks):
+        super().__init__(('localhost', 6000), allow_none=True)
+        self._callbacks = callbacks
+        self.register_introspection_functions()
+        self.register_function(self.screenshot, 'screenshot')
+        self.register_function(self.enforce_res, 'enforce_res')
+        self.register_function(self.verify, 'verify')
+        self.register_function(self.set_idx, 'set_idx')
+        self.register_function(self.enforce_resolution, 'enforce_resolution')
+        self.register_function(self.destroy, 'destroy')
+
+    def screenshot(self, arg):
+        taskMgr.doMethodLater(.01, self._callbacks[0], 'cb0', [arg])
+
+    def enforce_res(self, arg):
+        taskMgr.doMethodLater(.01, self._callbacks[1], 'cb1', [arg])
+
+    def verify(self):
+        taskMgr.doMethodLater(.01, self._callbacks[2], 'cb2')
+
+    def set_idx(self, arg):
+        taskMgr.doMethodLater(.01, self._callbacks[3], 'cb3', [arg])
+
+    def enforce_resolution(self, arg):
+        taskMgr.doMethodLater(.01, self._callbacks[4], 'cb4', [arg])
+
+    def destroy(self):
+        self._BaseServer__shutdown_request = True
+
+
+class RPCServerThread(Thread):
 
     def __init__(self, callbacks):
         Thread.__init__(self)
-        address = ('localhost', 6000)
-        self._listener = Listener(address)
-        self._listener._listener._socket.settimeout(15)
-        try:
-            self._conn = self._listener.accept()
-        except TimeoutError:
-            info('listener timeout')
         self._callbacks = callbacks
 
     def run(self):
-        running = hasattr(self, '_conn')
-        while running:
-            try:
-                msg = self._conn.recv()
-                if msg[0] == 'screenshot':
-                    taskMgr.doMethodLater(.01, self._callbacks[0], 'cb0', [msg[1]])
-                elif msg[0] == 'enforce_res':
-                    taskMgr.doMethodLater(.01, self._callbacks[1], 'cb1', [msg[1]])
-                elif msg[0] == 'verify':
-                    taskMgr.doMethodLater(.01, self._callbacks[2], 'cb2')
-                elif msg[0] == 'set_idx':
-                    taskMgr.doMethodLater(.01, self._callbacks[3], 'cb3', [msg[1]])
-                elif msg[0] == 'enforce_resolution':
-                    taskMgr.doMethodLater(.01, self._callbacks[4], 'cb4', [msg[1]])
-            except EOFError:
-                running = False
+        self.server = RPCServer(self._callbacks)
+        self.server.serve_forever()
 
 
 class FunctionalTest(GameObject):
 
     def __init__(self, ref):
         super().__init__()
-        self._listener = ListenerThread([self._do_screenshot, self._do_enforce_res, self.__verify, self._set_idx, self._do_enforce_resolution])
-        self._listener.start()
+        RPCServerThread([self._do_screenshot, self._do_enforce_res, self.__verify, self._set_idx, self._do_enforce_resolution]).start()
         self.txt = OnscreenText('', fg=(1, 0, 0, 1), scale=.16)
         #self._path = ''
         #if self.eng.is_appimage: