From 6451b0114faeebf690bc038827aae19b81c55add Mon Sep 17 00:00:00 2001 From: Elena Kolevska Date: Tue, 3 Dec 2024 00:07:43 +0000 Subject: [PATCH] Fixes try_add_state in actor state manger Signed-off-by: Elena Kolevska --- dapr/actor/runtime/state_manager.py | 2 +- tests/actor/test_state_manager.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/dapr/actor/runtime/state_manager.py b/dapr/actor/runtime/state_manager.py index 7132175b..b6a39c18 100644 --- a/dapr/actor/runtime/state_manager.py +++ b/dapr/actor/runtime/state_manager.py @@ -86,7 +86,7 @@ async def try_add_state(self, state_name: str, value: T) -> bool: existed = await self._actor.runtime_ctx.state_provider.contains_state( self._type_name, self._actor.id.id, state_name ) - if not existed: + if existed: return False state_change_tracker[state_name] = StateMetadata(value, StateChangeKind.add) diff --git a/tests/actor/test_state_manager.py b/tests/actor/test_state_manager.py index 98db0228..c9406dbd 100644 --- a/tests/actor/test_state_manager.py +++ b/tests/actor/test_state_manager.py @@ -46,7 +46,7 @@ def setUp(self): @mock.patch( 'tests.actor.fake_client.FakeDaprActorClient.get_state', - new=_async_mock(return_value=base64.b64encode(b'"value1"')), + new=_async_mock(), ) @mock.patch( 'tests.actor.fake_client.FakeDaprActorClient.save_state_transactionally', new=_async_mock() @@ -67,6 +67,20 @@ def test_add_state(self): added = _run(state_manager.try_add_state('state1', 'value1')) self.assertFalse(added) + @mock.patch( + 'tests.actor.fake_client.FakeDaprActorClient.get_state', + new=_async_mock(return_value=base64.b64encode(b'"value1"')), + ) + @mock.patch( + 'tests.actor.fake_client.FakeDaprActorClient.save_state_transactionally', new=_async_mock() + ) + def test_add_state_with_existing_state(self): + state_manager = ActorStateManager(self._fake_actor) + + # Add first 'state1' + added = _run(state_manager.try_add_state('state1', 'value1')) + self.assertFalse(added) + @mock.patch('tests.actor.fake_client.FakeDaprActorClient.get_state', new=_async_mock()) def test_get_state_for_no_state(self): state_manager = ActorStateManager(self._fake_actor)