예제 #1
0
    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)))
예제 #2
0
    async def register_actor(
        cls,
        actor: Type[Actor],
        message_serializer: Serializer = DefaultJSONSerializer(),
        state_serializer: Serializer = DefaultJSONSerializer(),
        http_timeout_seconds: int = settings.DAPR_HTTP_TIMEOUT_SECONDS
    ) -> None:
        """Registers an :class:`Actor` object with the runtime.

        Args:
            actor (:class:`Actor`): Actor implementation.
            message_serializer (:class:`Serializer`): A serializer that serializes message
                between actors.
            state_serializer (:class:`Serializer`): Serializer that serializes state values.
            http_timeout_seconds (:int:): a configurable timeout value
        """
        type_info = ActorTypeInformation.create(actor)
        # TODO: We will allow to use gRPC client later.
        actor_client = DaprActorHttpClient(message_serializer,
                                           timeout=http_timeout_seconds)
        ctx = ActorRuntimeContext(type_info, message_serializer,
                                  state_serializer, actor_client)

        # Create an ActorManager, override existing entry if registered again.
        async with cls._actor_managers_lock:
            cls._actor_managers[type_info.type_name] = ActorManager(ctx)
            cls._actor_config.update_entities(
                ActorRuntime.get_registered_actor_types())
예제 #3
0
    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')
예제 #4
0
 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))
예제 #5
0
 def setUp(self):
     self._testActorTypeInfo = ActorTypeInformation.create(FakeSimpleActor)
     self._serializer = DefaultJSONSerializer()
     self._fake_client = FakeDaprActorClient
     self._fake_runtime_ctx = ActorRuntimeContext(self._testActorTypeInfo,
                                                  self._serializer,
                                                  self._serializer,
                                                  self._fake_client)
예제 #6
0
    def setUp(self):
        self._test_type_info = ActorTypeInformation.create(FakeMultiInterfacesActor)
        self._serializer = DefaultJSONSerializer()

        self._fake_client = FakeDaprActorClient
        self._runtime_ctx = ActorRuntimeContext(
            self._test_type_info, self._serializer,
            self._serializer, self._fake_client)
        self._manager = ActorManager(self._runtime_ctx)
예제 #7
0
 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))
예제 #8
0
    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)
예제 #9
0
    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)