Skip to content
Browse files

More tests for LockingInMemoryCache

  • Loading branch information...
1 parent 38dee7e commit 6b4d6a0e612010fd21e46aad0b595e2db01a9242 @abdullin abdullin committed Mar 13, 2013
View
3 Cqrs.Portable.Tests/Cqrs.Portable.Tests.csproj
@@ -79,9 +79,12 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Partition\StatelessFileQueueReaderTest.cs" />
<Compile Include="StorageFramesEvilTest.cs" />
+ <Compile Include="TapeStorage\LockingInMemoryCacheTests\LockingInMemoryHelpers.cs" />
<Compile Include="TapeStorage\LockingInMemoryCacheTests\when_checking_store_version.cs" />
<Compile Include="TapeStorage\LockingInMemoryCacheTests\when_clearing_cache.cs" />
<Compile Include="TapeStorage\LockingInMemoryCacheTests\when_doing_concurrent_append.cs" />
+ <Compile Include="TapeStorage\LockingInMemoryCacheTests\when_enumerating_entire_store.cs" />
+ <Compile Include="TapeStorage\LockingInMemoryCacheTests\when_reading_stream.cs" />
<Compile Include="TapeStorage\LockingInMemoryCacheTests\when_reloading_all.cs" />
<Compile Include="TapeStorage\MemoryAppendOnlyStoreTest.cs" />
</ItemGroup>
View
60 Cqrs.Portable.Tests/TapeStorage/LockingInMemoryCacheTests/LockingInMemoryHelpers.cs
@@ -0,0 +1,60 @@
+using System.Collections.Generic;
+using System.Text;
+using Lokad.Cqrs;
+using Lokad.Cqrs.TapeStorage;
+using System.Linq;
+using NUnit.Framework;
+
+namespace Cqrs.Portable.Tests.TapeStorage.LockingInMemoryCacheTests
+{
+ public abstract class LockingInMemoryHelpers
+ {
+ protected IEnumerable<StorageFrameDecoded> CreateFrames(params string[] streamNames)
+ {
+
+ for (int i = 0; i < streamNames.Length; i++)
+ {
+ var streamName = streamNames[i];
+ var storeVersion = i + 1;
+ var bytes = Encoding.UTF8.GetBytes("event-" + storeVersion);
+ yield return new StorageFrameDecoded(bytes, streamName, ("event-" + storeVersion).GetHashCode());
+ }
+ }
+
+ public static class DataAssert
+ {
+ public static void AreEqual(IEnumerable<DataWithKey> expected, IEnumerable<DataWithKey> actual)
+ {
+ var ea = expected.ToArray();
+ var aa = actual.ToArray();
+
+
+ Assert.AreEqual(ea.Length, aa.Length, "Array length");
+
+ for (int i = 0; i < ea.Length; i++)
+ {
+ var expectedItem = ea[i];
+ var actualItem = aa[i];
+
+ Assert.AreEqual(expectedItem.Key, actualItem.Key, "Item[{0}].Key", i);
+ Assert.AreEqual(expectedItem.StoreVersion, actualItem.StoreVersion, "Item[{0}].StoreVersion", i);
+ Assert.AreEqual(expectedItem.StreamVersion, actualItem.StreamVersion, "Item[{0}].StreamVersion", i);
+ CollectionAssert.AreEqual(expectedItem.Data, actualItem.Data, "Item[{0}].Data", i);
+
+ }
+
+ }
+ }
+
+ protected DataWithKey CreateKey(int storeVersion, int streamVersion, string streamName)
+ {
+ var bytes = GetEventBytes(storeVersion);
+ return new DataWithKey(streamName,bytes, streamVersion, storeVersion);
+ }
+
+ protected byte[] GetEventBytes(int storeVersion)
+ {
+ return Encoding.UTF8.GetBytes("event-" + storeVersion);
+ }
+ }
+}
View
2 Cqrs.Portable.Tests/TapeStorage/LockingInMemoryCacheTests/when_clearing_cache.cs
@@ -1,3 +1,5 @@
+using System.Text;
+using Lokad.Cqrs;
using NUnit.Framework;
namespace Cqrs.Portable.Tests.TapeStorage.LockingInMemoryCacheTests
View
14 Cqrs.Portable.Tests/TapeStorage/LockingInMemoryCacheTests/when_doing_concurrent_append.cs
@@ -1,14 +1,11 @@
-using System.Collections.Generic;
using System.IO;
-using System.Text;
-using Lokad.Cqrs;
using Lokad.Cqrs.TapeStorage;
using NUnit.Framework;
namespace Cqrs.Portable.Tests.TapeStorage.LockingInMemoryCacheTests
{
[TestFixture]
- public sealed class when_doing_concurrent_append
+ public sealed class when_doing_concurrent_append : LockingInMemoryHelpers
{
[Test]
public void given_empty_cache_and_valid_commit_function()
@@ -44,14 +41,7 @@ public void given_reloaded_and_appended_cache_with_specified_version_expectation
}
- IEnumerable<StorageFrameDecoded> CreateFrames(params string[] streamNames)
- {
- foreach (var streamName in streamNames)
- {
- yield return new StorageFrameDecoded(Encoding.UTF8.GetBytes(streamName), streamName, -1);
- }
- }
-
+
[Test]
View
72 Cqrs.Portable.Tests/TapeStorage/LockingInMemoryCacheTests/when_enumerating_entire_store.cs
@@ -0,0 +1,72 @@
+using System.Linq;
+using Lokad.Cqrs.TapeStorage;
+using NUnit.Framework;
+
+namespace Cqrs.Portable.Tests.TapeStorage.LockingInMemoryCacheTests
+{
+ [TestFixture]
+ public sealed class when_enumerating_entire_store : LockingInMemoryHelpers
+ {
+ [Test]
+ public void given_empty_store_with_full_range()
+ {
+ var cache = new LockingInMemoryCache();
+
+ CollectionAssert.IsEmpty(cache.ReadRecords(0, int.MaxValue));
+ }
+
+ [Test]
+ public void given_reloaded_store_and_non_matching_range()
+ {
+ var cache = new LockingInMemoryCache();
+ cache.ReloadEverything(CreateFrames("stream1", "stream2"));
+ CollectionAssert.IsEmpty(cache.ReadRecords(2, 10));
+ }
+
+ [Test]
+ public void given_reloaded_store_and_intersecting_range()
+ {
+ var cache = new LockingInMemoryCache();
+ cache.ReloadEverything(CreateFrames("stream1", "stream2","stream3"));
+ var dataWithKeys = cache.ReadRecords(1, 1).ToArray();
+ DataAssert.AreEqual(new[] { CreateKey(2, 1, "stream2") }, dataWithKeys);
+ }
+
+
+ [Test]
+ public void given_reloaded_store_and_matching_range()
+ {
+ var cache = new LockingInMemoryCache();
+ cache.ReloadEverything(CreateFrames("stream1", "stream2", "stream1"));
+ var dataWithKeys = cache.ReadRecords(0, 3).ToArray();
+ DataAssert.AreEqual(new[]
+ {
+ CreateKey(1, 1, "stream1"),
+ CreateKey(2, 1, "stream2"),
+ CreateKey(3, 2, "stream1")
+ }, dataWithKeys);
+ }
+
+
+ [Test]
+ public void given_reloaded_and_appended_store_and_matching_range()
+ {
+ var cache = new LockingInMemoryCache();
+
+
+ cache.ReloadEverything(CreateFrames("stream1", "stream2"));
+ var frame = GetEventBytes(3);
+ cache.ConcurrentAppend("stream2", frame, (version, storeVersion) => { });
+
+ var dataWithKeys = cache.ReadRecords(0, 3);
+ DataAssert.AreEqual(new[]
+ {
+ CreateKey(1,1,"stream1"),
+ CreateKey(2,1,"stream2"),
+ CreateKey(3,2,"stream2"),
+ }, dataWithKeys);
+ }
+
+
+ }
+}
View
11 Cqrs.Portable.Tests/TapeStorage/LockingInMemoryCacheTests/when_reading_stream.cs
@@ -0,0 +1,11 @@
+using NUnit.Framework;
+
+namespace Cqrs.Portable.Tests.TapeStorage.LockingInMemoryCacheTests
+{
+ [TestFixture]
+ public sealed class when_reading_stream
+ {
+
+
+ }
+}
View
3 Cqrs.Portable/TapeStorage/IAppendOnlyStore.cs
@@ -49,12 +49,13 @@ public sealed class DataWithKey
public DataWithKey(string key, byte[] data, long streamVersion, long storeVersion)
{
+ if (null == data)
+ throw new ArgumentNullException("data");
Key = key;
Data = data;
StreamVersion = streamVersion;
StoreVersion = storeVersion;
}
-
}
/// <summary>

0 comments on commit 6b4d6a0

Please sign in to comment.
Something went wrong with that request. Please try again.