Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feature: Added FSM Test Kit #1

Merged
merged 2 commits into from
Oct 4, 2024

Conversation

Mitrug
Copy link
Collaborator

@Mitrug Mitrug commented Oct 3, 2024

Description

The goal of this PR is to provide a way to set and retrieve the current FSM state during testing. In the current implementation, the FSM's state can be retrieved using the FSMConfig, but there is no way to explicitly transition the FSM into a particular state without exposing the FSMBuilder in the main source code.

To address this, the FSM Actor can no longer remain an anonymous class. As a result, the FSM class is now an abstract class that extends Actor.

The newly introduced FSMTestKit serves as a wrapper around the FSM, exposing only the necessary functionality, specifically: currentState and setState. Additionally, a new syntax, fsmTestKit, has been added to convert a ReplyingActor into an FSMTestKit, simplifying state manipulation for testing purposes.

Ideally, this TestKit should be moved into its own test library, perhaps something like cats_actors_testkit, rather than residing in the main cats_actors library. To achieve this, the project would need to be split into two submodules. However, given that we currently only have this class and its associated syntax, it may not be worth the effort at this point in time.

Example

(for {
  actorSystem <- ActorSystem[IO]("FSM Actor", (_: Any) => IO.unit).allocated.map(_._1)

  actor <- FSMTestSyntaxSuite.actor(startWith = FsmIdle)
  _ <- actorSystem.actorOf(actor)

  fsmTestKit <- actor.fsmTestKit[FsmState, Int]
  _ <- fsmTestKit.setState(FsmRunning, 4)
  state <- fsmTestKit.currentState
} yield state).unsafeToFuture().map { case (stateName, stateData) =>
  stateName should be(FsmRunning)
  stateData should be(4)
}

Massimo Saliba added 2 commits September 18, 2024 15:27
… be able to convert the FSM into a sealed class.
@PetrosPapapa PetrosPapapa merged commit ef0a73c into PetrosPapapa:v2.0.0-RC5 Oct 4, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants