예제 #1
0
    def read_request_headers(self):
        """
        Reads the request headers out of the underlying TTransportBase and
        return an FContext

        Returns:
            FContext
        """
        headers = _Headers._read(self.get_transport())

        context = FContext()

        for key, value in headers.items():
            context.set_request_header(key, value)

        op_id = headers[_OPID_HEADER]
        context._set_response_op_id(op_id)
        # Put a new opid in the request headers so this context an be
        # used/propagated on the receiver
        context.set_request_header(_OPID_HEADER, _get_next_op_id())

        cid = context.correlation_id
        if cid:
            context.set_response_header(_CID_HEADER, cid)
        return context
예제 #2
0
    def test_write_read(self):
        context = FContext("corrId")
        context.set_request_header("foo", "bar")

        expected = context.get_request_headers()

        buff = self.headers._write_to_bytearray(expected)

        actual = self.headers._read(BytesIO(buff))

        self.assertEqual(expected["_opid"], actual["_opid"])
        self.assertEqual(expected["_cid"], actual["_cid"])
        self.assertEqual(expected["foo"], actual["foo"])
예제 #3
0
async def test_pub_sub(nats_client, protocol_factory, port):
    global response_received
    pub_transport_factory = FNatsPublisherTransportFactory(nats_client)
    sub_transport_factory = FNatsSubscriberTransportFactory(nats_client)
    provider = FScopeProvider(pub_transport_factory, sub_transport_factory,
                              protocol_factory)
    publisher = EventsPublisher(provider)

    await publisher.open()

    def subscribe_handler(context, event):
        print("Response received {}".format(event))
        global response_received
        if context:
            response_received = True

    # Subscribe to response
    preamble = "foo"
    ramble = "bar"
    subscriber = EventsSubscriber(provider)
    await subscriber.subscribe_EventCreated(preamble, ramble, "response",
                                            "{}".format(port),
                                            subscribe_handler)

    event = Event(Message="Sending Call")
    context = FContext("Call")
    context.set_request_header(PREAMBLE_HEADER, preamble)
    context.set_request_header(RAMBLE_HEADER, ramble)
    print("Publishing...")
    await publisher.publish_EventCreated(context, preamble, ramble, "call",
                                         "{}".format(port), event)

    # Loop with sleep interval. Fail if not received within 3 seconds
    total_time = 0
    interval = 0.1
    while total_time < 3:
        if response_received:
            break
        else:
            await asyncio.sleep(interval)
            total_time += interval

    if not response_received:
        print("Pub/Sub response timed out!")
        exit(1)

    await publisher.close()
    exit(0)
예제 #4
0
    def test_copy(self):
        ctx = FContext()
        ctx.set_request_header('foo', 'bar')
        copied = ctx.copy()
        ctxHeaders = ctx.get_request_headers()
        copiedHeaders = copied.get_request_headers()

        # Should not have the same opid
        self.assertNotEqual(ctxHeaders[_OPID_HEADER],
                            copiedHeaders[_OPID_HEADER])

        # Everything else should be the same
        del ctxHeaders[_OPID_HEADER]
        del copiedHeaders[_OPID_HEADER]
        self.assertEqual(ctxHeaders, copiedHeaders)

        # Modifying the originals headers shouldn't affect the clone
        ctx.set_request_header('baz', 'qux')
        self.assertIsNone(copied.get_request_header('baz'))
예제 #5
0
 def test_cant_set_opid_public_method(self):
     context = FContext(self.correlation_id)
     context.set_request_header("_opid", "foo")
     self.assertNotEqual(context.get_request_header("_opid"), "foo")
예제 #6
0
 def test_response_header_put_allows_string_unicode(self):
     context = FContext(self.correlation_id)
     self.assertRaises(TypeError, context.set_response_header, 1, "foo")
     self.assertRaises(TypeError, context.set_response_header, "foo", 3)
     context.set_request_header(u'foo', u'bar')
예제 #7
0
 def test_request_headers(self):
     context = FContext(self.correlation_id)
     context.set_request_header("foo", "bar")
     headers = context.get_request_headers()
     self.assertEqual("bar", headers.get('foo'))
예제 #8
0
 def test_request_header(self):
     context = FContext(self.correlation_id)
     self.assertEqual(context, context.set_request_header("foo", "bar"))
     self.assertEqual("bar", context.get_request_header("foo"))
     self.assertEqual(self.correlation_id,
                      context.get_request_header("_cid"))
예제 #9
0
파일: publisher.py 프로젝트: yuanw/frugal
async def main():
    global response_received
    parser = argparse.ArgumentParser(
        description='Run a python asyncio stomp publisher')
    parser.add_argument('--port', dest='port', default='9090')
    parser.add_argument('--protocol', dest='protocol_type', default='binary',
                        choices='binary, compact, json')
    parser.add_argument('--transport', dest='transport_type',
                        default=ACTIVEMQ_NAME, choices='activemq')
    args = parser.parse_args()

    protocol_factory = get_protocol_factory(args.protocol_type)

    if args.transport_type == ACTIVEMQ_NAME:
        stomp_client = AioStomp('localhost', 61613)
        await stomp_client.connect()

        pub_transport_factory = FStompPublisherTransportFactory(stomp_client)
        sub_transport_factory = FStompSubscriberTransportFactory(stomp_client)
        provider = FScopeProvider(
            pub_transport_factory, sub_transport_factory, protocol_factory)
        publisher = EventsPublisher(provider)
    else:
        print(
            'Unknown transport type: {type}'.format(type=args.transport_type))
        sys.exit(1)

    await publisher.open()

    def subscribe_handler(context, event):
        print('Response received {}'.format(event))
        global response_received
        if context:
            response_received = True

    # Subscribe to response
    preamble = 'foo'
    ramble = 'bar'
    subscriber = EventsSubscriber(provider)
    await subscriber.subscribe_EventCreated(preamble, ramble, 'response',
                                            '{}'.format(args.port),
                                            subscribe_handler)

    event = Event(Message='Sending Call')
    context = FContext('Call')
    context.set_request_header(PREAMBLE_HEADER, preamble)
    context.set_request_header(RAMBLE_HEADER, ramble)
    print('Publishing...')
    await publisher.publish_EventCreated(context, preamble, ramble, 'call',
                                         '{}'.format(args.port), event)

    # Loop with sleep interval. Fail if not received within 3 seconds
    total_time = 0
    interval = 0.1
    while total_time < 3:
        if response_received:
            break
        else:
            await asyncio.sleep(interval)
            total_time += interval

    if not response_received:
        print('publisher did not get a response from subscriber!')
        exit(1)

    await publisher.close()
    exit(0)
예제 #10
0
 def test_op_id(self):
     context = FContext(self.correlation_id)
     context.set_request_header("_opid", "12345")
     self.assertEqual(self.correlation_id, context.correlation_id)
     self.assertEqual("12345", context.get_request_header("_opid"))