Skip to content

Commit 6b5e0c9

Browse files
author
Tomas Lycken
authored
Exit early when appending empty event list (#42)
1 parent f4a04a6 commit 6b5e0c9

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/RdbmsEventStore.EntityFramework.Tests/EventStoreTests/WriteEventTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55
using RdbmsEventStore.EntityFramework.Tests.Infrastructure;
66
using RdbmsEventStore.EntityFramework.Tests.TestData;
7+
using Moq;
78
using Xunit;
89

910

@@ -34,6 +35,25 @@ public async Task CommittingWithOutOfSyncDataThrowsConflictException()
3435
await Assert.ThrowsAsync<ConflictException>(() => store.Append(stream, 0, new[] { new FooEvent { Foo = "Qux" } }));
3536
}
3637

38+
[Fact]
39+
public async Task CommittingNoEventsExitsEarly() {
40+
var context = new Mock<EventStoreContext<GuidGuidPersistedEvent>>(MockBehavior.Strict);
41+
var set = new Mock<DbSet<GuidGuidPersistedEvent>>(MockBehavior.Strict);
42+
context.Setup(c => c.Set<GuidGuidPersistedEvent>()).Returns(set.Object);
43+
var stream = Guid.NewGuid();
44+
45+
var store = _fixture.BuildEventStore(context.Object);
46+
47+
try {
48+
await store.Append(stream, 0, new object[] { });
49+
} catch (NotImplementedException) {
50+
// Thrown by the mock DbSet if we try to query for existing events
51+
// This indicates that we didn't exit early
52+
53+
Assert.False(true, "Expected to exit early, but apparently didn't.");
54+
}
55+
}
56+
3757
[Fact]
3858
public async Task CommittingMultipleEventsStoresAllEventsInContext()
3959
{
@@ -77,4 +97,4 @@ public async Task CommittingMultipleEventsIncrementsVersionForEachEvent()
7797
bar => Assert.Equal(2, bar.Version));
7898
}
7999
}
80-
}
100+
}

src/RdbmsEventStore.EntityFramework.Tests/RdbmsEventStore.EntityFramework.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<PackageReference Include="Effort.EF6" Version="1.3.0" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
1313
<PackageReference Include="tlycken.Extensions" Version="0.5.0" />
14+
<PackageReference Include="Moq" Version="4.7.145" />
1415
<PackageReference Include="xunit" Version="2.3.0" />
1516
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0" />
1617
</ItemGroup>

src/RdbmsEventStore.EntityFramework/EntityFrameworkEventStore.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public async Task<IEnumerable<TEvent>> Events(Func<IQueryable<TEventMetadata>, I
4545

4646
public async Task Append(TStreamId streamId, long versionBefore, IEnumerable<object> payloads)
4747
{
48+
if (!payloads.Any()) {
49+
return;
50+
}
51+
4852
using (await _writeLock.Aquire(streamId))
4953
{
5054
var highestVersionNumber = await _context.Events

0 commit comments

Comments
 (0)