Exemplo n.º 1
0
        public NotificationSimplePipelineShould()
        {
            _notification = new Notification();

            _processor = MockNotificationProcessor(_notification);

            _pipeline = new NotificationSimplePipeline <Notification>(_processor.Object);
        }
Exemplo n.º 2
0
        public NotificationSequentialPipelineShould()
        {
            _notification = new Notification();

            _processors = Many(() => MockNotificationProcessor(_notification));

            _pipeline = new NotificationSequentialPipeline <Notification>(_processors
                                                                          .Select(mock => mock.Object)
                                                                          .ToArray());
        }
Exemplo n.º 3
0
        public async Task HandleAsync(ProcessBufferedNotificationsJob job, CancellationToken cancellationToken)
        {
            foreach (IBufferGovernor bufferGovernor in bufferGovernors)
            {
                MultiValueDictionary <NotificationBuffer, BufferedNotification> toRelease = await bufferGovernor
                                                                                            .SelectNotificationsForReleaseAsync(crudRepository);

                var byPipeline = toRelease.GroupBy(x => x.Key.PipelineId);

                foreach (var pipelineNotifications in byPipeline)
                {
                    INotificationPipeline pipeline =
                        notificationPipelines.FirstOrDefault(x => x.Id == pipelineNotifications.Key);
                    if (pipeline == null)
                    {
                        throw new InvalidOperationException(
                                  $"Notification pipeline not found: {pipelineNotifications.Key}");
                    }

                    IReadOnlyCollection <INotification> notifications = pipelineNotifications
                                                                        .SelectMany(x => x.Value)
                                                                        .Select(
                        x =>
                        notificationSerializer.FromJson(new SerializedNotification()
                    {
                        NotificationJson      = x.NotificationJson,
                        NotificationClassName = x.NotificationClassName
                    }))
                                                                        .ToArray();

                    await pipeline.ProcessNotificationsAsync(notifications);

                    foreach (BufferedNotification buffNotification in pipelineNotifications.SelectMany(x => x.Value))
                    {
                        crudRepository.Remove(buffNotification);
                    }

                    await crudRepository.SaveChangesAsync();
                }
            }

            DateTimeOffset scheduledTime = job.ScheduledTime + TimeSpan.FromMinutes(1);

            if (scheduledTime <= Clock.Current.Now)
            {
                await inMemoryJobScheduler.EnqeueJobAsync(new ProcessBufferedNotificationsJob(Clock.Current.Now), null);
            }
            else
            {
                await inMemoryJobScheduler.ScheduleJobAsync(new ProcessBufferedNotificationsJob(scheduledTime), scheduledTime);
            }
        }
Exemplo n.º 4
0
 public BufferingNotificationChannel(
     IBufferGovernor bufferGovernor,
     IBufferSelector <T> bufferSelector,
     INotificationPipeline notificationPipeline,
     INotificationSerializer notificationSerializer,
     IBufferedNotificationStore bufferedNotificationStore)
 {
     this.bufferGovernor            = bufferGovernor;
     this.bufferSelector            = bufferSelector;
     this.notificationPipeline      = notificationPipeline;
     this.notificationSerializer    = notificationSerializer;
     this.bufferedNotificationStore = bufferedNotificationStore;
 }
        public BufferingNotificationChannelTests()
        {
            bufferGovernor = Substitute.For <IBufferGovernor>();
            bufferGovernor.Id.Returns(Guid.NewGuid());
            bufferSelector       = Substitute.For <IBufferSelector <Notification1> >();
            notificationPipeline = Substitute.For <INotificationPipeline>();
            notificationPipeline.Id.Returns(Guid.NewGuid());
            notificationSerializer    = Substitute.For <INotificationSerializer>();
            bufferedNotificationStore = Substitute.For <IBufferedNotificationStore>();

            FakeClock.Setup();

            sut = new BufferingNotificationChannel <Notification1>(bufferGovernor,
                                                                   bufferSelector, notificationPipeline, notificationSerializer,
                                                                   bufferedNotificationStore);
        }
Exemplo n.º 6
0
        public async Task Run()
        {
            try
            {
                foreach (IBufferGovernor bufferGovernor in bufferGovernors)
                {
                    MultiValueDictionary <NotificationBuffer, BufferedNotification> toRelease = await bufferGovernor
                                                                                                .SelectNotificationsForReleaseAsync(crudRepository);

                    foreach (var bufferPair in toRelease)
                    {
                        INotificationPipeline pipeline =
                            notificationPipelines.FirstOrDefault(x => x.Id == bufferPair.Key.PipelineId);
                        if (pipeline == null)
                        {
                            throw new InvalidOperationException(
                                      $"Notification pipeline not found: {bufferPair.Key.PipelineId}");
                        }

                        IEnumerable <INotification> notifications = bufferPair.Value
                                                                    .Select(
                            x =>
                            notificationSerializer.FromJson(new SerializedNotification()
                        {
                            NotificationJson      = x.NotificationJson,
                            NotificationClassName = x.NotificationClassName
                        }));

                        await pipeline.ProcessNotificationsAsync(notifications);

                        foreach (BufferedNotification buffNotification in bufferPair.Value)
                        {
                            crudRepository.Remove(buffNotification);
                        }
                    }
                }
            }
            finally
            {
                await crudRepository.SaveChangesAsync();
            }
        }
Exemplo n.º 7
0
        public BufferedNotificationProcessJobTests()
        {
            bufferGovernor1 = Substitute.For <IBufferGovernor>();
            bufferGovernor1.Id.Returns(Guid.NewGuid());
            inMemoryCrudRepository = new InMemoryCrudRepository();
            notificationSerializer = Substitute.For <INotificationSerializer>();
            notificationPipeline1  = Substitute.For <INotificationPipeline>();
            notificationPipeline1.Id.Returns(Guid.NewGuid());
            notificationPipeline2 = Substitute.For <INotificationPipeline>();
            notificationPipeline2.Id.Returns(Guid.NewGuid());

            notificationSerializer.FromJson(null).ReturnsForAnyArgs(ci =>
                                                                    new TestNotification()
            {
                Data     = ci.ArgAt <SerializedNotification>(0).NotificationJson,
                TypeName = ci.ArgAt <SerializedNotification>(0).NotificationClassName
            });

            sut = new BufferedNotificationProcessJob(new[] { bufferGovernor1 }, inMemoryCrudRepository,
                                                     new[] { notificationPipeline1, notificationPipeline2 }, notificationSerializer);
        }