async def test__actually_start__add_dependencies(self, *, service): s1 = Mock() s2 = Mock() self._mock_for_start(service, init_deps=[s1, s2]) service.on_first_start.coro.side_effect = service._stopped.set await service._actually_start() service.add_dependency.assert_has_calls([call(s1), call(s2)])
def test_on_stream_event_in_out(self, *, mon, stream, event): state = mon.on_stream_event_in(TP1, 401, stream, event) client = mon.client.client client.increment.assert_has_calls([ call( "events", sample_rate=mon.rate, tags=["topic:foo", "partition:3", "stream:topic_foo"], value=1.0, ), call( "events_active", sample_rate=mon.rate, tags=["topic:foo", "partition:3", "stream:topic_foo"], value=1.0, ), ]) mon.on_stream_event_out(TP1, 401, stream, event, state) client.decrement.assert_called_once_with( "events_active", sample_rate=mon.rate, tags=["topic:foo", "partition:3", "stream:topic_foo"], value=1.0, ) client.timing.assert_called_once_with( "events_runtime", value=mon.secs_to_ms(mon.events_runtime[-1]), sample_rate=mon.rate, tags=["topic:foo", "partition:3", "stream:topic_foo"], )
def test_on_tp_commit(self, *, mon): offsets = { TP("foo", 0): 1001, TP("foo", 1): 2002, TP("bar", 3): 3003, } mon.on_tp_commit(offsets) client = mon.client.client client.gauge.assert_has_calls([ call( "committed_offset", value=1001, tags=["topic:foo", "partition:0"], sample_rate=mon.rate, ), call( "committed_offset", value=2002, tags=["topic:foo", "partition:1"], sample_rate=mon.rate, ), call( "committed_offset", value=3003, tags=["topic:bar", "partition:3"], sample_rate=mon.rate, ), ])
def test_update__kwargs(self, d): d.update(a=1, b=2, c=3) d.key_set.assert_has_calls([ call('a', 1), call('b', 2), call('c', 3), ])
def test_iterate(self, *, map): map.by_name = Mock(name='by_name') map._maybe_finalize = Mock() classes = list(map.iterate()) map._maybe_finalize.assert_called_once_with() map.by_name.assert_has_calls([call('redis'), call('rabbitmq')]) assert classes == [map.by_name(), map.by_name()]
def test_update__args(self, d): d.update({"a": 1, "b": 2, "c": 3}) d.key_set.assert_has_calls([ call("a", 1), call("b", 2), call("c", 3), ])
def test_update__kwargs(self, d): d.update(a=1, b=2, c=3) d.key_set.assert_has_calls([ call("a", 1), call("b", 2), call("c", 3), ])
def test_route__with_cors_options(self, *, web): handler = Mock() cors_options = { 'http://example.com': ResourceOptions( allow_credentials=True, expose_headers='*', allow_headers='*', max_age=300, allow_methods='*', ), } web.web_app = Mock() web._cors = Mock() web.route( pattern='/foo/', handler=handler, cors_options=cors_options, ) web.web_app.router.add_route.assert_has_calls( [call(method, '/foo/', ANY) for method in NON_OPTIONS_METHODS]) web._cors.add.assert_has_calls([ call(web.web_app.router.add_route(), _prepare_cors_options(cors_options)) for _ in NON_OPTIONS_METHODS ])
def test_discover_ignore(self, *, app): with patch('faust.app.base.venusian') as venusian: app.conf.origin = 'faust' app.conf.autodiscover = ['re', 'collections'] app.discover(categories=['faust.agent'], ignore=['re', 'faust']) assert venusian.Scanner().scan.assert_has_calls( [ call( re, categories=('faust.agent', ), ignore=['re', 'faust'], onerror=app._on_autodiscovery_error, ), call( faust, categories=('faust.agent', ), ignore=['re', 'faust'], onerror=app._on_autodiscovery_error, ), call( collections, categories=('faust.agent', ), ignore=['re', 'faust'], onerror=app._on_autodiscovery_error, ), ], any_order=True) is None
async def test_slurp(self, *, agent, app): aref = agent(index=None, active_partitions=None) stream = aref.stream.get_active_stream() agent._delegate_to_sinks = AsyncMock(name="_delegate_to_sinks") agent._reply = AsyncMock(name="_reply") def on_delegate(value): raise StopAsyncIteration() word = Word("word") word_req = ReqRepRequest(word, "reply_to", "correlation_id") message1 = Mock(name="message1", autospec=Message) message2 = Mock(name="message2", autospec=Message) event1 = Event(app, None, word_req, {}, message1) event2 = Event(app, "key", "bar", {}, message2) values = [ (event1, word), (event2, "bar"), ] class AIT: async def __aiter__(self): for event, value in values: stream.current_event = event yield value it = aiter(AIT()) await agent._slurp(aref, it) agent._reply.assert_called_once_with(None, word, word_req.reply_to, word_req.correlation_id) agent._delegate_to_sinks.coro.assert_has_calls([ call(word), call("bar"), ])
async def test_slurp(self, *, agent, app): aref = agent(index=None, active_partitions=None) stream = aref.stream.get_active_stream() agent._delegate_to_sinks = AsyncMock(name='_delegate_to_sinks') agent._reply = AsyncMock(name='_reply') def on_delegate(value): raise StopAsyncIteration() word = Word('word') word_req = ReqRepRequest(word, 'reply_to', 'correlation_id') message1 = Mock(name='message1', autospec=Message) message2 = Mock(name='message2', autospec=Message) event1 = Event(app, None, word_req, message1) event2 = Event(app, 'key', 'bar', message2) values = [ (event1, word), (event2, 'bar'), ] class AIT: async def __aiter__(self): for event, value in values: stream.current_event = event yield value it = aiter(AIT()) await agent._slurp(aref, it) agent._reply.assert_called_once_with(None, word, word_req) agent._delegate_to_sinks.coro.assert_has_calls([ call(word), call('bar'), ])
async def test_handle_attached(self, *, consumer): consumer.app = Mock( name='app', autospec=App, _attachments=Mock( autospec=Attachments, publish_for_tp_offset=AsyncMock(), ), producer=Mock( autospec=Service, wait_many=AsyncMock(), ), ) await consumer._handle_attached({ TP1: 3003, TP2: 6006, }) consumer.app._attachments.publish_for_tp_offset.coro.assert_has_calls([ call(TP1, 3003), call(TP2, 6006), ]) consumer.app.producer.wait_many.coro.assert_called_with(ANY) att = consumer.app._attachments att.publish_for_tp_offset.coro.return_value = None await consumer._handle_attached({ TP1: 3003, TP2: 6006, })
def test_del_old_keys(self, *, table): on_window_close = table._on_window_close = Mock(name='on_window_close') table.window = Mock(name='window') table._data = { ('boo', (1.1, 1.4)): 'BOO', ('moo', (1.4, 1.6)): 'MOO', ('faa', (1.9, 2.0)): 'FAA', } table._partition_timestamps = { TP1: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], } table._partition_timestamp_keys = { (TP1, 2.0): [ ('boo', (1.1, 1.4)), ('moo', (1.4, 1.6)), ('faa', (1.9, 2.0)), ], } def is_stale(timestamp, latest_timestamp): return timestamp < 4.0 table.window.stale.side_effect = is_stale table._del_old_keys() assert table._partition_timestamps[TP1] == [4.0, 5.0, 6.0, 7.0] assert not table.data on_window_close.assert_has_calls([ call(('boo', (1.1, 1.4)), 'BOO'), call(('moo', (1.4, 1.6)), 'MOO'), call(('faa', (1.9, 2.0)), 'FAA'), ])
def test_on_send_initiated_completed(self, *, mon): producer = Mock(name='producer') state = mon.on_send_initiated(producer, 'topic1', 'message', 321, 123) mon.on_send_completed(producer, state, Mock(name='metadata')) client = mon.client.client client.increment.assert_has_calls([ call('topic_messages_sent', sample_rate=mon.rate, tags=['topic:topic1'], value=1.0), call('messages_sent', sample_rate=mon.rate, tags=None, value=1.0), ]) client.timing.assert_called_once_with( 'send_latency', value=ANY, sample_rate=mon.rate, tags=None, ) mon.on_send_error(producer, KeyError('foo'), state) client.increment.assert_has_calls([ call('messages_send_failed', sample_rate=mon.rate, tags=None, value=1.0), ]) client.timing.assert_has_calls([ call('send_latency_for_error', value=ANY, sample_rate=mon.rate, tags=None), ])
async def test_declare__defaults(self, *, topic): topic.app.conf.topic_allow_declare = True producer = Mock(create_topic=AsyncMock()) topic._get_producer = AsyncMock(return_value=producer) topic.partitions = None topic.replicas = None topic.topics = ["foo", "bar"] await topic.declare() producer.create_topic.coro.assert_has_calls([ call( topic="foo", partitions=topic.app.conf.topic_partitions, replication=topic.app.conf.topic_replication_factor, config=topic.config, compacting=topic.compacting, deleting=topic.deleting, retention=topic.retention, ), call( topic="bar", partitions=topic.app.conf.topic_partitions, replication=topic.app.conf.topic_replication_factor, config=topic.config, compacting=topic.compacting, deleting=topic.deleting, retention=topic.retention, ), ])
async def test_declare(self, *, topic): topic.app.conf.topic_allow_declare = True producer = Mock(create_topic=AsyncMock()) topic._get_producer = AsyncMock(return_value=producer) topic.partitions = 101 topic.replicas = 202 topic.topics = ['foo', 'bar'] await topic.declare() producer.create_topic.coro.assert_has_calls([ call( topic='foo', partitions=101, replication=202, config=topic.config, compacting=topic.compacting, deleting=topic.deleting, retention=topic.retention, ), call( topic='bar', partitions=101, replication=202, config=topic.config, compacting=topic.compacting, deleting=topic.deleting, retention=topic.retention, ), ])
def test_on_stream_event_in_out(self, *, mon, stream, event): mon.on_stream_event_in(TP1, 401, stream, event) client = mon.client.client client.increment.assert_has_calls([ call('events', sample_rate=mon.rate, tags=['topic:foo', 'partition:3', 'stream:topic_foo'], value=1.0), call('events_active', sample_rate=mon.rate, tags=['topic:foo', 'partition:3', 'stream:topic_foo'], value=1.0), ]) mon.on_stream_event_out(TP1, 401, stream, event) client.decrement.assert_called_once_with( 'events_active', sample_rate=mon.rate, tags=['topic:foo', 'partition:3', 'stream:topic_foo'], value=1.0, ) client.timing.assert_called_once_with( 'events_runtime', value=mon._time(mon.events_runtime[-1]), sample_rate=mon.rate, tags=['topic:foo', 'partition:3', 'stream:topic_foo'], )
def test_on_message_in_out(self, *, mon): message = Mock(name='message') mon.on_message_in(TP1, 400, message) client = mon.client.client client.increment.assert_has_calls([ call('messages_received', sample_rate=mon.rate, tags=['topic:foo', 'partition:3'], value=1.0), call('messages_active', sample_rate=mon.rate, tags=['topic:foo', 'partition:3'], value=1.0), ]) client.gauge.assert_called_once_with( 'read_offset', sample_rate=1.0, tags=['topic:foo', 'partition:3'], value=400, ) mon.on_message_out(TP1, 400, message) client.decrement.assert_called_once_with( 'messages_active', sample_rate=mon.rate, tags=['topic:foo', 'partition:3'], value=1.0)
def test_update__args(self, d): d.update({'a': 1, 'b': 2, 'c': 3}) d.key_set.assert_has_calls([ call('a', 1), call('b', 2), call('c', 3), ])
def test_discover_ignore(self, *, app): with patch("faust.app.base.venusian") as venusian: app.conf.origin = "faust" app.conf.autodiscover = ["re", "collections"] app.discover(categories=["faust.agent"], ignore=["re", "faust"]) assert (venusian.Scanner().scan.assert_has_calls( [ call( re, categories=("faust.agent", ), ignore=["re", "faust"], onerror=app._on_autodiscovery_error, ), call( faust, categories=("faust.agent", ), ignore=["re", "faust"], onerror=app._on_autodiscovery_error, ), call( collections, categories=("faust.agent", ), ignore=["re", "faust"], onerror=app._on_autodiscovery_error, ), ], any_order=True, ) is None)
async def test__stop_transactions(self, *, manager, producer): await manager._stop_transactions(["0-0", "1-0"]) producer.stop_transaction.assert_has_calls([ call("0-0"), call.coro("0-0"), call("1-0"), call.coro("1-0"), ])
async def test__stop_transactions(self, *, manager, producer): await manager._stop_transactions(['0-0', '1-0']) producer.stop_transaction.assert_has_calls([ call('0-0'), call.coro('0-0'), call('1-0'), call.coro('1-0'), ])
def test_write(self): s = self.spinner(isatty=True) s.write('f') s.file.write.assert_has_calls([ call('f'), call(''), ]) s.file.flush.assert_called_once_with()
def test__install_signal_handlers_unix(self, worker): worker.loop = Mock() worker._install_signal_handlers_unix() worker.loop.add_signal_handler.assert_has_calls([ call(signal.SIGINT, worker._on_sigint), call(signal.SIGTERM, worker._on_sigterm), call(signal.SIGUSR1, worker._on_sigusr1), ])
def test_load_extension_classes(): with patch_iter_entry_points(): with patch("mode.utils.imports.symbol_by_name") as sbn: assert list(load_extension_classes("foo")) == [ EntrypointExtension("ep1", sbn.return_value), EntrypointExtension("ep2", sbn.return_value), ] sbn.assert_has_calls([call("foo:a"), call("bar:c")])
def test_load_extension_classes(): with patch_iter_entry_points(): with patch('mode.utils.imports.symbol_by_name') as sbn: assert list(load_extension_classes('foo')) == [ EntrypointExtension('ep1', sbn.return_value), EntrypointExtension('ep2', sbn.return_value), ] sbn.assert_has_calls([call('foo:a'), call('bar:c')])
async def test_start_transactions(self, *, manager, producer): manager._start_new_producer = AsyncMock() await manager._start_transactions(['0-0', '1-0']) producer.maybe_begin_transaction.assert_has_calls([ call('0-0'), call.coro('0-0'), call('1-0'), call.coro('1-0'), ])
def test_import_relative_to_app__with_origin_l2(self, *, command, ctx): app = ctx.find_root().app app.conf.origin = 'root.moo:bar' with patch('faust.cli.base.symbol_by_name') as symbol_by_name: symbol_by_name.side_effect = [ImportError(), 'x'] assert command.import_relative_to_app('foo') == 'x' symbol_by_name.assert_has_calls([ call('foo'), call('root.moo.models.foo'), ])
def test_import_relative_to_app__with_origin_l2(self, *, command, ctx): app = ctx.find_root().app app.conf.origin = "root.moo:bar" with patch("faust.cli.base.symbol_by_name") as symbol_by_name: symbol_by_name.side_effect = [ImportError(), "x"] assert command.import_relative_to_app("foo") == "x" symbol_by_name.assert_has_calls([ call("foo"), call("root.moo.models.foo"), ])
def test_on_rebalance(self, *, mon): app = Mock(name='app') state = mon.on_rebalance_start(app) mon.client.incr.assert_has_calls([ call('rebalances', rate=mon.rate), ]) mon.on_rebalance_return(app, state) mon.client.incr.assert_has_calls([ call('rebalances', rate=mon.rate), call('rebalances_recovering', rate=mon.rate), ]) mon.client.decr.assert_has_calls([ call('rebalances', rate=mon.rate), ]) mon.client.timing.assert_has_calls([ call('rebalance_return_latency', mon.ms_since(state['time_return']), rate=mon.rate), ]) mon.on_rebalance_end(app, state) mon.client.decr.assert_has_calls([ call('rebalances', rate=mon.rate), call('rebalances_recovering', rate=mon.rate), ]) mon.client.timing.assert_has_calls([ call('rebalance_return_latency', mon.ms_since(state['time_return']), rate=mon.rate), call('rebalance_end_latency', mon.ms_since(state['time_end']), rate=mon.rate), ])