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 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 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))
Exemple #4
0
    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_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')))
Exemple #6
0
    def test_dispatch(self):
        _run(ActorRuntime.register_actor(FakeMultiInterfacesActor))

        request_body = {
            "message": "hello dapr",
        }

        test_request_body = self._serializer.serialize(request_body)
        response = _run(ActorRuntime.dispatch(
            FakeMultiInterfacesActor.__name__, 'test-id',
            "ActionMethod", test_request_body))

        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_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)
Exemple #8
0
    def test_actor_config(self):
        config = ActorRuntime.get_actor_config()

        self.assertTrue(config._drain_rebalanced_actors)
        self.assertEqual(timedelta(hours=1), config._actor_idle_timeout)
        self.assertEqual(timedelta(seconds=30), config._actor_scan_interval)
        self.assertEqual(timedelta(minutes=1), config._drain_ongoing_call_timeout)
        self.assertEqual(2, len(config._entities))

        # apply new config
        new_config = ActorRuntimeConfig(
            timedelta(hours=3), timedelta(seconds=10), timedelta(minutes=1), False)

        ActorRuntime.set_actor_config(new_config)
        config = ActorRuntime.get_actor_config()

        self.assertFalse(config._drain_rebalanced_actors)
        self.assertEqual(timedelta(hours=3), config._actor_idle_timeout)
        self.assertEqual(timedelta(seconds=10), config._actor_scan_interval)
        self.assertEqual(timedelta(minutes=1), config._drain_ongoing_call_timeout)
        self.assertEqual(2, len(config._entities))
    def test_reentrancy_header_passthrough(self):
        _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={'Dapr-Reentrancy-Id': reentrancy_id})

        _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_interleaved_reentrant_actor_dispatch(self):
        _run(ActorRuntime.register_actor(FakeReentrantActor))
        _run(ActorRuntime.register_actor(FakeSlowReentrantActor))

        request_body = self._serializer.serialize({
            "message": "Normal",
        })

        normal_reentrancy_id = "f6319f23-dc0a-4880-90d9-87b23c19c20a"
        slow_reentrancy_id = "b1653a2f-fe54-4514-8197-98b52d156454"

        async def dispatchReentrantCall(actorName: str, method: str,
                                        reentrancy_id: str):
            return await ActorRuntime.dispatch(actorName,
                                               'test-id',
                                               method,
                                               request_body,
                                               reentrancy_id=reentrancy_id)

        async def run_parallel_actors():
            slow = dispatchReentrantCall(FakeSlowReentrantActor.__name__,
                                         "ReentrantMethod", slow_reentrancy_id)
            normal = dispatchReentrantCall(FakeReentrantActor.__name__,
                                           "ReentrantMethod",
                                           normal_reentrancy_id)

            res = await asyncio.gather(slow, normal)
            self.slow_res = res[0]
            self.normal_res = res[1]

        _run(run_parallel_actors())

        self.assertEqual(self.normal_res,
                         bytes('"' + normal_reentrancy_id + '"', 'utf-8'))
        self.assertEqual(self.slow_res,
                         bytes('"' + slow_reentrancy_id + '"', 'utf-8'))

        _run(
            ActorRuntime.deactivate(FakeSlowReentrantActor.__name__,
                                    'test-id'))
        _run(ActorRuntime.deactivate(FakeReentrantActor.__name__, 'test-id'))

        # Ensure test-id is deactivated
        with self.assertRaises(ValueError):
            _run(
                ActorRuntime.deactivate(FakeSlowReentrantActor.__name__,
                                        'test-id'))
            _run(
                ActorRuntime.deactivate(FakeReentrantActor.__name__,
                                        'test-id'))
Exemple #11
0
 def test_get_registered_actor_types(self):
     actor_types = ActorRuntime.get_registered_actor_types()
     self.assertTrue(actor_types.index('FakeSimpleActor') >= 0)
     self.assertTrue(actor_types.index(FakeMultiInterfacesActor.__name__) >= 0)