예제 #1
0
        public void WhenSameMessageLikeReviewIsSentMultipleTimesInRow_ThenThrottleThemAndOnlyEmitLatestMessage()
        {
            var testScheduler = new TestScheduler();
            var listener      = new List <string>();
            var throttler     = new MessageThrottler(testScheduler);

            throttler.Messages().Subscribe(x => listener.Add(x.model.Title));

            throttler.Emit("same_channel", new SlackMessageModel("same_title", "_"));
            throttler.Emit("same_channel", new SlackMessageModel("same_title", "_"));
            throttler.Emit("same_channel", new SlackMessageModel("same_title", "_"));

            testScheduler.AdvanceBy(TimeSpan.FromSeconds(20).Ticks);

            listener.Should().HaveCount(1);
        }
예제 #2
0
        public SlackMessaging(IOptions <AppOptions> options, ILogger <SlackMessaging> logger, MessageThrottler throttler)
        {
            this.options   = options.Value;
            this.throttler = throttler;

            if (string.IsNullOrEmpty(this.options.SlackIntegrationUri))
            {
                throw new ArgumentException(nameof(this.options.SlackIntegrationUri));
            }

            throttler.Messages()
            .Subscribe(async toSend =>
            {
                try
                {
                    await SendMessageToChannel(toSend.slackChannel, toSend.model);
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, $"Error occurred while tried to send message '{toSend.model.Title}' to channel '{toSend.slackChannel}', error: {ex}");
                }
            });
        }