Commits

Joel Ross committed f3c3e39

Updated build status changed events to only be fired when the build status changes, and not on every check.

Comments (0)

Files changed (2)

src/Core/Services/ProjectService.cs

     public class ProjectService : IProjectService
     {
         private readonly IProjectStatusResolverService projectStatusResolverService;
+        private BuildStatus existingBuildStatus = BuildStatus.Unknown;
 
         public ProjectService(IProjectStatusResolverService projectStatusResolverService)
         {
 
             }
 
+            if (existingBuildStatus == buildStatus) return;
+
+            existingBuildStatus = buildStatus;
             DomainEvents.Raise(new BuildStatusChanged(buildStatus));
         }
     }

src/Tests/Services/ProjectServiceSpecs.cs

 using System.Collections.Generic;
 using Moq;
 using NUnit.Framework;
+using RossCode.TrafficLight.Core.Eventing;
+using RossCode.TrafficLight.Core.Eventing.Events;
 using RossCode.TrafficLight.Core.Services;
 using RossCode.TrafficLight.Tests.Utilties;
 using RossCode.TrafficLight.Core.Domain;
             {
                 Assert.That(Project.CurrentStatus, Is.EqualTo(BuildStatus.Building));
             }
+
+            [Test]
+            public void it_should_call_the_callback()
+            {
+                Assert.That(CallbackCalled, Is.EqualTo(1));
+            }
+        }
+
+        public class when_retrieving_the_same_project_status_twice : under.the_default_context
+        {
+            protected override void When()
+            {
+                SUT.CheckAndSetStatusesFor(Projects);
+                SUT.CheckAndSetStatusesFor(Projects);
+            }
+
+            [Test]
+            public void it_should_mark_the_project_as_building()
+            {
+                Assert.That(Project.CurrentStatus, Is.EqualTo(BuildStatus.Building));
+            }
+
+            [Test]
+            public void it_should_call_the_callback_only_once()
+            {
+                Assert.That(CallbackCalled, Is.EqualTo(1));
+            }
         }
 
         public static class under
                 protected IList<Project> Projects;
                 protected Project Project;
                 protected Mock<IProjectStatusResolverService> ProjectStatusResolver;
+                protected int CallbackCalled;
 
                 protected override void EstablishContext()
                 {
                     ProjectStatusResolver = new Mock<IProjectStatusResolverService>();
                     ProjectStatusResolver.Setup(resolver => resolver.CheckStatus(Project)).Returns(BuildStatus.Building);
 
+                    DomainEvents.Register<BuildStatusChanged>(BuildStatusChanged);
+
                     SUT = new ProjectService(ProjectStatusResolver.Object);
                 }
+
+                private void BuildStatusChanged(BuildStatusChanged obj)
+                {
+                    CallbackCalled++;
+                }
             }
         }
     }