Skip to content

Commit

Permalink
docs: sample how to wait for non-stale projections from IHost
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexZeitler authored and jeremydmiller committed Jun 4, 2024
1 parent 95e3612 commit 5c69422
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/events/projections/async-daemon.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,37 @@ The basic idea in your tests is to:
There is also another overload to wait for just one tenant database in the case of using a database per tenant. The default
overload **will wait for the daemon of all known databases to catch up to the latest sequence.**

### Accessing the daemon from IHost:

If you're integration testing with the `IHost` (e.g. using Alba) object, you can access the daemon and wait for non stale data like this:

<!-- snippet: sample_accessing_daemon_from_ihost -->
<a id='snippet-sample_accessing_daemon_from_ihost'></a>
```cs
[Fact]
public async Task run_simultaneously()
{
var host = await StartDaemonInHotColdMode();

StoreOptions(x => x.Projections.Add(new DistanceProjection(), ProjectionLifecycle.Async));

NumberOfStreams = 10;

var agent = await StartDaemon();

// This method publishes a random number of events
await PublishSingleThreaded();

// Wait for all projections to reach the highest event sequence point
// as of the time this method is called
await host.WaitForNonStaleProjectionDataAsync(15.Seconds());

await CheckExpectedResults();
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DaemonTests/event_projections_end_to_end_ihost.cs#L22-L45' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_accessing_daemon_from_ihost' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Diagnostics

The following code shows the diagnostics support for the async daemon as it is today:
Expand Down
88 changes: 88 additions & 0 deletions src/DaemonTests/event_projections_end_to_end_ihost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Linq;
using System.Threading.Tasks;
using DaemonTests.TestingSupport;
using JasperFx.Core;
using Marten;
using Marten.Events;
using Marten.Events.Projections;
using Microsoft.Extensions.Logging;
using Shouldly;
using Xunit;
using Xunit.Abstractions;

namespace DaemonTests;

public class event_projections_end_to_end_ihost : DaemonContext
{
public event_projections_end_to_end_ihost(ITestOutputHelper output) : base(output)
{
_output = output;
}

#region sample_accessing_daemon_from_ihost

[Fact]
public async Task run_simultaneously()
{
var host = await StartDaemonInHotColdMode();

StoreOptions(x => x.Projections.Add(new DistanceProjection(), ProjectionLifecycle.Async));

NumberOfStreams = 10;

var agent = await StartDaemon();

// This method publishes a random number of events
await PublishSingleThreaded();

// Wait for all projections to reach the highest event sequence point
// as of the time this method is called
await host.WaitForNonStaleProjectionDataAsync(15.Seconds());

await CheckExpectedResults();
}

#endregion

private Task CheckExpectedResults()
{
return CheckExpectedResults(theSession);
}

private async Task CheckExpectedResultsForTenants(params string[] tenants)
{
foreach (var tenantId in tenants)
{
await using (var session = theStore.LightweightSession(tenantId))
{
await CheckExpectedResults(session);
}
}
}



private async Task CheckExpectedResults(IDocumentSession session)
{
var distances = await session.Query<Distance>().ToListAsync();

var events = (await session.Events.QueryAllRawEvents().ToListAsync());
var travels = events.OfType<Event<Travel>>().ToDictionary(x => x.Id);

distances.Count.ShouldBe(travels.Count);
foreach (var distance in distances)
{
if (travels.TryGetValue(distance.Id, out var travel))
{
distance.Day.ShouldBe(travel.Data.Day);
distance.Total.ShouldBe(travel.Data.TotalDistance());
}
else
{
travel.ShouldNotBeNull();
}

Logger.LogDebug("Compared distance " + distance);
}
}
}

0 comments on commit 5c69422

Please sign in to comment.