Skip to content

Instantly share code, notes, and snippets.

@renestein
Created September 2, 2020 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renestein/3559b112b5b22b5e4c7e201176b56e3b to your computer and use it in GitHub Desktop.
Save renestein/3559b112b5b22b5e4c7e201176b56e3b to your computer and use it in GitHub Desktop.
TEST(SimpleActorTest, WhenUsingAsyncStatefulActorThenHasExpectedState)
{
const int MESSAGES_COUNT = 101;
const int EXPECTED_STATE = MESSAGES_COUNT;
auto seenMessages = 0;
auto testState = 0;
{
//Create the actor (asynchronous logic/with state/no reply).
auto stateFullActor = RStein::AsyncCpp::Actors::CreateAsyncSimpleActor<int, int>([&seenMessages, &testState](const int& message, const int& state)->Task<int>
{
//Process the message argument.
seenMessages++;
//We can use the co_await keyword in the asynchronous method.
co_await GetCompletedTask().ConfigureAwait(false);
//Update the actor state, the argument state is an old (last) actor state.
auto newState = state + 1;
testState = newState;
//Return the new state.
co_return newState;
}, testState);
for (int i = 0; i < MESSAGES_COUNT; i++)
{
stateFullActor->Tell(i);
}
}// The actor logic completes and the actor is destroyed. Alternatively, you can call the Complete method and wait for the actor Completion task.
ASSERT_EQ(EXPECTED_STATE, testState);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment