async def test_replica_tries_to_take_over(app): plugins = setup_plugins(app, pg_replication_role='replica') assert app.initialize() == None plugins.reset_mock() # if there is no lock owner, we start looping trying to become master app.master_lock_changed(None) assert plugins.mock_calls == [call.pg_replication_role(), call.master_lock_changed(None)] plugins.reset_mock() from asyncio import sleep as real_sleep with patch('asyncio.sleep') as sleep: sleeper = FakeSleeper() sleep.side_effect = sleeper # the first thing is to sleep a bit await sleeper.next() assert sleeper.log == [3] assert plugins.mock_calls == [] # takeover attempted states = [(app.my_id, {'willing': 99.0}), (app.my_id, {'willing': 100.0})] plugins.dcs_list_state.return_value = states await sleeper.next() assert sleeper.log == [3, 3] assert plugins.mock_calls == [ call.dcs_list_state(), call.best_replicas([('42', {'willing': 99.0}), ('42', {'willing': 100.0})]), call.dcs_lock('master')]
async def test_master_unhealthy(app): plugins = setup_plugins(app, pg_replication_role='master') app.initialize() plugins.reset_mock() app.unhealthy('boom', 'It went Boom', can_be_replica=True) assert plugins.mock_calls == [ call.dcs_set_state({ 'host': '127.0.0.1', 'replication_role': 'master', 'health_problems': { 'boom': { 'reason': 'It went Boom', 'can_be_replica': True } } }), call.pg_replication_role(), call.dcs_delete_conn_info(), ] plugins.reset_mock() # now we should have _handle_unhealthy_master running with patch('asyncio.sleep') as sleep, patch( 'zgres.deadman.App._stop') as exit, patch( 'time.sleep') as blocking_sleep: sleeper = FakeSleeper() sleep.side_effect = sleeper exit.side_effect = lambda: sleeper.finish() # there is no replica, so we just sleep and ping the # DCS to find a willing replica states = [iter([])] plugins.dcs_list_state.side_effect = states await sleeper.next() assert plugins.mock_calls == [call.dcs_list_state()] # we add a willing replica states = [iter([('other', {'willing': 1})])] plugins.dcs_list_state.side_effect = states plugins.reset_mock() await sleeper.next() assert plugins.mock_calls == [ call.dcs_list_state(), call.pg_replication_role(), call.pg_stop(), call.dcs_disconnect() ]
async def test_master_unhealthy(app): plugins = setup_plugins(app, pg_replication_role='master') app.initialize() plugins.reset_mock() app.unhealthy('boom', 'It went Boom', can_be_replica=True) assert plugins.mock_calls == [ call.dcs_set_state({ 'host': '127.0.0.1', 'replication_role': 'master', 'health_problems': {'boom': {'reason': 'It went Boom', 'can_be_replica': True}}}), call.pg_replication_role(), call.dcs_delete_conn_info(), ] plugins.reset_mock() # now we should have _handle_unhealthy_master running with patch('asyncio.sleep') as sleep, patch('zgres.deadman.App._stop') as exit, patch('time.sleep') as blocking_sleep: sleeper = FakeSleeper() sleep.side_effect = sleeper exit.side_effect = lambda : sleeper.finish() # there is no replica, so we just sleep and ping the # DCS to find a willing replica states = [iter([])] plugins.dcs_list_state.side_effect = states await sleeper.next() assert plugins.mock_calls == [call.dcs_list_state()] # we add a willing replica states = [iter([('other', {'willing': 1})])] plugins.dcs_list_state.side_effect = states plugins.reset_mock() await sleeper.next() assert plugins.mock_calls == [ call.dcs_list_state(), call.pg_replication_role(), call.pg_stop(), call.dcs_disconnect() ]
async def test_replica_tries_to_take_over(app): plugins = setup_plugins(app, pg_replication_role='replica') assert app.initialize() == None plugins.reset_mock() # if there is no lock owner, we start looping trying to become master app.master_lock_changed(None) assert plugins.mock_calls == [ call.pg_replication_role(), call.master_lock_changed(None) ] plugins.reset_mock() from asyncio import sleep as real_sleep with patch('asyncio.sleep') as sleep: sleeper = FakeSleeper() sleep.side_effect = sleeper # the first thing is to sleep a bit await sleeper.next() assert sleeper.log == [3] assert plugins.mock_calls == [] # takeover attempted states = [(app.my_id, { 'willing': 99.0 }), (app.my_id, { 'willing': 100.0 })] plugins.dcs_list_state.return_value = states await sleeper.next() assert sleeper.log == [3, 3] assert plugins.mock_calls == [ call.dcs_list_state(), call.best_replicas([('42', { 'willing': 99.0 }), ('42', { 'willing': 100.0 })]), call.dcs_lock('master') ]