Ejemplo n.º 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));
        }
Ejemplo n.º 2
0
        public async Task AddAsync_SecondValueTest()
        {
            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 = "A"
            }));

            var firstKeyHash = hashCodes[0];

            var secondKey     = "B";
            var secondKeyHash = (long)secondKey.GetHashCode();

            await state.AddAsync(secondKey, new TestState { Value = secondKey }, cts.Token);

            var manifest = await stateManager.GetStateAsync <HashManifest>("TestName", cts.Token);

            Assert.NotNull(manifest);
            Assert.Equal(2, manifest.Count);
            Assert.Equal(2, manifest.Next);
            Assert.Equal(firstKeyHash, manifest.Head);
            Assert.Equal(secondKeyHash, manifest.Tail);
            var bucket = await stateManager.GetStateAsync <HashBucket>($"TestName:{secondKeyHash:X}", cts.Token);

            Assert.NotNull(bucket);
            Assert.Equal(1, bucket.Head);
            Assert.Equal(1, bucket.Tail);
            Assert.Equal(firstKeyHash, bucket.Previous);
            Assert.Null(bucket.Next);
            Assert.Equal(secondKeyHash, bucket.HashCode);
            var node = await stateManager.GetStateAsync <HashKeyNode <string> >($"TestName:{secondKeyHash:X}:1", cts.Token);

            Assert.NotNull(node);
            Assert.Null(node.Previous);
            Assert.Null(node.Next);
            var value = await stateManager.GetStateAsync <TestState>($"TestName:{secondKeyHash:X}:1:v", cts.Token);

            Assert.NotNull(value);
            Assert.Equal(secondKey, value.Value);
        }
Ejemplo n.º 3
0
        public async Task GetStateAsync_NonExistingState_ThrowsKeyNotFound()
        {
            var          instance    = new MockActorStateManager();
            const string notExisting = "NonExistingState";

            Task Action() => instance.GetStateAsync <int>(notExisting);

            var ex = await Assert.ThrowsExceptionAsync <KeyNotFoundException>(Action);

            StringAssert.Contains(ex.Message, notExisting);
        }
Ejemplo n.º 4
0
        public async Task AddAsync_SingleValueTest()
        {
            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 key         = "A";
            var keyHashCode = (long)"A".GetHashCode();

            await state.AddAsync("A", new TestState { Value = "A" }, cts.Token);

            var manifest = await stateManager.GetStateAsync <HashManifest>("TestName", cts.Token);

            Assert.NotNull(manifest);
            Assert.Equal(1, manifest.Count);
            Assert.Equal(1, manifest.Next);
            Assert.Equal(keyHashCode, manifest.Head);
            Assert.Equal(keyHashCode, manifest.Tail);
            var bucket = await stateManager.GetStateAsync <HashBucket>($"TestName:{keyHashCode:X}", cts.Token);

            Assert.NotNull(bucket);
            Assert.Equal(0, bucket.Head);
            Assert.Equal(0, bucket.Tail);
            Assert.Null(bucket.Previous);
            Assert.Null(bucket.Next);
            Assert.Equal(keyHashCode, bucket.HashCode);
            var node = await stateManager.GetStateAsync <HashKeyNode <string> >($"TestName:{keyHashCode:X}:0", cts.Token);

            Assert.NotNull(node);
            Assert.Null(node.Previous);
            Assert.Null(node.Next);
            var value = await stateManager.GetStateAsync <TestState>($"TestName:{keyHashCode:X}:0:v", cts.Token);

            Assert.NotNull(value);
            Assert.Equal("A", value.Value);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public async Task CountAsyncTest()
        {
            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);

            Assert.Equal(1, await state.CountAsync(cts.Token));
            Assert.Equal(1, await stateManager.GetStateAsync <long>("TestName", cts.Token));
        }
Ejemplo n.º 7
0
        public async Task PushAsyncTest()
        {
            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 state.PushAsync(new TestState { Value = "A" }, cts.Token);

            await state.PushAsync(new TestState { Value = "B" }, cts.Token);

            await state.PushAsync(new TestState { Value = "C" }, cts.Token);

            var manifest = await stateManager.GetStateAsync <LinkedManifest>("TestName", cts.Token);

            Assert.Equal(3, manifest.Count);
            Assert.Equal(2, manifest.First);
            Assert.Equal(0, manifest.Last);
            Assert.Equal(3, manifest.Next);
            var value0 = await stateManager.TryGetStateAsync <LinkedNode <TestState> >("TestName:2", cts.Token);

            Assert.True(value0.HasValue);
            Assert.Null(value0.Value.Previous);
            Assert.Equal(1, value0.Value.Next);
            Assert.NotNull(value0.Value.Value);
            Assert.Equal("C", value0.Value.Value.Value);
            var value1 = await stateManager.TryGetStateAsync <LinkedNode <TestState> >("TestName:1", cts.Token);

            Assert.True(value1.HasValue);
            Assert.Equal(2, value1.Value.Previous);
            Assert.Equal(0, value1.Value.Next);
            Assert.NotNull(value1.Value.Value);
            Assert.Equal("B", value1.Value.Value.Value);
            var value2 = await stateManager.TryGetStateAsync <LinkedNode <TestState> >("TestName:0", cts.Token);

            Assert.True(value2.HasValue);
            Assert.Equal(1, value2.Value.Previous);
            Assert.Null(value2.Value.Next);
            Assert.NotNull(value2.Value.Value);
            Assert.Equal("A", value2.Value.Value.Value);
        }
Ejemplo n.º 8
0
        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);
        }