Beispiel #1
0
        private ISubscriptionGroup CreateSystemUnderTest()
        {
            var messageBackoffStrategy = Substitute.For <IMessageBackoffStrategy>();
            var messageContextAccessor = Substitute.For <IMessageContextAccessor>();

            Logger.LogInformation("Creating MessageDispatcher with serialization register type {Type}",
                                  SerializationRegister.GetType().FullName);

            var dispatcher = new MessageDispatcher(
                SerializationRegister,
                Monitor,
                MiddlewareMap,
                LoggerFactory,
                messageBackoffStrategy,
                messageContextAccessor);

            var defaults = new SubscriptionGroupSettingsBuilder()
                           .WithDefaultConcurrencyLimit(ConcurrencyLimit);

            var subscriptionGroupFactory = new SubscriptionGroupFactory(
                dispatcher,
                Monitor,
                LoggerFactory);

            var settings = SetupBusConfig();

            return(subscriptionGroupFactory.Create(defaults, settings));
        }
 public void SetGroupSettings(
     SubscriptionGroupSettingsBuilder defaults,
     IDictionary <string, SubscriptionGroupConfigBuilder> settings)
 {
     _defaultSubscriptionGroupSettings = defaults;
     _subscriptionGroupSettings        =
         new ConcurrentDictionary <string, SubscriptionGroupConfigBuilder>(settings);
 }
Beispiel #3
0
        public async Task Sqs_Client_Throwing_Exceptions_Continues_To_Request_Messages()
        {
            // Arrange
            int messagesRequested  = 0;
            int messagesDispatched = 0;

            IEnumerable <Message> GetMessages()
            {
                Interlocked.Increment(ref messagesRequested);
                throw new Exception();
            }

            var queue = new FakeSqsQueue(ct => Task.FromResult(GetMessages()));


            var queues = new List <ISqsQueue> {
                queue
            };
            IMessageDispatcher dispatcher =
                new FakeDispatcher(() =>
            {
                Interlocked.Increment(ref messagesDispatched);
            });

            var defaults = new SubscriptionGroupSettingsBuilder()
                           .WithDefaultConcurrencyLimit(8);
            var settings = new Dictionary <string, SubscriptionGroupConfigBuilder>
            {
                { "test", new SubscriptionGroupConfigBuilder("test").AddQueues(queues) },
            };

            var subscriptionGroupFactory = new SubscriptionGroupFactory(
                dispatcher,
                MessageMonitor,
                LoggerFactory);

            ISubscriptionGroup collection = subscriptionGroupFactory.Create(defaults, settings);

            var cts = new CancellationTokenSource();

            // Act
            var runTask = collection.RunAsync(cts.Token);

            await Patiently.AssertThatAsync(_outputHelper,
                                            () =>
            {
                messagesRequested.ShouldBeGreaterThan(1, $"but was {messagesRequested}");
                messagesDispatched.ShouldBe(0, $"but was {messagesDispatched}");
            });

            cts.Cancel();
            await runTask.HandleCancellation();
        }
