Skip to content

Commit 6a5c7c0

Browse files
Update persistence-testing.md docs (#7516)
* Update persistence-testing.md docs Fixed a few typo's Added more explicit code example with regards to working with custom interceptors. Added link to the akka.hosting integration testing page from the bootcamp. Regarding more advanced integration testing. * satisfy spellchecker * Satisfy linter * Update persistence-testing.md --------- Co-authored-by: Aaron Stannard <[email protected]>
1 parent 3ca3682 commit 6a5c7c0

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

docs/articles/persistence/persistence-testing.md

+42-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public class CounterActorTests : PersistenceTestKit
133133
}
134134
```
135135

136-
When we will launch this test it will fail, because the persistence journal failed when we tried to tell `inc` command to the actor. The actor failed with the journal and `read` was never delivered anb we had not received any answer.
136+
When we will launch this test it will fail, because the persistence journal failed when we tried to tell `inc` command to the actor. The actor failed with the journal and `read` was never delivered and we had not received any answer.
137137

138138
### How to Make Things Better
139139

@@ -205,7 +205,19 @@ Out of the box, the package has the following behaviors:
205205

206206
All methods have additional overload to add artificial delay - `*WithDelay`, i.e. `FailWithDelay`. This could be helpful to simulate network delay or retry of physical persistence operation within the journal.
207207

208-
When all mentioned above behaviors are not enough, it is always possible to implement custom one by implementing the `IJournalInterceptor` interface. An instance of a custom interceptor can be set using the `SetInterceptorAsync` method.
208+
When all mentioned above behaviors are not enough, it is always possible to implement a custom one by implementing the `IJournalInterceptor` interface. An instance of a custom interceptor can be set using the `SetInterceptorAsync` method.
209+
210+
``` csharp
211+
[Fact]
212+
public async Task Custom_interceptor_example()
213+
{
214+
WithJournalWrite(write => write.SetInterceptorAsync(new myCustomInterceptor()), () =>
215+
{
216+
//test code here
217+
218+
});
219+
}
220+
```
209221

210222
### Built-in Snapshot Store Behaviors
211223

@@ -243,3 +255,31 @@ public interface ISnapshotStoreInterceptor
243255
Task InterceptAsync(string persistenceId, SnapshotSelectionCriteria criteria);
244256
}
245257
```
258+
259+
### More Examples and Common Testing Scenario's
260+
261+
Sometimes you might want to verify more complex scenario's for your persistent actor. For example, your actor might persist events only under certain conditions and you want to test that.
262+
An easy way to do that is by implementing a custom interceptor, either a Journal or a Snapshot interceptor depending on your needs.
263+
You could have that interceptor then collect the events or snapshots being persisted and assert on the data in that interceptor.
264+
Depending on your needs you can make this as complicated or simple as you want.
265+
266+
``` csharp
267+
[Fact]
268+
public async Task Custom_interceptor_example_direct_usage()
269+
{
270+
var interceptor = new MyCollectingInterceptor();
271+
//Journal also has OnRecovery and OnConnect options.
272+
Journal.OnWrite.SetInterceptorAsync(interceptor);
273+
274+
//perform test code here
275+
276+
//assert at the end
277+
Assert.IsTrue(interceptor.HasEventsThatIExpect());
278+
}
279+
```
280+
281+
Should you run into race conditions, between executing your test code and performing the assertions. Wrapping your assertion code in a `AwaitAssert` call would be a good way to manage that.
282+
283+
### Integration Testing
284+
285+
Lets say you need more then just the InMemory persistence model. There is a bootcamp and corresponding video on those subjects that are well worth checking out. [Integration testing with Akka.Hosting](https://petabridge.com/bootcamp/lessons/unit-1/akka-hosting-testkit/)

0 commit comments

Comments
 (0)