From c5d8eb939cbdd6da8f434f7eeb6c30dd9e1baea0 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Fri, 2 Aug 2013 16:43:02 +0200 Subject: [PATCH] SONAR-4548 The NotificationService should not stop working when an error occur --- .../notifications/NotificationService.java | 8 +++-- .../NotificationServiceTest.java | 30 ++++++++++++++----- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java b/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java index ada17a1b4e1..29d1690e1e7 100644 --- a/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java +++ b/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java @@ -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); diff --git a/sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceTest.java b/sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceTest.java index 258aaa70dd7..1e3c109a206 100644 --- a/sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceTest.java @@ -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); -- 2.39.5