public void TestHandleCommandForBuildNotificationRaisesCommandReceivedEvent()
        {
            // Setup
            Listener listener = new Listener(IPAddress.Any, 0);
            IBuildServerNotification actualNotification = null;
            listener.OnCommandReceived += delegate(object sender, EventArgs args) { actualNotification = (IBuildServerNotification)sender; };
            BuildServerNotificationType notificationType = BuildServerNotificationType.BuildBuilding;
            string projectId = "project1";
            string buildConfigId = "buildconfig1";
            string username1 = "user1";
            string username2 = "user2";
            string[] recipients = { username1, username2 };
            BuildNotification expectedBuildNotification = new BuildNotification(notificationType, projectId, buildConfigId, recipients);
            string command = Parser.Encode(expectedBuildNotification);

            // Fire
            listener.HandleCommand(command);

            // Test
            Assert.That(actualNotification, Is.Not.Null);
            Assert.That(actualNotification, Is.InstanceOf(typeof(BuildNotification)));
            BuildNotification actualBuildNotification = (BuildNotification)actualNotification;
            Assert.That(actualBuildNotification.Type, Is.EqualTo(notificationType));
            Assert.That(actualBuildNotification.ProjectId.Equals(projectId));
            Assert.That(actualBuildNotification.BuildConfigId.Equals(buildConfigId));
            Assert.That(actualBuildNotification.Recipients.Length, Is.EqualTo(2));
            Assert.That(actualBuildNotification.Recipients, Is.EqualTo(recipients));
        }
        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 TestHandleUpdate()
        {
            NotificationManager notificationManager = null;
            UserCache userCache = null;
            try
            {
                // Setup the basics
                object syncRoot = new object();
                int serverPort = 30001;
                int clientPort = 30002;
                userCache = new UserCache();
                notificationManager = new NotificationManager(serverPort, clientPort, userCache, false);
                string buildKey1 = "buildkey1";
                string buildKey2 = "buildkey2";
                string username = "******";
                string hostname = "localhost";

                // Create a user that has one build building and is responsible for one other build
                User user = new User(username) {
                                                       Hostname = hostname
                                               };
                user.ActiveBuilds.Add(buildKey1);
                user.BuildsResponsibleFor.Add(buildKey2);

                // A dummy client which the notification manager will notify
                Listener listener = new Listener(IPAddress.Any, clientPort);
                Dictionary<RequestType, IRequest> requests = new Dictionary<RequestType, IRequest>();
                listener.OnCommandReceived += (sender, args) =>
                {
                    requests.Add(((IRequest)sender).Type, (IRequest)sender);
                    lock (syncRoot)
                    {
                        Monitor.Pulse(syncRoot);
                    }
                };
                listener.Start();
                Assert.That(listener.Running, Is.True);

                // Raise an update for this user and wait until all events caused have been raised
                lock (syncRoot)
                {
                    notificationManager.HandleUpdate(user, EventArgs.Empty);
                    ////Monitor.Wait(syncRoot, 5000);
                    Monitor.Wait(syncRoot, 5000);
                }

                listener.Stop();
                Assert.That(listener.Running, Is.False);

                // Test
                Assert.That(requests.ContainsKey(RequestType.BuildActive));
                BuildActiveRequest buildActiveRequest = (BuildActiveRequest)requests[RequestType.BuildActive];
                Assert.That(buildActiveRequest.IsBuildsActive, Is.True);
            }
            finally
            {
                if (notificationManager != null)
                {
                    notificationManager.Dispose();
                }

                if (userCache != null)
                {
                    userCache.Dispose();
                }
            }
        }
        public void TestHandleCommandForMultipleCommandsReceivedEvent()
        {
            // Setup
            int maxCommands = 200;
            int numberOfCommandsReceived = 0;
            object runLock = new object();
            int timeout = 30000;
            string hostname = "localhost";
            int port = 39191;
            Listener listener = new Listener(IPAddress.Any, port);
            ConcurrentQueue<IBuildServerNotification> actualNotifications = new ConcurrentQueue<IBuildServerNotification>();

            listener.OnCommandReceived += (sender, args) =>
            {
                lock (runLock)
                {
                    actualNotifications.Enqueue((IBuildServerNotification)sender);
                    Interlocked.Increment(ref numberOfCommandsReceived);
                    Console.WriteLine("Number of commands received: {0}/{1}", numberOfCommandsReceived, maxCommands);
                    if (numberOfCommandsReceived == maxCommands)
                    {
                        Monitor.Pulse(runLock);
                    }
                }
            };

            BuildServerNotificationType notificationType = BuildServerNotificationType.BuildBuilding;
            string projectId = "project1";
            string buildConfigId = "buildconfig1";
            string username1 = "user1";
            string username2 = "user2";
            string[] recipients = { username1, username2 };
            BuildNotification expectedBuildNotification = new BuildNotification(notificationType, projectId, buildConfigId, recipients);
            string command = Parser.Encode(expectedBuildNotification);
            listener.Start();
            Assert.That(listener.Running, Is.True);
            Console.WriteLine("Port: {0}", port);

            // Fire
            List<bool> sendResults = new List<bool>(maxCommands);
            Thread[] threads = new Thread[maxCommands];
            Console.WriteLine("Creating {0} threads", maxCommands);
            for (int i = 0; i < maxCommands; i++)
            {
                threads[i] = new Thread(() => sendResults.Add(Utils.SendCommand(hostname, port, command)));

                threads[i].Name = i.ToString(CultureInfo.InvariantCulture);
            }

            Console.WriteLine("Starting {0} threads", maxCommands);
            Parallel.ForEach(threads, thread =>
            {
                Console.WriteLine("Starting thread {0}", thread.Name);
                thread.Start();
            });

            lock (runLock)
            {
                Console.WriteLine("Waiting to receive all commands");
                Monitor.Wait(runLock, timeout);
            }

            listener.Stop();
            Assert.That(listener.Running, Is.False);
            Console.WriteLine("Total notifications received: {0}/{1}", actualNotifications.Count, maxCommands);
            Assert.That(sendResults.Contains(false), Is.False, "One or more commands could not be sent");
            Assert.That(actualNotifications.Count, Is.EqualTo(maxCommands), "Not all commands were received - test might have timed out");

            foreach (IBuildServerNotification actualNotification in actualNotifications)
            {
                Assert.That(actualNotification, Is.Not.Null);
                Assert.That(actualNotification, Is.InstanceOf(typeof(BuildNotification)));
                BuildNotification actualBuildNotification = (BuildNotification)actualNotification;
                Assert.That(actualBuildNotification.Type, Is.EqualTo(notificationType));
                Assert.That(actualBuildNotification.ProjectId.Equals(projectId));
                Assert.That(actualBuildNotification.BuildConfigId.Equals(buildConfigId));
                Assert.That(actualBuildNotification.Recipients.Length, Is.EqualTo(2));
                Assert.That(actualBuildNotification.Recipients, Is.EqualTo(recipients));
            }
        }
 public void TestStopTwiceWithoutStart()
 {
     Listener listener = new Listener(IPAddress.Any, 0);
     listener.Stop();
     Assert.That(listener.Running, Is.False);
     listener.Stop();
     Assert.That(listener.Running, Is.False);
 }
 public void TestStopAndStopAgain()
 {
     Listener listener = new Listener(IPAddress.Any, 0);
     listener.Start();
     Assert.That(listener.Running, Is.True);
     listener.Stop();
     Assert.That(listener.Running, Is.False);
     listener.Stop();
     Assert.That(listener.Running, Is.False);
 }
        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 TestHandleCommandForRegistrationRequestRaisesCommandReceivedEvent()
        {
            // Setup
            Listener listener = new Listener(IPAddress.Any, 0);
            IRequest actualRequest = null;
            listener.OnCommandReceived += delegate(object sender, EventArgs args) { actualRequest = (IRequest)sender; };
            string username = "******";
            string hostname = username + "-ws";
            RegistrationRequest actualRegistrationRequest = new RegistrationRequest(hostname, username);
            string command = Parser.Encode(actualRegistrationRequest);

            // Fire
            listener.HandleCommand(command);

            // Test
            Assert.That(actualRequest, Is.Not.Null);
            Assert.That(actualRequest, Is.InstanceOf(typeof(RegistrationRequest)));
            RegistrationRequest expectedRegistrationRequest = (RegistrationRequest)actualRequest;
            Assert.That(expectedRegistrationRequest.Username, Is.EqualTo(username));
            Assert.That(expectedRegistrationRequest.Hostname, Is.EqualTo(hostname));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ManagerBase"/> class.
 /// </summary>
 /// <param name="port">The port to listen on.</param>
 protected ManagerBase(int port)
 {
     this.serverPort = port;
     this.listenerSpinWaitTimeout = 1000;
     this.listener = new Listener(IPAddress.Any, this.serverPort);
 }