Example #1
0
    def _create_event(self, event_type=None, **kwargs):
        event_type = event_type or self.event_type
        assert event_type

        if 'ts_created' not in kwargs:
            kwargs['ts_created'] = get_ion_ts()

        event_msg = bootstrap.IonObject(event_type, **kwargs)
        event_msg.base_types = event_msg._get_extends()

        # Would like to validate here but blows up if an object is provided where a dict is declared
        #event_msg._validate()

        return event_msg
Example #2
0
 def _on_message(self, msg, *args, **kwargs):
     conv = {}
     unknown = 'unknown'
     conv['sender'] = args[0].get('sender', unknown)
     conv['recipient'] = args[0].get('receiver', unknown)
     conv['conversation_id'] = args[0].get('conv-id', unknown)
     conv['protocol'] = args[0].get('protocol', unknown)
     if (len(self.header_fields) == 0):
         conv['headers'] = args[0]
     else:
         conv['headers'] = {}
         for key in self.header_fields:
             conv['headers'][key] = args[0].get([key], unknown)
     conv_msg = bootstrap.IonObject("ConversationMessage", conv)
     self.conv_queue.put(conv_msg)
Example #3
0
    def publish_event(self, origin=None, event_type=None, **kwargs):
        """
        Publishes an event of given type for the given origin. Event_type defaults to an
        event_type set when initializing the EventPublisher. Other kwargs fill out the fields
        of the event. This operation will fail with an exception.
        @param origin     the origin field value
        @param event_type the event type (defaults to the EventPublisher's event_type if set)
        @param kwargs     additional event fields
        @retval event_object    the event object which was published
        """

        event_type = event_type or self.event_type
        assert event_type

        event_object = bootstrap.IonObject(event_type, origin=origin, **kwargs)
        event_object.base_types = event_object._get_extends()
        ret_val = self.publish_event_object(event_object)
        return ret_val
Example #4
0
    def test_pub_and_sub(self):
        ar = event.AsyncResult()
        gq = queue.Queue()
        self.count = 0

        def cb(*args, **kwargs):
            self.count += 1
            gq.put(args[0])
            if self.count == 2:
                ar.set()

        sub = EventSubscriber(event_type="ResourceEvent",
                              callback=cb,
                              origin="specific")
        pub = EventPublisher(event_type="ResourceEvent")

        self._listen(sub)
        pub.publish_event(origin="specific", description="hello")

        event_obj = bootstrap.IonObject('ResourceEvent',
                                        origin='specific',
                                        description='more testing')
        self.assertEqual(event_obj, pub.publish_event_object(event_obj))

        with self.assertRaises(BadRequest) as cm:
            event_obj = bootstrap.IonObject('ResourceEvent',
                                            origin='specific',
                                            description='more testing',
                                            ts_created='2423')
            pub.publish_event_object(event_obj)
        self.assertIn('The ts_created value is not a valid timestamp',
                      cm.exception.message)

        with self.assertRaises(BadRequest) as cm:
            event_obj = bootstrap.IonObject('ResourceEvent',
                                            origin='specific',
                                            description='more testing',
                                            ts_created='1000494978462')
            pub.publish_event_object(event_obj)
        self.assertIn('This ts_created value is too old', cm.exception.message)

        with self.assertRaises(BadRequest) as cm:
            event_obj = bootstrap.IonObject('ResourceEvent',
                                            origin='specific',
                                            description='more testing')
            event_obj._id = '343434'
            pub.publish_event_object(event_obj)
        self.assertIn('The event object cannot contain a _id field',
                      cm.exception.message)

        ar.get(timeout=5)

        res = []
        for x in xrange(self.count):
            res.append(gq.get(timeout=5))

        self.assertEquals(len(res), self.count)
        self.assertEquals(res[0].description, "hello")
        self.assertAlmostEquals(int(res[0].ts_created),
                                int(get_ion_ts()),
                                delta=5000)

        self.assertEquals(res[1].description, "more testing")
        self.assertAlmostEquals(int(res[1].ts_created),
                                int(get_ion_ts()),
                                delta=5000)