def test_from_message_missing_required_fields(self):
     body = {}
     m = Message(None, body=anyjson.dumps(body), backend='foo',
                 content_type='application/json',
                 content_encoding='utf-8')
     with self.assertRaises(KeyError):
         Request(m.decode(), message=m, app=self.app)
Exemple #2
0
 def test_from_message_nonexistant_task(self):
     body = {"task": "cu.mytask.doesnotexist", "id": uuid(), "args": [2], "kwargs": {u"æØåveéðƒeæ": "bar"}}
     m = Message(
         None, body=anyjson.dumps(body), backend="foo", content_type="application/json", content_encoding="utf-8"
     )
     with self.assertRaises(KeyError):
         TaskRequest.from_message(m, m.decode())
 def test_from_message_missing_required_fields(self):
     body = {}
     m = Message(None, body=anyjson.serialize(body), backend="foo",
                       content_type="application/json",
                       content_encoding="utf-8")
     with self.assertRaises(InvalidTaskError):
         TaskRequest.from_message(m, m.decode())
Exemple #4
0
 def test_from_message(self):
     us = u"æØåveéðƒeæ"
     body = {
         "task": mytask.name,
         "id": uuid(),
         "args": [2],
         "kwargs": {
             us: "bar"
         }
     }
     m = Message(None,
                 body=anyjson.serialize(body),
                 backend="foo",
                 content_type="application/json",
                 content_encoding="utf-8")
     tw = TaskRequest.from_message(m, m.decode())
     self.assertIsInstance(tw, Request)
     self.assertEqual(tw.task_name, body["task"])
     self.assertEqual(tw.task_id, body["id"])
     self.assertEqual(tw.args, body["args"])
     us = from_utf8(us)
     if sys.version_info < (2, 6):
         self.assertEqual(tw.kwargs.keys()[0], us)
         self.assertIsInstance(tw.kwargs.keys()[0], str)
     self.assertTrue(tw.logger)
Exemple #5
0
 def test_from_message_missing_required_fields(self):
     body = {}
     m = Message(None, body=anyjson.dumps(body), backend='foo',
                 content_type='application/json',
                 content_encoding='utf-8')
     with self.assertRaises(KeyError):
         TaskRequest.from_message(m, m.decode())
Exemple #6
0
 def test_from_message_missing_required_fields(self):
     body = {}
     m = Message(None, body=anyjson.dumps(body), backend="foo",
                       content_type="application/json",
                       content_encoding="utf-8")
     with self.assertRaises(KeyError):
         TaskRequest.from_message(m, m.decode())
Exemple #7
0
 def test_from_message_nonexistant_task(self):
     body = {'task': 'cu.mytask.doesnotexist', 'id': uuid(),
             'args': [2], 'kwargs': {u'æØåveéðƒeæ': 'bar'}}
     m = Message(None, body=anyjson.dumps(body), backend='foo',
                 content_type='application/json',
                 content_encoding='utf-8')
     with self.assertRaises(KeyError):
         TaskRequest.from_message(m, m.decode())
 def test_from_message_nonexistant_task(self):
     body = {"task": "cu.mytask.doesnotexist", "id": uuid(),
             "args": [2], "kwargs": {u"æØåveéðƒeæ": "bar"}}
     m = Message(None, body=anyjson.serialize(body), backend="foo",
                       content_type="application/json",
                       content_encoding="utf-8")
     with self.assertRaises(KeyError):
         TaskRequest.from_message(m, m.decode())
Exemple #9
0
 def test_from_message_nonexistant_task(self):
     body = {"task": "cu.mytask.doesnotexist", "id": gen_unique_id(),
             "args": [2], "kwargs": {u"æØåveéðƒeæ": "bar"}}
     m = Message(None, body=simplejson.dumps(body), backend="foo",
                       content_type="application/json",
                       content_encoding="utf-8")
     self.assertRaises(NotRegistered, TaskRequest.from_message,
                       m, m.decode())
