Skip to content

Commit

Permalink
Added EventsWithSameIdempotencyKeyAreFilterOut to EventSubscriptionTests
Browse files Browse the repository at this point in the history
  • Loading branch information
stidsborg committed Nov 15, 2023
1 parent e85c000 commit 129b6b2
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System.Threading.Tasks;
using Cleipnir.ResilientFunctions.Helpers;
using Cleipnir.ResilientFunctions.Storage;
using Cleipnir.ResilientFunctions.Tests.InMemoryTests;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cleipnir.ResilientFunctions.Tests.Messaging.InMemoryTests;

[TestClass]
public class EventsSubscriptionTests : Cleipnir.ResilientFunctions.Tests.Messaging.TestTemplates.EventSubscriptionTests
public class EventsSubscriptionTests : TestTemplates.EventSubscriptionTests
{
[TestMethod]
public override Task EventsSubscriptionSunshineScenario()
=> EventsSubscriptionSunshineScenario(FunctionStoreFactory.Create());

[TestMethod]
public override Task EventsWithSameIdempotencyKeyAreFilterOut()
=> EventsWithSameIdempotencyKeyAreFilterOut(FunctionStoreFactory.Create());
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,43 @@ await eventStore.AppendEvent(
events = await subscription.PullNewEvents();
events.ShouldBeEmpty();
}

public abstract Task EventsWithSameIdempotencyKeyAreFilterOut();
protected async Task EventsWithSameIdempotencyKeyAreFilterOut(Task<IFunctionStore> functionStoreTask)
{
var functionId = TestFunctionId.Create();
var functionStore = await functionStoreTask;
await functionStore.CreateFunction(
functionId,
Test.SimpleStoredParameter,
Test.SimpleStoredScrapbook,
storedEvents: null,
leaseExpiration: DateTime.UtcNow.Ticks,
postponeUntil: null,
timestamp: DateTime.UtcNow.Ticks
);

var storedEvent1 = new StoredEvent(
EventJson: "hello".ToJson(),
EventType: typeof(string).SimpleQualifiedName(),
IdempotencyKey: "someIdempotencyKey"
);
var storedEvent2 = new StoredEvent(
EventJson: "world".ToJson(),
EventType: typeof(string).SimpleQualifiedName(),
IdempotencyKey: "someIdempotencyKey"
);
await functionStore.EventStore.AppendEvent(functionId, storedEvent1);

await Safe.Try(
() => functionStore.EventStore.AppendEvent(functionId, storedEvent2)
);

await using var subscription = functionStore.EventStore.SubscribeToEvents(functionId);

var newEvents = await subscription.PullNewEvents();
newEvents.Count.ShouldBe(1);
newEvents[0].IdempotencyKey.ShouldBe("someIdempotencyKey");
newEvents[0].DefaultDeserialize().ShouldBe("hello");
}
}
18 changes: 18 additions & 0 deletions Core/Cleipnir.ResilientFunctions.Tests/Utils/Safe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Threading.Tasks;

namespace Cleipnir.ResilientFunctions.Tests.Utils;

public static class Safe
{
public static async Task Try(Func<Task> f, Action<Exception>? onException = null)
{
try
{
await f();
} catch (Exception e)
{
onException?.Invoke(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ public class EventsSubscriptionTests : Cleipnir.ResilientFunctions.Tests.Messagi
[TestMethod]
public override Task EventsSubscriptionSunshineScenario()
=> EventsSubscriptionSunshineScenario(FunctionStoreFactory.Create());

[TestMethod]
public override Task EventsWithSameIdempotencyKeyAreFilterOut()
=> EventsWithSameIdempotencyKeyAreFilterOut(FunctionStoreFactory.Create());
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class EventsSubscriptionTests : Cleipnir.ResilientFunctions.Tests.Messagi
[TestMethod]
public override Task EventsSubscriptionSunshineScenario()
=> EventsSubscriptionSunshineScenario(FunctionStoreFactory.Create());

[TestMethod]
public override Task EventsWithSameIdempotencyKeyAreFilterOut()
=> EventsWithSameIdempotencyKeyAreFilterOut(FunctionStoreFactory.Create());
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ public class EventsSubscriptionTests : Cleipnir.ResilientFunctions.Tests.Messagi
[TestMethod]
public override Task EventsSubscriptionSunshineScenario()
=> EventsSubscriptionSunshineScenario(FunctionStoreFactory.Create());

[TestMethod]
public override Task EventsWithSameIdempotencyKeyAreFilterOut()
=> EventsWithSameIdempotencyKeyAreFilterOut(FunctionStoreFactory.Create());
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ public class EventsSubscriptionTests : Cleipnir.ResilientFunctions.Tests.Messagi
[TestMethod]
public override Task EventsSubscriptionSunshineScenario()
=> EventsSubscriptionSunshineScenario(FunctionStoreFactory.Create());

[TestMethod]
public override Task EventsWithSameIdempotencyKeyAreFilterOut()
=> EventsWithSameIdempotencyKeyAreFilterOut(FunctionStoreFactory.Create());
}

0 comments on commit 129b6b2

Please sign in to comment.