public void TestHandleBuildNotification()
        {
            UserCache userCache = null;
            NotificationManager notificationManager = null;
            try
            {
                userCache = new UserCache();
                notificationManager = new NotificationManager(0, 0, userCache, false);
                string username = "******";
                string[] recipients = { username };
                BuildServerNotificationType notificationType = BuildServerNotificationType.BuildBuilding;
                string projectId = "project1";
                string buildConfigId = "buildconfig1";
                IBuildServerNotification buildServerNotification = new BuildNotification(notificationType, projectId, buildConfigId, recipients);
                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.ActiveBuilds.Contains(buildKey));
            }
            finally
            {
                if (notificationManager != null)
                {
                    notificationManager.Dispose();
                }

                if (userCache != null)
                {
                    userCache.Dispose();
                }
            }
        }
        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 TestHandleRegistrationRequestAfterBuildNotification()
        {
            UserCache userCache = null;
            NotificationManager notificationManager = null;
            try
            {
                // Setup
                userCache = new UserCache();
                notificationManager = new NotificationManager(0, 0, userCache, false);
                string username = "******";
                string[] recipients = { username };
                BuildServerNotificationType notificationType = BuildServerNotificationType.BuildBuilding;
                string projectId = "project1";
                string buildConfigId = "buildconfig1";
                IBuildServerNotification buildServerNotification = new BuildNotification(notificationType, projectId, buildConfigId, recipients);
                string buildKey = Utils.CreateBuildKey(buildServerNotification);

                // Check that the user doesn't exist prior to the notification
                try
                {
                    userCache.GetUser(username);
                    Assert.Fail();
                }
                catch (KeyNotFoundException)
                {
                    // Expected
                }

                // This will create a user in the cache, but without a hostname
                notificationManager.HandleCommand(buildServerNotification, EventArgs.Empty);
                User user = userCache.GetUser(username);
                Assert.That(user.Username, Is.EqualTo(username));
                Assert.That(user.ActiveBuilds.Contains(buildKey));
                Assert.That(string.IsNullOrEmpty(user.Hostname));

                // Now we create a registration request, which should update the existing user with a hostname
                string hostname = username + "-ws";
                IRequest registrationRequest = new RegistrationRequest(hostname, username);
                notificationManager.HandleCommand(registrationRequest, EventArgs.Empty);
                user = userCache.GetUser(username);
                Assert.That(user.Username, Is.EqualTo(username));
                Assert.That(user.Hostname, Is.EqualTo(hostname));
            }
            finally
            {
                if (notificationManager != null)
                {
                    notificationManager.Dispose();
                }

                if (userCache != null)
                {
                    userCache.Dispose();
                }
            }
        }
        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 TestHandleRegistrationRequest()
        {
            UserCache userCache = null;
            NotificationManager notificationManager = null;
            try
            {
                userCache = new UserCache();
                notificationManager = new NotificationManager(0, 0, userCache, false);
                string username = "******";
                string hostname = username + "-ws";
                IRequest registrationRequest = new RegistrationRequest(hostname, username);

                // Check that the user doesn't exist prior to the registration request
                try
                {
                    userCache.GetUser(username);
                    Assert.Fail();
                }
                catch (KeyNotFoundException)
                {
                }

                notificationManager.HandleCommand(registrationRequest, EventArgs.Empty);
                User user = userCache.GetUser(username);
                Assert.That(user.Username, Is.EqualTo(username));
                Assert.That(user.Hostname, Is.EqualTo(hostname));

                // Registering the same user a second time shouldn't fail
                notificationManager.HandleCommand(registrationRequest, EventArgs.Empty);
            }
            finally
            {
                if (notificationManager != null)
                {
                    notificationManager.Dispose();
                }

                if (userCache != null)
                {
                    userCache.Dispose();
                }
            }
        }