Exemple #10
0
def create_message(channel, **data):
    data.setdefault('id', uuid())
    m = Message(channel, body=pickle.dumps(dict(**data)),
                content_type='application/x-python-serialize',
                content_encoding='binary',
                delivery_info={'consumer_tag': 'mock'})
    m.accept = ['application/x-python-serialize']
    return m
Exemple #11
0
 def test_from_message_nonexistant_task(self):
     body = {'task': 'cu.mytask.doesnotexist', 'id': uuid(),
             'args': [2], 'kwargs': {'æØåveéðƒeæ': 'bar'}}
     m = Message(None, body=anyjson.dumps(body), backend='foo',
                 content_type='application/json',
                 content_encoding='utf-8')
     with self.assertRaises(KeyError):
         TaskRequest.from_message(m, m.decode(), app=self.app)
 def test_from_message_empty_args(self):
     body = {"task": mytask.name, "id": uuid()}
     m = Message(None, body=anyjson.serialize(body), backend="foo",
                       content_type="application/json",
                       content_encoding="utf-8")
     tw = TaskRequest.from_message(m, m.decode())
     self.assertIsInstance(tw, TaskRequest)
     self.assertEquals(tw.args, [])
     self.assertEquals(tw.kwargs, {})
Exemple #13
0
 def test_from_message_empty_args(self):
     body = {'task': mytask.name, 'id': uuid()}
     m = Message(None, body=anyjson.dumps(body), backend='foo',
                 content_type='application/json',
                 content_encoding='utf-8')
     tw = TaskRequest.from_message(m, m.decode())
     self.assertIsInstance(tw, Request)
     self.assertEquals(tw.args, [])
     self.assertEquals(tw.kwargs, {})
Exemple #14
0
def create_message(channel, **data):
    data.setdefault('id', uuid())
    m = Message(body=pickle.dumps(dict(**data)),
                channel=channel,
                content_type='application/x-python-serialize',
                content_encoding='binary',
                delivery_info={'consumer_tag': 'mock'})
    m.accept = ['application/x-python-serialize']
    return m
 def test_from_message_empty_args(self):
     body = {'task': self.mytask.name, 'id': uuid()}
     m = Message(None, body=anyjson.dumps(body), backend='foo',
                 content_type='application/json',
                 content_encoding='utf-8')
     job = Request(m.decode(), message=m, app=self.app)
     self.assertIsInstance(job, Request)
     self.assertEqual(job.args, [])
     self.assertEqual(job.kwargs, {})
Exemple #16
0
 def test_from_message_empty_args(self):
     body = {"task": self.mytask.name, "id": uuid()}
     m = Message(
         None, body=anyjson.dumps(body), backend="foo", content_type="application/json", content_encoding="utf-8"
     )
     job = Request(m.decode(), message=m, app=self.app)
     self.assertIsInstance(job, Request)
     self.assertEqual(job.args, [])
     self.assertEqual(job.kwargs, {})
Exemple #17
0
 def test_from_message_empty_args(self):
     body = {"task": mytask.name, "id": uuid()}
     m = Message(None, body=anyjson.dumps(body), backend="foo",
                       content_type="application/json",
                       content_encoding="utf-8")
     tw = TaskRequest.from_message(m, m.decode())
     self.assertIsInstance(tw, Request)
     self.assertEquals(tw.args, [])
     self.assertEquals(tw.kwargs, {})
Exemple #18
0
 def test_from_message_empty_args(self):
     body = {'task': self.mytask.name, 'id': uuid()}
     m = Message(None, body=anyjson.dumps(body), backend='foo',
                 content_type='application/json',
                 content_encoding='utf-8')
     job = TaskRequest.from_message(m, m.decode(), app=self.app)
     self.assertIsInstance(job, Request)
     self.assertEqual(job.args, [])
     self.assertEqual(job.kwargs, {})
Exemple #19
0
def create_message(channel, **data):
    data.setdefault("id", uuid())
    channel.no_ack_consumers = set()
    m = Message(
        channel,
        body=pickle.dumps(dict(**data)),
        content_type="application/x-python-serialize",
        content_encoding="binary",
        delivery_info={"consumer_tag": "mock"},
    )
    m.accept = ["application/x-python-serialize"]
    return m
Exemple #20
0
 def test_from_message(self):
     us = "æØåveéðƒeæ"
     body = {"task": self.mytask.name, "id": uuid(), "args": [2], "kwargs": {us: "bar"}}
     m = Message(
         None, body=anyjson.dumps(body), backend="foo", content_type="application/json", content_encoding="utf-8"
     )
     job = Request(m.decode(), message=m, app=self.app)
     self.assertIsInstance(job, Request)
     self.assertEqual(job.name, body["task"])
     self.assertEqual(job.id, body["id"])
     self.assertEqual(job.args, body["args"])
     us = from_utf8(us)
     if sys.version_info < (2, 6):
         self.assertEqual(next(keys(job.kwargs)), us)
         self.assertIsInstance(next(keys(job.kwargs)), str)
Exemple #21
0
 def test_from_message(self):
     body = {"task": mytask.name, "id": gen_unique_id(),
             "args": [2], "kwargs": {u"æØåveéðƒeæ": "bar"}}
     m = Message(None, body=anyjson.serialize(body), backend="foo",
                       content_type="application/json",
                       content_encoding="utf-8")
     tw = TaskRequest.from_message(m, m.decode())
     self.assertIsInstance(tw, TaskRequest)
     self.assertEqual(tw.task_name, body["task"])
     self.assertEqual(tw.task_id, body["id"])
     self.assertEqual(tw.args, body["args"])
     self.assertEqual(tw.kwargs.keys()[0],
                       u"æØåveéðƒeæ".encode("utf-8"))
     self.assertNotIsInstance(tw.kwargs.keys()[0], unicode)
     self.assertTrue(tw.logger)
Exemple #22
0
 def test_from_message(self):
     body = {"task": mytask.name, "id": gen_unique_id(),
             "args": [2], "kwargs": {u"æØåveéðƒeæ": "bar"}}
     m = Message(None, body=simplejson.dumps(body), backend="foo",
                       content_type="application/json",
                       content_encoding="utf-8")
     tw = TaskRequest.from_message(m, m.decode())
     self.assertIsInstance(tw, TaskRequest)
     self.assertEqual(tw.task_name, body["task"])
     self.assertEqual(tw.task_id, body["id"])
     self.assertEqual(tw.args, body["args"])
     self.assertEqual(tw.kwargs.keys()[0],
                       u"æØåveéðƒeæ".encode("utf-8"))
     self.assertNotIsInstance(tw.kwargs.keys()[0], unicode)
     self.assertTrue(tw.logger)
Exemple #23
0
 def test_from_message(self):
     us = u"æØåveéðƒeæ"
     body = {"task": mytask.name, "id": uuid(), "args": [2], "kwargs": {us: "bar"}}
     m = Message(
         None, body=anyjson.dumps(body), backend="foo", content_type="application/json", content_encoding="utf-8"
     )
     tw = TaskRequest.from_message(m, m.decode())
     self.assertIsInstance(tw, Request)
     self.assertEqual(tw.name, body["task"])
     self.assertEqual(tw.id, body["id"])
     self.assertEqual(tw.args, body["args"])
     us = from_utf8(us)
     if sys.version_info < (2, 6):
         self.assertEqual(tw.kwargs.keys()[0], us)
         self.assertIsInstance(tw.kwargs.keys()[0], str)
