class MiddlewareStackTestCase(TestCase): @inlineCallbacks def setUp(self): self.stack = MiddlewareStack([ (yield self.mkmiddleware('mw1')), (yield self.mkmiddleware('mw2')), (yield self.mkmiddleware('mw3')), ]) self.processed_messages = [] @inlineCallbacks def mkmiddleware(self, name): mw = ToyMiddleware(name, {}, self) yield mw.setup_middleware() returnValue(mw) def processed(self, name, direction, message, endpoint): self.processed_messages.append((name, direction, message, endpoint)) def assert_processed(self, expected): self.assertEqual(expected, self.processed_messages) @inlineCallbacks def test_apply_consume(self): self.assert_processed([]) yield self.stack.apply_consume('inbound', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw1', 'inbound', 'dummy_msg.mw1', 'end_foo'), ('mw2', 'inbound', 'dummy_msg.mw1.mw2', 'end_foo'), ('mw3', 'inbound', 'dummy_msg.mw1.mw2.mw3', 'end_foo'), ]) @inlineCallbacks def test_apply_publish(self): self.assert_processed([]) yield self.stack.apply_publish('inbound', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw3', 'inbound', 'dummy_msg.mw3', 'end_foo'), ('mw2', 'inbound', 'dummy_msg.mw3.mw2', 'end_foo'), ('mw1', 'inbound', 'dummy_msg.mw3.mw2.mw1', 'end_foo'), ])
class TestMiddlewareStack(VumiTestCase): @inlineCallbacks def setUp(self): self.stack = MiddlewareStack([ (yield self.mkmiddleware('mw1', ToyMiddleware)), (yield self.mkmiddleware('mw2', ToyAsymmetricMiddleware)), (yield self.mkmiddleware('mw3', ToyMiddleware)), ]) self.processed_messages = [] @inlineCallbacks def mkmiddleware(self, name, mw_class): mw = mw_class(name, {}, self) yield mw.setup_middleware() returnValue(mw) def processed(self, name, direction, message, connector_name): self.processed_messages.append( (name, direction, message, connector_name)) def assert_processed(self, expected): self.assertEqual(expected, self.processed_messages) @inlineCallbacks def test_apply_consume_inbound(self): self.assert_processed([]) yield self.stack.apply_consume('inbound', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw1', 'inbound', 'dummy_msg.mw1', 'end_foo'), ('mw2', 'consume_inbound', 'dummy_msg.mw1.mw2', 'end_foo'), ('mw3', 'inbound', 'dummy_msg.mw1.mw2.mw3', 'end_foo'), ]) @inlineCallbacks def test_apply_publish_inbound(self): self.assert_processed([]) yield self.stack.apply_publish('inbound', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw3', 'inbound', 'dummy_msg.mw3', 'end_foo'), ('mw2', 'publish_inbound', 'dummy_msg.mw3.mw2', 'end_foo'), ('mw1', 'inbound', 'dummy_msg.mw3.mw2.mw1', 'end_foo'), ]) @inlineCallbacks def test_apply_consume_outbound(self): self.assert_processed([]) yield self.stack.apply_consume('outbound', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw1', 'outbound', 'dummy_msg.mw1', 'end_foo'), ('mw2', 'consume_outbound', 'dummy_msg.mw1.mw2', 'end_foo'), ('mw3', 'outbound', 'dummy_msg.mw1.mw2.mw3', 'end_foo'), ]) @inlineCallbacks def test_apply_publish_outbound(self): self.assert_processed([]) yield self.stack.apply_publish('outbound', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw3', 'outbound', 'dummy_msg.mw3', 'end_foo'), ('mw2', 'publish_outbound', 'dummy_msg.mw3.mw2', 'end_foo'), ('mw1', 'outbound', 'dummy_msg.mw3.mw2.mw1', 'end_foo'), ]) @inlineCallbacks def test_apply_consume_event(self): self.assert_processed([]) yield self.stack.apply_consume('event', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw1', 'event', 'dummy_msg.mw1', 'end_foo'), ('mw2', 'consume_event', 'dummy_msg.mw1.mw2', 'end_foo'), ('mw3', 'event', 'dummy_msg.mw1.mw2.mw3', 'end_foo'), ]) @inlineCallbacks def test_apply_publish_event(self): self.assert_processed([]) yield self.stack.apply_publish('event', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw3', 'event', 'dummy_msg.mw3', 'end_foo'), ('mw2', 'publish_event', 'dummy_msg.mw3.mw2', 'end_foo'), ('mw1', 'event', 'dummy_msg.mw3.mw2.mw1', 'end_foo'), ]) @inlineCallbacks def test_teardown_in_reverse_order(self): def get_teardown_timestamps(): return [mw._teardown_done for mw in self.stack.middlewares] self.assertFalse(any(get_teardown_timestamps())) yield self.stack.teardown() self.assertTrue(all(get_teardown_timestamps())) teardown_order = sorted(self.stack.middlewares, key=lambda mw: mw._teardown_done) self.assertEqual([mw.name for mw in teardown_order], ['mw3', 'mw2', 'mw1'])
class TestMiddlewareStack(VumiTestCase): @inlineCallbacks def setUp(self): self.stack = MiddlewareStack([ (yield self.mkmiddleware('mw1', ToyMiddleware)), (yield self.mkmiddleware('mw2', ToyAsymmetricMiddleware)), (yield self.mkmiddleware('mw3', ToyMiddleware)), ]) self.processed_messages = [] @inlineCallbacks def mkmiddleware(self, name, mw_class): mw = mw_class( name, {'consume_priority': 0, 'publish_priority': 0}, self) yield mw.setup_middleware() returnValue(mw) @inlineCallbacks def mk_priority_middleware(self, name, mw_class, consume_pri, publish_pri): mw = mw_class( name, { 'consume_priority': consume_pri, 'publish_priority': publish_pri, }, self) yield mw.setup_middleware() returnValue(mw) def processed(self, name, direction, message, connector_name): self.processed_messages.append( (name, direction, message, connector_name)) def assert_processed(self, expected): self.assertEqual(expected, self.processed_messages) @inlineCallbacks def test_apply_consume_inbound(self): self.assert_processed([]) yield self.stack.apply_consume('inbound', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw1', 'inbound', 'dummy_msg.mw1', 'end_foo'), ('mw2', 'consume_inbound', 'dummy_msg.mw1.mw2', 'end_foo'), ('mw3', 'inbound', 'dummy_msg.mw1.mw2.mw3', 'end_foo'), ]) @inlineCallbacks def test_apply_publish_inbound(self): self.assert_processed([]) yield self.stack.apply_publish('inbound', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw3', 'inbound', 'dummy_msg.mw3', 'end_foo'), ('mw2', 'publish_inbound', 'dummy_msg.mw3.mw2', 'end_foo'), ('mw1', 'inbound', 'dummy_msg.mw3.mw2.mw1', 'end_foo'), ]) @inlineCallbacks def test_apply_consume_outbound(self): self.assert_processed([]) yield self.stack.apply_consume('outbound', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw1', 'outbound', 'dummy_msg.mw1', 'end_foo'), ('mw2', 'consume_outbound', 'dummy_msg.mw1.mw2', 'end_foo'), ('mw3', 'outbound', 'dummy_msg.mw1.mw2.mw3', 'end_foo'), ]) @inlineCallbacks def test_apply_publish_outbound(self): self.assert_processed([]) yield self.stack.apply_publish('outbound', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw3', 'outbound', 'dummy_msg.mw3', 'end_foo'), ('mw2', 'publish_outbound', 'dummy_msg.mw3.mw2', 'end_foo'), ('mw1', 'outbound', 'dummy_msg.mw3.mw2.mw1', 'end_foo'), ]) @inlineCallbacks def test_apply_consume_event(self): self.assert_processed([]) yield self.stack.apply_consume('event', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw1', 'event', 'dummy_msg.mw1', 'end_foo'), ('mw2', 'consume_event', 'dummy_msg.mw1.mw2', 'end_foo'), ('mw3', 'event', 'dummy_msg.mw1.mw2.mw3', 'end_foo'), ]) @inlineCallbacks def test_apply_publish_event(self): self.assert_processed([]) yield self.stack.apply_publish('event', 'dummy_msg', 'end_foo') self.assert_processed([ ('mw3', 'event', 'dummy_msg.mw3', 'end_foo'), ('mw2', 'publish_event', 'dummy_msg.mw3.mw2', 'end_foo'), ('mw1', 'event', 'dummy_msg.mw3.mw2.mw1', 'end_foo'), ]) @inlineCallbacks def test_teardown_in_reverse_order(self): def get_teardown_timestamps(): return [mw._teardown_done for mw in self.stack.consume_middlewares] self.assertFalse(any(get_teardown_timestamps())) yield self.stack.teardown() self.assertTrue(all(get_teardown_timestamps())) teardown_order = sorted(self.stack.consume_middlewares, key=lambda mw: mw._teardown_done) self.assertEqual([mw.name for mw in teardown_order], ['mw3', 'mw2', 'mw1']) @inlineCallbacks def test_middleware_priority_ordering(self): self.stack = MiddlewareStack([ (yield self.mk_priority_middleware('p2', ToyMiddleware, 3, 3)), (yield self.mkmiddleware('pn', ToyMiddleware)), (yield self.mk_priority_middleware('p1_1', ToyMiddleware, 2, 2)), (yield self.mk_priority_middleware('p1_2', ToyMiddleware, 2, 2)), (yield self.mk_priority_middleware('pasym', ToyMiddleware, 1, 4)), ]) # test consume self.assert_processed([]) yield self.stack.apply_consume('event', 'dummy_msg', 'end_foo') self.assert_processed([ ('pn', 'event', 'dummy_msg.pn', 'end_foo'), ('pasym', 'event', 'dummy_msg.pn.pasym', 'end_foo'), ('p1_1', 'event', 'dummy_msg.pn.pasym.p1_1', 'end_foo'), ('p1_2', 'event', 'dummy_msg.pn.pasym.p1_1.p1_2', 'end_foo'), ('p2', 'event', 'dummy_msg.pn.pasym.p1_1.p1_2.p2', 'end_foo'), ]) # test publish self.processed_messages = [] yield self.stack.apply_publish('event', 'dummy_msg', 'end_foo') self.assert_processed([ ('pn', 'event', 'dummy_msg.pn', 'end_foo'), ('p1_2', 'event', 'dummy_msg.pn.p1_2', 'end_foo'), ('p1_1', 'event', 'dummy_msg.pn.p1_2.p1_1', 'end_foo'), ('p2', 'event', 'dummy_msg.pn.p1_2.p1_1.p2', 'end_foo'), ('pasym', 'event', 'dummy_msg.pn.p1_2.p1_1.p2.pasym', 'end_foo'), ])
class TestMiddlewareStack(VumiTestCase): @inlineCallbacks def setUp(self): self.stack = MiddlewareStack( [ (yield self.mkmiddleware("mw1", ToyMiddleware)), (yield self.mkmiddleware("mw2", ToyAsymmetricMiddleware)), (yield self.mkmiddleware("mw3", ToyMiddleware)), ] ) self.processed_messages = [] @inlineCallbacks def mkmiddleware(self, name, mw_class): mw = mw_class(name, {"consume_priority": 0, "publish_priority": 0}, self) yield mw.setup_middleware() returnValue(mw) @inlineCallbacks def mk_priority_middleware(self, name, mw_class, consume_pri, publish_pri): mw = mw_class(name, {"consume_priority": consume_pri, "publish_priority": publish_pri}, self) yield mw.setup_middleware() returnValue(mw) def processed(self, name, direction, message, connector_name): self.processed_messages.append((name, direction, message, connector_name)) def assert_processed(self, expected): self.assertEqual(expected, self.processed_messages) @inlineCallbacks def test_apply_consume_inbound(self): self.assert_processed([]) yield self.stack.apply_consume("inbound", "dummy_msg", "end_foo") self.assert_processed( [ ("mw1", "inbound", "dummy_msg.mw1", "end_foo"), ("mw2", "consume_inbound", "dummy_msg.mw1.mw2", "end_foo"), ("mw3", "inbound", "dummy_msg.mw1.mw2.mw3", "end_foo"), ] ) @inlineCallbacks def test_apply_publish_inbound(self): self.assert_processed([]) yield self.stack.apply_publish("inbound", "dummy_msg", "end_foo") self.assert_processed( [ ("mw3", "inbound", "dummy_msg.mw3", "end_foo"), ("mw2", "publish_inbound", "dummy_msg.mw3.mw2", "end_foo"), ("mw1", "inbound", "dummy_msg.mw3.mw2.mw1", "end_foo"), ] ) @inlineCallbacks def test_apply_consume_outbound(self): self.assert_processed([]) yield self.stack.apply_consume("outbound", "dummy_msg", "end_foo") self.assert_processed( [ ("mw1", "outbound", "dummy_msg.mw1", "end_foo"), ("mw2", "consume_outbound", "dummy_msg.mw1.mw2", "end_foo"), ("mw3", "outbound", "dummy_msg.mw1.mw2.mw3", "end_foo"), ] ) @inlineCallbacks def test_apply_publish_outbound(self): self.assert_processed([]) yield self.stack.apply_publish("outbound", "dummy_msg", "end_foo") self.assert_processed( [ ("mw3", "outbound", "dummy_msg.mw3", "end_foo"), ("mw2", "publish_outbound", "dummy_msg.mw3.mw2", "end_foo"), ("mw1", "outbound", "dummy_msg.mw3.mw2.mw1", "end_foo"), ] ) @inlineCallbacks def test_apply_consume_event(self): self.assert_processed([]) yield self.stack.apply_consume("event", "dummy_msg", "end_foo") self.assert_processed( [ ("mw1", "event", "dummy_msg.mw1", "end_foo"), ("mw2", "consume_event", "dummy_msg.mw1.mw2", "end_foo"), ("mw3", "event", "dummy_msg.mw1.mw2.mw3", "end_foo"), ] ) @inlineCallbacks def test_apply_publish_event(self): self.assert_processed([]) yield self.stack.apply_publish("event", "dummy_msg", "end_foo") self.assert_processed( [ ("mw3", "event", "dummy_msg.mw3", "end_foo"), ("mw2", "publish_event", "dummy_msg.mw3.mw2", "end_foo"), ("mw1", "event", "dummy_msg.mw3.mw2.mw1", "end_foo"), ] ) @inlineCallbacks def test_teardown_in_reverse_order(self): def get_teardown_timestamps(): return [mw._teardown_done for mw in self.stack.consume_middlewares] self.assertFalse(any(get_teardown_timestamps())) yield self.stack.teardown() self.assertTrue(all(get_teardown_timestamps())) teardown_order = sorted(self.stack.consume_middlewares, key=lambda mw: mw._teardown_done) self.assertEqual([mw.name for mw in teardown_order], ["mw3", "mw2", "mw1"]) @inlineCallbacks def test_middleware_priority_ordering(self): self.stack = MiddlewareStack( [ (yield self.mk_priority_middleware("p2", ToyMiddleware, 3, 3)), (yield self.mkmiddleware("pn", ToyMiddleware)), (yield self.mk_priority_middleware("p1_1", ToyMiddleware, 2, 2)), (yield self.mk_priority_middleware("p1_2", ToyMiddleware, 2, 2)), (yield self.mk_priority_middleware("pasym", ToyMiddleware, 1, 4)), ] ) # test consume self.assert_processed([]) yield self.stack.apply_consume("event", "dummy_msg", "end_foo") self.assert_processed( [ ("pn", "event", "dummy_msg.pn", "end_foo"), ("pasym", "event", "dummy_msg.pn.pasym", "end_foo"), ("p1_1", "event", "dummy_msg.pn.pasym.p1_1", "end_foo"), ("p1_2", "event", "dummy_msg.pn.pasym.p1_1.p1_2", "end_foo"), ("p2", "event", "dummy_msg.pn.pasym.p1_1.p1_2.p2", "end_foo"), ] ) # test publish self.processed_messages = [] yield self.stack.apply_publish("event", "dummy_msg", "end_foo") self.assert_processed( [ ("pn", "event", "dummy_msg.pn", "end_foo"), ("p1_2", "event", "dummy_msg.pn.p1_2", "end_foo"), ("p1_1", "event", "dummy_msg.pn.p1_2.p1_1", "end_foo"), ("p2", "event", "dummy_msg.pn.p1_2.p1_1.p2", "end_foo"), ("pasym", "event", "dummy_msg.pn.p1_2.p1_1.p2.pasym", "end_foo"), ] )