Example #1
0
        public async Task SetAsyncTest()
        {
            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.SetAsync(-1, new TestState(), cts.Token));

            await Assert.ThrowsAsync <IndexOutOfRangeException>(() => state.SetAsync(1, new TestState(), cts.Token));

            Assert.False(await stateManager.ContainsStateAsync("TestName:-1", cts.Token));
            Assert.False(await stateManager.ContainsStateAsync("TestName:1", cts.Token));

            var value = new TestState {
                Value = "A"
            };
            await state.SetAsync(0, value, cts.Token);

            Assert.Equal(value, await stateManager.GetStateAsync <TestState>("TestName:0", cts.Token));
        }
Example #2
0
        public async Task DeleteStateAsyncTest()
        {
            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);

            await stateManager.AddStateAsync("TestName", new LinkedManifest
            {
                Count = 1,
                First = 0,
                Last  = 0,
                Next  = 1
            }, cts.Token);

            await stateManager.AddStateAsync("TestName:0", new LinkedNode <TestState>
            {
                Value = new TestState()
            }, cts.Token);

            await state.DeleteStateAsync(cts.Token);

            Assert.False(await stateManager.ContainsStateAsync("TestName", cts.Token), "State was expected to be deleted");
            Assert.False(await stateManager.ContainsStateAsync("TestName:0", cts.Token), "State was expected to be deleted");
        }
Example #3
0
        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));
        }
Example #4
0
        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);
        }
Example #5
0
        public async Task DeleteAsyncTest()
        {
            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);

            var hashCodes = await SetupValues(stateManager, "TestName",
                                              ("A", new TestState {
                Value = "A1"
            }),
                                              ("A", new TestState {
                Value = "A2"
            }),
                                              ("B", new TestState {
                Value = "B1"
            }));

            var firstKeyHash  = hashCodes[0];
            var secondKeyHash = hashCodes[1];

            await state.DeleteStateAsync(cts.Token);

            Assert.False(await stateManager.ContainsStateAsync("TestName", cts.Token));
            Assert.False(await stateManager.ContainsStateAsync($"TestName:{firstKeyHash:X}", cts.Token));
            Assert.False(await stateManager.ContainsStateAsync($"TestName:{firstKeyHash:X}:0", cts.Token));
            Assert.False(await stateManager.ContainsStateAsync($"TestName:{firstKeyHash:X}:0:v", cts.Token));
            Assert.False(await stateManager.ContainsStateAsync($"TestName:{firstKeyHash:X}:1", cts.Token));
            Assert.False(await stateManager.ContainsStateAsync($"TestName:{firstKeyHash:X}:1:v", cts.Token));
            Assert.False(await stateManager.ContainsStateAsync($"TestName:{secondKeyHash:X}", cts.Token));
            Assert.False(await stateManager.ContainsStateAsync($"TestName:{secondKeyHash:X}:2", cts.Token));
            Assert.False(await stateManager.ContainsStateAsync($"TestName:{secondKeyHash:X}:2:v", cts.Token));
        }
Example #6
0
        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);
        }