-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 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
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); | ||
} | ||
} |