def test_clear_cache(self): state_manager = ActorStateManager(self._fake_actor) _run(state_manager.set_state('state1', 'value1')) _run(state_manager.set_state('state2', 'value2')) _run(state_manager.set_state('state3', 'value3')) _run(state_manager.clear_cache()) self.assertEqual(0, len(state_manager._state_change_tracker))
def test_set_state_for_existing_state_only_in_mem(self): state_manager = ActorStateManager(self._fake_actor) _run(state_manager.set_state('state1', 'value1')) state = state_manager._state_change_tracker['state1'] self.assertEqual(StateChangeKind.add, state.change_kind) self.assertEqual('value1', state.value) _run(state_manager.set_state('state1', 'value2')) state = state_manager._state_change_tracker['state1'] self.assertEqual(StateChangeKind.add, state.change_kind) self.assertEqual('value2', state.value)
def test_get_state_names(self): state_manager = ActorStateManager(self._fake_actor) _run(state_manager.set_state('state1', 'value1')) _run(state_manager.set_state('state2', 'value2')) _run(state_manager.set_state('state3', 'value3')) names = _run(state_manager.get_state_names()) self.assertEqual(['state1', 'state2', 'state3'], names) self._fake_client.get_state.mock.assert_any_call( self._test_type_info._name, self._test_actor_id.id, 'state1') self._fake_client.get_state.mock.assert_any_call( self._test_type_info._name, self._test_actor_id.id, 'state2') self._fake_client.get_state.mock.assert_any_call( self._test_type_info._name, self._test_actor_id.id, 'state3')
def test_contains_state_for_removed_state(self): state_manager = ActorStateManager(self._fake_actor) _run(state_manager.set_state('state1', 'value1')) exist = _run(state_manager.contains_state('state1')) self.assertTrue(exist)
def test_remove_state_twice_for_existing_state_in_mem(self): state_manager = ActorStateManager(self._fake_actor) _run(state_manager.set_state('state1', 'value1')) removed = _run(state_manager.try_remove_state('state1')) self.assertTrue(removed) removed = _run(state_manager.try_remove_state('state1')) self.assertFalse(removed)
def test_set_state_for_existing_state(self): state_manager = ActorStateManager(self._fake_actor) state_change_tracker = state_manager._get_contextual_state_tracker() _run(state_manager.set_state('state1', 'value2')) state = state_change_tracker['state1'] self.assertEqual(StateChangeKind.update, state.change_kind) self.assertEqual('value2', state.value)
def test_save_state(self): state_manager = ActorStateManager(self._fake_actor) # set states which are StateChangeKind.add _run(state_manager.set_state('state1', 'value1')) _run(state_manager.set_state('state2', 'value2')) has_value, val = _run(state_manager.try_get_state('state3')) self.assertTrue(has_value) self.assertEqual("value3", val) # set state which is StateChangeKind.remove _run(state_manager.remove_state('state4')) # set state which is StateChangeKind.update _run(state_manager.set_state('state5', 'value5')) expected = b'[{"operation":"upsert","request":{"key":"state1","value":"value1"}},{"operation":"upsert","request":{"key":"state2","value":"value2"}},{"operation":"delete","request":{"key":"state4"}},{"operation":"upsert","request":{"key":"state5","value":"value5"}}]' # noqa: E501 # Save the state def mock_save_state(actor_type, actor_id, data): self.assertEqual(expected, data) self._fake_client.save_state_transactionally.mock = mock_save_state _run(state_manager.save_state())