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_fire_timer_unregistered(self): with self.assertRaises(ValueError): _run( ActorRuntime.fire_timer( 'UnknownType', 'test-id', 'test_timer', '{ "callback": "timer_callback", "data": "timer call" }'. encode('UTF8')))
def test_register_reminder(self): test_actor_id = ActorId('test_id') test_type_info = ActorTypeInformation.create(FakeSimpleReminderActor) test_client = FakeDaprActorClient ctx = ActorRuntimeContext( test_type_info, self._serializer, self._serializer, test_client) test_actor = FakeSimpleReminderActor(ctx, test_actor_id) # register reminder _run(test_actor.register_reminder( 'test_reminder', b'reminder_message', timedelta(seconds=1), timedelta(seconds=1))) test_client.register_reminder.mock.assert_called_once() test_client.register_reminder.mock.assert_called_with( 'FakeSimpleReminderActor', 'test_id', 'test_reminder', b'{"name":"test_reminder","dueTime":"0h0m1s","period":"0h0m1s","data":"cmVtaW5kZXJfbWVzc2FnZQ=="}') # noqa E501 # unregister reminder _run(test_actor.unregister_reminder('test_reminder')) test_client.unregister_reminder.mock.assert_called_once() test_client.unregister_reminder.mock.assert_called_with( 'FakeSimpleReminderActor', 'test_id', 'test_reminder')
def test_set_state_for_existing_state(self): state_manager = ActorStateManager(self._fake_actor) _run(state_manager.set_state('state1', 'value2')) state = state_manager._state_change_tracker['state1'] self.assertEqual(StateChangeKind.update, state.change_kind) self.assertEqual('value2', state.value)
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_fire_reminder_for_non_reminderable(self): test_type_info = ActorTypeInformation.create(FakeSimpleActor) ctx = ActorRuntimeContext( test_type_info, self._serializer, self._serializer, self._fake_client) manager = ActorManager(ctx) with self.assertRaises(ValueError): _run(manager.fire_reminder(ActorId('testid'), 'test_reminder', self._test_reminder_req))
def test_set_state_for_new_state(self): state_manager = ActorStateManager(self._fake_actor) state_change_tracker = state_manager._get_contextual_state_tracker() _run(state_manager.set_state('state1', 'value1')) state = state_change_tracker['state1'] self.assertEqual(StateChangeKind.add, state.change_kind) self.assertEqual('value1', state.value)
def test_fire_reminder_success(self): test_actor_id = ActorId('testid') test_type_info = ActorTypeInformation.create(FakeSimpleReminderActor) ctx = ActorRuntimeContext( test_type_info, self._serializer, self._serializer, self._fake_client) manager = ActorManager(ctx) _run(manager.activate_actor(test_actor_id)) _run(manager.fire_reminder(test_actor_id, 'test_reminder', self._test_reminder_req))
def test_activate_actor(self): """Activate ActorId(1)""" test_actor_id = ActorId('1') _run(self._manager.activate_actor(test_actor_id)) # assert self.assertEqual(test_actor_id, self._manager._active_actors[test_actor_id.id].id) self.assertTrue(self._manager._active_actors[test_actor_id.id].activated) self.assertFalse(self._manager._active_actors[test_actor_id.id].deactivated)
def test_fire_timer_success(self): # Fire timer _run( ActorRuntime.fire_timer( FakeSimpleTimerActor.__name__, 'test-id', 'test_timer', '{ "callback": "timer_callback", "data": "timer call" }'. encode('UTF8'))) manager = ActorRuntime._actor_managers[FakeSimpleTimerActor.__name__] actor = manager._active_actors['test-id'] self.assertTrue(actor.timer_called)
def test_get_or_add_state_for_removed_state(self): state_manager = ActorStateManager(self._fake_actor) _run(state_manager.remove_state('state1')) state = state_manager._state_change_tracker['state1'] self.assertEqual(StateChangeKind.remove, state.change_kind) val = _run(state_manager.get_or_add_state('state1', 'value2')) state = state_manager._state_change_tracker['state1'] self.assertEqual(StateChangeKind.update, state.change_kind) self.assertEqual('value2', val)
def test_add_or_update_state_for_removed_state(self): """add state value if state was removed.""" def test_update_value(name, value): return f'{name}-{value}' state_manager = ActorStateManager(self._fake_actor) _run(state_manager.remove_state('state1')) val = _run( state_manager.add_or_update_state('state1', 'value1', test_update_value)) self.assertEqual('value1', val)
def test_get_state_for_removed_value(self): state_manager = ActorStateManager(self._fake_actor) removed = _run(state_manager.try_remove_state('state1')) self.assertTrue(removed) state = state_manager._state_change_tracker['state1'] self.assertEqual(StateChangeKind.remove, state.change_kind) has_value, val = _run(state_manager.try_get_state('state1')) self.assertFalse(has_value) self.assertIsNone(val)
def test_entities_update(self): # Clean up managers ActorRuntime._actor_managers = {} ActorRuntime.set_actor_config(ActorRuntimeConfig()) config = ActorRuntime.get_actor_config() with self.assertRaises(ValueError): config._entities.index(FakeSimpleActor.__name__) _run(ActorRuntime.register_actor(FakeSimpleActor)) config = ActorRuntime.get_actor_config() self.assertTrue(config._entities.index(FakeSimpleActor.__name__) >= 0)
def test_get_state_for_no_state(self): state_manager = ActorStateManager(self._fake_actor) has_value, val = _run(state_manager.try_get_state('state1')) self.assertFalse(has_value) self.assertIsNone(val) # Test if the test value is empty string self._fake_client.get_state.return_value = '' has_value, val = _run(state_manager.try_get_state('state1')) self.assertFalse(has_value) self.assertIsNone(val)
def test_dispatch_success(self): """dispatch ActionMethod""" test_actor_id = ActorId('dispatch') _run(self._manager.activate_actor(test_actor_id)) request_body = { "message": "hello dapr", } test_request_body = self._serializer.serialize(request_body) response = _run(self._manager.dispatch(test_actor_id, "ActionMethod", test_request_body)) self.assertEqual(b'"hello dapr"', response)
def test_deactivate_actor(self): """Activate ActorId('2') and deactivate it""" test_actor_id = ActorId('2') _run(self._manager.activate_actor(test_actor_id)) # assert self.assertEqual(test_actor_id, self._manager._active_actors[test_actor_id.id].id) self.assertTrue(self._manager._active_actors[test_actor_id.id].activated) self.assertFalse(self._manager._active_actors[test_actor_id.id].deactivated) _run(self._manager.deactivate_actor(test_actor_id)) self.assertIsNone(self._manager._active_actors.get(test_actor_id.id))
def test_add_or_update_state_for_none_state_key(self): """update state value for StateChangeKind.none state """ def test_update_value(name, value): return f'{name}-{value}' state_manager = ActorStateManager(self._fake_actor) has_value, val = _run(state_manager.try_get_state('state1')) self.assertTrue(has_value) self.assertEqual('value1', val) val = _run( state_manager.add_or_update_state('state1', 'value1', test_update_value)) self.assertEqual('state1-value1', val)
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_add_state(self): state_manager = ActorStateManager(self._fake_actor) # Add first 'state1' added = _run(state_manager.try_add_state('state1', 'value1')) self.assertTrue(added) state = state_manager._state_change_tracker['state1'] self.assertEqual('value1', state.value) self.assertEqual(StateChangeKind.add, state.change_kind) # Add 'state1' again added = _run(state_manager.try_add_state('state1', 'value1')) self.assertFalse(added)
def test_remove_state_for_existing_state(self): state_manager = ActorStateManager(self._fake_actor) removed = _run(state_manager.try_remove_state('state1')) self.assertTrue(removed) state = state_manager._state_change_tracker['state1'] self.assertEqual(StateChangeKind.remove, state.change_kind)
def test_reentrant_dispatch(self): _run(ActorRuntime.register_actor(FakeMultiInterfacesActor)) request_body = { "message": "hello dapr", } reentrancy_id = "0faa4c8b-f53a-4dff-9a9d-c50205035085" test_request_body = self._serializer.serialize(request_body) response = _run( ActorRuntime.dispatch(FakeMultiInterfacesActor.__name__, 'test-id', "ReentrantMethod", test_request_body, reentrancy_id=reentrancy_id)) self.assertEqual(b'"hello dapr"', response) _run( ActorRuntime.deactivate(FakeMultiInterfacesActor.__name__, 'test-id')) # Ensure test-id is deactivated with self.assertRaises(ValueError): _run( ActorRuntime.deactivate(FakeMultiInterfacesActor.__name__, 'test-id'))
def test_get_or_add_state_for_non_existing_state(self): state_manager = ActorStateManager(self._fake_actor) val = _run(state_manager.get_or_add_state('state1', 'value2')) state = state_manager._state_change_tracker['state1'] self.assertEqual(StateChangeKind.add, state.change_kind) self.assertEqual('value2', val) self._fake_client.get_state.mock.assert_called_once_with( self._test_type_info._name, self._test_actor_id.id, 'state1')
def test_header_passthrough_reentrancy_disabled(self): config = ActorRuntimeConfig(reentrancy=None) ActorRuntime.set_actor_config(config) _run(ActorRuntime.register_actor(FakeReentrantActor)) _run(ActorRuntime.register_actor(FakeSlowReentrantActor)) request_body = self._serializer.serialize({ "message": "Normal", }) async def expected_return_value(*args, **kwargs): return ["expected", "None"] reentrancy_id = "f6319f23-dc0a-4880-90d9-87b23c19c20a" actor = FakeSlowReentrantActor.__name__ method = 'ReentrantMethod' with mock.patch('dapr.clients.http.client.DaprHttpClient.send_bytes' ) as mocked: mocked.side_effect = expected_return_value _run( ActorRuntime.dispatch(FakeReentrantActor.__name__, 'test-id', 'ReentrantMethodWithPassthrough', request_body, reentrancy_id=reentrancy_id)) mocked.assert_called_with( method="POST", url= f'http://127.0.0.1:3500/v1.0/actors/{actor}/test-id/method/{method}', data=None, headers={}) _run(ActorRuntime.deactivate(FakeReentrantActor.__name__, 'test-id')) # Ensure test-id is deactivated with self.assertRaises(ValueError): _run( ActorRuntime.deactivate(FakeReentrantActor.__name__, 'test-id'))
def test_add_or_update_state_for_new_state(self): """adds state if state does not exist.""" def test_update_value(name, value): return f'{name}-{value}' state_manager = ActorStateManager(self._fake_actor) val = _run( state_manager.add_or_update_state('state1', 'value1', test_update_value)) self.assertEqual('value1', val) state = state_manager._state_change_tracker['state1'] self.assertEqual(StateChangeKind.add, state.change_kind)
def test_register_timer(self): test_actor_id = ActorId('test_id') test_type_info = ActorTypeInformation.create(FakeSimpleTimerActor) test_client = FakeDaprActorClient ctx = ActorRuntimeContext( test_type_info, self._serializer, self._serializer, test_client) test_actor = FakeSimpleTimerActor(ctx, test_actor_id) # register timer _run(test_actor.register_timer( 'test_timer', test_actor.timer_callback, "mydata", timedelta(seconds=1), timedelta(seconds=2))) test_client.register_timer.mock.assert_called_once() test_client.register_timer.mock.assert_called_with( 'FakeSimpleTimerActor', 'test_id', 'test_timer', b'{"callback":"timer_callback","data":"mydata","dueTime":"0h0m1s","period":"0h0m2s"}') # unregister timer _run(test_actor.unregister_timer('test_timer')) test_client.unregister_timer.mock.assert_called_once() test_client.unregister_timer.mock.assert_called_with( 'FakeSimpleTimerActor', 'test_id', 'test_timer') # register timer without timer name _run(test_actor.register_timer( None, test_actor.timer_callback, "timer call", timedelta(seconds=1), timedelta(seconds=1)))
def setUp(self): ActorRuntime._actor_managers = {} ActorRuntime.set_actor_config(ActorRuntimeConfig()) self._serializer = DefaultJSONSerializer() _run(ActorRuntime.register_actor(FakeSimpleActor)) _run(ActorRuntime.register_actor(FakeMultiInterfacesActor)) _run(ActorRuntime.register_actor(FakeSimpleTimerActor))
def test_add_or_update_state_for_state_in_storage(self): """updates state value using update_value_factory if state is in the storage.""" def test_update_value(name, value): return f'{name}-{value}' state_manager = ActorStateManager(self._fake_actor) val = _run( state_manager.add_or_update_state('state1', 'value1', test_update_value)) self.assertEqual('state1-value1', val) state = state_manager._state_change_tracker['state1'] self.assertEqual(StateChangeKind.update, state.change_kind)
def setUp(self): ActorRuntime._actor_managers = {} ActorRuntime.set_actor_config( ActorRuntimeConfig(reentrancy=ActorReentrancyConfig(enabled=True))) self._serializer = DefaultJSONSerializer() _run(ActorRuntime.register_actor(FakeReentrantActor)) _run(ActorRuntime.register_actor(FakeSlowReentrantActor)) _run(ActorRuntime.register_actor(FakeMultiInterfacesActor))
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')