def test__send(self): ac = Mock(pchannel.Channel) self.ch._amq_chan = ac # test sending in params self.ch._send(NameTrio('xp', 'namen'), 'daten') # get our props self.assertTrue(ac.basic_publish.called) self.assertIn('exchange', ac.basic_publish.call_args[1]) self.assertIn('routing_key', ac.basic_publish.call_args[1]) self.assertIn('body', ac.basic_publish.call_args[1]) self.assertIn('immediate', ac.basic_publish.call_args[1]) self.assertIn('mandatory', ac.basic_publish.call_args[1]) self.assertIn('properties', ac.basic_publish.call_args[1]) props = ac.basic_publish.call_args[1].get('properties') self.assertIsInstance(props, BasicProperties) self.assertTrue(hasattr(props, 'headers')) self.assertEquals(props.headers, {}) # try another call to _send with a header self.ch._send(NameTrio('xp', 'namen'), 'daten', headers={'custom': 'val'}) # make sure our property showed up props = ac.basic_publish.call_args[1].get('properties') self.assertIn('custom', props.headers) self.assertEquals(props.headers['custom'], 'val')
def test__send(self): transport = Mock() transport.channel_number = sentinel.channel_number self.ch.on_channel_open(transport) # test sending in params self.ch._send(NameTrio('xp', 'namen'), 'daten') # get our props self.assertTrue(transport.publish_impl.called) self.assertIn('exchange', transport.publish_impl.call_args[1]) self.assertIn('routing_key', transport.publish_impl.call_args[1]) self.assertIn('body', transport.publish_impl.call_args[1]) self.assertIn('immediate', transport.publish_impl.call_args[1]) self.assertIn('mandatory', transport.publish_impl.call_args[1]) self.assertIn('properties', transport.publish_impl.call_args[1]) props = transport.publish_impl.call_args[1].get('properties') self.assertEquals(props, {}) # try another call to _send with a header self.ch._send(NameTrio('xp', 'namen'), 'daten', headers={'custom': 'val'}) # make sure our property showed up props = transport.publish_impl.call_args[1].get('properties') self.assertIn('custom', props) self.assertEquals(props['custom'], 'val')
def test_setup_listener_existing_recv_name_with_differing_name(self): ch = self._create_channel() recv_name = NameTrio(sentinel.xp, sentinel.queue, sentinel.binding) ch._recv_name = recv_name ch.setup_listener( name=NameTrio(sentinel.xp, sentinel.queue, sentinel.notbinding)) self.assertNotEquals(ch._recv_name, recv_name) self.assertEquals(ch._recv_name.exchange, sentinel.xp) self.assertEquals(ch._recv_name.queue, sentinel.queue) self.assertEquals(ch._recv_name.binding, sentinel.notbinding)
def on_start(self): TransformDataProcess.on_start(self) # set up subscriber to * self._bt_sub = Subscriber(callback=lambda m, h: self.call_process(m), from_name=NameTrio(get_sys_name(), 'bench_queue', '*')) # spawn listener self._sub_gl = spawn(self._bt_sub.listen) # set up publisher to anything! self._bt_pub = Publisher(to_name=NameTrio(get_sys_name(), str(uuid.uuid4())[0:6]))
def _unbind_subscription(self, exchange_point, exchange_name, routing_key): try: channel = self.container.node.channel(BindingChannel) channel._recv_name = NameTrio(exchange_point, exchange_name) channel._recv_name = NameTrio( channel._recv_name.exchange, '.'.join([exchange_point, exchange_name])) channel._recv_binding = routing_key channel._destroy_binding() except TransportError, te: log.exception( 'Raised transport error during deactivate_subscription. Assuming that it is due to deleting a binding that was already deleted and continuing!' )
def _bind_subscription(self, exchange_point, exchange_name, routing_key): try: channel = self.container.node.channel(BindingChannel) channel.setup_listener(NameTrio(exchange_point, exchange_name), binding=routing_key) except TransportError: log.exception( 'Caught Transport Error while creating a binding. Trying Subscriber Binding to make the queue first' ) channel = self.container.node.channel(SubscriberChannel) channel.setup_listener(NameTrio(exchange_point, exchange_name), binding=routing_key)
def test_purge(self): # declare both ex and queue self.transport.declare_exchange_impl(self.ex_name) self.transport.declare_queue_impl(self.queue_name) self.addCleanup(self.transport.delete_queue_impl, self.queue_name) #self.addCleanup(self.container.ex_manager.delete_exchange, self.ex_name) #@TODO exchange AD takes care of this when delete of queue # declare a bind self.transport.bind_impl(self.ex_name, self.queue_name, self.bind_name) # deliver some messages ch = self.container.node.channel(SendChannel) ch._send_name = NameTrio(self.ex_name, self.bind_name) ch.send('one') ch.send('two') ch.close() # should have two messages after routing happens (non-deterministic) time.sleep(2) queue_info = self.container.ex_manager.get_queue_info(self.queue_name) self.assertEquals(queue_info['messages'], 2) # now purge it self.transport.purge_impl(self.queue_name) time.sleep(2) queue_info = self.container.ex_manager.get_queue_info(self.queue_name) self.assertEquals(queue_info['messages'], 0)
def test_str(self): nt = NameTrio(sentinel.exchange, sentinel.queue, sentinel.binding) strnt = str(nt) self.assertIn(str(sentinel.exchange), strnt) self.assertIn(str(sentinel.queue), strnt) self.assertIn(str(sentinel.binding), strnt)
def _send(self, msg, headers=None, **kwargs): # could have a specified timeout in kwargs if 'timeout' in kwargs and kwargs['timeout'] is not None: timeout = kwargs['timeout'] else: timeout = CFG.endpoint.receive.timeout or 10 log.debug("RequestEndpointUnit.send (timeout: %s)", timeout) if not self._recv_greenlet: self.channel.setup_listener(NameTrio(self.channel._send_name.exchange)) # anon queue self.channel.start_consume() self.spawn_listener() self.response_queue = event.AsyncResult() self.message_received = lambda m, h: self.response_queue.set((m, h)) BidirectionalEndpointUnit._send(self, msg, headers=headers) try: result_data, result_headers = self.response_queue.get(timeout=timeout) except Timeout: raise exception.Timeout('Request timed out (%d sec) waiting for response from %s' % (timeout, str(self.channel._send_name))) log.debug("Got response to our request: %s, headers: %s", result_data, result_headers) return result_data, result_headers
def test_connect(self): self.ch.connect(NameTrio('xp', 'key')) self.assertTrue(hasattr(self.ch._send_name, 'exchange')) self.assertTrue(hasattr(self.ch._send_name, 'queue')) self.assertEquals(self.ch._send_name.exchange, 'xp') self.assertEquals(self.ch._send_name.queue, 'key') self.assertEquals(self.ch._exchange, 'xp')
def _setup_mock_channel(self, ch_type=BidirClientChannel, status_code=200, error_message="no problem", value="bidirmsg", op=None): """ Sets up a mocked channel, ready for fake bidir communication. @param ch_type Channel type the mock should spec to. @param status_code The status code of the operation, relevant only for RR comms. @param error_message The error message of the operation, relevant only for RR comms. @param op The op name, relevant only for RR comms. @param value The msg body to be returned. """ ch = MagicMock(spec=ch_type()) # set a return value for recv so we get an immediate response vals = [(value, { 'status_code': status_code, 'error_message': error_message, 'op': op, 'conv-id': sentinel.conv_id }, sentinel.delivery_tag)] def _ret(*args, **kwargs): if len(vals): return vals.pop() raise ChannelClosedError() ch.recv.side_effect = _ret # need to set a send_name for now ch._send_name = NameTrio('', '') return ch
def launch_benchmark(transform_number=1, primer=1, message_length=4): import gevent from gevent.greenlet import Greenlet from pyon.util.containers import DotDict from pyon.net.transport import NameTrio from pyon.net.endpoint import Publisher import uuid num = transform_number msg_len = message_length transforms = list() pids = 1 TransformBenchTesting.message_length = message_length cc = Container.instance pub = Publisher(to_name=NameTrio(get_sys_name(), str(uuid.uuid4())[0:6])) for i in xrange(num): tbt = cc.proc_manager._create_service_instance( str(pids), 'tbt', 'prototype.transforms.linear', 'TransformInPlace', DotDict({ 'process': { 'name': 'tbt%d' % pids, 'transform_id': pids } })) tbt.init() tbt.start() gevent.sleep(0.2) for i in xrange(primer): pub.publish(list(xrange(msg_len))) g = Greenlet(tbt.perf) g.start() transforms.append(tbt) pids += 1
def test_listen_exception_in_handling(self): # make a listen loop that will return one message (to blow up in processing) chmock = MagicMock(spec=ListenChannel) chmock.accept.return_value = Mock() chmock.accept.return_value.recv = Mock( return_value=(sentinel.msg, sentinel.headers, sentinel.delivery_tag)) chmock.accept.return_value._recv_queue.qsize.return_value = 1 nodemock = Mock(spec=NodeB) nodemock.channel.return_value = chmock recv_name = NameTrio(sentinel.ex, sentinel.queue) ep = ListeningBaseEndpoint(node=nodemock, from_name=recv_name) # make msg received error out! ep.create_endpoint = Mock(return_value=Mock(spec=EndpointUnit)) ep.create_endpoint.return_value._message_received.side_effect = TestError ep.create_endpoint.return_value.intercept_in.return_value = ( sentinel.msg, sentinel.headers) self.assertRaises(TestError, ep.listen) chmock.setup_listener.assert_called_once_with(recv_name, binding=sentinel.queue) chmock.start_consume.assert_called_once_with() chmock.accept.assert_called_once_with(n=1, timeout=None) chmock.accept.return_value.recv.assert_called_once_with() ep.create_endpoint.assert_called_once_with( existing_channel=chmock.accept.return_value)
def _create_accepted_channel(self, amq_chan, msg): send_name = NameTrio(tuple(msg[1].get('reply-to').split(','))) # @TODO: stringify is not the best ch = self.BidirAcceptChannel() ch._recv_name = self._recv_name # for debugging only ch.attach_underlying_channel(amq_chan) ch.connect(send_name) return ch
def test_init_tuples_both(self): # exchange kwarg has priority over queue kwarg nt = NameTrio(exchange=(sentinel.exchange1, ), queue=(sentinel.exchange2, sentinel.queue)) self.assertEquals(nt._exchange, sentinel.exchange1) self.assertEquals(nt._queue, None) self.assertEquals(nt._binding, None)
def listen(lch): """ The purpose of the this listen method is to trigger waits in code below. By setting up a listener that subscribes to both 3 and 5, and putting received messages into the appropriate gevent-queues client side, we can assume that the channel we're actually testing with get_stats etc has had the message delivered too. """ lch._queue_auto_delete = False lch.setup_listener( NameTrio(bootstrap.get_sys_name(), 'alternate_listener'), 'routed.3') lch._bind('routed.5') lch.start_consume() while True: try: with lch.accept() as newchan: m, h, d = newchan.recv() count = m.rsplit(',', 1)[-1] if m.startswith('5,'): self.five_events.put(int(count)) newchan.ack(d) elif m.startswith('3,'): self.three_events.put(int(count)) newchan.ack(d) else: raise StandardError("unknown message: %s" % m) except ChannelClosedError: break
def every_three(): p = self.container.node.channel(PublisherChannel) p._send_name = NameTrio(bootstrap.get_sys_name(), 'routed.3') counter = 0 while not self.publish_three.wait(timeout=3): p.send('3,' + str(counter)) counter += 1
def test_close_does_not_delete_if_anon_but_auto_delete(self): ch = SubscriberChannel() ch._queue_auto_delete = True ch._destroy_queue = Mock() ch._recv_name = NameTrio(sentinel.exchange, 'amq.gen-ABCD') ch.close_impl() self.assertFalse(ch._destroy_queue.called)
def test_setup_listener_existing_recv_name(self): ch = self._create_channel() recv_name = NameTrio(sentinel.xp, sentinel.queue, sentinel.binding) ch._recv_name = recv_name ch.setup_listener() self.assertEquals(ch._recv_name, recv_name)
def test_close_does_not_delete_if_named(self): ch = SubscriberChannel() ch._queue_auto_delete = False ch._destroy_queue = Mock() ch._recv_name = NameTrio(sentinel.exchange, 'some-named-queue') ch.close_impl() self.assertFalse(ch._destroy_queue.called)
def test_close_does_delete_if_anonymous_and_not_auto_delete(self): ch = SubscriberChannel() ch._queue_auto_delete = False ch._destroy_queue = Mock() ch._recv_name = NameTrio(sentinel.exchange, 'amq.gen-ABCD') ch.close_impl() ch._destroy_queue.assert_called_once_with()
def test_send(self): _sendmock = Mock() self.ch._send = _sendmock np = NameTrio('xp', 'key') self.ch.connect(np) self.ch.send('data', {'header': sentinel.headervalue}) _sendmock.assert_called_once_with( np, 'data', headers={'header': sentinel.headervalue})
def test_get_stats(self): self.ch._amq_chan = sentinel.amq_chan self.ch._transport = Mock() self.ch._recv_name = NameTrio(sentinel.ex, sentinel.queue) self.ch.get_stats() self.ch._transport.get_stats.assert_called_once_with( sentinel.amq_chan, queue=sentinel.queue)
def test_purge(self): self.ch._amq_chan = sentinel.amq_chan self.ch._transport = Mock() self.ch._recv_name = NameTrio(sentinel.ex, sentinel.queue) self.ch._purge() self.ch._transport.purge_impl.assert_called_once_with( sentinel.amq_chan, queue=sentinel.queue)
def test_purge(self): transport = Mock() self.ch.on_channel_open(transport) self.ch._recv_name = NameTrio(sentinel.ex, sentinel.queue) self.ch._purge() self.ch._transport.purge_impl.assert_called_once_with( queue=sentinel.queue)
def on_start(self): log.debug("StreamBinder start") queue_name = self.CFG.get('args', {}).get('queue_name', None) binding = self.CFG.get('args', {}).get('binding', None) # Create scoped exchange name XP = '.'.join([bootstrap.get_sys_name(), 'science_data']) self.channel = self.container.node.channel(BindingChannel) self.channel.setup_listener(NameTrio(XP, queue_name), binding=binding)
def __init__(self, node=None, to_name=None, name=None): BaseEndpoint.__init__(self, node=node) if name: log.warn("SendingBaseEndpoint: name param is deprecated, please use to_name instead") self._send_name = to_name or name # ensure NameTrio if not isinstance(self._send_name, NameTrio): self._send_name = NameTrio(bootstrap.get_sys_name(), self._send_name) # if send_name is a tuple it takes precedence
def test_listen(self): # make a listen loop that will exit right away chmock = Mock(spec=ListenChannel) chmock.accept.side_effect = ChannelClosedError nodemock = Mock(spec=NodeB) nodemock.channel.return_value = chmock ep = ListeningBaseEndpoint(node=nodemock, from_name=NameTrio(sentinel.ex, sentinel.queue)) ep.listen() chmock.setup_listener.assert_called_once_with(ep._recv_name, binding=sentinel.queue)
def test_start_consume_with_consumer_tag_and_auto_delete(self, mocklog): transport = AMQPTransport(Mock()) self.ch.on_channel_open(transport) self.ch._fsm.current_state = self.ch.S_ACTIVE # set up recv name for queue self.ch._recv_name = NameTrio(sentinel.xp, sentinel.queue) self.ch._consumer_tag = sentinel.consumer_tag self.ch._queue_auto_delete = True self.ch.start_consume() self.assertTrue(mocklog.warn.called)
def test__send_with_no_reply_to(self, mocksendchannel): # must set recv_name self.ch._recv_name = NameTrio(sentinel.xp, sentinel.queue) self.ch._send(sentinel.name, sentinel.data) mocksendchannel._send.assert_called_with( self.ch, sentinel.name, sentinel.data, headers={'reply-to': "%s,%s" % (sentinel.xp, sentinel.queue)})