From 129b6b29691d134fb7ab15c10d1aabb317386944 Mon Sep 17 00:00:00 2001 From: stidsborg Date: Wed, 15 Nov 2023 10:16:20 +0100 Subject: [PATCH] Added EventsWithSameIdempotencyKeyAreFilterOut to EventSubscriptionTests --- .../InMemoryTests/EventsSubscriptionTests.cs | 8 ++-- .../TestTemplates/EventsSubscriptionTests.cs | 39 +++++++++++++++++++ .../Utils/Safe.cs | 18 +++++++++ .../Messaging/EventSubscriptionTests.cs | 4 ++ .../Messaging/EventSubscriptionTests.cs | 4 ++ .../Messaging/EventSubscriptionTests.cs | 4 ++ .../Messaging/EventSubscriptionTests.cs | 4 ++ 7 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 Core/Cleipnir.ResilientFunctions.Tests/Utils/Safe.cs diff --git a/Core/Cleipnir.ResilientFunctions.Tests/Messaging/InMemoryTests/EventsSubscriptionTests.cs b/Core/Cleipnir.ResilientFunctions.Tests/Messaging/InMemoryTests/EventsSubscriptionTests.cs index fbeebe7f..31d393cb 100644 --- a/Core/Cleipnir.ResilientFunctions.Tests/Messaging/InMemoryTests/EventsSubscriptionTests.cs +++ b/Core/Cleipnir.ResilientFunctions.Tests/Messaging/InMemoryTests/EventsSubscriptionTests.cs @@ -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()); } \ No newline at end of file diff --git a/Core/Cleipnir.ResilientFunctions.Tests/Messaging/TestTemplates/EventsSubscriptionTests.cs b/Core/Cleipnir.ResilientFunctions.Tests/Messaging/TestTemplates/EventsSubscriptionTests.cs index 1911ff86..d4e81528 100644 --- a/Core/Cleipnir.ResilientFunctions.Tests/Messaging/TestTemplates/EventsSubscriptionTests.cs +++ b/Core/Cleipnir.ResilientFunctions.Tests/Messaging/TestTemplates/EventsSubscriptionTests.cs @@ -75,4 +75,43 @@ await eventStore.AppendEvent( events = await subscription.PullNewEvents(); events.ShouldBeEmpty(); } + + public abstract Task EventsWithSameIdempotencyKeyAreFilterOut(); + protected async Task EventsWithSameIdempotencyKeyAreFilterOut(Task 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"); + } } \ No newline at end of file diff --git a/Core/Cleipnir.ResilientFunctions.Tests/Utils/Safe.cs b/Core/Cleipnir.ResilientFunctions.Tests/Utils/Safe.cs new file mode 100644 index 00000000..40b1b5f1 --- /dev/null +++ b/Core/Cleipnir.ResilientFunctions.Tests/Utils/Safe.cs @@ -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 f, Action? onException = null) + { + try + { + await f(); + } catch (Exception e) + { + onException?.Invoke(e); + } + } +} \ No newline at end of file diff --git a/Stores/AzureBlob/Cleipnir.ResilientFunctions.AzureBlob.Tests/Messaging/EventSubscriptionTests.cs b/Stores/AzureBlob/Cleipnir.ResilientFunctions.AzureBlob.Tests/Messaging/EventSubscriptionTests.cs index 8d310a24..0f44b5cf 100644 --- a/Stores/AzureBlob/Cleipnir.ResilientFunctions.AzureBlob.Tests/Messaging/EventSubscriptionTests.cs +++ b/Stores/AzureBlob/Cleipnir.ResilientFunctions.AzureBlob.Tests/Messaging/EventSubscriptionTests.cs @@ -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()); } \ No newline at end of file diff --git a/Stores/MySQL/Cleipnir.ResilientFunctions.MySQL.Tests/Messaging/EventSubscriptionTests.cs b/Stores/MySQL/Cleipnir.ResilientFunctions.MySQL.Tests/Messaging/EventSubscriptionTests.cs index cbf4a81b..30f63649 100644 --- a/Stores/MySQL/Cleipnir.ResilientFunctions.MySQL.Tests/Messaging/EventSubscriptionTests.cs +++ b/Stores/MySQL/Cleipnir.ResilientFunctions.MySQL.Tests/Messaging/EventSubscriptionTests.cs @@ -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()); } \ No newline at end of file diff --git a/Stores/PostgreSQL/Cleipnir.ResilientFunctions.PostgreSQL.Tests/Messaging/EventSubscriptionTests.cs b/Stores/PostgreSQL/Cleipnir.ResilientFunctions.PostgreSQL.Tests/Messaging/EventSubscriptionTests.cs index 541254c4..c8236d69 100644 --- a/Stores/PostgreSQL/Cleipnir.ResilientFunctions.PostgreSQL.Tests/Messaging/EventSubscriptionTests.cs +++ b/Stores/PostgreSQL/Cleipnir.ResilientFunctions.PostgreSQL.Tests/Messaging/EventSubscriptionTests.cs @@ -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()); } \ No newline at end of file diff --git a/Stores/SqlServer/Cleipnir.ResilientFunctions.SqlServer.Tests/Messaging/EventSubscriptionTests.cs b/Stores/SqlServer/Cleipnir.ResilientFunctions.SqlServer.Tests/Messaging/EventSubscriptionTests.cs index a2c40e97..923dddb8 100644 --- a/Stores/SqlServer/Cleipnir.ResilientFunctions.SqlServer.Tests/Messaging/EventSubscriptionTests.cs +++ b/Stores/SqlServer/Cleipnir.ResilientFunctions.SqlServer.Tests/Messaging/EventSubscriptionTests.cs @@ -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()); } \ No newline at end of file