public async Task TryPopAsync_SingleValueTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var state = new ActorStackState <TestState>(stateManager, keyMock.Object); var cts = new CancellationTokenSource(1000); var values = new List <TestState> { new TestState { Value = "A" }, new TestState { Value = "B" } }; await stateManager.SetStateAsync("TestName", new LinkedManifest { Count = 1, First = 0, Last = 0, Next = 1 }, cts.Token); await stateManager.SetStateAsync("TestName:0", new LinkedNode <TestState> { Value = values[0] }, cts.Token); var result = await state.TryPopAsync(cts.Token); Assert.True(result.HasValue); Assert.Equal("A", result.Value.Value); Assert.False(await stateManager.ContainsStateAsync("TestName:0", cts.Token)); var manifest = await stateManager.GetStateAsync <LinkedManifest>("TestName", cts.Token); Assert.NotNull(manifest); Assert.Equal(0, manifest.Count); Assert.Null(manifest.First); Assert.Null(manifest.Last); Assert.Equal(0, manifest.Next); }
public async Task TryPeekAsync_MultipleValuesTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var state = new ActorStackState <TestState>(stateManager, keyMock.Object); var cts = new CancellationTokenSource(1000); var values = new List <TestState> { new TestState { Value = "A" }, new TestState { Value = "B" } }; await stateManager.SetStateAsync("TestName", new LinkedManifest { Count = 2, First = 0, Last = 1, Next = 2 }, cts.Token); await stateManager.SetStateAsync("TestName:0", new LinkedNode <TestState> { Next = 1, Value = values[0] }, cts.Token); await stateManager.AddStateAsync("TestName:1", new LinkedNode <TestState> { Previous = 0, Value = values[1] }, cts.Token); var result = await state.TryPeekAsync(cts.Token); Assert.True(result.HasValue); Assert.Equal("A", result.Value.Value); }
public async Task CountAsyncTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var state = new TestLinkedCollectionState(stateManager, keyMock.Object); var cts = new CancellationTokenSource(1000); Assert.Equal(0, await state.CountAsync(cts.Token)); await stateManager.AddStateAsync("TestName", new LinkedManifest { Count = 0, Next = 0 }, cts.Token); Assert.Equal(0, await state.CountAsync(cts.Token)); await stateManager.SetStateAsync("TestName", new LinkedManifest { Count = 1, Next = 1 }, cts.Token); Assert.Equal(1, await state.CountAsync(cts.Token)); }
public async Task EnumeratorTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var cts = new CancellationTokenSource(1000); var values = new List <TestState> { new TestState { Value = "A" }, new TestState { Value = "B" } }; await stateManager.SetStateAsync("TestName", new LinkedManifest { Count = 2, First = 0, Last = 1, Next = 2 }, cts.Token); await stateManager.AddStateAsync("TestName:0", new LinkedNode <TestState> { Next = 1, Value = values[0] }, cts.Token); await stateManager.AddStateAsync("TestName:1", new LinkedNode <TestState> { Previous = 0, Value = values[1] }, cts.Token); var state = new TestLinkedCollectionState(stateManager, keyMock.Object); var extractedValues = new List <TestState>(); var enumerator = state.GetAsyncEnumerator(); Assert.Null(enumerator.Current); while (await enumerator.MoveNextAsync(cts.Token)) { Assert.NotNull(enumerator.Current); extractedValues.Add(enumerator.Current); } Assert.Equal(values, extractedValues); enumerator.Reset(); Assert.Null(enumerator.Current); }
public async Task MultiType_SetStateAsyncTest() { var instance = new MockActorStateManager(); string stateName = "stateName"; await instance.SetStateAsync(stateName, string.Empty); await instance.SetStateAsync(stateName, 5); DateTimeOffset utcNow = DateTimeOffset.UtcNow; await instance.SetStateAsync(stateName, utcNow); var result = await instance.TryGetStateAsync <DateTimeOffset>(stateName); Assert.IsInstanceOfType(result, typeof(ConditionalValue <DateTimeOffset>)); Assert.IsNotNull(result.Value); Assert.IsTrue(result.HasValue); Assert.AreEqual(utcNow, result.Value); }
public async Task DeleteStateAsyncTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var state = new ActorArrayState <TestState>(stateManager, keyMock.Object, 1); var cts = new CancellationTokenSource(1000); await stateManager.SetStateAsync("TestName", 1L, cts.Token); await stateManager.SetStateAsync("TestName:0", new TestState(), cts.Token); await state.DeleteStateAsync(cts.Token); Assert.False(await stateManager.ContainsStateAsync("TestName", cts.Token)); Assert.False(await stateManager.ContainsStateAsync("TestName:0", cts.Token)); }
public async Task CountAsyncTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var state = new ActorDictionaryState <string, TestState>(stateManager, keyMock.Object); var cts = new CancellationTokenSource(1000); Assert.Equal(0, await state.CountAsync(cts.Token)); await stateManager.SetStateAsync("TestName", new HashManifest(), cts.Token); Assert.Equal(0, await state.CountAsync(cts.Token)); await stateManager.SetStateAsync("TestName", new HashManifest { Count = 1, Next = 1 }, cts.Token); Assert.Equal(1, await state.CountAsync(cts.Token)); }
public async Task ContainsAsyncTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var state = new ActorArrayState <TestState>(stateManager, keyMock.Object, 1); var cts = new CancellationTokenSource(1000); var i = 0; Assert.False(await state.ContainsAsync(s => i++ == 1, cts.Token)); await stateManager.SetStateAsync("TestName:0", new TestState { Value = "A" }, cts.Token); Assert.True(await state.ContainsAsync(s => s.Value == "A", cts.Token)); i = 0; Assert.False(await state.ContainsAsync(s => i++ == 2, cts.Token)); }
public async Task GetAsyncTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var state = new ActorArrayState <TestState>(stateManager, keyMock.Object, 1); var cts = new CancellationTokenSource(1000); await Assert.ThrowsAsync <IndexOutOfRangeException>(() => state.GetAsync(-1, cts.Token)); await Assert.ThrowsAsync <IndexOutOfRangeException>(() => state.GetAsync(1, cts.Token)); Assert.Null(await state.GetAsync(0, cts.Token)); await stateManager.SetStateAsync("TestName:0", new TestState(), cts.Token); Assert.NotNull(await state.GetAsync(0, cts.Token)); }
public async Task TryPeekAsync_When_NoValuesTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var state = new ActorStackState <TestState>(stateManager, keyMock.Object); var cts = new CancellationTokenSource(1000); await stateManager.SetStateAsync("TestName", new LinkedManifest { Count = 0, First = null, Last = null, Next = 0 }, cts.Token); var result = await state.TryPeekAsync(cts.Token); Assert.False(result.HasValue); }
public async Task TryFindAsyncTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var state = new ActorListState <TestState>(stateManager, keyMock.Object); var cts = new CancellationTokenSource(1000); var result = await state.TryFindAsync(s => true, cts.Token); Assert.False(result.HasValue, "State was expected to be missing"); var values = new List <TestState> { new TestState { Value = "A" }, new TestState { Value = "B" } }; await stateManager.SetStateAsync("TestName", new LinkedManifest { Count = 2, First = 0, Last = 1, Next = 2 }, cts.Token); await stateManager.SetStateAsync("TestName:0", new LinkedNode <TestState> { Next = 1, Value = values[0] }, cts.Token); await stateManager.AddStateAsync("TestName:1", new LinkedNode <TestState> { Previous = 0, Value = values[1] }, cts.Token); result = await state.TryFindAsync(s => true, cts.Token); Assert.True(result.HasValue, "State was expected to exist"); Assert.Equal(values[0], result.Value); var i = 0; result = await state.TryFindAsync(s => i++ == 1, cts.Token); Assert.True(result.HasValue, "State was expected to exist"); Assert.Equal(values[1], result.Value); i = 0; result = await state.TryFindAsync(s => i++ == 2, cts.Token); Assert.False(result.HasValue, "State was expected to be missing"); }
public async Task RemoveAsyncTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var state = new ActorListState <TestState>(stateManager, keyMock.Object); var cts = new CancellationTokenSource(1000); var values = new List <TestState> { new TestState { Value = "A" }, new TestState { Value = "B" } }; await stateManager.SetStateAsync("TestName", new LinkedManifest { Count = 2, First = 0, Last = 1, Next = 2 }, cts.Token); await stateManager.SetStateAsync("TestName:0", new LinkedNode <TestState> { Next = 1, Value = values[0] }, cts.Token); await stateManager.AddStateAsync("TestName:1", new LinkedNode <TestState> { Previous = 0, Value = values[1] }, cts.Token); var i = 0; await state.RemoveAsync(s => i++ == 1, cts.Token); Assert.False(await stateManager.ContainsStateAsync("TestName:1", cts.Token), "State was expected to be deleted"); var node = await stateManager.TryGetStateAsync <LinkedNode <TestState> >("TestName:0", cts.Token); Assert.True(node.HasValue, "State was expected to exist"); Assert.NotNull(node.Value.Value); Assert.Null(node.Value.Previous); Assert.Null(node.Value.Next); var manifest = await stateManager.TryGetStateAsync <LinkedManifest>("TestName", cts.Token); Assert.True(manifest.HasValue, "Manifest was expected to exist"); Assert.Equal(1, manifest.Value.Count); Assert.Equal(2, manifest.Value.Next); Assert.Equal(0, manifest.Value.First); Assert.Equal(0, manifest.Value.Last); i = 0; await state.RemoveAsync(s => i++ == 0, cts.Token); Assert.False(await stateManager.ContainsStateAsync("TestName:0", cts.Token), "State was expected to be deleted"); manifest = await stateManager.TryGetStateAsync <LinkedManifest>("TestName", cts.Token); Assert.True(manifest.HasValue, "Manifest was expected to exist"); Assert.Equal(0, manifest.Value.Count); Assert.Equal(0, manifest.Value.Next); Assert.Null(manifest.Value.First); Assert.Null(manifest.Value.Last); }
public async Task InsertAtAsync_EndTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var cts = new CancellationTokenSource(1000); await stateManager.SetStateAsync("TestName", new LinkedManifest { Count = 2, First = 0, Last = 1, Next = 2 }, cts.Token); await stateManager.AddStateAsync("TestName:0", new LinkedNode <TestState> { Next = 1, Value = new TestState { Value = "A" } }, cts.Token); await stateManager.AddStateAsync("TestName:1", new LinkedNode <TestState> { Previous = 0, Value = new TestState { Value = "B" } }, cts.Token); var state = new TestLinkedCollectionState(stateManager, keyMock.Object); var values = new List <TestState> { new TestState { Value = "I1" }, new TestState { Value = "I2" }, }; await state.TestInsertAtAsync(2, values, cts.Token); var manifest = await stateManager.GetStateAsync <LinkedManifest>("TestName", cts.Token); Assert.Equal(4, manifest.Count); Assert.Equal(0, manifest.First); Assert.Equal(3, manifest.Last); Assert.Equal(4, manifest.Next); var value0 = await stateManager.TryGetStateAsync <LinkedNode <TestState> >("TestName:0", cts.Token); Assert.True(value0.HasValue); Assert.Null(value0.Value.Previous); Assert.Equal(1, value0.Value.Next); Assert.NotNull(value0.Value.Value); Assert.Equal("A", value0.Value.Value.Value); var value1 = await stateManager.TryGetStateAsync <LinkedNode <TestState> >("TestName:1", cts.Token); Assert.True(value1.HasValue); Assert.Equal(0, value1.Value.Previous); Assert.Equal(2, value1.Value.Next); Assert.NotNull(value1.Value.Value); Assert.Equal("B", value1.Value.Value.Value); var value2 = await stateManager.TryGetStateAsync <LinkedNode <TestState> >("TestName:2", cts.Token); Assert.True(value2.HasValue); Assert.Equal(1, value2.Value.Previous); Assert.Equal(3, value2.Value.Next); Assert.NotNull(value2.Value.Value); Assert.Equal("I1", value2.Value.Value.Value); var value3 = await stateManager.TryGetStateAsync <LinkedNode <TestState> >("TestName:3", cts.Token); Assert.True(value3.HasValue); Assert.Equal(2, value3.Value.Previous); Assert.Null(value3.Value.Next); Assert.NotNull(value3.Value.Value); Assert.Equal("I2", value3.Value.Value.Value); }
public async Task ContainsAsyncTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var cts = new CancellationTokenSource(1000); var state = new TestLinkedCollectionState(stateManager, keyMock.Object); var searchMock = new Mock <Predicate <TestState> >(); searchMock.Setup(x => x(It.IsAny <TestState>())) .Returns <TestState>(s => s.Value == "A"); Assert.False(await state.ContainsAsync(searchMock.Object, cts.Token)); await stateManager.AddStateAsync("TestName", new LinkedManifest { Count = 0, Next = 0 }, cts.Token); Assert.False(await state.ContainsAsync(searchMock.Object, cts.Token)); await stateManager.SetStateAsync("TestName", new LinkedManifest { Count = 1, First = 0, Last = 0, Next = 1 }, cts.Token); await stateManager.AddStateAsync("TestName:0", new LinkedNode <TestState> { Value = new TestState { Value = "B" } }, cts.Token); Assert.False(await state.ContainsAsync(searchMock.Object, cts.Token)); await stateManager.SetStateAsync("TestName", new LinkedManifest { Count = 2, First = 0, Last = 1, Next = 2 }, cts.Token); await stateManager.SetStateAsync("TestName:0", new LinkedNode <TestState> { Next = 1, Value = new TestState { Value = "B" } }, cts.Token); await stateManager.AddStateAsync("TestName:1", new LinkedNode <TestState> { Previous = 0, Value = new TestState { Value = "A" } }, cts.Token); Assert.True(await state.ContainsAsync(searchMock.Object, cts.Token)); }