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 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 TryGetAsyncTest() { 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); await SetupValues(stateManager, "TestName", ("A", new TestState { Value = "A1" }), ("A", new TestState { Value = "A2" }), ("B", new TestState { Value = "B1" })); var value0 = await state.TryGetValueAsync("A", cts.Token); Assert.True(value0.HasValue); Assert.Equal("A1", value0.Value.Value); var value1 = await state.TryGetValueAsync("B", cts.Token); Assert.True(value1.HasValue); Assert.Equal("B1", value1.Value.Value); var value2 = await state.TryGetValueAsync("C", cts.Token); Assert.False(value2.HasValue); }
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"); }
public void ActorTests_SetUp() { ExpectedActorState = new Dictionary <string, object>(); ExpectedReminders = new Dictionary <string, MockReminderManager.Reminder>(); StateManager = new MockActorStateManager(); Reminders = new MockReminderManager(); }
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)); }
public void ExtensionTest() { var stateManager = new MockActorStateManager(); Assert.Throws <ArgumentNullException>(() => State.Factory.CreateUsingServiceFabricActors(null, c => {})); Assert.Throws <ArgumentNullException>(() => State.Factory.CreateUsingServiceFabricActors(stateManager, null)); }
public async Task ContainsKeyAsyncTest() { 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); await SetupValues(stateManager, "TestName", ("A", new TestState { Value = "A1" }), ("A", new TestState { Value = "A2" }), ("B", new TestState { Value = "B1" })); Assert.True(await state.ContainsKeyAsync("A", cts.Token)); Assert.True(await state.ContainsKeyAsync("B", cts.Token)); Assert.False(await state.ContainsKeyAsync("C", cts.Token)); }
public SubscriptionActorTests() { SubscriptionActor.CatchAllExceptions = false; var services = new ServiceCollection(); StateManager = new MockActorStateManager(); Darc = new Mock <IRemote>(MockBehavior.Strict); Env = new Mock <IHostingEnvironment>(MockBehavior.Strict); Reminders = new MockReminderManager(); services.AddSingleton(Env.Object); services.AddSingleton <IActorStateManager>(StateManager); services.AddSingleton <IReminderManager>(Reminders); services.AddMergePolicies(); services.AddLogging(); services.AddDbContext <BuildAssetRegistryContext>( options => { options.UseInMemoryDatabase("BuildAssetRegistry"); }); services.AddSingleton( Mock.Of <IDarcRemoteFactory>( f => f.CreateAsync(It.IsAny <string>(), It.IsAny <long>()) == Task.FromResult(Darc.Object))); Provider = services.BuildServiceProvider(); Scope = Provider.CreateScope(); _context = new Lazy <BuildAssetRegistryContext>(GetContext); }
public ActorTests() { StateManager = new MockActorStateManager(); Reminders = new MockReminderManager(); Builder.RegisterInstance(StateManager).As <IActorStateManager>(); Builder.RegisterInstance(Reminders).As <IReminderManager>(); }
public async Task Int_DuplicateAddStateAsyncTest() { var instance = new MockActorStateManager(); await instance.AddStateAsync("existing", 6); Assert.ThrowsException <InvalidOperationException>(() => { instance.AddStateAsync("existing", 6).ConfigureAwait(false).GetAwaiter().GetResult(); }); }
public async Task Int_Empty_TryGetStateAsyncTest() { var instance = new MockActorStateManager(); var result = await instance.TryGetStateAsync <int>("not existing"); Assert.IsInstanceOfType(result, typeof(ConditionalValue <int>)); Assert.AreEqual(default(int), result.Value); Assert.IsFalse(result.HasValue); }
public async Task Int_DuplicateTryAddStateAsyncTest() { var instance = new MockActorStateManager(); await instance.TryAddStateAsync("existing", 6); var result = await instance.TryAddStateAsync("existing", 6); Assert.IsFalse(result); }
public void ConstructorTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); new ActorStackState <TestState>(stateManager, keyMock.Object); Assert.Throws <ArgumentNullException>(() => new ActorStackState <TestState>(null, keyMock.Object)); Assert.Throws <ArgumentNullException>(() => new ActorStackState <TestState>(stateManager, null)); }
public async Task Nullable_Int_Empty_TryGetStateAsyncTest() { var instance = new MockActorStateManager(); var result = await instance.TryGetStateAsync <int?>("not existing"); Assert.IsInstanceOfType(result, typeof(ConditionalValue <int?>)); Assert.IsNull(result.Value); Assert.IsFalse(result.HasValue); }
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); }
public async Task Int_AddStateAsyncTest() { var instance = new MockActorStateManager(); await instance.AddStateAsync("existing", 6); var result = await instance.TryGetStateAsync <int>("existing"); Assert.IsInstanceOfType(result, typeof(ConditionalValue <int>)); Assert.IsNotNull(result.Value); Assert.IsTrue(result.HasValue); Assert.AreEqual(6, result.Value); }
public async Task CustomStatefulActor_ShouldHaveUniqueStateManagerPerId() { Dictionary <ActorId, IActorStateManager> stateManagerMap = new Dictionary <ActorId, IActorStateManager>(); Func <ActorBase, IActorStateProvider, IActorStateManager> stateManagerFactory = (actr, stateProvider) => { if (!stateManagerMap.TryGetValue(actr.Id, out var actorStateManager)) { actorStateManager = new MockActorStateManager(); stateManagerMap[actr.Id] = actorStateManager; } return(actorStateManager); }; // Every actor instance has its own state manager. Func <ActorService, ActorId, ActorBase> actorFactory = (service, actorId) => new MyStatefulActor(service, actorId); var customActorService = MockActorServiceFactory.CreateCustomActorServiceForActor <CustomActorService, MyStatefulActor>(actorFactory, stateManagerFactory: stateManagerFactory); var id1 = ActorId.CreateRandom(); var id2 = ActorId.CreateRandom(); var actor1 = customActorService.Activate <MyStatefulActor>(id1); var stateManager1 = (MockActorStateManager)actor1.StateManager; var actor2 = customActorService.Activate <MyStatefulActor>(id2); var stateManager2 = (MockActorStateManager)actor2.StateManager; var actor1_2 = customActorService.Activate <MyStatefulActor>(id1); var stateManager1_2 = (MockActorStateManager)actor1_2.StateManager; const string stateName = "test"; const string payloadText = "foo"; var payload = new Payload(payloadText); //create state await actor1.InsertAsync(stateName, payload); //get state var actualState1 = await stateManager1.GetStateAsync <Payload>(stateName); Assert.AreEqual(payloadText, actualState1.Content); var actualState2 = await stateManager2.TryGetStateAsync <Payload>(stateName); Assert.IsFalse(actualState2.HasValue); var actualState1_2 = await stateManager1_2.GetStateAsync <Payload>(stateName); Assert.AreEqual(payloadText, actualState1_2.Content); Assert.AreNotSame(stateManager1, stateManager2); Assert.AreSame(stateManager1, stateManager1_2); }
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)); }
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 void NoStatesTest() { var stateManager = new MockActorStateManager(); var f = State.Factory.CreateUsingServiceFabricActors(stateManager, c => { }); Assert.IsType <ActorStateFactory>(f); var unit = f.CreateTransaction(); Assert.IsType <ActorStateUnit>(unit); Assert.Throws <KeyNotFoundException>(() => unit.Get <IObjectState <string> >(new StateKey("Test"))); }
public async Task TryPeekAsync_When_NoStateTest() { 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 result = await state.TryPeekAsync(cts.Token); Assert.False(result.HasValue); }
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 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); }
public async Task HasStateAsyncTest() { 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.False(await state.HasStateAsync(cts.Token), "State was expected to not exist"); await stateManager.AddStateAsync("TestName", new LinkedManifest(), cts.Token); Assert.True(await state.HasStateAsync(cts.Token), "State was expected to exist"); }
public async Task DeleteStateAsyncTest() { var stateManager = new MockActorStateManager(); var keyMock = new Mock <IStateKey>(); keyMock.Setup(x => x.ToString()).Returns("TestName"); var state = new ActorObjectState <TestState>(stateManager, keyMock.Object); var cts = new CancellationTokenSource(1000); await stateManager.AddStateAsync("TestName", new TestState(), cts.Token); await state.DeleteStateAsync(cts.Token); Assert.False(await stateManager.ContainsStateAsync("TestName", cts.Token), "State was expected to be deleted"); }
public async Task HasStateAsyncTest() { 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.False(await state.HasStateAsync(cts.Token)); await stateManager.SetStateAsync("TestName", 1L, cts.Token); Assert.True(await state.HasStateAsync(cts.Token)); }
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); }
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 void TestAnotherCustomActorService() { //an ActorService with a NON standard constructor can be created by passing Mock arguments: var stateManager = new MockActorStateManager(); Func <ActorBase, IActorStateProvider, IActorStateManager> stateManagerFactory = (actr, stateProvider) => stateManager; IActorStateProvider actorStateProvider = new MockActorStateProvider(); actorStateProvider.Initialize(ActorTypeInformation.Get(typeof(OnActivateActor))); var context = MockStatefulServiceContextFactory.Default; var dummy = new object(); //this argument causes the 'non standard' ctor. var customActorService = new AnotherCustomActorService(dummy, context, ActorTypeInformation.Get(typeof(OnActivateActor))); var actor = customActorService.Activate <OnActivateActor>(new ActorId(123L)); Assert.IsInstanceOfType(actor, typeof(OnActivateActor)); Assert.AreEqual(123L, actor.Id.GetLongId()); }