Ejemplo n.º 1
0
        public void DeviceGroup_actor_must_be_able_to_list_active_devices_after_one_shuts_down()
        {
            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 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") &&
                                                  !s.Ids.Contains("device1"));
            });
        }
Ejemplo n.º 2
0
        public void DeviceGroup_actor_must_return_same_actor_for_same_deviceId()
        {
            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 deviceActor1 = probe.LastSender;

            groupActor.Tell(new RequestTrackDevice("group", "device1"), probe.Ref);
            probe.ExpectMsg <DeviceRegistered>();
            var deviceActor2 = probe.LastSender;

            deviceActor1.Should().Be(deviceActor2);
        }
Ejemplo n.º 3
0
        public void DeviceGroupQuery_must_return_return_DeviceNotAvailable_if_device_stops_before_answering()
        {
            var sys       = ActorSystem.Create("mysys");
            var requester = new TestKit().CreateTestProbe();
            var device1   = new TestKit().CreateTestProbe();
            var device2   = new TestKit().CreateTestProbe();

            var queryActor = sys.ActorOf(DeviceGroupQuery.Props(
                                             actorToDeviceId: new Dictionary <IActorRef, string> {
                [device1.Ref] = "device1", [device2.Ref] = "device2"
            },
                                             requestId: 1,
                                             requester: requester.Ref,
                                             timeout: TimeSpan.FromSeconds(3)
                                             ));

            device1.ExpectMsg <ReadTemperature>(read => read.RequestId == 0);
            device2.ExpectMsg <ReadTemperature>(read => read.RequestId == 0);

            queryActor.Tell(new RespondTemperature(requestId: 0, value: 1.0), device1.Ref);
            device2.Tell(PoisonPill.Instance);

            requester.ExpectMsg <RespondAllTemperatures>(msg =>
                                                         msg.Temperatures["device1"].AsInstanceOf <Temperature>().Value == 1.0 &&
                                                         msg.Temperatures["device2"] is DeviceNotAvailable &&
                                                         msg.RequestId == 1);
        }
Ejemplo n.º 4
0
        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"));
        }
Ejemplo n.º 5
0
        public void Device_actor_must_reply_to_registration_requests()
        {
            var sys         = ActorSystem.Create("mysys");
            var probe       = new TestKit().CreateTestProbe();
            var deviceActor = sys.ActorOf(Device.Props("group", "device"));

            deviceActor.Tell(new RequestTrackDevice("group", "device"), probe.Ref);
            probe.ExpectMsg <DeviceRegistered>();
            probe.LastSender.Should().Be(deviceActor);
        }
Ejemplo n.º 6
0
        public T[] WaitFor <T>(TestKit kit, int number)
        {
            var resultMessages = new List <T>();

            for (var num = 0; num < number; num++)
            {
                resultMessages.Add(kit.ExpectMsg <T>(TimeSpan.FromSeconds(10)));
            }

            return(resultMessages.ToArray());
        }
Ejemplo n.º 7
0
        public void Device_actor_must_reply_with_empty_reading_if_no_temperature_is_known()
        {
            var sys         = ActorSystem.Create("mysys");
            var probe       = new TestKit().CreateTestProbe();
            var deviceActor = sys.ActorOf(Device.Props("group", "device"));

            deviceActor.Tell(new ReadTemperature(requestId: 42), probe.Ref);
            var response = probe.ExpectMsg <RespondTemperature>();

            response.RequestId.Should().Be(42);
            response.Value.Should().BeNull();
        }
Ejemplo n.º 8
0
        public void DeviceGroup_actor_must_be_able_to_collect_temperatures_from_all_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 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);
        }
Ejemplo n.º 9
0
        public void DeviceGroup_actor_must_be_able_to_register_a_device_actor()
        {
            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 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);
        }
Ejemplo n.º 10
0
        public void Device_actor_must_reply_with_latest_temperature_reading()
        {
            var sys         = ActorSystem.Create("mysys");
            var probe       = new TestKit().CreateTestProbe();
            var deviceActor = sys.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);
            var 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);
            var response2 = probe.ExpectMsg <RespondTemperature>();

            response2.RequestId.Should().Be(4);
            response2.Value.Should().Be(55.0);
        }