public async Task SelectNotificationsForReleaseAsync_DoesntSelectFromOtherGovernors()
        {
            FakeClock.Setup();
            FakeClock.Now = DateTime.Now;

            NotificationBuffer buffer1 = new NotificationBuffer(Guid.NewGuid(), governorId, Guid.NewGuid());

            inMemoryCrudRepository.Attach(buffer1);
            BufferedNotification notification1 = new BufferedNotification(Guid.NewGuid(), "Notification1", "{}",
                                                                          buffer1, FakeClock.Now.Subtract(TimeSpan.FromMinutes(6)));

            inMemoryCrudRepository.Attach(notification1);

            NotificationBuffer buffer2 = new NotificationBuffer(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid());

            inMemoryCrudRepository.Attach(buffer2);
            BufferedNotification notification3 = new BufferedNotification(Guid.NewGuid(), "Notification3", "{}",
                                                                          buffer2, FakeClock.Now.Subtract(TimeSpan.FromMinutes(8)));

            inMemoryCrudRepository.Attach(notification3);

            var notifications = await sut.SelectNotificationsForReleaseAsync(inMemoryCrudRepository);

            Assert.Equal(1, notifications.Keys.Count());
            Assert.Contains(buffer1, notifications.Keys);
        }
Exemplo n.º 2
0
        public async Task SelectNotificationsForReleaseAsync_ReturnsAllExpiredNotifications()
        {
            FakeClock.Setup();
            FakeClock.Now = DateTime.Now;

            NotificationBuffer buffer1 = new NotificationBuffer(Guid.NewGuid(), governorId, Guid.NewGuid());

            inMemoryCrudRepository.Attach(buffer1);
            BufferedNotification notification1 = new BufferedNotification(Guid.NewGuid(), "Notification1", "{}",
                                                                          buffer1, FakeClock.Now.Subtract(TimeSpan.FromMinutes(6)));

            inMemoryCrudRepository.Attach(notification1);
            BufferedNotification notification2 = new BufferedNotification(Guid.NewGuid(), "Notification2", "{}",
                                                                          buffer1, FakeClock.Now.Subtract(TimeSpan.FromMinutes(3)));

            inMemoryCrudRepository.Attach(notification2);
            buffer1.Notifications = new List <BufferedNotification>()
            {
                notification1, notification2
            };

            NotificationBuffer buffer2 = new NotificationBuffer(Guid.NewGuid(), governorId, Guid.NewGuid());

            inMemoryCrudRepository.Attach(buffer2);
            BufferedNotification notification3 = new BufferedNotification(Guid.NewGuid(), "Notification3", "{}",
                                                                          buffer2, FakeClock.Now.Subtract(TimeSpan.FromMinutes(8)));

            inMemoryCrudRepository.Attach(notification3);
            buffer2.Notifications = new List <BufferedNotification>()
            {
                notification3
            };

            NotificationBuffer buffer3 = new NotificationBuffer(Guid.NewGuid(), governorId, Guid.NewGuid());

            inMemoryCrudRepository.Attach(buffer3);
            BufferedNotification notification4 = new BufferedNotification(Guid.NewGuid(), "Notification3", "{}",
                                                                          buffer3, FakeClock.Now.Subtract(TimeSpan.FromMinutes(1)));

            inMemoryCrudRepository.Attach(notification4);
            buffer3.Notifications = new List <BufferedNotification>()
            {
                notification4
            };

            var notifications = await sut.SelectNotificationsForReleaseAsync(inMemoryCrudRepository);

            Assert.Equal(2, notifications.Keys.Count());
            Assert.Equal(2, notifications[buffer1].Count);
            Assert.Contains(notification1, notifications[buffer1]);
            Assert.Contains(notification2, notifications[buffer1]);
            Assert.Equal(1, notifications[buffer2].Count);
            Assert.Contains(notification3, notifications[buffer2]);
        }
        public async Task HandleAsync_ProcessesNotificationsWithPipeline()
        {
            var notificationsToRelease = new MultiValueDictionary <NotificationBuffer, BufferedNotification>();

            NotificationBuffer buffer1 = new NotificationBuffer(Guid.NewGuid(), bufferGovernor1.Id,
                                                                notificationPipeline1.Id);

            inMemoryCrudRepository.Attach(buffer1);
            BufferedNotification notification1 = new BufferedNotification(Guid.NewGuid(), "Notification1", "{}",
                                                                          buffer1, DateTime.Today);

            notificationsToRelease.Add(buffer1, notification1);
            inMemoryCrudRepository.Attach(notification1);
            BufferedNotification notification2 = new BufferedNotification(Guid.NewGuid(), "Notification2", "{}",
                                                                          buffer1, DateTime.Today);

            notificationsToRelease.Add(buffer1, notification2);
            inMemoryCrudRepository.Attach(notification2);

            NotificationBuffer buffer2 = new NotificationBuffer(Guid.NewGuid(), bufferGovernor1.Id,
                                                                notificationPipeline2.Id);

            inMemoryCrudRepository.Attach(buffer2);
            BufferedNotification notification3 = new BufferedNotification(Guid.NewGuid(), "Notification3", "{}",
                                                                          buffer2, DateTime.Today);

            notificationsToRelease.Add(buffer2, notification3);
            inMemoryCrudRepository.Attach(notification3);

            bufferGovernor1.SelectNotificationsForReleaseAsync(inMemoryCrudRepository)
            .Returns(notificationsToRelease);

            var job = new ProcessBufferedNotificationsJob(Clock.Current.Now);
            await sut.HandleAsync(job, CancellationToken.None);

            notificationPipeline1.Received(1).ProcessNotificationsAsync(
                Arg.Is <IReadOnlyCollection <INotification> >(
                    x => x.Count() == 2 &&
                    x.Count(y => ((TestNotification)y).TypeName == "Notification1") == 1 &&
                    x.Count(y => ((TestNotification)y).TypeName == "Notification2") == 1));

            notificationPipeline2.Received(1).ProcessNotificationsAsync(
                Arg.Is <IReadOnlyCollection <INotification> >(
                    x => x.Count() == 1 &&
                    x.Count(y => ((TestNotification)y).TypeName == "Notification3") == 1));
        }
