def test_worker_context_gets_stack(container_factory): context_cls = worker_context_factory() class FooService(object): name = 'baz' container = container_factory(FooService, {}) service = FooService() context = context_cls(container, service, DummyProvider("bar")) assert context.call_id == 'baz.bar.0' assert context.call_id_stack == ['baz.bar.0'] assert context.parent_call_stack == [] # Build stack context = context_cls(container, service, DummyProvider("foo"), data={'call_id_stack': context.call_id_stack}) assert context.call_id == 'baz.foo.1' assert context.call_id_stack == ['baz.bar.0', 'baz.foo.1'] # Long stack many_ids = [str(i) for i in xrange(10)] context = context_cls(container, service, DummyProvider("long"), data={'call_id_stack': many_ids}) expected = many_ids + ['baz.long.2'] assert context.call_id_stack == expected
def test_short_call_stack(container_factory): context_cls = worker_context_factory() class FooService(object): name = 'baz' container = container_factory(FooService, {PARENT_CALLS_CONFIG_KEY: 1}) service = FooService() # Trim stack many_ids = [str(i) for i in xrange(100)] context = context_cls(container, service, DummyProvider("long"), data={'call_id_stack': many_ids}) assert context.call_id_stack == ['99', 'baz.long.0']
def test_header_decoder(): headers = { "testprefix.foo": "FOO", "testprefix.bar": "BAR", "testprefix.baz": "BAZ", "bogusprefix.foo": "XXX", "testprefix.call_id_stack": ["a", "b", "c"], } decoder = HeaderDecoder() with patch.object(decoder, "header_prefix", new="testprefix"): worker_ctx_cls = worker_context_factory("foo", "bar", "call_id_stack") message = Mock(headers=headers) res = decoder.unpack_message_headers(worker_ctx_cls, message) assert res == {"foo": "FOO", "bar": "BAR", "call_id_stack": ["a", "b", "c"]}
def test_header_encoder(empty_config): context_data = { "foo": "FOO", "bar": "BAR", "baz": "BAZ", # unserialisable, shouldn't be included in the processed headers "none": None, } encoder = HeaderEncoder() with patch.object(encoder, "header_prefix", new="testprefix"): worker_ctx_cls = worker_context_factory("foo", "bar", "xxx") worker_ctx = worker_ctx_cls(data=context_data) worker_ctx.call_id_stack = ["x"] res = encoder.get_message_headers(worker_ctx) assert res == {"testprefix.foo": "FOO", "testprefix.bar": "BAR", "testprefix.call_id_stack": ["x"]}
def test_header_encoder(empty_config): context_data = { 'foo': 'FOO', 'bar': 'BAR', 'baz': 'BAZ', # unserialisable, shouldn't be included in the processed headers 'none': None, } encoder = HeaderEncoder() with patch.object(encoder, 'header_prefix', new="testprefix"): worker_ctx_cls = worker_context_factory('foo', 'bar', 'xxx') worker_ctx = worker_ctx_cls(data=context_data) worker_ctx.call_id_stack = ['x'] res = encoder.get_message_headers(worker_ctx) assert res == {'testprefix.foo': 'FOO', 'testprefix.bar': 'BAR', 'testprefix.call_id_stack': ['x']}
def test_header_decoder(): headers = { 'testprefix.foo': 'FOO', 'testprefix.bar': 'BAR', 'testprefix.baz': 'BAZ', 'bogusprefix.foo': 'XXX', 'testprefix.call_id_stack': ['a', 'b', 'c'], } decoder = HeaderDecoder() with patch.object(decoder, 'header_prefix', new="testprefix"): worker_ctx_cls = worker_context_factory("foo", "bar", "call_id_stack") message = Mock(headers=headers) res = decoder.unpack_message_headers(worker_ctx_cls, message) assert res == { 'foo': 'FOO', 'bar': 'BAR', 'call_id_stack': ['a', 'b', 'c'], }