public void Generate100KLocations() { var tp = _testkit.CreateTestProbe("genTest"); _locationGenerator.Tell(new Observe(), tp); _locationGenerator.Tell(new LocationNameGeneratorActor.GenerateLocationNames(10000), tp); var msg = tp.ExpectMsg <LocationNameGeneratorActor.LocationNamesAdded>(TimeSpan.FromMinutes(10)); Assert.That(msg.AddedLocations.Length == 10000); var rnd = _testkit.CreateTestProbe("rnd"); var lg = _testkit.CreateTestProbe("lg"); rnd.Watch(_random); lg.Watch(_locationGenerator); _random.Tell(PoisonPill.Instance); _locationGenerator.Tell(PoisonPill.Instance); rnd.ExpectMsg <Terminated>(); lg.ExpectMsg <Terminated>(); _testkit.Shutdown(TimeSpan.FromSeconds(20), true); }
public static ActorPaths CreateActorPaths(TestKit testKit, IActorRef simContext) { var simSystem = testKit.CreateTestProbe(); var inbox = Inbox.Create(system: testKit.Sys); var agentPaths = new ActorPaths(simulationContext: simContext, systemMailBox: inbox.Receiver); agentPaths.SetSupervisorAgent(systemAgent: simSystem); agentPaths.Guardians.Add(GuardianType.Contract, testKit.CreateTestProbe()); agentPaths.Guardians.Add(GuardianType.Dispo, testKit.CreateTestProbe()); agentPaths.Guardians.Add(GuardianType.Production, testKit.CreateTestProbe()); return(agentPaths); }
static void Main(string[] args) { TestKit testKit = new TestKit(); ActorSystem actorSystem = testKit.Sys; using (actorSystem) { TestProbe probe = testKit.CreateTestProbe("test-probe"); Action test1 = new Action(() => { // TestKit-based probe which allows sending, reception and reply. IActorRef deviceActor = actorSystem.ActorOf(Device.Props("group", "device")); IActorRef probeRef = probe.Ref; deviceActor.Tell(new RequestTrackDevice("group", "device"), probeRef); probe.ExpectMsg <DeviceRegistered>(); probe.LastSender.Should().Be(deviceActor); Console.WriteLine("Test 1 passed."); }); test1.Invoke(); Action test2 = new Action(() => { // TestKit-based probe which allows sending, reception and reply. var deviceActor = actorSystem.ActorOf(Device.Props("group", "device")); deviceActor.Tell(new RequestTrackDevice("wrongGroup", "device"), probe.Ref); probe.ExpectNoMsg(TimeSpan.FromMilliseconds(500)); deviceActor.Tell(new RequestTrackDevice("group", "Wrongdevice"), probe.Ref); probe.ExpectNoMsg(TimeSpan.FromMilliseconds(500)); Console.WriteLine("Test 2 passed."); }); test2.Invoke(); Console.WriteLine("UserMessage: App is finished."); // Exit the system after ENTER is pressed Console.ReadLine(); } }
public void FixtureArrangerWithSnapshot_CanBeLoaded() { using (var testKit = new TestKit(_config, "fixture-tests-4")) { var fixture = new AggregateFixture <TestAggregate, TestAggregateId>(testKit); var aggregateIdentity = TestAggregateId.New; var snapshot = new TestAggregateSnapshot(Enumerable.Range(0, 10) .Select(x => new TestAggregateSnapshot.TestModel(Guid.NewGuid())).ToList()); var snapshotStore = Persistence.Instance.Apply(testKit.Sys).SnapshotStoreFor(null); var receiverProbe = testKit.CreateTestProbe("snapshot-probe"); var snapshotSequenceNumber = 1L; fixture .For(aggregateIdentity) .Given(snapshot, snapshotSequenceNumber); snapshotStore.Tell(new LoadSnapshot(aggregateIdentity.Value, new SnapshotSelectionCriteria(long.MaxValue, DateTime.MaxValue), long.MaxValue), receiverProbe.Ref); receiverProbe.ExpectMsg <LoadSnapshotResult>(x => x.Snapshot.Snapshot is ComittedSnapshot <TestAggregate, TestAggregateId, IAggregateSnapshot <TestAggregate, TestAggregateId> > && x.Snapshot.Metadata.SequenceNr == snapshotSequenceNumber && x.Snapshot.Metadata.PersistenceId == aggregateIdentity.Value && x.Snapshot.Snapshot .As <ComittedSnapshot <TestAggregate, TestAggregateId, IAggregateSnapshot <TestAggregate, TestAggregateId> > >().AggregateSnapshot .As <TestAggregateSnapshot>().Tests.Count == snapshot.Tests.Count && x.ToSequenceNr == long.MaxValue); } }
public void FixtureArrangerWithEvents_CanBeReplayed() { using (var testKit = new TestKit(_config, "fixture-tests-3")) { var fixture = new AggregateFixture <TestAggregate, TestAggregateId>(testKit); var aggregateIdentity = TestAggregateId.New; var events = new List <IAggregateEvent <TestAggregate, TestAggregateId> >(); events.Add(new TestCreatedEvent(aggregateIdentity)); events.AddRange(Enumerable.Range(0, 10).Select(x => new TestAddedEvent(new Test(TestId.New)))); var journal = Persistence.Instance.Apply(testKit.Sys).JournalFor(null); var receiverProbe = testKit.CreateTestProbe("journal-probe"); fixture .For(aggregateIdentity) .Given(events.ToArray()); journal.Tell(new ReplayMessages(1, long.MaxValue, long.MaxValue, aggregateIdentity.ToString(), receiverProbe.Ref)); var from = 1; foreach (var _ in events) { var index = from; receiverProbe.ExpectMsg <ReplayedMessage>(x => x.Persistent.SequenceNr == index && x.Persistent.Payload is ICommittedEvent <TestAggregate, TestAggregateId, IAggregateEvent <TestAggregate, TestAggregateId> >); from++; } receiverProbe.ExpectMsg <RecoverySuccess>(); } }
static void Main(string[] args) { TestKit testKit = new TestKit(); ActorSystem actorSystem = testKit.Sys; using (actorSystem) { // TestKit-based probe which allows sending, reception and reply. TestProbe probe = testKit.CreateTestProbe("test-probe"); IActorRef deviceActor = actorSystem.ActorOf(Device.Props("group", "device")); deviceActor.Tell(new RecordTemperature(requestId: 1, value: 24.0), probe.Ref); probe.ExpectMsg <TemperatureRecorded>(s => s.RequestId == 1); deviceActor.Tell(new ReadTemperature(requestId: 2), probe.Ref); RespondTemperature response1 = probe.ExpectMsg <RespondTemperature>(); response1.RequestId.Should().Be(2); response1.Value.Should().Be(24.0); deviceActor.Tell(new RecordTemperature(requestId: 3, value: 55.0), probe.Ref); probe.ExpectMsg <TemperatureRecorded>(s => s.RequestId == 3); deviceActor.Tell(new ReadTemperature(requestId: 4), probe.Ref); RespondTemperature response2 = probe.ExpectMsg <RespondTemperature>(); response2.RequestId.Should().Be(4); response2.Value.Should().Be(55.0); Console.WriteLine("UserMessage: App is finished."); // Exit the system after ENTER is pressed Console.ReadLine(); } }
public static void Init(TestKit testKit) { _testProbe = testKit.CreateTestProbe("event-probe"); _serviceProvider = testKit.Sys.GetExtension <ServiceProviderHolder>().ServiceProvider; _commandBus = _serviceProvider.GetRequiredService <ICommandBus>(); _eventStream = testKit.Sys.EventStream; }
private static TestProbe CreateTestProbe() { TestKit testKit = new TestKit(); TestProbe probe = testKit.CreateTestProbe("test-probe"); return(probe); }
public void TearDown() { var tp = _testkit.CreateTestProbe("tp"); tp.Watch(_random); _random.Tell(PoisonPill.Instance); tp.ExpectMsg <Terminated>(); _testkit.Shutdown(); }
public void SqlEventSnapShotJournal() { var testSystem = new TestKit(Config, "sql"); var persistor = testSystem.ActorOfAsTestActorRef <SqlPersistenceTests.SqlTestActor>(Props.Create(() => new SqlPersistenceTests.SqlTestActor("SomeTest"))); var probe = testSystem.CreateTestProbe("testies"); persistor.Tell(new SqlPersistenceTests.SqlTestActor.Incrementor(10), probe); persistor.Tell(new SqlPersistenceTests.SqlTestActor.RequestCurrentValue(), probe); var result = probe.ExpectMsg <SqlPersistenceTests.SqlTestActor.IncrementorResult>(); Assert.AreEqual(10, result.Value); testSystem.Shutdown(TimeSpan.FromSeconds(5), true); testSystem = new TestKit(Config, "sql"); persistor = testSystem.ActorOfAsTestActorRef <SqlPersistenceTests.SqlTestActor>(Props.Create(() => new SqlPersistenceTests.SqlTestActor("SomeTest"))); probe = testSystem.CreateTestProbe("testies2"); persistor.Tell(new SqlPersistenceTests.SqlTestActor.RequestCurrentValue(), probe); result = probe.ExpectMsg <SqlPersistenceTests.SqlTestActor.IncrementorResult>(); Assert.AreEqual(10, result.Value); persistor.Tell("snap", probe); persistor.Tell(new SqlPersistenceTests.SqlTestActor.Incrementor(10), probe); persistor.Tell(new SqlPersistenceTests.SqlTestActor.RequestCurrentValue(), probe); result = probe.ExpectMsg <SqlPersistenceTests.SqlTestActor.IncrementorResult>(); Assert.AreEqual(20, result.Value); testSystem.Shutdown(TimeSpan.FromSeconds(5), true); testSystem = new TestKit(Config, "sql"); persistor = testSystem.ActorOfAsTestActorRef <SqlPersistenceTests.SqlTestActor>(Props.Create(() => new SqlPersistenceTests.SqlTestActor("SomeTest"))); probe = testSystem.CreateTestProbe("testies3"); persistor.Tell(new SqlPersistenceTests.SqlTestActor.RequestCurrentValue(), probe); result = probe.ExpectMsg <SqlPersistenceTests.SqlTestActor.IncrementorResult>(); Assert.AreEqual(20, result.Value); }
static void Main(string[] args) { TestKit testKit = new TestKit(); ActorSystem actorSystem = testKit.Sys; using (actorSystem) { // TestKit-based probe which allows sending, reception and reply. TestProbe probe = testKit.CreateTestProbe("test-probe"); IActorRef deviceActor = actorSystem.ActorOf(Device.Props("group", "device")); IActorRef probeRef = probe.Ref; deviceActor.Tell(new MainDevice.ReadTemperature(requestId: 42), probeRef); RespondTemperature response = probe.ExpectMsg <RespondTemperature>(); response.RequestId.Should().Be(42); response.Value.Should().BeNull(); Console.WriteLine("UserMessage: App is finished."); // Exit the system after ENTER is pressed Console.ReadLine(); } }
public static IEnumerable <object[]> GetProposalTestData() { var testKit = new TestKit(); var operatorTestProbe = testKit.CreateTestProbe(); var machineTestProbe = testKit.CreateTestProbe(); var workerTestProbe = testKit.CreateTestProbe(); var machine2TestProbe = testKit.CreateTestProbe(); OperatorResource = new M_Resource { Name = "Operator", IResourceRef = operatorTestProbe.Ref }; MachineResource = new M_Resource { Name = "Machine", IResourceRef = machineTestProbe.Ref }; WorkerResource = new M_Resource { Name = "Worker", IResourceRef = workerTestProbe.Ref }; MachineResource2 = new M_Resource { Name = "Machine2", IResourceRef = machine2TestProbe.Ref }; _proposalForCapabilityProvider = new ProposalForCapabilityProvider( new M_ResourceCapabilityProvider { Name = "TestCapability", ResourceSetups = new List <M_ResourceSetup> { new M_ResourceSetup { Name = "Operator", UsedInSetup = true, UsedInProcess = false, Resource = OperatorResource, SetupTime = 2 }, new M_ResourceSetup { Name = "Machine", UsedInSetup = true, UsedInProcess = true, Resource = MachineResource }, new M_ResourceSetup { Name = "Worker", UsedInSetup = false, UsedInProcess = true, Resource = WorkerResource }, new M_ResourceSetup { Name = "Machine2", UsedInSetup = true, UsedInProcess = true, Resource = MachineResource2 }, } }); yield return(new object[] { // Test One with Setup new List <FProposal>() { new FProposal(new List <FQueueingScope> { // operator new FQueueingScope(true, false, new FScope(2, 6)), new FQueueingScope(true, false, new FScope(9, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , OperatorResource.IResourceRef as IActorRef , jobKey), new FProposal(new List <FQueueingScope> { // machine new FQueueingScope(true, true, new FScope(3, 8)), new FQueueingScope(true, true, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , MachineResource.IResourceRef as IActorRef , jobKey), new FProposal(new List <FQueueingScope> { // worker new FQueueingScope(true, false, new FScope(1, 1)), new FQueueingScope(true, false, new FScope(4, 8)), new FQueueingScope(true, false, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , WorkerResource.IResourceRef as IActorRef, jobKey), new FProposal(new List <FQueueingScope> { // machine2 new FQueueingScope(true, true, new FScope(4, 8)), new FQueueingScope(true, true, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , MachineResource2.IResourceRef as IActorRef, jobKey), }, /* OperatorResource Start = */ 4L, /* MachineResource = */ 4L, /* Worker Start = */ 6L, "#1 Fit with setup" }); yield return(new object[] { // Test Two without setup new List <FProposal>() { new FProposal(new List <FQueueingScope> { // operator new FQueueingScope(true, false, new FScope(4, 6)), new FQueueingScope(true, false, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , OperatorResource.IResourceRef as IActorRef, jobKey), new FProposal(new List <FQueueingScope> { // machine new FQueueingScope(true, false, new FScope(4, 8)), new FQueueingScope(true, true, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , MachineResource.IResourceRef as IActorRef, jobKey), new FProposal(new List <FQueueingScope> { // worker new FQueueingScope(true, false, new FScope(1, 1)), new FQueueingScope(true, false, new FScope(4, 8)), new FQueueingScope(true, false, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , WorkerResource.IResourceRef as IActorRef, jobKey) }, /* Operator Start = */ 0L, /* Machine Start = */ 4L, /* Worker Start = */ 4, "#2 fit without setup" }); yield return(new object[] { // Test Three NoFit new List <FProposal>() { new FProposal(new List <FQueueingScope> { // operator new FQueueingScope(true, false, new FScope(5, 7)), new FQueueingScope(true, false, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , OperatorResource.IResourceRef as IActorRef , jobKey), new FProposal(new List <FQueueingScope> { // machine new FQueueingScope(true, true, new FScope(3, 6)), new FQueueingScope(true, true, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , MachineResource.IResourceRef as IActorRef, jobKey), new FProposal(new List <FQueueingScope> { // worker new FQueueingScope(true, false, new FScope(1, 1)), new FQueueingScope(true, false, new FScope(4, 6)), new FQueueingScope(true, false, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , WorkerResource.IResourceRef as IActorRef, jobKey) }, /* Operator Start = */ 10L, /* Machine Start = */ 10L, /* Worker Start = */ 12, "#3 No fit" }); yield return(new object[] { // Fits with Setup but setup pushes scope new List <FProposal>() { new FProposal(new List <FQueueingScope> { // operator new FQueueingScope(true, false, new FScope(4, 7)), new FQueueingScope(true, false, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , OperatorResource.IResourceRef as IActorRef, jobKey), new FProposal(new List <FQueueingScope> { // machine new FQueueingScope(true, true, new FScope(3, 8)), new FQueueingScope(true, true, new FScope(11, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , MachineResource.IResourceRef as IActorRef, jobKey), new FProposal(new List <FQueueingScope> { // worker new FQueueingScope(true, false, new FScope(2, 8)), new FQueueingScope(true, false, new FScope(11, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , WorkerResource.IResourceRef as IActorRef, jobKey) }, /* Operator Start = */ 4L, /* Machine Start = */ 4L, /* Worker Start = */ 6, "#4 Fit with Setup but setup pushes scope" }); yield return(new object[] { // Exact Fit new List <FProposal>() { new FProposal(new List <FQueueingScope> { // operator new FQueueingScope(true, false, new FScope(3, 5)), new FQueueingScope(true, false, new FScope(9, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , OperatorResource.IResourceRef as IActorRef, jobKey), new FProposal(new List <FQueueingScope> { // machine new FQueueingScope(true, true, new FScope(3, 7)), new FQueueingScope(true, true, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , MachineResource.IResourceRef as IActorRef, jobKey), new FProposal(new List <FQueueingScope> { // worker new FQueueingScope(true, false, new FScope(5, 7)), new FQueueingScope(true, false, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , WorkerResource.IResourceRef as IActorRef, jobKey) }, /* Operator Start = */ 3L, /* Machine Start = */ 3L, /* Worker Start = */ 5L, "#5 Exact Fit" }); yield return(new object[] { // Detached Setup new List <FProposal>() { new FProposal(new List <FQueueingScope> { // operator new FQueueingScope(true, false, new FScope(2, 3)), new FQueueingScope(true, false, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , OperatorResource.IResourceRef as IActorRef, jobKey), new FProposal(new List <FQueueingScope> { // machine new FQueueingScope(true, true, new FScope(3, 8)), new FQueueingScope(true, true, new FScope(11, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , MachineResource.IResourceRef as IActorRef, jobKey), new FProposal(new List <FQueueingScope> { // worker new FQueueingScope(true, false, new FScope(5, 7)), new FQueueingScope(true, false, new FScope(10, long.MaxValue)) } , new FPostponeds.FPostponed(0) , _proposalForCapabilityProvider.ProviderId , WorkerResource.IResourceRef as IActorRef, jobKey) }, /* Operator Start = */ 11L, /* Machine Start = */ 11L, /* Worker Start = */ 13L, "#6 Detached Setup" }); }
static void Main(string[] args) { TestKit testKit = new TestKit(); ActorSystem actorSystem = testKit.Sys; using (actorSystem) { TestProbe probe = testKit.CreateTestProbe("test-probe"); Action test1 = new Action(() => { IActorRef groupActor = actorSystem.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); IActorRef deviceActor1 = probe.LastSender; groupActor.Tell(new RequestTrackDevice("group", "device2"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); IActorRef deviceActor2 = probe.LastSender; deviceActor1.Should().NotBe(deviceActor2); // Check that the device actors are working deviceActor1.Tell(new RecordTemperature(requestId: 0, value: 1.0), probe.Ref); probe.ExpectMsg <TemperatureRecorded>(s => s.RequestId == 0); deviceActor2.Tell(new RecordTemperature(requestId: 1, value: 2.0), probe.Ref); probe.ExpectMsg <TemperatureRecorded>(s => s.RequestId == 1); Console.WriteLine("Test 1 passed."); Console.WriteLine(""); }); test1.Invoke(); Action test2 = new Action(() => { IActorRef groupActor = actorSystem.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("wrongGroup", "device1"), probe.Ref); probe.ExpectNoMsg(TimeSpan.FromMilliseconds(500)); Console.WriteLine("Test 2 passed."); Console.WriteLine(""); }); test2.Invoke(); /*It might be, that a device actor already exists for the registration request. * In this case, we would like to use the existing actor instead of a new one. * We have not tested this yet, so we need to fix this:*/ Action test3 = new Action(() => { IActorRef groupActor = actorSystem.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); IActorRef deviceActor1 = probe.LastSender; groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); IActorRef deviceActor2 = probe.LastSender; deviceActor1.Should().Be(deviceActor2); Console.WriteLine("Test 3 passed."); Console.WriteLine(""); }); test3.Invoke(); /*In the first, we just test that we get back the list of * proper IDs once we have added a few devices.*/ Action test4 = new Action(() => { IActorRef groupActor = actorSystem.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); groupActor.Tell(new RequestTrackDevice("group", "device2"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); groupActor.Tell(new RequestDeviceList(requestId: 0), probe.Ref); probe.ExpectMsg <ReplyDeviceList>(s => s.RequestId == 0 && s.Ids.Contains("device1") && s.Ids.Contains("device2")); Console.WriteLine("Test 4 passed."); Console.WriteLine(""); }); test4.Invoke(); /*The second test case makes sure that the device ID * is properly removed after the device actor has been stopped.*/ Action test5 = new Action(() => { IActorRef groupActor = actorSystem.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); IActorRef toShutDown = probe.LastSender; groupActor.Tell(new RequestTrackDevice("group", "device2"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); groupActor.Tell(new RequestDeviceList(requestId: 0), probe.Ref); probe.ExpectMsg <ReplyDeviceList>(s => s.RequestId == 0 && s.Ids.Contains("device1") && s.Ids.Contains("device2")); probe.Watch(toShutDown); toShutDown.Tell(PoisonPill.Instance); probe.ExpectTerminated(toShutDown); // using awaitAssert to retry because it might take longer for the groupActor // to see the Terminated, that order is undefined probe.AwaitAssert(() => { groupActor.Tell(new RequestDeviceList(requestId: 1), probe.Ref); probe.ExpectMsg <ReplyDeviceList>(s => s.RequestId == 1 && s.Ids.Contains("device2")); }); Console.WriteLine("Test 5 passed."); Console.WriteLine(""); }); test5.Invoke(); Console.WriteLine("UserMessage: App is finished."); // Exit the system after ENTER is pressed Console.ReadLine(); } }
static void Main(string[] args) { TestKit testKit = new TestKit(); ActorSystem actorSystem = testKit.Sys; using (actorSystem) { TestProbe probe = testKit.CreateTestProbe("test-probe"); Action test1 = new Action(() => { IActorRef groupActor = actorSystem.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); IActorRef deviceActor1 = probe.LastSender; groupActor.Tell(new RequestTrackDevice("group", "device2"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); IActorRef deviceActor2 = probe.LastSender; deviceActor1.Should().NotBe(deviceActor2); // Check that the device actors are working deviceActor1.Tell(new RecordTemperature(requestId: 0, value: 1.0), probe.Ref); probe.ExpectMsg <TemperatureRecorded>(s => s.RequestId == 0); deviceActor2.Tell(new RecordTemperature(requestId: 1, value: 2.0), probe.Ref); probe.ExpectMsg <TemperatureRecorded>(s => s.RequestId == 1); Console.WriteLine("Test 1 passed."); Console.WriteLine(""); }); test1.Invoke(); Action test2 = new Action(() => { IActorRef groupActor = actorSystem.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("wrongGroup", "device1"), probe.Ref); probe.ExpectNoMsg(TimeSpan.FromMilliseconds(500)); Console.WriteLine("Test 2 passed."); Console.WriteLine(""); }); test2.Invoke(); /*It might be, that a device actor already exists for the registration request. * In this case, we would like to use the existing actor instead of a new one. * We have not tested this yet, so we need to fix this:*/ Action test3 = new Action(() => { IActorRef groupActor = actorSystem.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); IActorRef deviceActor1 = probe.LastSender; groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); IActorRef deviceActor2 = probe.LastSender; deviceActor1.Should().Be(deviceActor2); Console.WriteLine("Test 3 passed."); Console.WriteLine(""); }); test3.Invoke(); Console.WriteLine("UserMessage: App is finished."); // Exit the system after ENTER is pressed Console.ReadLine(); } }
static void Main(string[] args) { TestKit testKit = new TestKit(); ActorSystem actorSystem = testKit.Sys; using (actorSystem) { TestProbe probe = testKit.CreateTestProbe("test-probe"); /*In the first, we just test that we get back the list of * proper IDs once we have added a few devices.*/ Action test1 = new Action(() => { IActorRef deviceManagerActor = actorSystem.ActorOf(DeviceManager.Props("device-manager")); deviceManagerActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); deviceManagerActor.Tell(new RequestTrackDevice("group", "device2"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); deviceManagerActor.Tell(new RequestDeviceList(requestId: 0), probe.Ref); probe.ExpectMsg <ReplyDeviceList>(s => s.RequestId == 0 && s.Ids.Contains("device1") && s.Ids.Contains("device2")); Console.WriteLine("Test 1 passed."); Console.WriteLine(""); }); test1.Invoke(); /*The second test case makes sure that the device ID * is properly removed after the device actor has been stopped.*/ Action test2 = new Action(() => { IActorRef deviceManagerActor = actorSystem.ActorOf(DeviceManager.Props("device-manager")); deviceManagerActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); IActorRef toShutDown = probe.LastSender; deviceManagerActor.Tell(new RequestTrackDevice("group", "device2"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); deviceManagerActor.Tell(new RequestDeviceList(requestId: 0), probe.Ref); probe.ExpectMsg <ReplyDeviceList>(s => s.RequestId == 0 && s.Ids.Contains("device1") && s.Ids.Contains("device2")); probe.Watch(toShutDown); toShutDown.Tell(PoisonPill.Instance); probe.ExpectTerminated(toShutDown); // using awaitAssert to retry because it might take longer for the groupActor // to see the Terminated, that order is undefined probe.AwaitAssert(() => { deviceManagerActor.Tell(new RequestDeviceList(requestId: 1), probe.Ref); probe.ExpectMsg <ReplyDeviceList>(s => s.RequestId == 1 && s.Ids.Contains("device2")); }); Console.WriteLine("Test 2 passed."); Console.WriteLine(""); }); test2.Invoke(); Console.WriteLine("UserMessage: App is finished."); // Exit the system after ENTER is pressed Console.ReadLine(); } }