Skip to content

Commit

Permalink
ExceptionPolicy delegate is now async (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
corvansteijn authored and dennisdoomen committed Mar 23, 2017
1 parent 75fbbde commit 0058964
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Src/LiquidProjections.NHibernate/NHibernateProjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public sealed class NHibernateProjector<TProjection, TKey, TState>
private readonly NHibernateEventMapConfigurator<TProjection, TKey> mapConfigurator;
private int batchSize = 1;
private string stateKey = typeof(TProjection).Name;
private ShouldRetry shouldRetry = (exception, count) => false;
private ShouldRetry shouldRetry = (exception, count) => Task.FromResult(false);

/// <summary>
/// Creates a new instance of <see cref="NHibernateProjector{TProjection,TKey,TState}"/>.
Expand Down Expand Up @@ -127,7 +127,7 @@ private async Task ExecuteWithRetry(Func<Task> action)
}
catch (ProjectionException exception)
{
if (!ShouldRetry(exception, attempt))
if (!await ShouldRetry(exception, attempt))
{
throw;
}
Expand Down
4 changes: 3 additions & 1 deletion Src/LiquidProjections.NHibernate/ShouldRetry.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Threading.Tasks;

namespace LiquidProjections.NHibernate
{
/// <summary>
Expand All @@ -6,5 +8,5 @@ namespace LiquidProjections.NHibernate
/// <returns>Returns true if the projector should retry to project the batch of transactions, false if it shoud fail with the specified exception.</returns>
/// <param name="exception">The exception that occured that caused this batch to fail.</param>
/// <param name="attempts">The number of attempts that were made to project this batch of transactions.</param>
public delegate bool ShouldRetry(ProjectionException exception, int attempts);
public delegate Task<bool> ShouldRetry(ProjectionException exception, int attempts);
}
4 changes: 2 additions & 2 deletions Src/LiquidProjections.RavenDB/RavenProjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class RavenProjector<TProjection>
private readonly Func<IAsyncDocumentSession> sessionFactory;
private int batchSize;
private readonly RavenEventMapConfigurator<TProjection> mapConfigurator;
private ShouldRetry shouldRetry = (exception, count) => false;
private ShouldRetry shouldRetry = (exception, count) => Task.FromResult(false);

/// <summary>
/// Creates a new instance of <see cref="RavenProjector{TProjection}"/>.
Expand Down Expand Up @@ -137,7 +137,7 @@ private async Task ExecuteWithRetry(Func<Task> action)
}
catch (ProjectionException exception)
{
if (!ShouldRetry(exception, attempt))
if (!await ShouldRetry(exception, attempt))
{
throw;
}
Expand Down
4 changes: 3 additions & 1 deletion Src/LiquidProjections.RavenDB/ShouldRetry.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Threading.Tasks;

namespace LiquidProjections.RavenDB
{
/// <summary>
Expand All @@ -6,5 +8,5 @@ namespace LiquidProjections.RavenDB
/// <returns>Returns true if the projector should retry to project the batch of transactions, false if it shoud fail with the specified exception.</returns>
/// <param name="exception">The exception that occured that caused this batch to fail.</param>
/// <param name="attempts">The number of attempts that were made to project this batch of transactions.</param>
public delegate bool ShouldRetry(ProjectionException exception, int attempts);
public delegate Task<bool> ShouldRetry(ProjectionException exception, int attempts);
}
4 changes: 2 additions & 2 deletions Src/LiquidProjections/Projector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Projector
{
private readonly IEventMap<ProjectionContext> map;
private readonly IReadOnlyList<Projector> children;
private ShouldRetry shouldRetry = (exception, count) => false;
private ShouldRetry shouldRetry = (exception, count) => Task.FromResult(false);

public Projector(IEventMapBuilder<ProjectionContext> eventMapBuilder, IEnumerable<Projector> children = null)
{
Expand Down Expand Up @@ -75,7 +75,7 @@ private async Task ExecuteWithRetry(Func<Task> action)
}
catch (ProjectionException exception)
{
if (!ShouldRetry(exception, attempt))
if (!await ShouldRetry(exception, attempt))
{
throw;
}
Expand Down
4 changes: 3 additions & 1 deletion Src/LiquidProjections/ShouldRetry.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Threading.Tasks;

namespace LiquidProjections
{
/// <summary>
Expand All @@ -6,5 +8,5 @@ namespace LiquidProjections
/// <returns>Returns true if the projector should retry to project the transaction, false if it shoud fail with the specified exception.</returns>
/// <param name="exception">The exception that occured that caused this batch to fail.</param>
/// <param name="attempts">The number of attempts that were made to project this transaction.</param>
public delegate bool ShouldRetry(ProjectionException exception, int attempts);
public delegate Task<bool> ShouldRetry(ProjectionException exception, int attempts);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1198,13 +1198,16 @@ public When_event_handling_fails_with_a_custom_exception_policy()

Subject.ShouldRetry = (exception, attempts) =>
{
numerOfFailedAttempts = attempts;
if (attempts <= NumberOfTimesToFail)
return Task.Run(() =>
{
return true;
}
numerOfFailedAttempts = attempts;
if (attempts <= NumberOfTimesToFail)
{
return true;
}

return false;
return false;
});
};

UseThe(new Transaction
Expand Down
13 changes: 8 additions & 5 deletions Tests/LiquidProjections.RavenDB.Specs/RavenProjectorSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,13 +1621,16 @@ public When_event_handling_fails_with_a_custom_exception_policy()

Subject.ShouldRetry = (exception, attempts) =>
{
numerOfFailedAttempts = attempts;
if (attempts <= NumberOfTimesToFail)
return Task.Run(() =>
{
return true;
}
numerOfFailedAttempts = attempts;
if (attempts <= NumberOfTimesToFail)
{
return true;
}

return false;
return false;
});
};

UseThe(new Transaction
Expand Down
14 changes: 9 additions & 5 deletions Tests/LiquidProjections.Specs/ProjectorSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Chill;
using FluentAssertions;
using Xunit;
Expand Down Expand Up @@ -279,13 +280,16 @@ public When_event_handling_fails_with_a_custom_exception_policy()

Subject.ShouldRetry = (exception, attempts) =>
{
numerOfFailedAttempts = attempts;
if (attempts <= NumberOfTimesToFail)
return Task.Run(() =>
{
return true;
}
numerOfFailedAttempts = attempts;
if (attempts <= NumberOfTimesToFail)
{
return true;
}

return false;
return false;
});
};

UseThe(new Transaction
Expand Down

0 comments on commit 0058964

Please sign in to comment.