public void DeviceGroup_WhenOneDeviceShutdown_AndRequestDeviceList_ThenReturnsActiveDevices() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props(_defaultGroup, _secondsWaitingForReplies)); groupActor.Tell(new RequestTrackDevice(_defaultGroup, "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var toShutDown = probe.LastSender; groupActor.Tell(new RequestTrackDevice(_defaultGroup, "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.Count == 1 && s.Ids.Contains("device2")); }); }
public void Device_group_must_return_temperature_for_all_devices() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props("group1")); groupActor.Tell(new RequestTrackDevice("group1", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(o => true); var deviceActor1 = probe.LastSender; groupActor.Tell(new RequestTrackDevice("group1", "device2"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var deviceActor2 = probe.LastSender; groupActor.Tell(new RequestTrackDevice("group1", "device3"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var deviceActor3 = probe.LastSender; deviceActor1.Tell(new RecordTemperature(requestId: 1, value: 25.0), probe.Ref); probe.ExpectMsg <TemperatureRecorded>(r => r.RequestId == 1); deviceActor2.Tell(new RecordTemperature(requestId: 2, value: 75.0), probe.Ref); probe.ExpectMsg <TemperatureRecorded>(r => r.RequestId == 2); // Get all devices temperature. groupActor.Tell(new RequestAllTemperatures(requestId: 3), probe.Ref); probe.ExpectMsg <RespondAllTemperatures>( r => r.Temperatures["device1"].AsInstanceOf <Temperature>().Value == 25.0 && r.Temperatures["device2"].AsInstanceOf <Temperature>().Value == 75.0 && r.Temperatures["device3"] is TemperatureNotAvailable && r.RequestId == 3 ); }
public void DeviceGroup_actor_must_be_able_to_collect_temperatures_from_all_active_devices() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var deviceActor1 = probe.LastSender; groupActor.Tell(new RequestTrackDevice("group", "device2"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var deviceActor2 = probe.LastSender; groupActor.Tell(new RequestTrackDevice("group", "device3"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var deviceActor3 = probe.LastSender; // 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); // No temperature for device3 groupActor.Tell(new RequestAllTemperatures(0), probe.Ref); probe.ExpectMsg <RespondAllTemperatures>(msg => msg.Temperatures["device1"].AsInstanceOf <Temperature>().Value == 1.0 && msg.Temperatures["device2"].AsInstanceOf <Temperature>().Value == 2.0 && msg.Temperatures["device3"] is TemperatureNotAvailable && msg.RequestId == 0); }
public void DeviceGroup_actor_must_be_able_to_list_active_devices_after_one_shuts_down() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var 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")); }); }
protected override void OnReceive(object message) { switch (message) { case RequestTrackDevice trackMsg: if (groupIdToActor.TryGetValue(trackMsg.GroupId, out var actorRef)) { actorRef.Forward(trackMsg); } else { Log.Info($"Creating device group actor for {trackMsg.GroupId}"); var groupActor = Context.ActorOf(DeviceGroup.Props(trackMsg.GroupId), $"group-{trackMsg.GroupId}"); Context.Watch(groupActor); groupActor.Forward(trackMsg); groupIdToActor.Add(trackMsg.GroupId, groupActor); actorToGroupId.Add(groupActor, trackMsg.GroupId); } break; case Terminated t: var groupId = actorToGroupId[t.ActorRef]; Log.Info($"Device group actor for {groupId} has been terminated"); actorToGroupId.Remove(t.ActorRef); groupIdToActor.Remove(groupId); break; } }
public void DeviceGroup_actor_must_ignore_requests_for_wrong_groupId() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("wrongGroup", "device1"), probe.Ref); probe.ExpectNoMsg(TimeSpan.FromMilliseconds(500)); }
public void Device_group_should_ignore_device_registration_requests_with_wrong_group_id() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props("Group1")); groupActor.Tell(new RequestTrackDevice("Group2", "Device1"), probe.Ref); probe.ExpectNoMsg(TimeSpan.FromMilliseconds(value: 500)); }
public void DeviceGroup_WhenRequestTrackDeviceWithGroupId_ThenActorIgnoresRequest() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props(_defaultGroup, _secondsWaitingForReplies)); groupActor.Tell(new RequestTrackDevice("wrongGroup", "device1"), probe.Ref); probe.ExpectNoMsg(TimeSpan.FromMilliseconds(500)); }
public void DeviceGroup_actor_must_be_able_to_list_active_devices() { var probe = CreateTestProbe(); var groupActor = Sys.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")); }
public void DeviceGroup_actor_must_return_same_actor_for_same_deviceId() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var deviceActor1 = probe.LastSender; groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var deviceActor2 = probe.LastSender; deviceActor1.Should().Be(deviceActor2); }
public void DeviceGroup_WhenMultipleRequestTrackDeviceForSameDevice_ThenReturnsSameActor() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props(_defaultGroup, _secondsWaitingForReplies)); groupActor.Tell(new RequestTrackDevice(_defaultGroup, "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var deviceActor1 = probe.LastSender; groupActor.Tell(new RequestTrackDevice(_defaultGroup, "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var deviceActor2 = probe.LastSender; deviceActor1.Should().Be(deviceActor2); }
public void Device_group_should_return_the_same_actor_for_the_same_device_id() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props("Group1")); groupActor.Tell(new RequestTrackDevice("Group1", "Device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var device1Actor = probe.LastSender; groupActor.Tell(new RequestTrackDevice("Group1", "Device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var device2Actor = probe.LastSender; device2Actor.ShouldBe(device1Actor); }
public void DeviceGroup_WhenRequestDeviceListAndMultipleRegistered_ThenReturnsThem() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props(_defaultGroup, _secondsWaitingForReplies)); groupActor.Tell(new RequestTrackDevice(_defaultGroup, "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); groupActor.Tell(new RequestTrackDevice(_defaultGroup, "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")); }
public void Device_group_should_be_able_to_list_devices() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props("Group1")); var deviceIds = new HashSet <string> { "Device1", "Device2" }; foreach (var deviceId in deviceIds) { groupActor.Tell(new RequestTrackDevice("Group1", deviceId), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); } groupActor.Tell(new RequestDeviceList(requestId: 128), probe.Ref); probe.ExpectMsg <ReplyDeviceList>(r => r.RequestId == 128 && deviceIds.SetEquals(r.DeviceIds)); }
public void DeviceGroup_actor_must_be_able_to_list_active_devices() { var sys = ActorSystem.Create("mysys"); var probe = new TestKit().CreateTestProbe(); var groupActor = sys.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var device1 = probe.LastSender; groupActor.Tell(new RequestTrackDevice("group", "device2"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var device2 = probe.LastSender; groupActor.Tell(new RequestDeviceList(requestId: 0), probe.Ref); probe.ExpectMsg <ReplyDeviceList>(rep => rep.RequestId == 0 && rep.Ids.Contains("device1") && rep.Ids.Contains("device2")); }
public void DeviceGroup_WhenRequestTrackDevice_ThenDeviceActorRegistered() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props(_defaultGroup, _secondsWaitingForReplies)); groupActor.Tell(new RequestTrackDevice(_defaultGroup, "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var deviceActor1 = probe.LastSender; groupActor.Tell(new RequestTrackDevice(_defaultGroup, "device2"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var 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); }
public void Device_group_should_accept_device_registration_requests() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props("Group1")); groupActor.Tell(new RequestTrackDevice("Group1", "Device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var device1Actor = probe.LastSender; groupActor.Tell(new RequestTrackDevice("Group1", "Device2"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var device2Actor = probe.LastSender; device2Actor.ShouldNotBe(device1Actor); // Check that devices are working. device1Actor.Tell(new RecordTemperature(requestId: 55, value: 42.0), probe.Ref); probe.ExpectMsg <TemperatureRecorded>(r => r.RequestId == 55); device2Actor.Tell(new RecordTemperature(requestId: 56, value: 42.0), probe.Ref); probe.ExpectMsg <TemperatureRecorded>(r => r.RequestId == 56); }
public void DeviceGroup_actor_must_be_able_to_register_a_device_actor() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props("group")); groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var deviceActor1 = probe.LastSender; groupActor.Tell(new RequestTrackDevice("group", "device2"), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); var 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); }
public void Device_group_should_be_able_to_list_devices_after_one_shutdown() { var probe = CreateTestProbe(); var groupActor = Sys.ActorOf(DeviceGroup.Props("Group1")); var deviceIds = new HashSet <string> { "Device1", "Device2" }; var deviceToShutdownId = deviceIds.First(); IActorRef deviceToShutdown = null; foreach (var deviceId in deviceIds) { groupActor.Tell(new RequestTrackDevice("Group1", deviceId), probe.Ref); probe.ExpectMsg <DeviceRegistered>(); if (deviceId == deviceToShutdownId) { deviceToShutdown = probe.LastSender; } } // Check devices after registration. groupActor.Tell(new RequestDeviceList(requestId: 128), probe.Ref); probe.ExpectMsg <ReplyDeviceList>(r => r.RequestId == 128 && deviceIds.SetEquals(r.DeviceIds)); deviceIds.Remove(deviceToShutdownId); probe.Watch(deviceToShutdown); deviceToShutdown.Tell(PoisonPill.Instance); probe.ExpectTerminated(deviceToShutdown); // Check devices after termination. probe.AwaitAssert(() => { groupActor.Tell(new RequestDeviceList(requestId: 129), probe.Ref); probe.ExpectMsg <ReplyDeviceList>(r => r.RequestId == 129 && deviceIds.SetEquals(r.DeviceIds)); }); }
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"); 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(); } }