Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add Akka.Hosting.TestKit module #102

Merged
merged 15 commits into from
Oct 4, 2022

Conversation

Arkatufus
Copy link
Contributor

@Arkatufus Arkatufus commented Sep 6, 2022

A proof of concept implementation of Akka.Hosting test kit.
Comments and reviews are welcome.

Configuring the spec

The biggest difference is that, since the test is started asynchronously, the TestKit properties and methods would not be available in the unit test class constructor anymore. Since the spec depends on Microsoft HostBuilder, configuration has to be broken down into steps. There are overridable methods that user can use to override virtually all of the setup process.

The steps of what overridable methods gets called is:

  1. ConfigureLogging(ILoggingBuilder) -- Add custom logger and filtering rules here.
  2. ConfigureHostConfiguration(IConfigurationBuilder) -- Inject any additional hosting environment configuration here, such as faking environment variables.
  3. ConfigureAppConfiguration(HostBuilderContext, IConfigurationBuilder) -- Inject the application configuration here.
  4. ConfigureServices(HostBuilderContext, IServiceCollection) -- Add additional services needed by the test here, such as mocked up services used inside the dependency injection.
  5. User defined HOCON configuration is injected by overriding the Config property, it is not passed as part of the constructor anymore.
  6. ConfigureAkka(AkkaConfigurationBuilder, IServiceProvider) -- This is called inside AddAkka, use this to configure the ActorSystem
  7. There is a new BeforeTestStart() method. This method is called after the TestKit is initialized. Move all of the codes that used to belong in the constructor here.

Akka.Hosting.TestKit extends Akka.TestKit.Xunit2 directly, all testing methods are available out of the box.
All of the properties, such as Sys and TestActor will be available once the unit test class is invoked.

NOTES

Would not compile until akkadotnet/akka.net#6093 is merged and released

@Arkatufus Arkatufus marked this pull request as draft September 6, 2022 19:45
@Arkatufus
Copy link
Contributor Author

Arkatufus commented Sep 9, 2022

Would not compile until akkadotnet/akka.net#6093 is merged and released

@Arkatufus Arkatufus changed the title Akka.Hosting.TestKit Proof of Concept Add Akka.Hosting.TestKit module Sep 23, 2022
@Arkatufus Arkatufus marked this pull request as ready for review September 23, 2022 17:17
@Arkatufus
Copy link
Contributor Author

Tests are passing

Copy link
Member

@Aaronontheweb Aaronontheweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By way of example, let's refactor at least 1 of the existing test suites in Akka.Hosting to use the TestKit. If there are issues with friction / the ease of use of the API that would be a valuable way to discover it before we push this out to end users.

src/Akka.Hosting.TestKit/TestKit.cs Outdated Show resolved Hide resolved
@Aaronontheweb
Copy link
Member

I think https://github.com/akkadotnet/Akka.Hosting/tree/dev/src/Akka.Persistence.Hosting.Tests is a good candidate for testing out the new Akka.Hosting TestKit - not too big, reasonably complex use cases.

@Arkatufus
Copy link
Contributor Author

One snag that I've seen so far: This TestKit can only be used if a spec uses the same hosting configuration for all its unit test. If the unit tests were testing different hosting configurations, then the only way to use the test kit is to split each unit tests into classes.

@Aaronontheweb Aaronontheweb merged commit cf87206 into akkadotnet:dev Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants