]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4548 The NotificationService should not stop working when an error occur
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 2 Aug 2013 14:43:02 +0000 (16:43 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 2 Aug 2013 14:43:02 +0000 (16:43 +0200)
sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java
sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceTest.java

index ada17a1b4e1ac7c5312e1687bf534dc78da36a6c..29d1690e1e7e488558f0c24aa81a33f127d71c26 100644 (file)
@@ -70,7 +70,7 @@ public class NotificationService implements ServerComponent {
   private boolean stopping = false;
 
   /**
-   * Constructor for {@link NotificationService} 
+   * Constructor for {@link NotificationService}
    */
   public NotificationService(Settings settings, DefaultNotificationManager manager, NotificationDispatcher[] dispatchers) {
     delayInSeconds = settings.getLong(PROPERTY_DELAY);
@@ -90,7 +90,11 @@ public class NotificationService implements ServerComponent {
     executorService = Executors.newSingleThreadScheduledExecutor();
     executorService.scheduleWithFixedDelay(new Runnable() {
       public void run() {
-        processQueue();
+        try {
+          processQueue();
+        } catch (Exception e) {
+          LOG.error("Error in NotificationService", e);
+        }
       }
     }, 0, delayInSeconds, TimeUnit.SECONDS);
     LOG.info("Notification service started (delay {} sec.)", delayInSeconds);
index 258aaa70dd7567ae5ef0d9cec4c78163a4b96a0d..1e3c109a206b8ee7ea96896f014bacc04948e5c9 100644 (file)
@@ -75,7 +75,7 @@ public class NotificationServiceTest {
    *
    * When:
    * Freddy adds comment to review created by Simon and assigned to Simon.
-   * 
+   *
    * Then:
    * Only one notification should be delivered to Simon by Email.
    */
@@ -96,10 +96,10 @@ public class NotificationServiceTest {
    * Given:
    * Evgeny wants to receive notification by GTalk on comments for reviews created by him.
    * Simon wants to receive notification by Email on comments for reviews assigned to him.
-   * 
+   *
    * When:
    * Freddy adds comment to review created by Evgeny and assigned to Simon.
-   * 
+   *
    * Then:
    * Two notifications should be delivered - one to Simon by Email and another to Evgeny by GTalk.
    */
@@ -121,10 +121,10 @@ public class NotificationServiceTest {
   /**
    * Given:
    * Simon wants to receive notifications by Email and GTLak on comments for reviews assigned to him.
-   * 
+   *
    * When:
    * Freddy adds comment to review created by Evgeny and assigned to Simon.
-   * 
+   *
    * Then:
    * Two notifications should be delivered to Simon - one by Email and another by GTalk.
    */
@@ -146,10 +146,10 @@ public class NotificationServiceTest {
   /**
    * Given:
    * Nobody wants to receive notifications.
-   * 
+   *
    * When:
    * Freddy adds comment to review created by Evgeny and assigned to Simon.
-   * 
+   *
    * Then:
    * No notifications.
    */
@@ -164,6 +164,22 @@ public class NotificationServiceTest {
     verify(gtalkChannel, never()).deliver(any(Notification.class), anyString());
   }
 
+  // SONAR-4548
+  @Test
+  public void shouldNotStopWhenException() {
+    setUpMocks(CREATOR_SIMON, ASSIGNEE_SIMON);
+    when(queueElement.getNotification()).thenThrow(new RuntimeException("Unexpected exception")).thenReturn(notification);
+    when(manager.getFromQueue()).thenReturn(queueElement).thenReturn(queueElement).thenReturn(null);
+    doAnswer(addUser(ASSIGNEE_SIMON, emailChannel)).when(commentOnReviewAssignedToMe).dispatch(same(notification), any(NotificationDispatcher.Context.class));
+    doAnswer(addUser(CREATOR_SIMON, emailChannel)).when(commentOnReviewCreatedByMe).dispatch(same(notification), any(NotificationDispatcher.Context.class));
+
+    service.start();
+    verify(emailChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON);
+    service.stop();
+
+    verify(gtalkChannel, never()).deliver(notification, ASSIGNEE_SIMON);
+  }
+
   @Test
   public void shouldNotAddNullAsUser() {
     setUpMocks(CREATOR_EVGENY, ASSIGNEE_SIMON);