Esempio n. 1
0
class TestJsonFormatter(VumiTestCase):
    def setUp(self):
        self.msg_helper = self.add_helper(MessageHelper())
        self.request = DummyRequest([''])
        self.formatter = JsonFormatter()

    def test_implements_IMessageFormatter(self):
        self.assertTrue(IMessageFormatter.providedBy(self.formatter))

    def test_add_http_headers(self):
        self.formatter.add_http_headers(self.request)
        self.assertEqual(
            self.request.responseHeaders.getRawHeaders('Content-Type'),
            ['application/json; charset=utf-8'])

    def test_write_row_header(self):
        self.formatter.write_row_header(self.request)
        self.assertEqual(self.request.written, [])

    def test_write_row(self):
        msg = self.msg_helper.make_inbound("foo")
        self.formatter.write_row(self.request, msg)
        self.assertEqual(self.request.written, [
            msg.to_json(),
            "\n",
        ])
Esempio n. 2
0
class TestJsonFormatter(VumiTestCase):
    def setUp(self):
        self.msg_helper = self.add_helper(MessageHelper())
        self.request = DummyRequest([''])
        self.formatter = JsonFormatter()

    def test_implements_IMessageFormatter(self):
        self.assertTrue(IMessageFormatter.providedBy(self.formatter))

    def test_add_http_headers(self):
        self.formatter.add_http_headers(self.request)
        self.assertEqual(
            self.request.responseHeaders.getRawHeaders('Content-Type'),
            ['application/json; charset=utf-8'])

    def test_write_row_header(self):
        self.formatter.write_row_header(self.request)
        self.assertEqual(self.request.written, [])

    def test_write_row(self):
        msg = self.msg_helper.make_inbound("foo")
        self.formatter.write_row(self.request, msg)
        self.assertEqual(self.request.written, [
            msg.to_json(), "\n",
        ])
Esempio n. 3
0
 def setUp(self):
     self.msg_helper = self.add_helper(MessageHelper())
     self.request = DummyRequest([''])
     self.formatter = JsonFormatter()
Esempio n. 4
0
    def test_disconnect_kills_server(self):
        """
        If the client connection is lost, we stop processing the request.

        This test is a bit hacky, because it has to muck about inside the
        resource in order to pause and resume at appropriate places.
        """
        yield self.start_server()

        from vumi.components.message_store_resource import InboundResource

        batch_id = yield self.make_batch(('foo', 'bar'))
        msgs = [(yield self.make_inbound(batch_id, 'føø')) for _ in range(6)]

        class PausingInboundResource(InboundResource):
            def __init__(self, *args, **kw):
                InboundResource.__init__(self, *args, **kw)
                self.pause_after = 3
                self.pause_d = Deferred()
                self.resume_d = Deferred()
                self.fetch = {}

            def _finish_fetching(self, msg):
                self.fetch[msg['message_id']].callback(msg['message_id'])
                return msg

            def get_message(self, message_store, message_id):
                self.fetch[message_id] = Deferred()
                d = succeed(None)
                if self.pause_after > 0:
                    self.pause_after -= 1
                else:
                    if not self.pause_d.called:
                        self.pause_d.callback(None)
                    d.addCallback(lambda _: self.resume_d)
                d.addCallback(lambda _: InboundResource.get_message(
                    self, message_store, message_id))
                d.addCallback(self._finish_fetching)
                return d

        res = PausingInboundResource(self.store, batch_id, JsonFormatter())
        site = Site(res)
        server = yield reactor.listenTCP(0, site, interface='127.0.0.1')
        self.add_cleanup(server.loseConnection)
        addr = server.getHost()
        url = 'http://%s:%s?concurrency=2' % (addr.host, addr.port)

        resp_d = http_request_full(method='GET', url=url)
        # Wait until we've processed some messages.
        yield res.pause_d
        # Kill the client connection.
        yield resp_d.cancel()
        # Continue processing messages.
        res.resume_d.callback(None)

        # This will fail because we've cancelled the request. We don't care
        # about the exception, so we swallow it and move on.
        yield resp_d.addErrback(lambda _: None)

        # Wait for all the in-progress loads to finish.
        fetched_msg_ids = yield gatherResults(res.fetch.values())

        sorted_message_ids = sorted(msg['message_id'] for msg in msgs)
        self.assertEqual(set(fetched_msg_ids), set(sorted_message_ids[:4]))
Esempio n. 5
0
 def setUp(self):
     self.msg_helper = self.add_helper(MessageHelper())
     self.request = DummyRequest([''])
     self.formatter = JsonFormatter()