Exemple #24
0
    def _send_dhcp_notification(self, context, data, methodname):
        #self._dhcp_agent_notifier.notify(context, data, methodname)
        task_exchange = Exchange('nspagent', type='topic')   
	create_result = {"oslo.message" :{ 
			  #'message_id': '5c329c20-d435-444c-8fa2-e1b54592219c',
			  #'publisher_id': 'compute.host1',
			  'method': 'network_create_end',
			  #'_context_user_id': None,
		 	  #'_context_project_id': None,
			  #'_context_is_admin': True,
			  "args" : {"payload": {"network": network
					#{"id": "24783e1a-63fe-43d5-9989-e1515c24eecd"}
				      }		
				   }
		         },
			"oslo.version":'2.0',
		      }
	create_result["oslo.message" ] = json.dumps(create_result["oslo.message"])
	connection = Connection('amqp://*****:*****@192.168.49.22:5672//')  
	channel = connection.channel()  
   
	message=Message(channel,body= 'subenet_create_end')  
   
	# produce  
	producer = Producer(channel,serializer='json') 
        print message.body, task_exchange 
	producer.publish(create_result, routing_key='dhcp_agent')  
Exemple #25
0
 def test_from_message(self):
     us = u'æØåveéðƒeæ'
     body = {'task': mytask.name, 'id': uuid(),
             'args': [2], 'kwargs': {us: 'bar'}}
     m = Message(None, body=anyjson.dumps(body), backend='foo',
                 content_type='application/json',
                 content_encoding='utf-8')
     tw = TaskRequest.from_message(m, m.decode())
     self.assertIsInstance(tw, Request)
     self.assertEqual(tw.name, body['task'])
     self.assertEqual(tw.id, body['id'])
     self.assertEqual(tw.args, body['args'])
     us = from_utf8(us)
     if sys.version_info < (2, 6):
         self.assertEqual(tw.kwargs.keys()[0], us)
         self.assertIsInstance(tw.kwargs.keys()[0], str)
 def test_from_message(self):
     us = 'æØåveéðƒeæ'
     body = {'task': self.mytask.name, 'id': uuid(),
             'args': [2], 'kwargs': {us: 'bar'}}
     m = Message(None, body=anyjson.dumps(body), backend='foo',
                 content_type='application/json',
                 content_encoding='utf-8')
     job = Request(m.decode(), message=m, app=self.app)
     self.assertIsInstance(job, Request)
     self.assertEqual(job.name, body['task'])
     self.assertEqual(job.id, body['id'])
     self.assertEqual(job.args, body['args'])
     us = from_utf8(us)
     if sys.version_info < (2, 6):
         self.assertEqual(next(keys(job.kwargs)), us)
         self.assertIsInstance(next(keys(job.kwargs)), str)
Exemple #27
0
def create_message(channel, **data):
    data.setdefault('id', uuid())
    channel.no_ack_consumers = set()
    return Message(channel, body=pickle.dumps(dict(**data)),
                   content_type='application/x-python-serialize',
                   content_encoding='binary',
                   delivery_info={'consumer_tag': 'mock'})
Exemple #28
0
 def test_from_message(self):
     us = 'æØåveéðƒeæ'
     body = {'task': self.mytask.name, 'id': uuid(),
             'args': [2], 'kwargs': {us: 'bar'}}
     m = Message(None, body=anyjson.dumps(body), backend='foo',
                 content_type='application/json',
                 content_encoding='utf-8')
     job = TaskRequest.from_message(m, m.decode(), app=self.app)
     self.assertIsInstance(job, Request)
     self.assertEqual(job.name, body['task'])
     self.assertEqual(job.id, body['id'])
     self.assertEqual(job.args, body['args'])
     us = from_utf8(us)
     if sys.version_info < (2, 6):
         self.assertEqual(next(keys(job.kwargs)), us)
         self.assertIsInstance(next(keys(job.kwargs)), str)
Exemple #29
0
def create_message(channel, **data):
    data.setdefault("id", uuid())
    channel.no_ack_consumers = set()
    return Message(channel, body=pickle.dumps(dict(**data)),
                   content_type="application/x-python-serialize",
                   content_encoding="binary",
                   delivery_info={"consumer_tag": "mock"})
