/// <summary> /// The main method. /// </summary> private static void Main() { UserCache userCache = null; NotificationManager notificationManager = null; try { userCache = new UserCache(5000, new TimeSpan(0, 5, 0)); notificationManager = new NotificationManager(Config.GetNotificationManagerPort(), Config.GetLightsManagerPort(), userCache, Config.GetInitializationEnabled()); Thread thread = new Thread(notificationManager.Start); thread.Start(); Console.WriteLine("Press any key to terminate..."); Console.ReadKey(true); notificationManager.Stop(); thread.Join(); Console.WriteLine("Press any key to close..."); Console.ReadKey(true); } catch (Exception e) { Console.Error.WriteLine(e); Console.ReadKey(true); } finally { if (notificationManager != null) { notificationManager.Dispose(); } if (userCache != null) { userCache.Dispose(); } } }
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 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 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 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 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 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 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 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 TestRegistrationUpdateEvent() { UserCache userCache = null; try { userCache = new UserCache(); User user = null; userCache.OnUpdate += delegate(object sender, EventArgs args) { user = (User)sender; }; string username = "******"; string hostname = username + "-ws"; RegistrationRequest request = new RegistrationRequest(hostname, username); userCache.Register(request); Assert.That(user, NUnit.Framework.Is.Not.Null); Assert.That(user.Username, NUnit.Framework.Is.EqualTo(username)); Assert.That(user.Hostname, NUnit.Framework.Is.EqualTo(hostname)); Assert.That(user.ActiveBuilds.Count, NUnit.Framework.Is.EqualTo(0)); Assert.That(user.BuildsResponsibleFor.Count, NUnit.Framework.Is.EqualTo(0)); } 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 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 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 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 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(); } } }
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(); } } }
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 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 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(); } } }
public void TestInitialize() { UserCache userCache = null; NotificationManager notificationManager = null; try { userCache = new UserCache(); notificationManager = new NotificationManager(0, 0, userCache, true); notificationManager.Start(); Assert.That(notificationManager.Running, Is.True); notificationManager.Stop(); Assert.That(notificationManager.Running, Is.False); } finally { if (notificationManager != null) { notificationManager.Dispose(); } 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 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 TestGetRegisteredUsers() { UserCache userCache = null; try { userCache = new UserCache(); List<User> userList = new List<User>(userCache.GetRegisteredUsers()); Assert.That(userList.Count, NUnit.Framework.Is.EqualTo(0)); string username1 = "user1"; string hostname1 = username1 + "-ws"; string username2 = "user2"; string hostname2 = string.Empty; RegistrationRequest request1 = new RegistrationRequest(hostname1, username1); RegistrationRequest request2 = new RegistrationRequest(hostname2, username2); userCache.Register(request1); userCache.Register(request2); userList = new List<User>(userCache.GetRegisteredUsers()); Assert.That(userList.Count, NUnit.Framework.Is.EqualTo(1)); User user = userList[0]; Assert.That(user.Hostname, NUnit.Framework.Is.EqualTo(hostname1)); Assert.That(user.Username, NUnit.Framework.Is.EqualTo(username1)); } finally { if (userCache != null) { userCache.Dispose(); } } }