Example #1
0
        public void ShouldStartTrafficLightCycle()
        {
            var configuration = new TrafficLightConfig();

            configuration.Add(TrafficLightState.Go, 4);
            configuration.Add(TrafficLightState.Stop, 4);
            SetTrafficLightConfiguration(configuration);

            var lights = new TrafficLights {
                _l1, _l2, _l3, _l4
            };
            var timer = new TrafficLightTimer();

            var tc1 = new TetheredTrafficLightCluster {
                _l1, _l2
            };

            tc1.Name = "TC-1";

            var pc1 = new PolarTrafficLightCluster(_l3)
            {
                { _l4 }
            };

            pc1.Name = "TC-2";

            Assert.IsTrue(lights.Add(tc1));
            Assert.IsTrue(lights.Add(pc1));

            var controller = new TrafficLightController(lights, timer);

            //Assert initial state is where expected
            Assert.AreEqual(TrafficLightState.StopThenGo, _l1.State);
            Assert.AreEqual(TrafficLightState.StopThenGo, _l2.State);
            Assert.AreEqual(TrafficLightState.StopThenGo, _l3.State);
            Assert.AreEqual(TrafficLightState.StopThenGo, _l4.State);

            controller.Start();  //Will trigger traffic light state changes

            Thread.Sleep(2000);
            Assert.AreEqual(TrafficLightState.Go, _l1.State);
            Assert.AreEqual(TrafficLightState.Go, _l2.State);
            Assert.AreEqual(TrafficLightState.Go, _l3.State);
            Assert.AreEqual(TrafficLightState.Stop, _l4.State);

            Thread.Sleep(5000);
            Assert.AreEqual(TrafficLightState.Stop, _l1.State);
            Assert.AreEqual(TrafficLightState.Stop, _l2.State);
            Assert.AreEqual(TrafficLightState.Stop, _l3.State);
            Assert.AreEqual(TrafficLightState.Go, _l4.State);

            controller.Stop();
        }
Example #2
0
        public void ShouldRaiseEventWhenClusterRequiresUpdating()
        {
            var configuration = new TrafficLightConfig();

            configuration.Add(TrafficLightState.Go, 3);

            var light = new TrafficLight
            {
                Name          = "State Expired Test Light",
                Configuration = configuration
            };

            var cluster = new TetheredTrafficLightCluster {
                light
            };

            var receivedEvents = new List <TetheredClusterRequiresUpdateEventArg>();

            cluster.ClusterRequiresUpdateEvent += (sender, e)
                                                  => receivedEvents.Add(e);

            light.State = TrafficLightState.Go;
            Thread.Sleep(4000);

            Assert.AreEqual(1, receivedEvents.Count);
        }
Example #3
0
        public void ShouldRevertToSafeStateAfterStateTimerExpires()
        {
            var config = new TrafficLightConfig();

            config.Add(TrafficLightState.Go, 1); //Set to one second

            var light = new TrafficLight {
                Configuration = config, State = TrafficLightState.Go
            };

            Assert.AreEqual(TrafficLightState.Go, light.State);

            Thread.Sleep(7000);    //Wait for fail-safe to kick in

            Assert.AreEqual(TrafficLightState.StopThenGo, light.State);
        }
Example #4
0
        public void ShouldRaiseEventWhenStateExpires()
        {
            var configuration = new TrafficLightConfig();

            configuration.Add(TrafficLightState.Go, 3);

            var light = new TrafficLight
            {
                Name          = "State Expired Test Light",
                Configuration = configuration
            };

            var receivedEvents = new List <TrafficLightStateExpirationEventArgs>();

            light.StateExpirationEvent += (sender, e)
                                          => receivedEvents.Add(e);

            light.State = TrafficLightState.Go;
            Thread.Sleep(4000);

            Assert.AreEqual(1, receivedEvents.Count);
            Assert.AreEqual(light.Id, receivedEvents[0].TrafficLightId);
        }