Exemple #30
0
class test_Message(TestCase):

    def setUp(self):
        self.conn = Connection('memory://')
        self.channel = self.conn.channel()
        self.message = Message(self.channel, delivery_tag=313)

    def test_ack_respects_no_ack_consumers(self):
        self.channel.no_ack_consumers = set(['abc'])
        self.message.delivery_info['consumer_tag'] = 'abc'
        ack = self.channel.basic_ack = Mock()

        self.message.ack()
        self.assertNotEqual(self.message._state, 'ACK')
        self.assertFalse(ack.called)

    def test_ack_missing_consumer_tag(self):
        self.channel.no_ack_consumers = set(['abc'])
        self.message.delivery_info = {}
        ack = self.channel.basic_ack = Mock()

        self.message.ack()
        ack.assert_called_with(self.message.delivery_tag)

    def test_ack_not_no_ack(self):
        self.channel.no_ack_consumers = set()
        self.message.delivery_info['consumer_tag'] = 'abc'
        ack = self.channel.basic_ack = Mock()

        self.message.ack()
        ack.assert_called_with(self.message.delivery_tag)

    def test_ack_log_error_when_no_error(self):
        ack = self.message.ack = Mock()
        self.message.ack_log_error(Mock(), KeyError)
        ack.assert_called_with()

    def test_ack_log_error_when_error(self):
        ack = self.message.ack = Mock()
        ack.side_effect = KeyError('foo')
        logger = Mock()
        self.message.ack_log_error(logger, KeyError)
        ack.assert_called_with()
        self.assertTrue(logger.critical.called)
        self.assertIn("Couldn't ack", logger.critical.call_args[0][0])
class test_Message(TestCase):

    def setUp(self):
        self.conn = BrokerConnection("memory://")
        self.channel = self.conn.channel()
        self.message = Message(self.channel, delivery_tag=313)

    def test_ack_respects_no_ack_consumers(self):
        self.channel.no_ack_consumers = set(["abc"])
        self.message.delivery_info["consumer_tag"] = "abc"
        ack = self.channel.basic_ack = Mock()

        self.message.ack()
        self.assertNotEqual(self.message._state, "ACK")
        self.assertFalse(ack.called)

    def test_ack_missing_consumer_tag(self):
        self.channel.no_ack_consumers = set(["abc"])
        self.message.delivery_info = {}
        ack = self.channel.basic_ack = Mock()

        self.message.ack()
        ack.assert_called_with(self.message.delivery_tag)

    def test_ack_not_no_ack(self):
        self.channel.no_ack_consumers = set()
        self.message.delivery_info["consumer_tag"] = "abc"
        ack = self.channel.basic_ack = Mock()

        self.message.ack()
        ack.assert_called_with(self.message.delivery_tag)

    def test_ack_log_error_when_no_error(self):
        ack = self.message.ack = Mock()
        self.message.ack_log_error(Mock(), KeyError)
        ack.assert_called_with()

    def test_ack_log_error_when_error(self):
        ack = self.message.ack = Mock()
        ack.side_effect = KeyError("foo")
        logger = Mock()
        self.message.ack_log_error(logger, KeyError)
        ack.assert_called_with()
        self.assertTrue(logger.critical.called)
        self.assertIn("Couldn't ack", logger.critical.call_args[0][0])
Exemple #32
0
def create_message(backend, **data):
    data.setdefault("id", gen_unique_id())
    return Message(backend,
                   body=pickle.dumps(dict(**data)),
                   content_type="application/x-python-serialize",
                   content_encoding="binary")
 def setUp(self):
     self.conn = BrokerConnection("memory://")
     self.channel = self.conn.channel()
     self.message = Message(self.channel, delivery_tag=313)
Exemple #34
0
 def setUp(self):
     self.conn = Connection('memory://')
     self.channel = self.conn.channel()
     self.message = Message(self.channel, delivery_tag=313)
Exemple #35
0
from kombu import Connection
from kombu.messaging import Producer
from kombu.transport.base import Message
from kombu_queues import task_exchange
from kombu_tasks import echo_task

