Created
September 2, 2020 10:10
-
-
Save renestein/3559b112b5b22b5e4c7e201176b56e3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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