Beispiel #4
0
    /// <summary>
    /// Creates a <see cref="ISubscriptionGroup"/> for the given configuration.
    /// </summary>
    /// <param name="defaults">The default values to use while building each <see cref="SubscriptionGroup"/>.</param>
    /// <param name="subscriptionGroupSettings"></param>
    /// <returns>An <see cref="ISubscriptionGroup"/> to run.</returns>
    public ISubscriptionGroup Create(
        SubscriptionGroupSettingsBuilder defaults,
        IDictionary <string, SubscriptionGroupConfigBuilder> subscriptionGroupSettings)
    {
        ReceiveMiddleware receiveMiddleware = defaults.SqsMiddleware ?? _defaultSqsMiddleware;

        List <ISubscriptionGroup> groups = subscriptionGroupSettings
                                           .Values
                                           .Select(builder => Create(receiveMiddleware, builder.Build(defaults)))
                                           .ToList();

        return(new SubscriptionGroupCollection(
                   groups,
                   _loggerFactory.CreateLogger <SubscriptionGroupCollection>()));
    }
        public async Task ErrorHandlingAroundSqs_WithCustomPolicy_CanSwallowExceptions()
        {
            // Arrange
            int queueCalledCount       = 0;
            int dispatchedMessageCount = 0;
            var sqsQueue = TestQueue(() => Interlocked.Increment(ref queueCalledCount));

            var queues = new List <ISqsQueue> {
                sqsQueue
            };

            var config = new SubscriptionGroupSettingsBuilder()
                         .WithDefaultConcurrencyLimit(8);

            config.WithCustomMiddleware(
                new ErrorHandlingMiddleware <GetMessagesContext, IList <Message>, InvalidOperationException>());

            var settings = new Dictionary <string, SubscriptionGroupConfigBuilder>
            {
                { "test", new SubscriptionGroupConfigBuilder("test").AddQueues(queues) },
            };

            IMessageDispatcher dispatcher = new FakeDispatcher(() => Interlocked.Increment(ref dispatchedMessageCount));

            var groupFactory = new SubscriptionGroupFactory(
                dispatcher,
                MessageMonitor,
                LoggerFactory);

            ISubscriptionGroup collection = groupFactory.Create(config, settings);

            var cts        = new CancellationTokenSource();
            var completion = collection.RunAsync(cts.Token);

            await Patiently.AssertThatAsync(_outputHelper,
                                            () =>
            {
                queueCalledCount.ShouldBeGreaterThan(1);
                dispatchedMessageCount.ShouldBe(0);
            });

            cts.Cancel();
            // Act and Assert

            await completion.HandleCancellation();
        }
Beispiel #6
0
        private ISubscriptionGroup CreateSubscriptionGroup(
            IList <ISqsQueue> queues,
            IMessageDispatcher dispatcher)
        {
            var defaults = new SubscriptionGroupSettingsBuilder();

            var settings = new Dictionary <string, SubscriptionGroupConfigBuilder>
            {
                { "test", new SubscriptionGroupConfigBuilder("test").AddQueues(queues) },
            };

            var consumerGroupFactory = new SubscriptionGroupFactory(
                dispatcher,
                MessageMonitor,
                LoggerFactory);

            return(consumerGroupFactory.Create(defaults, settings));
        }
Beispiel #7
0
    private JustSaying.JustSayingBus CreateBus()
    {
        var config = Substitute.For <IMessagingConfig>();
        var serializationRegister = new MessageSerializationRegister(
            new NonGenericMessageSubjectProvider(),
            new NewtonsoftSerializationFactory());

        var bus = new JustSaying.JustSayingBus(config, serializationRegister, LoggerFactory, MessageMonitor);

        var defaultSubscriptionSettings = new SubscriptionGroupSettingsBuilder()
                                          .WithDefaultMultiplexerCapacity(1)
                                          .WithDefaultPrefetch(1)
                                          .WithDefaultBufferSize(1)
                                          .WithDefaultConcurrencyLimit(1);

        bus.SetGroupSettings(defaultSubscriptionSettings, new Dictionary <string, SubscriptionGroupConfigBuilder>());

        return(bus);
    }
        public async Task Message_Processing_Throwing_Exceptions_Continues_To_Request_Messages()
        {
            // Arrange
            int messagesRequested  = 0;
            int messagesDispatched = 0;

            var sqsQueue1 = TestQueue(() => GetErrorMessages(() => messagesRequested++));

            var queues = new List <ISqsQueue> {
                sqsQueue1
            };
            IMessageDispatcher dispatcher =
                new FakeDispatcher(() => Interlocked.Increment(ref messagesDispatched));

            var defaults = new SubscriptionGroupSettingsBuilder()
                           .WithDefaultConcurrencyLimit(1);
            var settings = new Dictionary <string, SubscriptionGroupConfigBuilder>
            {
                { "test", new SubscriptionGroupConfigBuilder("test").AddQueues(queues) },
            };

            var subscriptionGroupFactory = new SubscriptionGroupFactory(
                dispatcher,
                MessageMonitor,
                LoggerFactory);

            ISubscriptionGroup collection = subscriptionGroupFactory.Create(defaults, settings);

            var cts = new CancellationTokenSource();

            // Act
            var runTask = collection.RunAsync(cts.Token);

            await Patiently.AssertThatAsync(_outputHelper,
                                            () =>
            {
                messagesRequested.ShouldBeGreaterThan(1);
                messagesDispatched.ShouldBe(0);
            });

            cts.Cancel();
            await runTask.HandleCancellation();
        }
        public JustSayingBus(
            IMessagingConfig config,
            IMessageSerializationRegister serializationRegister,
            ILoggerFactory loggerFactory)
        {
            _loggerFactory = loggerFactory;
            _startupTasks  = new List <Func <Task> >();
            _log           = _loggerFactory.CreateLogger("JustSaying");

            Config  = config;
            Monitor = new NullOpMessageMonitor();
            MessageContextAccessor = new MessageContextAccessor();
            SerializationRegister  = serializationRegister;
            MiddlewareMap          = new MiddlewareMap();

            _publishersByType          = new Dictionary <Type, IMessagePublisher>();
            _subscriptionGroupSettings =
                new ConcurrentDictionary <string, SubscriptionGroupConfigBuilder>(StringComparer.Ordinal);
            _defaultSubscriptionGroupSettings = new SubscriptionGroupSettingsBuilder();
        }
Beispiel #10
0
    public JustSayingBus(
        IMessagingConfig config,
        IMessageSerializationRegister serializationRegister,
        ILoggerFactory loggerFactory,
        IMessageMonitor monitor)
    {
        _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
        _monitor       = monitor ?? throw new ArgumentNullException(nameof(monitor));

        _startupTasks = new List <Func <CancellationToken, Task> >();
        _log          = _loggerFactory.CreateLogger("JustSaying");

        Config = config;
        SerializationRegister = serializationRegister;
        MiddlewareMap         = new MiddlewareMap();

        _publishersByType          = new Dictionary <Type, IMessagePublisher>();
        _subscriptionGroupSettings =
            new ConcurrentDictionary <string, SubscriptionGroupConfigBuilder>(StringComparer.Ordinal);
        _defaultSubscriptionGroupSettings = new SubscriptionGroupSettingsBuilder();
    }
Beispiel #11
0
    /// <summary>
    /// Given a set of defaults and overrides from this builder, builds a concrete <see cref="SubscriptionGroupSettings"/>
    /// that can be passed to an <see cref="ISubscriptionGroupFactory"/> to build an <see cref="ISubscriptionGroup"/>.
    /// </summary>
    /// <param name="defaults">The default values to use if no override given.</param>
    /// <returns>A <see cref="SubscriptionGroupSettings"/>.</returns>
    public SubscriptionGroupSettings Build(SubscriptionGroupSettingsBuilder defaults)
    {
        if (defaults == null)
        {
            throw new InvalidOperationException("Defaults must be set before building settings.");
        }

        var settings = new SubscriptionGroupSettings(
            _groupName,
            _concurrencyLimit ?? defaults.ConcurrencyLimit,
            _bufferSize ?? defaults.BufferSize,
            _receiveBufferReadTimeout ?? defaults.ReceiveBufferReadTimeout,
            _receiveMessagesWaitTime ?? defaults.ReceiveMessagesWaitTime,
            _multiplexerCapacity ?? defaults.MultiplexerCapacity,
            _prefetch ?? defaults.Prefetch,
            _sqsQueues);

        settings.Validate();

        return(settings);
    }