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);
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);
*
* When:
* Freddy adds comment to review created by Simon and assigned to Simon.
- *
+ *
* Then:
* Only one notification should be delivered to Simon by Email.
*/
* 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.
*/
/**
* 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.
*/
/**
* Given:
* Nobody wants to receive notifications.
- *
+ *
* When:
* Freddy adds comment to review created by Evgeny and assigned to Simon.
- *
+ *
* Then:
* No notifications.
*/
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);