def test_get_event_consumer_max_msg_age_invalid(consumer_config, auth_client, subscriber_client, caplog, emulator, metrics, pw_value): """max_msg_age is invalid""" consumer_config['max_msg_age'] = pw_value success_chnl, error_chnl = asyncio.Queue(), asyncio.Queue() with pytest.raises(exceptions.GCPConfigError) as e: service.get_event_consumer(consumer_config, success_chnl, error_chnl, metrics) subscriber_client.create_subscription.assert_not_called() e.match('Invalid configuration:\nInvalid value for max_msg_age \\(discard ' 'messages older than this many seconds\\).') assert 1 == len(caplog.records)
def test_get_event_consumer_config_raises(config_key, exp_msg, consumer_config, auth_client, subscriber_client, caplog, emulator, metrics): """Raise with improper configuration.""" consumer_config.pop(config_key) success_chnl, error_chnl = asyncio.Queue(), asyncio.Queue() with pytest.raises(exceptions.GCPConfigError) as e: service.get_event_consumer(consumer_config, success_chnl, error_chnl, metrics) subscriber_client.create_subscription.assert_not_called() e.match('Invalid configuration:\n' + exp_msg) assert 1 == len(caplog.records)
def test_get_event_consumer_raises(consumer_config, auth_client, subscriber_client, caplog, emulator, exp_topic, exp_sub, metrics): """Raise when any other error occured with creating a subscription.""" success_chnl, error_chnl = asyncio.Queue(), asyncio.Queue() exp = google_exceptions.BadRequest('foo') sub_inst = subscriber_client.return_value sub_inst.create_subscription.side_effect = [exp] with pytest.raises(exceptions.GCPGordonError) as e: service.get_event_consumer(consumer_config, success_chnl, error_chnl, metrics) sub_inst.create_subscription.assert_called_once_with(exp_sub, exp_topic) e.match(f'Error trying to create subscription "{exp_sub}"') assert 3 == len(caplog.records)
def test_get_event_consumer_raises_topic(consumer_config, auth_client, subscriber_client, caplog, emulator, exp_topic, exp_sub, metrics): """Raise when there is no topic to subscribe to.""" success_chnl, error_chnl = asyncio.Queue(), asyncio.Queue() exp = google_exceptions.NotFound('foo') sub_inst = subscriber_client.return_value sub_inst.create_subscription.side_effect = [exp] with pytest.raises(exceptions.GCPGordonError) as e: service.get_event_consumer(consumer_config, success_chnl, error_chnl, metrics) sub_inst.create_subscription.assert_called_once_with(exp_sub, exp_topic) e.match(f'Topic "{exp_topic}" does not exist.') assert 3 == len(caplog.records)
def test_get_event_consumer_max_msg_age_set(consumer_config, auth_client, subscriber_client, caplog, emulator, metrics): """max_msg_age is correctly set.""" success_chnl, error_chnl = asyncio.Queue(), asyncio.Queue() plugin = service.get_event_consumer(consumer_config, success_chnl, error_chnl, metrics) assert consumer_config['max_msg_age'] == plugin._max_msg_age
def test_get_event_consumer_max_msg_age_unset(consumer_config, auth_client, subscriber_client, caplog, emulator, metrics): """max_msg_age is unset (default).""" success_chnl, error_chnl = asyncio.Queue(), asyncio.Queue() consumer_config.pop('max_msg_age') plugin = service.get_event_consumer(consumer_config, success_chnl, error_chnl, metrics) assert 300 == plugin._max_msg_age
def test_get_event_consumer_sub_exists(consumer_config, auth_client, subscriber_client, emulator, exp_topic, exp_sub, metrics): """Do not raise if topic already exists.""" success_chnl, error_chnl = asyncio.Queue(), asyncio.Queue() exp = google_exceptions.AlreadyExists('foo') sub_inst = subscriber_client.return_value sub_inst.create_subscription.side_effect = [exp] client = service.get_event_consumer(consumer_config, success_chnl, error_chnl, metrics) assert client._subscriber sub_inst.create_subscription.assert_called_once_with(exp_sub, exp_topic)
def test_get_event_consumer(local, provide_loop, topic, sub, consumer_config, exp_topic, auth_client, exp_sub, subscriber_client, emulator, monkeypatch, event_loop, metrics): """Happy path to initialize an Event Consumer client.""" success_chnl, error_chnl = asyncio.Queue(), asyncio.Queue() if local: monkeypatch.setenv('PUBSUB_EMULATOR_HOST', True) consumer_config['topic'], consumer_config['subscription'] = topic, sub kwargs = { 'config': consumer_config, 'success_channel': success_chnl, 'error_channel': error_chnl, 'metrics': metrics } if provide_loop: kwargs['loop'] = event_loop client = service.get_event_consumer(**kwargs) creds = None if not local: creds = auth_client.return_value.creds subscriber_client.assert_called_once_with(credentials=creds) sub_inst = subscriber_client.return_value sub_inst.create_subscription.assert_called_once_with(exp_sub, exp_topic) assert client._validator assert client._parser assert client.success_channel is success_chnl assert client.error_channel is error_chnl assert client._subscriber assert exp_sub == client._subscription assert ['audit-log', 'event'] == sorted(client._message_schemas) if provide_loop: assert event_loop is client._loop else: assert event_loop is not client._loop