def test_comparison(self): actor_id_1 = ActorId('1') actor_id_1a = ActorId('1') self.assertTrue(actor_id_1 == actor_id_1a) actor_id_2 = ActorId('2') self.assertFalse(actor_id_1 == actor_id_2)
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 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')
async def dispatch(cls, actor_type_name: str, actor_id: str, actor_method_name: str, request_body: bytes, reentrancy_id: Optional[str] = None) -> bytes: """Dispatches actor method defined in actor_type. Args: actor_type_name (str): the name of actor type. actor_id (str): Actor ID. actor_method_name (str): the method name that is dispatched. request_body (bytes): the body of request that is passed to actor method arguments. reentrancy_id (str): reentrancy ID obtained from the dapr_reentrancy_id header if present. Returns: bytes: serialized response data. Raises: ValueError: `actor_type_name` actor type is not registered. """ if cls._actor_config._reentrancy and cls._actor_config._reentrancy._enabled: reentrancy_ctx.set(reentrancy_id) manager = await cls._get_actor_manager(actor_type_name) if not manager: raise ValueError(f'{actor_type_name} is not registered.') return await manager.dispatch(ActorId(actor_id), actor_method_name, request_body)
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 setUp(self): # Create mock client self._fake_client = FakeDaprActorClient self._fake_factory = FakeActoryProxyFactory(self._fake_client) self._proxy = ActorProxy.create(FakeMultiInterfacesActor.__name__, ActorId('fake-id'), FakeActorCls2Interface, self._fake_factory)
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_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_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_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 setUp(self): # Create mock client self._fake_client = FakeDaprActorClient self._test_actor_id = ActorId('1') self._test_type_info = ActorTypeInformation.create(FakeSimpleActor) self._serializer = DefaultJSONSerializer() self._runtime_ctx = ActorRuntimeContext(self._test_type_info, self._serializer, self._serializer, self._fake_client) self._fake_actor = FakeSimpleActor(self._runtime_ctx, self._test_actor_id)
async def deactivate(cls, actor_type_name: str, actor_id: str) -> None: """Deactivates an actor for an actor type with given actor id. Args: actor_type_name (str): the name of actor type. actor_id (str): the actor id. Raises: ValueError: `actor_type_name` actor type is not registered. """ manager = await cls._get_actor_manager(actor_type_name) if not manager: raise ValueError(f'{actor_type_name} is not registered.') await manager.deactivate_actor(ActorId(actor_id))
async def fire_timer(cls, actor_type_name: str, actor_id: str, name: str) -> None: """Fires a timer for the Actor. Args: actor_type_name (str): the name of actor type. actor_id (str): Actor ID. name (str): the timer name. Raises: ValueError: `actor_type_name` actor type is not registered. """ manager = await cls._get_actor_manager(actor_type_name) if not manager: raise ValueError(f'{actor_type_name} is not registered.') await manager.fire_timer(ActorId(actor_id), name)
async def fire_reminder(cls, actor_type_name: str, actor_id: str, name: str, state: bytes) -> None: """Fires a reminder for the Actor. Args: actor_type_name (str): the name of actor type. actor_id (str): Actor ID. name (str): the name of reminder. state (bytes): the body of request that is passed to reminder callback. Raises: ValueError: `actor_type_name` actor type is not registered. """ manager = await cls._get_actor_manager(actor_type_name) if not manager: raise ValueError(f'{actor_type_name} is not registered.') await manager.fire_reminder(ActorId(actor_id), name, state)
def test_fire_timer_success(self): test_actor_id = ActorId('testid') test_type_info = ActorTypeInformation.create(FakeSimpleTimerActor) ctx = ActorRuntimeContext(test_type_info, self._serializer, self._serializer, self._fake_client) manager = ActorManager(ctx) _run(manager.activate_actor(test_actor_id)) actor = manager._active_actors.get(test_actor_id.id, None) # Setup timer _run( actor.register_timer('test_timer', actor.timer_callback, "timer call", timedelta(seconds=1), timedelta(seconds=1))) # Fire timer _run(manager.fire_timer(test_actor_id, 'test_timer')) self.assertTrue(actor.timer_called)
async def dispatch(cls, actor_type_name: str, actor_id: str, actor_method_name: str, request_body: bytes) -> bytes: """Dispatches actor method defined in actor_type. Args: actor_type_name (str): the name of actor type. actor_id (str): Actor ID. actor_method_name (str): the method name that is dispatched. request_body (bytes): the body of request that is passed to actor method arguments. Returns: bytes: serialized response data. Raises: ValueError: `actor_type_name` actor type is not registered. """ manager = await cls._get_actor_manager(actor_type_name) if not manager: raise ValueError(f'{actor_type_name} is not registered.') return await manager.dispatch(ActorId(actor_id), actor_method_name, request_body)
def test_get_hash(self): actor_test_id = ActorId('testId') self.assertIsNotNone(actor_test_id.__hash__)
def test_create_actor_id(self): actor_id_1 = ActorId('1') self.assertEqual('1', actor_id_1.id)