Esempio n. 1
0
 def run(self):
     self._jsonrpc.addmethod('jobchanged', self._jobchanged)
     task = self._jsonrpc.request("hello", {})
     task.stoppedevent.attach(self._hellocallback)
     task.start()
     try:
         self._jsonrpc.run()
     except IOError as e:
         if not self._stopped:
             raise
     return self._code
Esempio n. 2
0
 def _invokemethod(self, id, func, args, kwargs):
     self._log.debug(
         'id=%r, func=%r, args=%r, kwargs=%r', id, func, args, kwargs)
     response = None
     kwargs = self._fixkwargs(kwargs)
     try:
         result = func(*args, **kwargs)
     except TypeError as e:
         self._log.warning('handled exception', exc_info=True)
         if None is not id:
             response = self._invalidparams(id)
     except JsonRpcException as e:
         self._log.warning('handled exception', exc_info=True)
         if None is not id:
             response = self._errorresponse(id, e.code, e.message, e.data)
     except Exception as e:
         self._log.warning('uncaught exception', exc_info=True)
         if None is not id:
             e = sys.exc_info()[1]
             data = {'name': e.__class__.__name__, 'args': e.args}
             response = self._errorresponse(
                 id, -32000, 'uncaught exception', data)
     else:
         if not isinstance(result, conveyor.task.Task):
             if None is not id:
                 response = self._successresponse(id, result)
         else:
             task = result
             def stoppedcallback(task):
                 if conveyor.task.TaskConclusion.ENDED == task.conclusion:
                     response = self._successresponse(id, task.result)
                 elif conveyor.task.TaskConclusion.FAILED == task.conclusion:
                     response = self._errorresponse(id, -32001, 'task failed', task.failure)
                 elif conveyor.task.TaskConclusion.CANCELED == task.conclusion:
                     response = self._errorresponse(id, -32002, 'task canceled', None)
                 else:
                     raise ValueError(task.conclusion)
                 outdata = conveyor.json.dumps(response)
                 self._send(outdata)
             task.stoppedevent.attach(stoppedcallback)
             task.start()
         self._log.debug('response=%r', response)
     return response
Esempio n. 3
0
    def test_request_error(self):
        '''Test that the request method handles a server-side exception.'''

        stoc = StringIO.StringIO()
        ctos = StringIO.StringIO()
        client = JsonRpc(stoc, ctos)
        server = JsonRpc(ctos, stoc)

        def method(*args, **kwargs):
            raise Exception('failure')

        server.addmethod('method', method)
        task = client.request('method', [1])
        task.start()
        eventqueue = conveyor.event.geteventqueue()
        while True:
            result = eventqueue.runiteration(False)
            if not result:
                break
        ctos.seek(0)
        server.run()
        while True:
            result = eventqueue.runiteration(False)
            if not result:
                break
        stoc.seek(0)
        client.run()
        while True:
            result = eventqueue.runiteration(False)
            if not result:
                break
        self.assertTrue(conveyor.task.TaskState.STOPPED, task.state)
        self.assertTrue(conveyor.task.TaskConclusion.FAILED, task.conclusion)
        expected = {
            'message': 'uncaught exception',
            'code': -32000,
            'data': {
                'args': ['failure'],
                'name': 'Exception'
            }
        }
        self.assertEqual(expected, task.failure)
Esempio n. 4
0
    def test_request_error(self):
        '''Test that the request method handles a server-side exception.'''

        stoc = StringIO.StringIO()
        ctos = StringIO.StringIO()
        client = JsonRpc(stoc, ctos)
        server = JsonRpc(ctos, stoc)
        def method(*args, **kwargs):
            raise Exception('failure')
        server.addmethod('method', method)
        task = client.request('method', [1])
        task.start()
        eventqueue = conveyor.event.geteventqueue()
        while True:
            result = eventqueue.runiteration(False)
            if not result:
                break
        ctos.seek(0)
        server.run()
        while True:
            result = eventqueue.runiteration(False)
            if not result:
                break
        stoc.seek(0)
        client.run()
        while True:
            result = eventqueue.runiteration(False)
            if not result:
                break
        self.assertTrue(conveyor.task.TaskState.STOPPED, task.state)
        self.assertTrue(conveyor.task.TaskConclusion.FAILED, task.conclusion)
        expected = {
            'message': 'uncaught exception',
            'code': -32000,
            'data': {
                'args': ['failure'],
                'name': 'Exception'
            }
        }
        self.assertEqual(expected, task.failure)
Esempio n. 5
0
    def test_request(self):
        '''Test the request method.'''

        stoc = StringIO.StringIO()
        ctos = StringIO.StringIO()
        client = JsonRpc(stoc, ctos)
        server = JsonRpc(ctos, stoc)
        callback = conveyor.event.Callback()

        def method(*args, **kwargs):
            callback(*args, **kwargs)
            return 2

        server.addmethod('method', method)
        self.assertFalse(callback.delivered)
        task = client.request('method', [1])
        task.start()
        eventqueue = conveyor.event.geteventqueue()
        while True:
            result = eventqueue.runiteration(False)
            if not result:
                break
        ctos.seek(0)
        server.run()
        while True:
            result = eventqueue.runiteration(False)
            if not result:
                break
        stoc.seek(0)
        client.run()
        while True:
            result = eventqueue.runiteration(False)
            if not result:
                break
        self.assertTrue(callback.delivered)
        self.assertEqual((1, ), callback.args)
        self.assertEqual({}, callback.kwargs)
        self.assertTrue(conveyor.task.TaskState.STOPPED, task.state)
        self.assertTrue(conveyor.task.TaskConclusion.ENDED, task.conclusion)
        self.assertTrue(2, task.result)
Esempio n. 6
0
    def test_request(self):
        '''Test the request method.'''

        stoc = StringIO.StringIO()
        ctos = StringIO.StringIO()
        client = JsonRpc(stoc, ctos)
        server = JsonRpc(ctos, stoc)
        callback = conveyor.event.Callback()
        def method(*args, **kwargs):
            callback(*args, **kwargs)
            return 2
        server.addmethod('method', method)
        self.assertFalse(callback.delivered)
        task = client.request('method', [1])
        task.start()
        eventqueue = conveyor.event.geteventqueue()
        while True:
            result = eventqueue.runiteration(False)
            if not result:
                break
        ctos.seek(0)
        server.run()
        while True:
            result = eventqueue.runiteration(False)
            if not result:
                break
        stoc.seek(0)
        client.run()
        while True:
            result = eventqueue.runiteration(False)
            if not result:
                break
        self.assertTrue(callback.delivered)
        self.assertEqual((1,), callback.args)
        self.assertEqual({}, callback.kwargs)
        self.assertTrue(conveyor.task.TaskState.STOPPED, task.state)
        self.assertTrue(conveyor.task.TaskConclusion.ENDED, task.conclusion)
        self.assertTrue(2, task.result)