Skip to content

Instantly share code, notes, and snippets.

@elexisvenator
Created March 25, 2022 03:09
Show Gist options
  • Save elexisvenator/4284b3cb49638fd0f18fc3eb1abfc7cb to your computer and use it in GitHub Desktop.
Save elexisvenator/4284b3cb49638fd0f18fc3eb1abfc7cb to your computer and use it in GitHub Desktop.
Testing IQuerySession in async projections
public class QuerySessionInProjections : OneOffConfigurationsContext
{
private readonly ITestOutputHelper _output;
public QuerySessionInProjections(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public async Task OnAsyncProjection()
{
StoreOptions(_ => { _.Projections.SelfAggregate<SessionProjection>(ProjectionLifecycle.Async); });
var theEvent = new SomeEvent(Guid.NewGuid());
TheSession.Events.StartStream<SessionProjection>(theEvent.Id, theEvent);
await TheSession.SaveChangesAsync();
using var daemon = await TheStore.BuildProjectionDaemonAsync();
await daemon.RebuildProjection<SessionProjection>(CancellationToken.None);
var asyncResult = await TheSession.Events.AggregateStreamAsync<SessionProjection>(theEvent.Id);
_output.WriteLine("Results::");
if (asyncResult is null)
{
_output.WriteLine(" Null. Projection doesn't exist.");
return;
}
_output.WriteLine(" Id: {0}", asyncResult.Id);
_output.WriteLine(" Connection Valid: {0}", asyncResult.ConnectionIsValid);
_output.WriteLine(" Exception details: {0}", asyncResult.ConnectionExceptionDetails);
}
public record SomeEvent(Guid Id);
public record SessionProjection(Guid Id, bool ConnectionIsValid,
string ConnectionExceptionDetails)
{
public static SessionProjection Create(SomeEvent @event, IQuerySession session)
{
var connectionIsValid = false;
var connectionExceptionDetails = string.Empty;
try
{
// Debug here! You will see that the async daemon goes through this method 3 times, with the first 2 times throwing exception inside session.Connection.
_ = session.Connection;
connectionIsValid = true;
}
catch (Exception e)
{
connectionExceptionDetails = e.ToString();
}
return new SessionProjection(@event.Id, connectionIsValid, connectionExceptionDetails);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment