public void TestHandleCommandForPriorityAttentionRequest()
        {
            UserCache userCache = null;
            NotificationManager notificationManager = null;

            try
            {
                // Setup the notification manager
                object syncRoot = new object();
                int serverPort = 20000;
                int clientPort = 20001;

                // If the timer's expiry (trigger) period is too fast, the test will fail in step 2.
                // We set this (hopefully) sufficiently slow so that we can properly test the
                // sequence of events.
                int priorityTimerPeriod = 10000;

                // Any user's attention required will immediately be set as a priority.
                TimeSpan priorityPeriod = TimeSpan.Zero;

                // Note: The cache's timer starts upon construction
                userCache = new UserCache(priorityTimerPeriod, priorityPeriod);
                notificationManager = new NotificationManager(serverPort, clientPort, userCache, false);

                // A dummy client which the notification manager will notify
                Listener listener = new Listener(IPAddress.Any, clientPort);

                // We only want the attention requests
                AttentionRequest attentionRequest = null;
                listener.OnCommandReceived += (sender, args) =>
                {
                    if (sender.GetType() == typeof(AttentionRequest))
                    {
                        attentionRequest = (AttentionRequest)sender;
                    }

                    lock (syncRoot)
                    {
                        Monitor.Pulse(syncRoot);
                    }
                };

                // Create the requests and notifications
                string username = "******";
                string hostname = "localhost";
                string projectId = "project1";
                string buildConfigId = "buildConfig1";
                string state = BuildServerResponsibilityState.Taken;
                RegistrationRequest registrationRequest = new RegistrationRequest(hostname, username);
                ResponsibilityNotification responsibilityNotification = new ResponsibilityNotification(BuildServerNotificationType.BuildResponsibilityAssigned, projectId, buildConfigId, username, state);

                // Start the server and the client
                notificationManager.Start();
                Assert.That(notificationManager.Running, Is.True);
                listener.Start();
                Assert.That(listener.Running, Is.True);

                // STEP 1
                // Register the dummy client with the server
                lock (syncRoot)
                {
                    // This will cause one attention request and one build active request
                    Utils.SendCommand(hostname, serverPort, Parser.Encode(registrationRequest));
                    Monitor.Wait(syncRoot);
                }

                Assert.That(attentionRequest, Is.Not.Null);
                Assert.That(attentionRequest.IsAttentionRequired, Is.False);
                Assert.That(attentionRequest.IsPriority, Is.False);

                // STEP 2
                attentionRequest = null;
                lock (syncRoot)
                {
                    // This will cause another attention request and another build active request
                    notificationManager.HandleCommand(responsibilityNotification, EventArgs.Empty);
                    Monitor.Wait(syncRoot);
                }

                Assert.That(attentionRequest, Is.Not.Null);
                Assert.That(attentionRequest != null && attentionRequest.IsAttentionRequired, Is.True);
                Assert.That(attentionRequest != null && attentionRequest.IsPriority, Is.False);

                // STEP 3
                // The next two pulses (again for an attention and a build active request) must
                // be because of the timer that expired
                attentionRequest = null;
                lock (syncRoot)
                {
                    Monitor.Wait(syncRoot);
                }

                // Shut down
                notificationManager.Stop();
                Assert.That(notificationManager.Running, Is.False);
                listener.Stop();
                Assert.That(listener.Running, Is.False);

                // Test
                Assert.That(attentionRequest, Is.Not.Null);
                Assert.That(attentionRequest != null && attentionRequest.IsAttentionRequired, Is.True);
                Assert.That(attentionRequest != null && attentionRequest.IsPriority, Is.True);
            }
            finally
            {
                if (notificationManager != null)
                {
                    notificationManager.Dispose();
                }

                if (userCache != null)
                {
                    userCache.Dispose();
                }
            }
        }
        public void TestHandleCommandForResponsibilityNotificationRaisesCommandReceivedEvent()
        {
            // Setup
            Listener listener = new Listener(IPAddress.Any, 0);
            IBuildServerNotification actualNotification = null;
            listener.OnCommandReceived += delegate(object sender, EventArgs args) { actualNotification = (IBuildServerNotification)sender; };
            BuildServerNotificationType notificationType = BuildServerNotificationType.BuildResponsibilityAssigned;
            string projectId = "project1";
            string buildConfigId = "buildconfig1";
            string username = "******";
            string state = BuildServerResponsibilityState.Taken;
            ResponsibilityNotification actualResponsibilityNotification = new ResponsibilityNotification(notificationType, projectId, buildConfigId, username, state);
            string command = Parser.Encode(actualResponsibilityNotification);

            // Fire
            listener.HandleCommand(command);

            // Test
            Assert.That(actualNotification, Is.Not.Null);
            Assert.That(actualNotification, Is.InstanceOf(typeof(ResponsibilityNotification)));
            ResponsibilityNotification expectedResponsibilityNotification = (ResponsibilityNotification)actualNotification;
            Assert.That(expectedResponsibilityNotification.Type, Is.EqualTo(notificationType));
            Assert.That(expectedResponsibilityNotification.ProjectId.Equals(projectId));
            Assert.That(expectedResponsibilityNotification.BuildConfigId.Equals(buildConfigId));
            Assert.That(expectedResponsibilityNotification.Recipient, Is.EqualTo(username));
            Assert.That(expectedResponsibilityNotification.State, Is.EqualTo(state));
        }
        public void TestHandleResponsibilityNotification()
        {
            UserCache userCache = null;
            NotificationManager notificationManager = null;
            try
            {
                userCache = new UserCache();
                notificationManager = new NotificationManager(0, 0, userCache, false);
                string username = "******";
                BuildServerNotificationType notificationType = BuildServerNotificationType.BuildResponsibilityAssigned;
                string state = BuildServerResponsibilityState.Taken;
                string projectId = "project1";
                string buildConfigId = "buildconfig1";
                IBuildServerNotification buildServerNotification = new ResponsibilityNotification(notificationType, projectId, buildConfigId, username, state);
                string buildKey = Utils.CreateBuildKey(buildServerNotification);
                notificationManager.HandleCommand(buildServerNotification, EventArgs.Empty);
                User user = userCache.GetUser(username);
                Assert.That(user.Username, Is.EqualTo(username));
                Assert.That(user.BuildsResponsibleFor.Contains(buildKey));
            }
            finally
            {
                if (notificationManager != null)
                {
                    notificationManager.Dispose();
                }

                if (userCache != null)
                {
                    userCache.Dispose();
                }
            }
        }
        public void TestUpdateUserBothResponsibleAndHasActiveBuild()
        {
            UserCache userCache = null;
            try
            {
                // Setup
                userCache = new UserCache();
                User actualUser = null;
                userCache.OnUpdate += delegate(object sender, EventArgs args) { actualUser = (User)sender; };
                string username = "******";
                string projectId = "project1";

                // First notification
                BuildServerNotificationType notificationType1 = BuildServerNotificationType.BuildResponsibilityAssigned;
                string buildConfigId1 = "buildconfig1";
                string state = BuildServerResponsibilityState.Taken;
                IBuildServerNotification notification1 = new ResponsibilityNotification(notificationType1, projectId, buildConfigId1, username, state);
                userCache.Update(notification1);

                // Second notification
                BuildServerNotificationType notificationType2 = BuildServerNotificationType.BuildBuilding;
                string buildConfigId2 = "buildconfig2";
                string[] recipients = { username };
                IBuildServerNotification notification2 = new BuildNotification(notificationType2, projectId, buildConfigId2, recipients);
                userCache.Update(notification2);

                // Test
                Assert.That(actualUser, NUnit.Framework.Is.Not.Null);
                Assert.That(actualUser.Username, NUnit.Framework.Is.EqualTo(username));
                Assert.That(actualUser.IsBuildActive(), NUnit.Framework.Is.True);
                Assert.That(actualUser.IsAttentionRequired(), NUnit.Framework.Is.True);
            }
            finally
            {
                if (userCache != null)
                {
                    userCache.Dispose();
                }
            }
        }
 public void TestUpdateWithResponsibilityTaken([Values(BuildServerNotificationType.BuildResponsibilityAssigned, BuildServerNotificationType.TestResponsibilityAssigned)] BuildServerNotificationType notificationType)
 {
     UserCache userCache = null;
     try
     {
         string projectId = "project1";
         string buildConfigId = "buildconfig1";
         string username = "******";
         string recipients = username;
         string state = "TAKEN";
         IBuildServerNotification notification = new ResponsibilityNotification(notificationType, projectId, buildConfigId, recipients, state);
         userCache = new UserCache();
         userCache.Update(notification);
         User user = userCache.GetUser(username);
         Assert.That(user.IsAttentionRequired());
     }
     finally
     {
         if (userCache != null)
         {
             userCache.Dispose();
         }
     }
 }
        public void TestUpdateNotificationForResponsibleForBuild()
        {
            UserCache userCache = null;
            try
            {
                // Setup
                userCache = new UserCache();
                User actualUser = null;
                userCache.OnUpdate += delegate(object sender, EventArgs args) { actualUser = (User)sender; };
                BuildServerNotificationType notificationType = BuildServerNotificationType.BuildResponsibilityAssigned;
                string projectId = "project1";
                string buildConfigId = "buildconfig1";
                string username = "******";
                string state = BuildServerResponsibilityState.Taken;
                IBuildServerNotification expectedNotification = new ResponsibilityNotification(notificationType, projectId, buildConfigId, username, state);

                // Execute
                userCache.Update(expectedNotification);

                // Test
                Assert.That(actualUser, NUnit.Framework.Is.Not.Null);
                Assert.That(actualUser.Username, NUnit.Framework.Is.EqualTo(username));
                Assert.That(actualUser.IsBuildActive(), NUnit.Framework.Is.False);
                Assert.That(actualUser.IsAttentionRequired(), NUnit.Framework.Is.True);
            }
            finally
            {
                if (userCache != null)
                {
                    userCache.Dispose();
                }
            }
        }
        public void TestUpdateSameUserMadeResponsibleTwiceForSameBuild()
        {
            UserCache userCache = null;
            try
            {
                // Shared
                string projectId = "project1";
                string buildConfigId = "buildconfig1";
                userCache = new UserCache();

                // Events
                BuildServerNotificationType notificationType = BuildServerNotificationType.BuildResponsibilityAssigned;
                string username = "******";
                IBuildServerNotification notification = new ResponsibilityNotification(notificationType, projectId, buildConfigId, username, BuildServerResponsibilityState.Taken);
                string buildKey = Utils.CreateBuildKey(notification);
                userCache.Update(notification);
                userCache.Update(notification);

                // Test
                Assert.That(userCache.GetUser(username).BuildsResponsibleFor.Count, NUnit.Framework.Is.EqualTo(1));
                Assert.That(userCache.GetUser(username).BuildsResponsibleFor.Contains(buildKey));
            }
            finally
            {
                if (userCache != null)
                {
                    userCache.Dispose();
                }
            }
        }
        public void TestUpdateNoMultipleUsersResponsibleForBuild()
        {
            UserCache userCache = null;
            try
            {
                // Shared
                string projectId = "project1";
                string buildConfigId = "buildconfig1";
                userCache = new UserCache();

                // User 1 gets assigned
                BuildServerNotificationType notificationType = BuildServerNotificationType.BuildResponsibilityAssigned;
                string username1 = "user1";
                IBuildServerNotification notification1 = new ResponsibilityNotification(notificationType, projectId, buildConfigId, username1, BuildServerResponsibilityState.Taken);
                userCache.Update(notification1);

                // User 2 gets assigned
                string username2 = "user2";
                IBuildServerNotification notification2 = new ResponsibilityNotification(notificationType, projectId, buildConfigId, username2, BuildServerResponsibilityState.Taken);
                userCache.Update(notification2);

                // User 2 but not user 1 must be responsible
                User user1 = userCache.GetUser(username1);
                Assert.That(user1.IsAttentionRequired(), NUnit.Framework.Is.False);
                User user2 = userCache.GetUser(username2);
                Assert.That(user2.IsAttentionRequired());
            }
            finally
            {
                if (userCache != null)
                {
                    userCache.Dispose();
                }
            }
        }
        public void TestPriorityTimerWithResponsibilityNotification()
        {
            UserCache userCache = null;
            try
            {
                object syncRoot = new object();
                int numberOfEvents = 0;
                int priorityTimerPeriod = 1000;
                TimeSpan priorityPeriod = TimeSpan.Zero;
                string projectId = "project1";
                string buildConfigId = "buildConfig1";
                string username = "******";
                string state = BuildServerResponsibilityState.Taken;
                userCache = new UserCache(priorityTimerPeriod, priorityPeriod);
                User user = null;
                userCache.OnUpdate += (sender, args) =>
                {
                    numberOfEvents++;
                    if (numberOfEvents != 2)
                    {
                        return;
                    }

                    user = (User)sender;
                    lock (syncRoot)
                    {
                        Monitor.Pulse(syncRoot);
                    }
                };
                ResponsibilityNotification notification = new ResponsibilityNotification(BuildServerNotificationType.BuildResponsibilityAssigned, projectId, buildConfigId, username, state);
                userCache.Update(notification);
                lock (syncRoot)
                {
                    Monitor.Wait(syncRoot, 2 * priorityTimerPeriod);
                }

                Assert.That(numberOfEvents, NUnit.Framework.Is.EqualTo(2));
                Assert.That(user, NUnit.Framework.Is.Not.Null);
                Assert.That(user.BuildsResponsibleFor.Count, NUnit.Framework.Is.EqualTo(1));
                Assert.That(user.ActiveBuilds.Count, NUnit.Framework.Is.EqualTo(0));
                Assert.That(user.IsAttentionPriority, NUnit.Framework.Is.True);
            }
            finally
            {
                if (userCache != null)
                {
                    userCache.Dispose();
                }
            }
        }
 public void TestUpdateWithResponsibilityTakenUserExists()
 {
     UserCache userCache = null;
     try
     {
         BuildServerNotificationType notificationType = BuildServerNotificationType.BuildResponsibilityAssigned;
         string projectId = "project1";
         string buildConfigId = "buildconfig1";
         string recipient = "user1";
         string state = BuildServerResponsibilityState.Taken;
         IBuildServerNotification notification = new ResponsibilityNotification(notificationType, projectId, buildConfigId, recipient, state);
         userCache = new UserCache();
         userCache.Update(notification);
         state = BuildServerResponsibilityState.Fixed;
         notification = new ResponsibilityNotification(notificationType, projectId, buildConfigId, recipient, state);
         userCache.Update(notification);
         User user = userCache.GetUser(recipient);
         Assert.That(user.IsAttentionRequired(), NUnit.Framework.Is.False);
     }
     finally
     {
         if (userCache != null)
         {
             userCache.Dispose();
         }
     }
 }
        public void TestEncodeResponsibilityNotification()
        {
            // Construct notification object
            BuildServerNotificationType notificationType = BuildServerNotificationType.BuildResponsibilityAssigned;
            string projectId = "project1";
            string buildConfigId = "buildconfig1";
            string username = "******";
            string state = BuildServerResponsibilityState.Taken;
            ResponsibilityNotification notification = new ResponsibilityNotification(notificationType, projectId, buildConfigId, username, state);

            // Manually construct a packet
            string notificationPart = Field.NotificationTypeId + Packet.FieldSeparator + ((int)notificationType).ToString(CultureInfo.InvariantCulture) + Packet.CommandSeparator;
            string projectPart = Field.ProjectId + Packet.FieldSeparator + projectId + Packet.CommandSeparator;
            string buildConfigPart = Field.BuildConfigId + Packet.FieldSeparator + buildConfigId + Packet.CommandSeparator;
            string recipientPart = Field.ResponsibleUsername + Packet.FieldSeparator + username + Packet.CommandSeparator;
            string statePart = Field.ResponsibilityState + Packet.FieldSeparator + state + Packet.PacketTerminator;
            string expectedCommand = notificationPart + projectPart + buildConfigPart + recipientPart + statePart;

            // Test
            string actualCommand = Parser.Encode(notification);
            Assert.That(actualCommand.Equals(expectedCommand));
        }
        /// <summary>
        /// Updates the builds the user is responsible for.
        /// </summary>
        /// <param name="buildKey">The build key.</param>
        /// <param name="responsibilityNotification">The responsibility notification.</param>
        /// <param name="user">The user.</param>
        private void UpdateUserBuildsResponsibleFor(string buildKey, ResponsibilityNotification responsibilityNotification, ref User user)
        {
            if (Utils.IsAttentionRequired(responsibilityNotification.Type, responsibilityNotification.State))
            {
                if (!user.BuildsResponsibleFor.Contains(buildKey))
                {
                    // Only one user can be responsible for a given build key
                    foreach (User otherUser in this.userCache.Values.Where(x => x.BuildsResponsibleFor.Contains(buildKey)))
                    {
                        otherUser.BuildsResponsibleFor.Remove(buildKey);
                        if (otherUser.BuildsResponsibleFor.Count == 0)
                        {
                            CancelPriority(otherUser);
                        }
                    }

                    if (user.BuildsResponsibleFor.Count == 0)
                    {
                        user.AttentionFirstRequired = DateTime.Now;
                    }

                    // We could be removing the build key we just added, so this needs to happen afterwards
                    user.BuildsResponsibleFor.Add(buildKey);
                }
            }
            else
            {
                user.BuildsResponsibleFor.Remove(buildKey);
                if (user.BuildsResponsibleFor.Count == 0)
                {
                    CancelPriority(user);
                }
            }
        }