Skip to content

Commit

Permalink
Fixes try_add_state in actor state manger
Browse files Browse the repository at this point in the history
Signed-off-by: Elena Kolevska <[email protected]>
  • Loading branch information
elena-kolevska committed Dec 3, 2024
1 parent b97d68f commit 6451b01
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dapr/actor/runtime/state_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion tests/actor/test_state_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 6451b01

Please sign in to comment.