Skip to content

Commit

Permalink
Adjusted Async projection test with IoC injection to remove flakiness
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Oct 19, 2023
1 parent da307b5 commit d65c772
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
17 changes: 10 additions & 7 deletions docs/events/projections/ioc.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Let's say you have a custom aggregation projection like this one below that need
<!-- snippet: sample_ProductProjection -->
<a id='snippet-sample_productprojection'></a>
```cs
public class ProductProjection : CustomProjection<Product, Guid>
public class ProductProjection: CustomProjection<Product, Guid>
{
private readonly IPriceLookup _lookup;

Expand All @@ -28,10 +28,14 @@ public class ProductProjection : CustomProjection<Product, Guid>
ProjectionName = "Product";
}

public override ValueTask ApplyChangesAsync(DocumentSessionBase session, EventSlice<Product, Guid> slice, CancellationToken cancellation,
ProjectionLifecycle lifecycle = ProjectionLifecycle.Inline)
public override ValueTask ApplyChangesAsync(
DocumentSessionBase session,
EventSlice<Product, Guid> slice,
CancellationToken cancellation,
ProjectionLifecycle lifecycle = ProjectionLifecycle.Inline
)
{
slice.Aggregate ??= new Product{Id = slice.Id};
slice.Aggregate ??= new Product { Id = slice.Id };

foreach (var data in slice.AllData())
{
Expand All @@ -52,7 +56,7 @@ public class ProductProjection : CustomProjection<Product, Guid>
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/EventSourcingTests/Projections/projections_with_IoC_services.cs#L149-L187' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_productprojection' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/EventSourcingTests/Projections/projections_with_IoC_services.cs#L143-L185' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_productprojection' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Now, we *want* to use this projection at runtime within Marten, and need to register the projection
Expand All @@ -74,11 +78,10 @@ using var host = await Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
})
// Note that this is chained after the call to AddMarten()
.AddProjectionWithServices<ProductProjection>(ProjectionLifecycle.Inline, ServiceLifetime.Singleton);

})
.StartAsync();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/EventSourcingTests/Projections/projections_with_IoC_services.cs#L27-L45' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_registering_projection_built_by_services' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/EventSourcingTests/Projections/projections_with_IoC_services.cs#L26-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_registering_projection_built_by_services' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Note that we're having to explicitly specify the projection lifecycle for the projection used within
Expand Down
28 changes: 13 additions & 15 deletions src/EventSourcingTests/Projections/projections_with_IoC_services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

namespace EventSourcingTests.Projections;


[Collection("ioc")]
public class projections_with_IoC_services
{
Expand All @@ -38,22 +37,20 @@ public async Task use_projection_as_singleton_and_inline()
})
// Note that this is chained after the call to AddMarten()
.AddProjectionWithServices<ProductProjection>(ProjectionLifecycle.Inline, ServiceLifetime.Singleton);

})
.StartAsync();

#endregion

var store = host.Services.GetRequiredService<IDocumentStore>();

using var session = store.LightweightSession();
await using var session = store.LightweightSession();
var streamId = session.Events.StartStream<Product>(new ProductRegistered("Ankle Socks", "Socks")).Id;
await session.SaveChangesAsync();

var product = await session.LoadAsync<Product>(streamId);
product.Price.ShouldBeGreaterThan(0);
product.Name.ShouldBe("Ankle Socks");

}

[Fact]
Expand All @@ -68,16 +65,16 @@ public async Task use_projection_as_singleton_and_async()
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "ioc";
opts.Projections.DaemonLockId = 99123;
})
.AddAsyncDaemon(DaemonMode.Solo)
.AddProjectionWithServices<ProductProjection>(ProjectionLifecycle.Async, ServiceLifetime.Singleton);

}).StartAsync();

var store = host.Services.GetRequiredService<IDocumentStore>();
await store.Advanced.Clean.CompletelyRemoveAllAsync();

using var session = store.LightweightSession();
await using var session = store.LightweightSession();
var streamId = session.Events.StartStream<Product>(new ProductRegistered("Ankle Socks", "Socks")).Id;
await session.SaveChangesAsync();

Expand All @@ -87,9 +84,9 @@ public async Task use_projection_as_singleton_and_async()
await daemon.Tracker.WaitForShardState("Product:All", 1);

var product = await session.LoadAsync<Product>(streamId);
product.ShouldNotBeNull();
product.Price.ShouldBeGreaterThan(0);
product.Name.ShouldBe("Ankle Socks");

}

[Fact]
Expand All @@ -105,23 +102,20 @@ public async Task use_projection_as_scoped_and_inline()
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "ioc";
}).AddProjectionWithServices<ProductProjection>(ProjectionLifecycle.Inline, ServiceLifetime.Scoped);

}).StartAsync();

var store = host.Services.GetRequiredService<IDocumentStore>();

using var session = store.LightweightSession();
await using var session = store.LightweightSession();
var streamId = session.Events.StartStream<Product>(new ProductRegistered("Ankle Socks", "Socks")).Id;
await session.SaveChangesAsync();

var product = await session.LoadAsync<Product>(streamId);
product.Price.ShouldBeGreaterThan(0);
product.Name.ShouldBe("Ankle Socks");

}
}


public interface IPriceLookup
{
double PriceFor(string category);
Expand All @@ -148,7 +142,7 @@ public record ProductRegistered(string Name, string Category);

#region sample_ProductProjection

public class ProductProjection : CustomProjection<Product, Guid>
public class ProductProjection: CustomProjection<Product, Guid>
{
private readonly IPriceLookup _lookup;

Expand All @@ -160,10 +154,14 @@ public ProductProjection(IPriceLookup lookup)
ProjectionName = "Product";
}

public override ValueTask ApplyChangesAsync(DocumentSessionBase session, EventSlice<Product, Guid> slice, CancellationToken cancellation,
ProjectionLifecycle lifecycle = ProjectionLifecycle.Inline)
public override ValueTask ApplyChangesAsync(
DocumentSessionBase session,
EventSlice<Product, Guid> slice,
CancellationToken cancellation,
ProjectionLifecycle lifecycle = ProjectionLifecycle.Inline
)
{
slice.Aggregate ??= new Product{Id = slice.Id};
slice.Aggregate ??= new Product { Id = slice.Id };

foreach (var data in slice.AllData())
{
Expand Down

0 comments on commit d65c772

Please sign in to comment.