Exemplo n.º 4
0
        public async Task Run_ProcessesNotificationsWithPipeline()
        {
            var notificationsToRelease = new MultiValueDictionary <NotificationBuffer, BufferedNotification>();

            NotificationBuffer buffer1 = new NotificationBuffer(Guid.NewGuid(), bufferGovernor1.Id,
                                                                notificationPipeline1.Id);

            inMemoryCrudRepository.Attach(buffer1);
            BufferedNotification notification1 = new BufferedNotification(Guid.NewGuid(), "Notification1", "{}",
                                                                          buffer1, DateTime.Today);

            notificationsToRelease.Add(buffer1, notification1);
            inMemoryCrudRepository.Attach(notification1);
            BufferedNotification notification2 = new BufferedNotification(Guid.NewGuid(), "Notification2", "{}",
                                                                          buffer1, DateTime.Today);

            notificationsToRelease.Add(buffer1, notification2);
            inMemoryCrudRepository.Attach(notification2);

            NotificationBuffer buffer2 = new NotificationBuffer(Guid.NewGuid(), bufferGovernor1.Id,
                                                                notificationPipeline2.Id);

            inMemoryCrudRepository.Attach(buffer2);
            BufferedNotification notification3 = new BufferedNotification(Guid.NewGuid(), "Notification3", "{}",
                                                                          buffer2, DateTime.Today);

            notificationsToRelease.Add(buffer2, notification3);
            inMemoryCrudRepository.Attach(notification3);

            bufferGovernor1.SelectNotificationsForReleaseAsync(inMemoryCrudRepository)
            .Returns(notificationsToRelease);

            await sut.Run();

            notificationPipeline1.Received(1).ProcessNotificationsAsync(
                Arg.Is <IEnumerable <INotification> >(
                    x => x.Count() == 2 &&
                    x.Count(y => ((TestNotification)y).TypeName == "Notification1") == 1 &&
                    x.Count(y => ((TestNotification)y).TypeName == "Notification2") == 1));

            notificationPipeline2.Received(1).ProcessNotificationsAsync(
                Arg.Is <IEnumerable <INotification> >(
                    x => x.Count() == 1 &&
                    x.Count(y => ((TestNotification)y).TypeName == "Notification3") == 1));
        }
Exemplo n.º 5
0
        public async Task Add(SerializedNotification serializedNotification, Guid bufferId,
                              DateTimeOffset timeQueued, Guid bufferGovernorId, Guid notificationPipelineId)
        {
            NotificationBuffer buffer = await crudRepository.FindAsync <NotificationBuffer>(bufferId);

            if (buffer == null)
            {
                buffer = new NotificationBuffer(bufferId, bufferGovernorId, notificationPipelineId);
                crudRepository.Add(buffer); // TODO race condition - use AddOrUpdate instead
            }

            BufferedNotification notification = new BufferedNotification(
                id: Guid.NewGuid(),
                buffer: buffer,
                notificationClassName: serializedNotification.NotificationClassName,
                notificationJson: serializedNotification.NotificationJson,
                timeQueued: timeQueued
                );

            crudRepository.Add(notification);
        }
Exemplo n.º 6
0
        public async Task Run_RemovesNotificationFromRepository()
        {
            var notificationsToRelease = new MultiValueDictionary <NotificationBuffer, BufferedNotification>();

            NotificationBuffer buffer1 = new NotificationBuffer(Guid.NewGuid(), bufferGovernor1.Id,
                                                                notificationPipeline1.Id);

            inMemoryCrudRepository.Attach(buffer1);
            BufferedNotification notification1 = new BufferedNotification(Guid.NewGuid(), "Notification1", "{}",
                                                                          buffer1, DateTime.Today);

            notificationsToRelease.Add(buffer1, notification1);
            inMemoryCrudRepository.Attach(notification1);

            bufferGovernor1.SelectNotificationsForReleaseAsync(inMemoryCrudRepository)
            .Returns(notificationsToRelease);

            await sut.Run();

            Assert.DoesNotContain(notification1, inMemoryCrudRepository.FindAll <BufferedNotification>());
        }
        public async Task HandleAsync_RemovesNotificationFromRepository()
        {
            var notificationsToRelease = new MultiValueDictionary <NotificationBuffer, BufferedNotification>();

            NotificationBuffer buffer1 = new NotificationBuffer(Guid.NewGuid(), bufferGovernor1.Id,
                                                                notificationPipeline1.Id);

            inMemoryCrudRepository.Attach(buffer1);
            BufferedNotification notification1 = new BufferedNotification(Guid.NewGuid(), "Notification1", "{}",
                                                                          buffer1, DateTime.Today);

            notificationsToRelease.Add(buffer1, notification1);
            inMemoryCrudRepository.Attach(notification1);

            bufferGovernor1.SelectNotificationsForReleaseAsync(inMemoryCrudRepository)
            .Returns(notificationsToRelease);

            var job = new ProcessBufferedNotificationsJob(Clock.Current.Now);
            await sut.HandleAsync(job, CancellationToken.None);

            Assert.DoesNotContain(notification1, inMemoryCrudRepository.FindAll <BufferedNotification>());
        }