rabbitmq_url = 'amqp://*****:*****@localhost:5672//'
message_num = 400

if __name__ == '__main__':
    connection = Connection(rabbitmq_url)
    channel = connection.channel()

    body_json = {'url': 'http://127.0.0.1', 'delay': 5}
    message_json = Message(channel, body=body_json)

    body_pickle = {
        'func': echo_task,
        'args': ('Hello Rabbit', 5),
        'kwargs': {}
    }
    message_pickle = Message(channel, body=body_pickle)

    producer_json = Producer(channel, exchange=task_exchange)
    producer_pickle = Producer(channel,
                               exchange=task_exchange,
                               serializer='pickle')
    for i in xrange(message_num):
        producer_json.publish(message_json.body, routing_key='json_queue')
        producer_pickle.publish(message_pickle.body,
                                routing_key='pickle_queue')
Exemple #36
0
 def test_postencode(self):
     with self.assertRaises(LookupError):
         Message(self.channel, text_t('FOO'), postencode='ccyzz')
Exemple #37
0
 def setUp(self):
     self.conn = Connection('memory://')
     self.channel = self.conn.channel()
     self.message = Message(self.channel, delivery_tag=313)
Exemple #38
0
class test_Message(Case):
    def setUp(self):
        self.conn = Connection('memory://')
        self.channel = self.conn.channel()
        self.message = Message(self.channel, delivery_tag=313)

    def test_postencode(self):
        with self.assertRaises(LookupError):
            Message(self.channel, text_t('FOO'), postencode='ccyzz')

    def test_ack_respects_no_ack_consumers(self):
        self.channel.no_ack_consumers = set(['abc'])
        self.message.delivery_info['consumer_tag'] = 'abc'
        ack = self.channel.basic_ack = Mock()

        self.message.ack()
        self.assertNotEqual(self.message._state, 'ACK')
        self.assertFalse(ack.called)

    def test_ack_missing_consumer_tag(self):
        self.channel.no_ack_consumers = set(['abc'])
        self.message.delivery_info = {}
        ack = self.channel.basic_ack = Mock()

        self.message.ack()
        ack.assert_called_with(self.message.delivery_tag)

    def test_ack_not_no_ack(self):
        self.channel.no_ack_consumers = set()
        self.message.delivery_info['consumer_tag'] = 'abc'
        ack = self.channel.basic_ack = Mock()

        self.message.ack()
        ack.assert_called_with(self.message.delivery_tag)

    def test_ack_log_error_when_no_error(self):
        ack = self.message.ack = Mock()
        self.message.ack_log_error(Mock(), KeyError)
        ack.assert_called_with()

    def test_ack_log_error_when_error(self):
        ack = self.message.ack = Mock()
        ack.side_effect = KeyError('foo')
        logger = Mock()
        self.message.ack_log_error(logger, KeyError)
        ack.assert_called_with()
        self.assertTrue(logger.critical.called)
        self.assertIn("Couldn't ack", logger.critical.call_args[0][0])

    def test_reject_log_error_when_no_error(self):
        reject = self.message.reject = Mock()
        self.message.reject_log_error(Mock(), KeyError)
        reject.assert_called_with()

    def test_reject_log_error_when_error(self):
        reject = self.message.reject = Mock()
        reject.side_effect = KeyError('foo')
        logger = Mock()
        self.message.reject_log_error(logger, KeyError)
        reject.assert_called_with()
        self.assertTrue(logger.critical.called)
        self.assertIn("Couldn't ack", logger.critical.call_args[0][0])
Exemple #39
0
# send.py

from kombu import Connection
from kombu.messaging import Producer
from entity import task_exchange
from kombu.transport.base import Message

connection = Connection('amqp://*****:*****@10.120.120.11:5672//')
channel = connection.channel()

message=Message(channel,body='Hello Kombu')

# produce
producer = Producer(channel,exchange=task_exchange)
producer.publish(message.body,routing_key='suo_piao')
import ipdb; ipdb.set_trace() ### XXX BREAKPOINT