Example #1
0
class FakeRemoteAgent(threading.Thread):
    """ A fake agent used for tests of the RPyC interface """

    def __init__(self, port, handle_job_func,
                 update_image_aliases_func=(lambda aliases: ""),
                 get_task_directory_hashes_func=(lambda: []),
                 update_task_directory_func=(lambda remote_tar_file, to_delete: "")):
        threading.Thread.__init__(self)
        self.port = port
        self.handle_job_func = handle_job_func
        self.update_image_aliases_func = update_image_aliases_func
        self.get_task_directory_hashes_func = get_task_directory_hashes_func
        self.update_task_directory_func = update_task_directory_func
        self.start()

    def run(self):
        try:
            self._backend_server = OneShotServer(self._get_agent_backend_service(), port=self.port,
                                                 protocol_config={"allow_public_attrs": True, 'allow_pickle': True})
            self._backend_server.start()
        except EOFError:
            pass

    def close(self):
        self._backend_server.close()

    def _get_agent_backend_service(self):
        """ Returns a RPyC service associated with this Agent """
        handle_job = self.handle_job_func
        update_image_aliases_func = self.update_image_aliases_func
        get_task_directory_hashes_func = self.get_task_directory_hashes_func
        update_task_directory_func = self.update_task_directory_func

        class AgentService(rpyc.Service):
            def exposed_update_image_aliases(self, image_aliases):
                update_image_aliases_func(image_aliases)

            def exposed_get_task_directory_hashes(self):
                return get_task_directory_hashes_func()

            def exposed_update_task_directory(self, remote_tar_file, to_delete):
                update_task_directory_func(remote_tar_file.read(), copy.deepcopy(to_delete))

            def exposed_new_job(self, job_id, course_id, task_id, inputdata, debug, callback_status, callback_return):
                """ Creates, executes and returns the results of a new job """
                try:
                    retval = handle_job(job_id, course_id, task_id, inputdata, debug, callback_status)
                except Exception as e:
                    callback_return({"result": "crash", "text": "An error occured in the Agent: {}".format(str(e))})
                    return
                callback_return(retval)

        return AgentService
class Test_OneShotServer(unittest.TestCase):
    def setUp(self):
        self.server = OneShotServer(MyService, port=18878, auto_register=False)
        self.server.logger.quiet = False
        self.server._start_in_thread()

    def tearDown(self):
        self.server.close()

    def test_server_stops(self):
        conn = rpyc.connect("localhost", port=18878)
        self.assertEqual("bar", conn.root.foo())
        conn.close()
        with self.assertRaises(Exception):
            for i in range(3):
                conn = rpyc.connect("localhost", port=18878)
                conn.close()
                time.sleep()
Example #3
0
class Test_OneShotServer(unittest.TestCase):
    def setUp(self):
        self.server = OneShotServer(MyService, port=18878, auto_register=False)
        self.server.logger.quiet = False
        self.thd = self.server._start_in_thread()

    def tearDown(self):
        self.server.close()
        self.thd.join()

    def test_server_stops(self):
        conn = rpyc.connect("localhost", port=18878)
        self.assertEqual("bar", conn.root.foo())
        conn.close()
        while not self.server._closed:
            pass
        self.assertTrue(self.server._closed)
        with self.assertRaises(Exception):
            conn = rpyc.connect("localhost", port=18878)
Example #4
0
class FakeRemoteAgent(threading.Thread):
    """ A fake agent used for tests of the RPyC interface """
    def __init__(
        self,
        port,
        handle_job_func,
        update_image_aliases_func=(lambda aliases: ""),
        get_task_directory_hashes_func=(lambda: []),
        update_task_directory_func=(lambda remote_tar_file, to_delete: "")):
        threading.Thread.__init__(self)
        self.port = port
        self.handle_job_func = handle_job_func
        self.update_image_aliases_func = update_image_aliases_func
        self.get_task_directory_hashes_func = get_task_directory_hashes_func
        self.update_task_directory_func = update_task_directory_func
        self.start()

    def run(self):
        try:
            self._backend_server = OneShotServer(
                self._get_agent_backend_service(),
                port=self.port,
                protocol_config={
                    "allow_public_attrs": True,
                    'allow_pickle': True
                })
            self._backend_server.start()
        except EOFError:
            pass

    def close(self):
        self._backend_server.close()

    def _get_agent_backend_service(self):
        """ Returns a RPyC service associated with this Agent """
        handle_job = self.handle_job_func
        update_image_aliases_func = self.update_image_aliases_func
        get_task_directory_hashes_func = self.get_task_directory_hashes_func
        update_task_directory_func = self.update_task_directory_func

        class AgentService(rpyc.Service):
            def exposed_update_image_aliases(self, image_aliases):
                update_image_aliases_func(image_aliases)

            def exposed_get_task_directory_hashes(self):
                return get_task_directory_hashes_func()

            def exposed_update_task_directory(self, remote_tar_file,
                                              to_delete):
                update_task_directory_func(remote_tar_file.read(),
                                           copy.deepcopy(to_delete))

            def exposed_new_job(self, job_id, course_id, task_id, inputdata,
                                debug, callback_status, callback_return):
                """ Creates, executes and returns the results of a new job """
                try:
                    retval = handle_job(job_id, course_id, task_id, inputdata,
                                        debug, callback_status)
                except Exception as e:
                    callback_return({
                        "result":
                        "crash",
                        "text":
                        "An error occured in the Agent: {}".format(str(e))
                    })
                    return
                callback_return(retval)

        return AgentService