|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Cleipnir.ResilientFunctions.Storage; |
| 6 | +using Shouldly; |
| 7 | + |
| 8 | +namespace Cleipnir.ResilientFunctions.Tests.TestTemplates; |
| 9 | + |
| 10 | +public abstract class StateStoreTests |
| 11 | +{ |
| 12 | + public abstract Task GetAndReadReturnsEmptyDictionaryWhenNoStateExists(); |
| 13 | + protected async Task GetAndReadReturnsEmptyDictionaryWhenNoStateExists<TStateStore, TStoredState>( |
| 14 | + Task<TStateStore> storeTask, |
| 15 | + Func<TStateStore, IReadOnlyList<StoredId>, Task<Dictionary<StoredId, Dictionary<long, TStoredState>>>> getAndRead |
| 16 | + ) |
| 17 | + { |
| 18 | + var stateStore = await storeTask; |
| 19 | + var storedId = new StoredId(Guid.NewGuid()); |
| 20 | + |
| 21 | + var result = await getAndRead(stateStore, new[] { storedId }); |
| 22 | + |
| 23 | + result.ShouldNotBeNull(); |
| 24 | + result.Count.ShouldBe(0); |
| 25 | + } |
| 26 | + |
| 27 | + public abstract Task SetAndExecuteAndGetAndReadReturnsSingleStateSuccessfully(); |
| 28 | + protected async Task SetAndExecuteAndGetAndReadReturnsSingleStateSuccessfully<TStateStore, TStoredState>( |
| 29 | + Task<TStateStore> storeTask, |
| 30 | + Func<TStateStore, Dictionary<StoredId, Dictionary<long, TStoredState>>, Task> setAndExecute, |
| 31 | + Func<TStateStore, IReadOnlyList<StoredId>, Task<Dictionary<StoredId, Dictionary<long, TStoredState>>>> getAndRead, |
| 32 | + Func<StoredId, long, byte[], TStoredState> createState, |
| 33 | + Func<TStoredState, StoredId> getId, |
| 34 | + Func<TStoredState, long> getPosition, |
| 35 | + Func<TStoredState, byte[]> getContent |
| 36 | + ) |
| 37 | + { |
| 38 | + var stateStore = await storeTask; |
| 39 | + var storedId = new StoredId(Guid.NewGuid()); |
| 40 | + var content = new byte[] { 1, 2, 3, 4, 5 }; |
| 41 | + var position = 0L; |
| 42 | + |
| 43 | + // Insert state using SetAndExecute |
| 44 | + await setAndExecute(stateStore, new Dictionary<StoredId, Dictionary<long, TStoredState>> |
| 45 | + { |
| 46 | + [storedId] = new() { [position] = createState(storedId, position, content) } |
| 47 | + }); |
| 48 | + |
| 49 | + // Read state back using GetAndRead |
| 50 | + var result = await getAndRead(stateStore, new[] { storedId }); |
| 51 | + |
| 52 | + result.ShouldNotBeNull(); |
| 53 | + result.Count.ShouldBe(1); |
| 54 | + result.ContainsKey(storedId).ShouldBeTrue(); |
| 55 | + result[storedId].Count.ShouldBe(1); |
| 56 | + result[storedId].ContainsKey(position).ShouldBeTrue(); |
| 57 | + |
| 58 | + var storedState = result[storedId][position]; |
| 59 | + getId(storedState).ShouldBe(storedId); |
| 60 | + getPosition(storedState).ShouldBe(position); |
| 61 | + getContent(storedState).ShouldBe(content); |
| 62 | + } |
| 63 | + |
| 64 | + public abstract Task SetAndExecuteAndGetAndReadReturnsMultipleStatesForSingleId(); |
| 65 | + protected async Task SetAndExecuteAndGetAndReadReturnsMultipleStatesForSingleId<TStateStore, TStoredState>( |
| 66 | + Task<TStateStore> storeTask, |
| 67 | + Func<TStateStore, Dictionary<StoredId, Dictionary<long, TStoredState>>, Task> setAndExecute, |
| 68 | + Func<TStateStore, IReadOnlyList<StoredId>, Task<Dictionary<StoredId, Dictionary<long, TStoredState>>>> getAndRead, |
| 69 | + Func<StoredId, long, byte[], TStoredState> createState, |
| 70 | + Func<TStoredState, byte[]> getContent |
| 71 | + ) |
| 72 | + { |
| 73 | + var stateStore = await storeTask; |
| 74 | + var storedId = new StoredId(Guid.NewGuid()); |
| 75 | + var content1 = new byte[] { 1, 2, 3 }; |
| 76 | + var content2 = new byte[] { 4, 5, 6 }; |
| 77 | + var position1 = 0L; |
| 78 | + var position2 = 1L; |
| 79 | + |
| 80 | + // Insert multiple states for the same ID using SetAndExecute |
| 81 | + await setAndExecute(stateStore, new Dictionary<StoredId, Dictionary<long, TStoredState>> |
| 82 | + { |
| 83 | + [storedId] = new() |
| 84 | + { |
| 85 | + [position1] = createState(storedId, position1, content1), |
| 86 | + [position2] = createState(storedId, position2, content2) |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + // Read states back using GetAndRead |
| 91 | + var result = await getAndRead(stateStore, new[] { storedId }); |
| 92 | + |
| 93 | + result.ShouldNotBeNull(); |
| 94 | + result.Count.ShouldBe(1); |
| 95 | + result.ContainsKey(storedId).ShouldBeTrue(); |
| 96 | + result[storedId].Count.ShouldBe(2); |
| 97 | + |
| 98 | + getContent(result[storedId][position1]).ShouldBe(content1); |
| 99 | + getContent(result[storedId][position2]).ShouldBe(content2); |
| 100 | + } |
| 101 | + |
| 102 | + public abstract Task SetAndExecuteAndGetAndReadReturnsMultipleIdsWithMultiplePositions(); |
| 103 | + protected async Task SetAndExecuteAndGetAndReadReturnsMultipleIdsWithMultiplePositions<TStateStore, TStoredState>( |
| 104 | + Task<TStateStore> storeTask, |
| 105 | + Func<TStateStore, Dictionary<StoredId, Dictionary<long, TStoredState>>, Task> setAndExecute, |
| 106 | + Func<TStateStore, IReadOnlyList<StoredId>, Task<Dictionary<StoredId, Dictionary<long, TStoredState>>>> getAndRead, |
| 107 | + Func<StoredId, long, byte[], TStoredState> createState, |
| 108 | + Func<TStoredState, byte[]> getContent |
| 109 | + ) |
| 110 | + { |
| 111 | + var stateStore = await storeTask; |
| 112 | + var storedId1 = new StoredId(Guid.NewGuid()); |
| 113 | + var storedId2 = new StoredId(Guid.NewGuid()); |
| 114 | + var content1 = new byte[] { 1, 2, 3 }; |
| 115 | + var content2 = new byte[] { 4, 5, 6 }; |
| 116 | + var content3 = new byte[] { 7, 8, 9 }; |
| 117 | + |
| 118 | + // Insert states for multiple IDs using SetAndExecute |
| 119 | + await setAndExecute(stateStore, new Dictionary<StoredId, Dictionary<long, TStoredState>> |
| 120 | + { |
| 121 | + [storedId1] = new() |
| 122 | + { |
| 123 | + [0L] = createState(storedId1, 0L, content1), |
| 124 | + [1L] = createState(storedId1, 1L, content2) |
| 125 | + }, |
| 126 | + [storedId2] = new() |
| 127 | + { |
| 128 | + [0L] = createState(storedId2, 0L, content3) |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + // Read states back using GetAndRead |
| 133 | + var result = await getAndRead(stateStore, new[] { storedId1, storedId2 }); |
| 134 | + |
| 135 | + result.ShouldNotBeNull(); |
| 136 | + result.Count.ShouldBe(2); |
| 137 | + result.ContainsKey(storedId1).ShouldBeTrue(); |
| 138 | + result.ContainsKey(storedId2).ShouldBeTrue(); |
| 139 | + result[storedId1].Count.ShouldBe(2); |
| 140 | + result[storedId2].Count.ShouldBe(1); |
| 141 | + |
| 142 | + getContent(result[storedId1][0L]).ShouldBe(content1); |
| 143 | + getContent(result[storedId1][1L]).ShouldBe(content2); |
| 144 | + getContent(result[storedId2][0L]).ShouldBe(content3); |
| 145 | + } |
| 146 | + |
| 147 | + public abstract Task SetAndExecuteAndGetAndReadHandlesNullContent(); |
| 148 | + protected async Task SetAndExecuteAndGetAndReadHandlesNullContent<TStateStore, TStoredState>( |
| 149 | + Task<TStateStore> storeTask, |
| 150 | + Func<TStateStore, Dictionary<StoredId, Dictionary<long, TStoredState>>, Task> setAndExecute, |
| 151 | + Func<TStateStore, IReadOnlyList<StoredId>, Task<Dictionary<StoredId, Dictionary<long, TStoredState>>>> getAndRead, |
| 152 | + Func<StoredId, long, byte[], TStoredState> createState, |
| 153 | + Func<TStoredState, StoredId> getId, |
| 154 | + Func<TStoredState, long> getPosition, |
| 155 | + Func<TStoredState, byte[]> getContent |
| 156 | + ) |
| 157 | + { |
| 158 | + var stateStore = await storeTask; |
| 159 | + var storedId = new StoredId(Guid.NewGuid()); |
| 160 | + var position = 0L; |
| 161 | + |
| 162 | + // Insert state with null content using SetAndExecute |
| 163 | + await setAndExecute(stateStore, new Dictionary<StoredId, Dictionary<long, TStoredState>> |
| 164 | + { |
| 165 | + [storedId] = new() |
| 166 | + { |
| 167 | + [position] = createState(storedId, position, null) |
| 168 | + } |
| 169 | + }); |
| 170 | + |
| 171 | + // Read state back using GetAndRead |
| 172 | + var result = await getAndRead(stateStore, new[] { storedId }); |
| 173 | + |
| 174 | + result.ShouldNotBeNull(); |
| 175 | + result.Count.ShouldBe(1); |
| 176 | + result.ContainsKey(storedId).ShouldBeTrue(); |
| 177 | + result[storedId].Count.ShouldBe(1); |
| 178 | + |
| 179 | + var storedState = result[storedId][position]; |
| 180 | + getId(storedState).ShouldBe(storedId); |
| 181 | + getPosition(storedState).ShouldBe(position); |
| 182 | + getContent(storedState).ShouldBeNull(); |
| 183 | + } |
| 184 | + |
| 185 | + public abstract Task SetAndExecuteUpdatesExistingState(); |
| 186 | + protected async Task SetAndExecuteUpdatesExistingState<TStateStore, TStoredState>( |
| 187 | + Task<TStateStore> storeTask, |
| 188 | + Func<TStateStore, Dictionary<StoredId, Dictionary<long, TStoredState>>, Task> setAndExecute, |
| 189 | + Func<TStateStore, IReadOnlyList<StoredId>, Task<Dictionary<StoredId, Dictionary<long, TStoredState>>>> getAndRead, |
| 190 | + Func<StoredId, long, byte[], TStoredState> createState, |
| 191 | + Func<TStoredState, byte[]> getContent |
| 192 | + ) |
| 193 | + { |
| 194 | + var stateStore = await storeTask; |
| 195 | + var storedId = new StoredId(Guid.NewGuid()); |
| 196 | + var position = 0L; |
| 197 | + var initialContent = new byte[] { 1, 2, 3 }; |
| 198 | + var updatedContent = new byte[] { 4, 5, 6, 7 }; |
| 199 | + |
| 200 | + // Insert initial state |
| 201 | + await setAndExecute(stateStore, new Dictionary<StoredId, Dictionary<long, TStoredState>> |
| 202 | + { |
| 203 | + [storedId] = new() |
| 204 | + { |
| 205 | + [position] = createState(storedId, position, initialContent) |
| 206 | + } |
| 207 | + }); |
| 208 | + |
| 209 | + // Update the same state |
| 210 | + await setAndExecute(stateStore, new Dictionary<StoredId, Dictionary<long, TStoredState>> |
| 211 | + { |
| 212 | + [storedId] = new() |
| 213 | + { |
| 214 | + [position] = createState(storedId, position, updatedContent) |
| 215 | + } |
| 216 | + }); |
| 217 | + |
| 218 | + // Read state back - should have updated content |
| 219 | + var result = await getAndRead(stateStore, new[] { storedId }); |
| 220 | + |
| 221 | + result.ShouldNotBeNull(); |
| 222 | + result.Count.ShouldBe(1); |
| 223 | + result[storedId].Count.ShouldBe(1); |
| 224 | + getContent(result[storedId][position]).ShouldBe(updatedContent); |
| 225 | + } |
| 226 | + |
| 227 | + public abstract Task SetAndExecuteHandlesEmptyDictionary(); |
| 228 | + protected async Task SetAndExecuteHandlesEmptyDictionary<TStateStore, TStoredState>( |
| 229 | + Task<TStateStore> storeTask, |
| 230 | + Func<TStateStore, Dictionary<StoredId, Dictionary<long, TStoredState>>, Task> setAndExecute, |
| 231 | + Func<TStateStore, IReadOnlyList<StoredId>, Task<Dictionary<StoredId, Dictionary<long, TStoredState>>>> getAndRead |
| 232 | + ) |
| 233 | + { |
| 234 | + var stateStore = await storeTask; |
| 235 | + |
| 236 | + // Should not throw when called with empty dictionary |
| 237 | + await setAndExecute(stateStore, new Dictionary<StoredId, Dictionary<long, TStoredState>>()); |
| 238 | + |
| 239 | + // Verify no data was added |
| 240 | + var storedId = new StoredId(Guid.NewGuid()); |
| 241 | + var result = await getAndRead(stateStore, new[] { storedId }); |
| 242 | + |
| 243 | + result.ShouldNotBeNull(); |
| 244 | + result.Count.ShouldBe(0); |
| 245 | + } |
| 246 | +} |
0 commit comments