Exemplo n.º 1
0
    def test_invalidrequest(self):
        '''Test an invalid request.'''

        infp = None
        outfp = StringIO.StringIO()
        jsonrpcserver = JsonRpc(infp, outfp)
        jsonrpcserver._jsonreadercallback('1')
        response = json.loads(outfp.getvalue())
        self._asserterror(-32600, 'invalid request', None, response)
Exemplo n.º 2
0
    def test_invalidrequest(self):
        '''Test an invalid request.'''

        infp = None
        outfp = StringIO.StringIO()
        jsonrpcserver = JsonRpc(infp, outfp)
        jsonrpcserver._jsonreadercallback('1')
        response = json.loads(outfp.getvalue())
        self._asserterror(-32600, 'invalid request', None, response)
Exemplo n.º 3
0
    def test__handleresponse_ValueError(self):
        '''Test that the _handleresponse method throws a ValueError when it
        reads an object that is neither a request nor a response.

        '''

        jsonrpc = JsonRpc(None, None)
        jsonrpc._tasks[0] = conveyor.task.Task()
        with self.assertRaises(ValueError) as cm:
            jsonrpc._handleresponse({}, 0)
        self.assertEqual(({},), cm.exception.args)      
Exemplo n.º 4
0
 def _test_stringresponse(self, data, addmethods):
     infp = StringIO.StringIO(data.encode())
     outfp = StringIO.StringIO()
     jsonrpcserver = JsonRpc(infp, outfp)
     if addmethods:
         self._addmethods(jsonrpcserver)
     jsonrpcserver.run()
     eventqueue = conveyor.event.geteventqueue()
     eventqueue.runiteration(False)
     response = outfp.getvalue()
     return response
Exemplo n.º 5
0
    def test__handleresponse_ValueError(self):
        '''Test that the _handleresponse method throws a ValueError when it
        reads an object that is neither a request nor a response.

        '''

        jsonrpc = JsonRpc(None, None)
        jsonrpc._tasks[0] = conveyor.task.Task()
        with self.assertRaises(ValueError) as cm:
            jsonrpc._handleresponse({}, 0)
        self.assertEqual(({}, ), cm.exception.args)
Exemplo n.º 6
0
 def _test_stringresponse(self, data, addmethods):
     infp = StringIO.StringIO(data.encode())
     outfp = StringIO.StringIO()
     jsonrpcserver = JsonRpc(infp, outfp)
     if addmethods:
         self._addmethods(jsonrpcserver)
     jsonrpcserver.run()
     eventqueue = conveyor.event.geteventqueue()
     eventqueue.runiteration(False)
     response = outfp.getvalue()
     return response
Exemplo n.º 7
0
    def test_notify(self):
        '''Test the notify method.'''

        stoc = StringIO.StringIO()
        ctos = StringIO.StringIO()
        client = JsonRpc(stoc, ctos)
        server = JsonRpc(ctos, stoc)
        callback = conveyor.event.Callback()
        server.addmethod('method', callback)
        self.assertFalse(callback.delivered)
        client.notify('method', [1])
        ctos.seek(0)
        server.run()
        eventqueue = conveyor.event.geteventqueue()
        eventqueue.runiteration(False)
        self.assertTrue(callback.delivered)
        self.assertEqual((1, ), callback.args)
        self.assertEqual({}, callback.kwargs)
Exemplo n.º 8
0
    def test__handleresponse_unknown(self):
        '''Test that the _handleresponse method logs a debugging message when
        it reads a response for an unknown request.

        '''

        conveyor.test.listlogging(logging.DEBUG)
        jsonrpc = JsonRpc(None, None)
        conveyor.test.ListHandler.list = []
        jsonrpc._handleresponse(None, 0)
        self.assertEqual(2, len(conveyor.test.ListHandler.list))
        self.assertEqual('ignoring response for unknown id: 0',
                         conveyor.test.ListHandler.list[1].getMessage())
Exemplo n.º 9
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)
Exemplo n.º 10
0
    def test_notify(self):
        '''Test the notify method.'''

        stoc = StringIO.StringIO()
        ctos = StringIO.StringIO()
        client = JsonRpc(stoc, ctos)
        server = JsonRpc(ctos, stoc)
        callback = conveyor.event.Callback()
        server.addmethod('method', callback)
        self.assertFalse(callback.delivered)
        client.notify('method', [1])
        ctos.seek(0)
        server.run()
        eventqueue = conveyor.event.geteventqueue()
        eventqueue.runiteration(False)
        self.assertTrue(callback.delivered)
        self.assertEqual((1,), callback.args)
        self.assertEqual({}, callback.kwargs)
Exemplo n.º 11
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)
Exemplo n.º 12
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)
Exemplo n.º 13
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)