Skip to content

Commit

Permalink
(wip) TestSnapshotStore
Browse files Browse the repository at this point in the history
  • Loading branch information
valdisz committed Aug 13, 2019
1 parent 17ac3b3 commit 7ed6cef
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/core/Akka.Persistence.TestKit/TestSnapshotStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace Akka.Persistence.TestKit
{
using System;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Snapshot;

public interface ISnapshotStoreInterceptor
{
Task Intercept(string persistenceId, SnapshotSelectionCriteria criteria);
}

internal static class SnapshotStoreInterceptors
{
internal class Failure : ISnapshotStoreInterceptor
{
public Task Intercept(string persistenceId, SnapshotSelectionCriteria criteria)
{
throw new TestSnapshotStoreFailureException();
}
}
}

[Serializable]
public class TestSnapshotStoreFailureException : Exception
{
public TestSnapshotStoreFailureException() { }
public TestSnapshotStoreFailureException(string message) : base(message) { }
public TestSnapshotStoreFailureException(string message, Exception inner) : base(message, inner) { }
protected TestSnapshotStoreFailureException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}

public class TestSnapshotStore : MemorySnapshotStore
{
private ISnapshotStoreInterceptor _saveInterceptor;
private ISnapshotStoreInterceptor _loadInterceptor;
private ISnapshotStoreInterceptor _deleteInterceptor;

protected override async Task SaveAsync(SnapshotMetadata metadata, object snapshot)
{
await _saveInterceptor.Intercept(metadata.PersistenceId, ToSelectionCriteria(metadata));
await base.SaveAsync(metadata, snapshot);
}

protected override async Task<SelectedSnapshot> LoadAsync(string persistenceId, SnapshotSelectionCriteria criteria)
{
await _loadInterceptor.Intercept(persistenceId, criteria);
return await base.LoadAsync(persistenceId, criteria);
}

protected override async Task DeleteAsync(SnapshotMetadata metadata)
{
await _deleteInterceptor.Intercept(metadata.PersistenceId, ToSelectionCriteria(metadata));
await base.DeleteAsync(metadata);
}

protected override async Task DeleteAsync(string persistenceId, SnapshotSelectionCriteria criteria)
{
await _deleteInterceptor.Intercept(persistenceId, criteria);
await base.DeleteAsync(persistenceId, criteria);
}

static SnapshotSelectionCriteria ToSelectionCriteria(SnapshotMetadata metadata)
=> new SnapshotSelectionCriteria(metadata.SequenceNr, metadata.Timestamp, metadata.SequenceNr,
metadata.Timestamp);
}
}

0 comments on commit 7ed6cef

Please sign in to comment.