Exemplo n.º 1
0
class EventTest(unittest.TestCase):

    Type = "BasicFlow"
    Version = "v01"
    Flow_id = "Id01"
    Owner = "Owner"
    Enabled = True
    Priority = "65535"
    Status = "none"
    Attributes = {"bandwidth": "10Mbps", "req_bandwidth": "11Mbps", "latency": "10msec", "req_latency": "11msec"}
    Publisher_id = "Id1"
    Event_type = "flowchanged"

    flow_target = Flow(Type, Version, Flow_id, Owner, Enabled, Priority, Status, Attributes)

    flow_target_packed_object = flow_target.packed_object()

    def setUp(self):
        self.target = Event(self.Publisher_id, self.Event_type, self.flow_target)

    def tearDown(self):
        self.target = None

    def test_constructor(self):
        self.assertEqual(self.target.publisher_id, self.Publisher_id)
        self.assertEqual(self.target.event_type, self.Event_type)
        self.assertEqual(self.target.body, self.flow_target)

    def test_create_from_packed(self):
        Type = "BasicFlow"
        Version = "v02"
        Flow_id = "Id02"
        Owner = "Owner"
        Enabled = True
        Priority = "65535"
        Status = "none"
        Attributes = {"bandwidth": "20Mbps", "req_bandwidth": "11Mbps", "latency": "30msec", "req_latency": "11msec"}
        Publisher_id = "Id1"
        Event_type = "flowchanged"

        flow_target = Flow(Type, Version, Flow_id, Owner, Enabled, Priority, Status, Attributes)
        value = [Publisher_id, Event_type, flow_target]
        self.result = self.target.create_from_packed(value)

        self.assertEqual(self.result.publisher_id, self.Publisher_id)
        self.assertEqual(self.result.event_type, self.Event_type)
        self.assertEqual(self.result.body, flow_target)

    def test_packed_object_hasattr_True(self):
        self.result = self.target.packed_object()
        self.assertEqual(self.result[0], self.Publisher_id)
        self.assertEqual(self.result[1], self.Event_type)
        self.assertEqual(self.result[2], self.flow_target_packed_object)

    def test_packed_object_hasattr_False(self):
        self.newtarget = Event(self.Publisher_id, self.Event_type, self.flow_target_packed_object)
        self.result = self.newtarget.packed_object()
        self.assertEqual(self.result[0], self.Publisher_id)
        self.assertEqual(self.result[1], self.Event_type)
        self.assertEqual(self.result[2], self.flow_target_packed_object)
Exemplo n.º 2
0
    def test_redisSubscriberRunnable_except(self):
        event = Event("publisher_id", "event_type", "event_body")

        pk = msgpack.Packer()
        resb = bytearray()
        resb.extend(pk.pack(2))
        resb.extend(pk.pack(0))
        resb.extend(pk.pack("publisher_id:event_type"))
        resb.extend(pk.pack(event.packed_object()))
        self.value = {"type": "message",
                      "data": resb}

        with nested(
            patch("redis.client.PubSub.subscribe"),
                patch("logging.error"),
                patch("redis.client.PubSub.unsubscribe"),
                patch("redis.client.PubSub.listen"),
                patch(self.EVENT_PATH + ".create_from_packed")) as (
                mock_subscribe,
                logging_error,
                mock_unsubscribe,
                mock_listen,
                mock_event):
            mock_subscribe.return_value = None
            mock_unsubscribe.return_value = None
            mock_listen.return_value = [self.value]
            mock_event.side_effect = KeyError()

            self.target.start()
            self.target._MessageDispatcher__redisSubscriberRunnable()
            self.target.close()

            mock_event.assert_called_any_with(
                ["publisher_id", "event_type", "event_body"])
    def test_redisSubscriberRunnable_except(self):
        event = Event("publisher_id", "event_type", "event_body")

        pk = msgpack.Packer()
        resb = bytearray()
        resb.extend(pk.pack(2))
        resb.extend(pk.pack(0))
        resb.extend(pk.pack("publisher_id:event_type"))
        resb.extend(pk.pack(event.packed_object()))
        self.value = {"type": "message", "data": resb}

        with nested(patch("redis.client.PubSub.subscribe"),
                    patch("logging.error"),
                    patch("redis.client.PubSub.unsubscribe"),
                    patch("redis.client.PubSub.listen"),
                    patch(self.EVENT_PATH +
                          ".create_from_packed")) as (mock_subscribe,
                                                      logging_error,
                                                      mock_unsubscribe,
                                                      mock_listen, mock_event):
            mock_subscribe.return_value = None
            mock_unsubscribe.return_value = None
            mock_listen.return_value = [self.value]
            mock_event.side_effect = KeyError()

            self.target.start()
            self.target._MessageDispatcher__redisSubscriberRunnable()
            self.target.close()

            mock_event.assert_called_any_with(
                ["publisher_id", "event_type", "event_body"])
    def test_redisSubscriberRunnable_TYPE_EVENT(self):
        event = Event("publisher_id", "event_type", "event_body")

        pk = msgpack.Packer()
        resb = bytearray()
        resb.extend(pk.pack(2))
        resb.extend(pk.pack(0))
        resb.extend(pk.pack("publisher_id:event_type"))
        resb.extend(pk.pack(event.packed_object()))
        self.value = {"type": "message", "data": resb}

        self.target.thread_pool = futures.ThreadPoolExecutor(max_workers=8)
        self.target._MessageDispatcher__redisSubscriber = redis.StrictRedis(
            host=self.target._MessageDispatcher__redisServer,
            port=self.target._MessageDispatcher__redisPort)
        self.target._MessageDispatcher__redisSubscriber =\
            self.target._MessageDispatcher__redisSubscriber.pubsub()

        with nested(patch("redis.client.PubSub.subscribe"),
                    patch("redis.client.PubSub.unsubscribe"),
                    patch("redis.client.PubSub.listen"),
                    patch(self.EVENT_PATH +
                          ".create_from_packed")) as (mock_subscribe,
                                                      mock_unsubscribe,
                                                      mock_listen, mock_event):
            mock_subscribe.return_value = None
            mock_unsubscribe.return_value = None
            mock_listen.return_value = [self.value]

            self.target._MessageDispatcher__redisSubscriberRunnable()

            mock_event.assert_called_once_with(
                ["publisher_id", "event_type", "event_body"])
Exemplo n.º 5
0
    def test_redisSubscriberRunnable_TYPE_EVENT(self):
        event = Event("publisher_id", "event_type", "event_body")

        pk = msgpack.Packer()
        resb = bytearray()
        resb.extend(pk.pack(2))
        resb.extend(pk.pack(0))
        resb.extend(pk.pack("publisher_id:event_type"))
        resb.extend(pk.pack(event.packed_object()))
        self.value = {"type": "message",
                      "data": resb}

        self.target.thread_pool = futures.ThreadPoolExecutor(max_workers=8)
        self.target._MessageDispatcher__redisSubscriber = redis.StrictRedis(
            host=self.target._MessageDispatcher__redisServer,
            port=self.target._MessageDispatcher__redisPort)
        self.target._MessageDispatcher__redisSubscriber =\
            self.target._MessageDispatcher__redisSubscriber.pubsub()

        with nested(
            patch("redis.client.PubSub.subscribe"),
                patch("redis.client.PubSub.unsubscribe"),
                patch("redis.client.PubSub.listen"),
                patch(self.EVENT_PATH + ".create_from_packed")) as (
                mock_subscribe,
                mock_unsubscribe,
                mock_listen,
                mock_event):
            mock_subscribe.return_value = None
            mock_unsubscribe.return_value = None
            mock_listen.return_value = [self.value]

            self.target._MessageDispatcher__redisSubscriberRunnable()

            mock_event.assert_called_once_with(
                ["publisher_id", "event_type", "event_body"])
Exemplo n.º 6
0
class EventTest(unittest.TestCase):

    Type = "BasicFlow"
    Version = "v01"
    Flow_id = "Id01"
    Owner = "Owner"
    Enabled = True
    Priority = "65535"
    Status = "none"
    Attributes = {
        "bandwidth": "10Mbps",
        "req_bandwidth": "11Mbps",
        "latency": "10msec",
        "req_latency": "11msec"
    }
    Publisher_id = 'Id1'
    Event_type = 'flowchanged'

    flow_target = Flow(Type, Version, Flow_id, Owner, Enabled, Priority,
                       Status, Attributes)

    flow_target_packed_object = flow_target.packed_object()

    def setUp(self):
        self.target = Event(self.Publisher_id, self.Event_type,
                            self.flow_target)

    def tearDown(self):
        self.target = None

    def test_constructor(self):
        self.assertEqual(self.target.publisher_id, self.Publisher_id)
        self.assertEqual(self.target.event_type, self.Event_type)
        self.assertEqual(self.target.body, self.flow_target)

    def test_create_from_packed(self):
        Type = "BasicFlow"
        Version = "v02"
        Flow_id = "Id02"
        Owner = "Owner"
        Enabled = True
        Priority = "65535"
        Status = "none"
        Attributes = {
            "bandwidth": "20Mbps",
            "req_bandwidth": "11Mbps",
            "latency": "30msec",
            "req_latency": "11msec"
        }
        Publisher_id = 'Id1'
        Event_type = 'flowchanged'

        flow_target = Flow(Type, Version, Flow_id, Owner, Enabled, Priority,
                           Status, Attributes)
        value = [Publisher_id, Event_type, flow_target]
        self.result = self.target.create_from_packed(value)

        self.assertEqual(self.result.publisher_id, self.Publisher_id)
        self.assertEqual(self.result.event_type, self.Event_type)
        self.assertEqual(self.result.body, flow_target)

    def test_packed_object_hasattr_True(self):
        self.result = self.target.packed_object()
        self.assertEqual(self.result[0], self.Publisher_id)
        self.assertEqual(self.result[1], self.Event_type)
        self.assertEqual(self.result[2], self.flow_target_packed_object)

    def test_packed_object_hasattr_False(self):
        self.newtarget = Event(self.Publisher_id, self.Event_type,
                               self.flow_target_packed_object)
        self.result = self.newtarget.packed_object()
        self.assertEqual(self.result[0], self.Publisher_id)
        self.assertEqual(self.result[1], self.Event_type)
        self.assertEqual(self.result[2], self.flow_target_packed_object)