PriorityQueueEmailSchedulerService implements EmailSchedulerService { @Override @Async public void schedule(@NonNull final Email mimeEmail, final int desiredPriorityLevel) { scheduleEmail(mimeEmail, TimeUtils.offsetDateTimeNow(), desiredPriorityLevel); } @Autowired  PriorityQueueEmailSchedulerService(
            final EmailService emailService,
            final EmailSchedulerProperties emailSchedulerProperties,
            final Optional<PersistenceService> persistenceServiceOptional,
            final EmailLogRenderer emailLogRenderer); @Override @Async void schedule(@NonNull final Email mimeEmail, final int desiredPriorityLevel); @Override @Async void schedule(@NonNull final Email mimeEmail, @NonNull final OffsetDateTime scheduledDateTime, final int desiredPriorityLevel); @Override @Async void schedule(@NonNull final Email mimeEmail, final int desiredPriorityLevel, @NonNull final String template,
                         @NonNull final Map<String, Object> modelObject, final InlinePicture... inlinePictures); @Override @Async void schedule(@NonNull final Email mimeEmail, @NonNull final OffsetDateTime scheduledDateTime, final int desiredPriorityLevel,
                         @NonNull final String template, @NonNull final Map<String, Object> modelObject, final InlinePicture... inlinePictures);  }
@Test public void shouldScheduleAndSendEmailWithoutTemplateAndOffsetDateTime() throws Exception { final PriorityQueueEmailSchedulerService priorityQueueSchedulerService = scheduler(1); final Email email = getSimpleMail(); priorityQueueSchedulerService.schedule(email, 1); Thread.sleep(twoSecondsInMillis()); verify(emailService).send(email); }
@Test public void shouldScheduleAndSendEmailWithoutTemplate() throws Exception { final PriorityQueueEmailSchedulerService priorityQueueSchedulerService = scheduler(1); final Email email = getSimpleMail(); priorityQueueSchedulerService.schedule(email, TimeUtils.offsetDateTimeNow(), 1); Thread.sleep(twoSecondsInMillis()); verify(emailService).send(email); }
@Test public void shouldScheduleAndSendEmailWithTemplateAndOffsetDateTime() throws Exception { final PriorityQueueEmailSchedulerService priorityQueueSchedulerService = scheduler(1); final Email email = getSimpleMail(); priorityQueueSchedulerService.schedule(email,1, TemplatingTestUtils.TEMPLATE, TemplatingTestUtils.MODEL_OBJECT); Thread.sleep(twoSecondsInMillis()); verify(emailService).send(email, TemplatingTestUtils.TEMPLATE, TemplatingTestUtils.MODEL_OBJECT); }
@Test public void shouldScheduleAndSendEmailWithTemplate() throws Exception { final PriorityQueueEmailSchedulerService priorityQueueSchedulerService = scheduler(1); final Email email = getSimpleMail(); priorityQueueSchedulerService.schedule(email, TimeUtils.offsetDateTimeNow(), 1, TemplatingTestUtils.TEMPLATE, TemplatingTestUtils.MODEL_OBJECT); Thread.sleep(twoSecondsInMillis()); verify(emailService).send(email, TemplatingTestUtils.TEMPLATE, TemplatingTestUtils.MODEL_OBJECT); }
@Test public void shouldRespectPriorityForSameDateTime() throws Exception { final PriorityQueueEmailSchedulerService priorityQueueSchedulerService = scheduler(3); final Email emailLowPriority = lowPriority(); final Email emailMidPriority = midPriority(); final Email emailHighPriority = highPriority(); final OffsetDateTime dateTime = TimeUtils.offsetDateTimeNow(); priorityQueueSchedulerService.schedule(emailLowPriority, dateTime.plusSeconds(twoSeconds()), 3); priorityQueueSchedulerService.schedule(emailHighPriority, dateTime.plusSeconds(twoSeconds()), 1); priorityQueueSchedulerService.schedule(emailMidPriority, dateTime.plusSeconds(twoSeconds()), 2); TimeUnit.SECONDS.sleep(5); final InOrder inOrder = inOrder(emailService); inOrder.verify(emailService).send(emailHighPriority); inOrder.verify(emailService).send(emailMidPriority); inOrder.verify(emailService).send(emailLowPriority); }
@Test public void shouldRespectPriorityWhenDifferentDateTime() throws Exception { final PriorityQueueEmailSchedulerService priorityQueueSchedulerService = scheduler(3); final Email emailLowPriority = lowPriority(); final Email emailMidPriority = midPriority(); final Email emailHighPriority = highPriority(); final OffsetDateTime dateTime = TimeUtils.offsetDateTimeNow(); priorityQueueSchedulerService.schedule(emailHighPriority, dateTime.plusSeconds(threeSeconds()), 1); priorityQueueSchedulerService.schedule(emailMidPriority, dateTime.plusSeconds(twoSeconds()), 2); priorityQueueSchedulerService.schedule(emailLowPriority, dateTime.plusSeconds(oneSecond()), 3); Thread.sleep(fiveSecondsInMillis()); final InOrder inOrder = inOrder(emailService); inOrder.verify(emailService).send(emailLowPriority); inOrder.verify(emailService).send(emailMidPriority); inOrder.verify(emailService).send(emailHighPriority); }