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 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 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 TestUpdateWithBuildFailingNotification([Values(BuildServerNotificationType.BuildFailed, BuildServerNotificationType.BuildFailedToStart, BuildServerNotificationType.BuildFailing, BuildServerNotificationType.BuildHanging)] BuildServerNotificationType notificationType) { UserCache userCache = null; try { string projectId = "project1"; string buildConfigId = "buildconfig1"; string username = "******"; string[] recipients = { username }; IBuildServerNotification notification = new BuildNotification(notificationType, projectId, buildConfigId, recipients); userCache = new UserCache(); userCache.Update(notification); User user = userCache.GetUser(username); Assert.That(user.IsAttentionRequired()); } finally { if (userCache != null) { userCache.Dispose(); } } }
public void TestUpdateWithBuildBuildingNotificationForSingleUserDoesNotExist() { UserCache userCache = null; try { BuildServerNotificationType notificationType = BuildServerNotificationType.BuildBuilding; string projectId = "project1"; string buildConfigId = "buildconfig1"; string username = "******"; string[] recipients = { username }; IBuildServerNotification notification = new BuildNotification(notificationType, projectId, buildConfigId, recipients); userCache = new UserCache(); userCache.Update(notification); User user = userCache.GetUser(username); Assert.That(user.IsBuildActive()); } finally { 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 TestUpdateNotificationForActiveBuild() { UserCache userCache = null; try { // Setup userCache = new UserCache(); ConcurrentDictionary<string, User> userDictionary = new ConcurrentDictionary<string, User>(); userCache.OnUpdate += delegate(object sender, EventArgs args) { User user = (User)sender; userDictionary[user.Username] = user; }; BuildServerNotificationType notificationType = BuildServerNotificationType.BuildBuilding; string projectId = "project1"; string buildConfigId = "buildconfig1"; string username1 = "user11"; string username2 = "user21"; string[] recipients = { username1, username2 }; IBuildServerNotification expectedNotification = new BuildNotification(notificationType, projectId, buildConfigId, recipients); // Execute userCache.Update(expectedNotification); // Test foreach (string recipient in recipients) { Assert.That(userDictionary.ContainsKey(recipient)); User actualUser = userDictionary[recipient]; Assert.That(actualUser.Username, NUnit.Framework.Is.EqualTo(recipient)); Assert.That(actualUser.IsBuildActive(), NUnit.Framework.Is.True); Assert.That(actualUser.IsAttentionRequired(), NUnit.Framework.Is.False); } } catch (AggregateException ex) { if (ex.InnerExceptions != null) { foreach (Exception exception in ex.InnerExceptions) { Console.WriteLine(exception.Message); Console.WriteLine(exception.StackTrace); } } throw; } finally { if (userCache != null) { userCache.Dispose(); } } }
/// <summary> /// Updates the builds the user is responsible for. /// </summary> /// <param name="buildKey">The build key.</param> /// <param name="buildNotification">The build notification.</param> /// <param name="user">The user.</param> private static void UpdateUserBuildsResponsibleFor(string buildKey, BuildNotification buildNotification, ref User user) { // Active vs. inactive build is mutually exclusive, but unfortunately not attention when it is // a build notification. Since IsAttentionRequired tests for build failures, it's negation doesn't // imply success. E.g. if the build is not failed, failed to start, failing or hanging, it doesn't // imply success. This is because we're mixing these with responsibility notifications, which // make it ambiguous. So, we need to actively test that attention can be cancelled. if (Utils.IsAttentionRequired(buildNotification.Type)) { // If this is the first notification of responsibility if (user.BuildsResponsibleFor.Count == 0) { user.AttentionFirstRequired = DateTime.Now; } if (!user.BuildsResponsibleFor.Contains(buildKey)) { user.BuildsResponsibleFor.Add(buildKey); } } else if (Utils.IsNoAttentionRequired(buildNotification.Type)) { user.BuildsResponsibleFor.Remove(buildKey); if (user.BuildsResponsibleFor.Count == 0) { CancelPriority(user); } } }
public void TestUpdateDuplicateBuildKeyOnActiveBuildForResponsibleUser() { UserCache userCache = null; try { // Shared string projectId = "project1"; string buildConfigId = "buildconfig1"; userCache = new UserCache(); // Events BuildServerNotificationType notificationType = BuildServerNotificationType.BuildFailed; string username = "******"; string[] recipients = { username }; IBuildServerNotification notification = new BuildNotification(notificationType, projectId, buildConfigId, recipients); string buildKey = Utils.CreateBuildKey(notification); userCache.Update(notification); userCache.Update(notification); // Test Assert.That(userCache.GetUser(username).BuildsResponsibleFor.Contains(buildKey)); } finally { if (userCache != null) { userCache.Dispose(); } } }
public void TestUpdateDuplicateBuildKeyForActiveBuildsOneMustRemainAfterTheOtherFinished() { UserCache userCache = null; try { // Shared string projectId = "project1"; string buildConfigId = "buildconfig1"; userCache = new UserCache(); // Events BuildServerNotificationType notificationType = BuildServerNotificationType.BuildBuilding; string username = "******"; string[] recipients = { username }; IBuildServerNotification notification = new BuildNotification(notificationType, projectId, buildConfigId, recipients); string buildKey = Utils.CreateBuildKey(notification); userCache.Update(notification); userCache.Update(notification); notificationType = BuildServerNotificationType.BuildSuccessful; notification = new BuildNotification(notificationType, projectId, buildConfigId, recipients); userCache.Update(notification); // Test Assert.That(userCache.GetUser(username).ActiveBuilds.Contains(buildKey)); } finally { if (userCache != null) { userCache.Dispose(); } } }
public void TestPriorityTimerWithBuildFailureFollowedByBuildSuccess() { 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[] recipients = { username }; 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); } }; // We fail a build and after the priority timer period expires this user's // priority attention will be required BuildNotification notification = new BuildNotification(BuildServerNotificationType.BuildFailed, projectId, buildConfigId, recipients); 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); // A successful build must now correctly reset the priority properties for the user notification = new BuildNotification(BuildServerNotificationType.BuildSuccessful, projectId, buildConfigId, recipients); userCache.Update(notification); lock (syncRoot) { Monitor.Wait(syncRoot, 2 * priorityTimerPeriod); } Assert.That(numberOfEvents, NUnit.Framework.Is.EqualTo(3)); Assert.That(user, NUnit.Framework.Is.Not.Null); Assert.That(user.BuildsResponsibleFor.Count, NUnit.Framework.Is.EqualTo(0)); Assert.That(user.ActiveBuilds.Count, NUnit.Framework.Is.EqualTo(0)); Assert.That(user.IsAttentionPriority, NUnit.Framework.Is.False); } finally { if (userCache != null) { userCache.Dispose(); } } }
public void TestInitializeWithMocks() { UserCache userCache = null; try { // Setup string username = "******"; string projectId = "project1"; string buildConfigId = "config1"; string[] recipients = { username }; IBuildServerNotification buildBuildingNotification = new BuildNotification(BuildServerNotificationType.BuildBuilding, projectId, buildConfigId, recipients); IBuildServerNotification buildFailedNotification = new BuildNotification(BuildServerNotificationType.BuildFailed, projectId, buildConfigId, recipients); string buildKey = Utils.CreateBuildKey(buildFailedNotification); // Mocks Mock<IBuildServerClient> mockBuildServerClient = this.mockFactory.CreateMock<IBuildServerClient>(); List<IBuildServerNotification> activeBuilds = new List<IBuildServerNotification> { buildBuildingNotification }; mockBuildServerClient.Expects.One.Method(x => x.GetAllActiveBuildNotifications()).WillReturn(activeBuilds); List<IBuildServerNotification> buildsThatRequireAttention = new List<IBuildServerNotification> { buildFailedNotification }; mockBuildServerClient.Expects.One.Method(x => x.GetAllBuildsThatRequireAttention()).WillReturn(buildsThatRequireAttention); // Execute userCache = new UserCache(); userCache.Initialize(mockBuildServerClient.MockObject); // Test User actualUser = userCache.GetUser(username); Assert.That(actualUser.ActiveBuilds.Count, NUnit.Framework.Is.EqualTo(1)); Assert.That(actualUser.ActiveBuilds.Contains(buildKey)); Assert.That(actualUser.BuildsResponsibleFor.Count, NUnit.Framework.Is.EqualTo(1)); Assert.That(actualUser.BuildsResponsibleFor.Contains(buildKey)); this.mockFactory.VerifyAllExpectationsHaveBeenMet(); } finally { if (userCache != null) { userCache.Dispose(); } } }
public void TestUsernameKeyIsLowerCase() { UserCache userCache = null; try { // Setup string username = "******"; string projectId = "project1"; string buildConfigId = "buildConfigId"; string[] recipients = { username }; userCache = new UserCache(); BuildNotification buildNotification = new BuildNotification(BuildServerNotificationType.BuildBuilding, projectId, buildConfigId, recipients); userCache.Update(buildNotification); // User1 must not exist try { userCache.GetUser(username); Assert.Fail(); } catch (KeyNotFoundException) { } // But user1 must User user = userCache.GetUser(username.ToLower()); // Check the details Assert.That(user, NUnit.Framework.Is.Not.Null); Assert.That(user.Username, NUnit.Framework.Is.EqualTo(username.ToLower())); } finally { if (userCache != null) { userCache.Dispose(); } } }
public void TestEncodeBuildNotification() { // Construct notification object BuildServerNotificationType notificationType = BuildServerNotificationType.BuildBuilding; string projectId = "project1"; string buildConfigId = "buildconfig1"; string username1 = "user1"; string username2 = "user2"; string[] recipients = { username1, username2 }; BuildNotification notification = new BuildNotification(notificationType, projectId, buildConfigId, recipients); // 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 recipientList = string.Join(Packet.ListSeparator.ToString(CultureInfo.InvariantCulture), recipients); string recipientsPart = Field.BuildRecipients + Packet.FieldSeparator + recipientList + Packet.PacketTerminator; string expectedCommand = notificationPart + projectPart + buildConfigPart + recipientsPart; // Test string actualCommand = Parser.Encode(notification); Assert.That(actualCommand.Equals(expectedCommand)); }
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 TestUpdateMultipleUsersMultipleBuildsOneBuildingOneSuccessful() { UserCache userCache = null; try { // Shared string projectId1 = "project1"; string buildConfigId1 = "buildconfig1"; string projectId2 = "project2"; string buildConfigId2 = "buildconfig2"; userCache = new UserCache(); // Events -- all building BuildServerNotificationType notificationType = BuildServerNotificationType.BuildBuilding; string username1 = "user1"; string username2 = "user2"; string[] recipients = { username1, username2 }; IBuildServerNotification notification1 = new BuildNotification(notificationType, projectId1, buildConfigId1, recipients); string buildKey1 = Utils.CreateBuildKey(notification1); userCache.Update(notification1); IBuildServerNotification notification2 = new BuildNotification(notificationType, projectId2, buildConfigId2, recipients); string buildKey2 = Utils.CreateBuildKey(notification2); userCache.Update(notification2); // Events -- one successful notificationType = BuildServerNotificationType.BuildSuccessful; notification1 = new BuildNotification(notificationType, projectId1, buildConfigId1, recipients); userCache.Update(notification1); // Both users must have only 1 build active User user1 = userCache.GetUser(username1); Assert.That(user1.IsBuildActive()); Assert.That(user1.ActiveBuilds.Count, NUnit.Framework.Is.EqualTo(1)); Assert.That(user1.ActiveBuilds.Contains(buildKey1), NUnit.Framework.Is.False); Assert.That(user1.ActiveBuilds.Contains(buildKey2)); User user2 = userCache.GetUser(username2); Assert.That(user2.IsBuildActive()); Assert.That(user2.ActiveBuilds.Count, NUnit.Framework.Is.EqualTo(1)); Assert.That(user2.ActiveBuilds.Contains(buildKey1), NUnit.Framework.Is.False); Assert.That(user2.ActiveBuilds.Contains(buildKey2)); } finally { if (userCache != null) { userCache.Dispose(); } } }
/// <summary> /// Updates the user's active builds. /// </summary> /// <param name="buildKey">The build key.</param> /// <param name="buildNotification">The build notification.</param> /// <param name="user">The user.</param> private static void UpdateUserActiveBuilds(string buildKey, BuildNotification buildNotification, ref User user) { // Active and inactive is mutually exclusive, i.e. if a build is not building (active) it must consequently // be inactive. if (Utils.IsActiveBuild(buildNotification.Type)) { if (!user.ActiveBuilds.Contains(buildKey)) { user.ActiveBuilds.Add(buildKey); } } else { user.ActiveBuilds.Remove(buildKey); } }