-
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: sample how to wait for non-stale projections from IHost
- Loading branch information
1 parent
95e3612
commit 5c69422
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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); | ||
} | ||
} | ||
} |