Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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_update_entities(self):
config = ActorRuntimeConfig()
config.update_entities(['actortype1'])
self.assertEqual(config._actor_idle_timeout, timedelta(seconds=3600))
self.assertEqual(config._actor_scan_interval, timedelta(seconds=30))
self.assertEqual(config._drain_ongoing_call_timeout, timedelta(seconds=60))
self.assertEqual(config._drain_rebalanced_actors, True)
self.assertEqual(config._entities, ['actortype1'])
def test_default_config(self):
config = ActorRuntimeConfig()
self.assertEqual(config._actor_idle_timeout, timedelta(seconds=3600))
self.assertEqual(config._actor_scan_interval, timedelta(seconds=30))
self.assertEqual(config._drain_ongoing_call_timeout, timedelta(seconds=60))
self.assertEqual(config._drain_rebalanced_actors, True)
self.assertEqual(config._entities, [])
def test_update_entities_two_types(self):
config = ActorRuntimeConfig()
config.update_entities(['actortype1', 'actortype1'])
self.assertEqual(config._actor_idle_timeout, timedelta(seconds=3600))
self.assertEqual(config._actor_scan_interval, timedelta(seconds=30))
self.assertEqual(config._drain_ongoing_call_timeout, timedelta(seconds=60))
self.assertEqual(config._drain_rebalanced_actors, True)
self.assertEqual(config._entities, ['actortype1', 'actortype1'])
from dapr.actor.id import ActorId
from dapr.actor.runtime.actor import Actor
from dapr.actor.runtime.config import ActorRuntimeConfig
from dapr.actor.runtime.context import ActorRuntimeContext
from dapr.actor.runtime.type_information import ActorTypeInformation
from dapr.actor.runtime.manager import ActorManager
from dapr.clients import DaprActorHttpClient
from dapr.serializers import Serializer, DefaultJSONSerializer
class ActorRuntime:
"""The class that creates instances of :class:`Actor` and
activates and deactivates :class:`Actor`.
"""
_actor_config = ActorRuntimeConfig()
_actor_managers: Dict[str, ActorManager] = {}
_actor_managers_lock = asyncio.Lock()
@classmethod
async def register_actor(
cls, actor: Type[Actor],
message_serializer: Serializer = DefaultJSONSerializer(),
state_serializer: Serializer = DefaultJSONSerializer()) -> 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.
def serialize(
self, obj: object,
custom_hook: Optional[Callable[[object], bytes]] = None) -> bytes:
dict_obj = obj
if callable(custom_hook):
dict_obj = custom_hook(obj)
elif isinstance(obj, bytes):
dict_obj = base64.b64encode(obj).decode('utf-8')
elif isinstance(obj, ActorRuntimeConfig):
dict_obj = obj.as_dict()
serialized = json.dumps(dict_obj, cls=DaprJSONEncoder, separators=(',', ':'))
return serialized.encode('utf-8')