Пример #1
0
        public void EnsureNotDuplicateAsync()
        {
            var value           = new byte[] { 1, 2, 3 };
            var acceptor        = Substitute.For <IMessageAcceptor>();
            var lastMiddleware  = Substitute.For <IEventMiddleware <string> >();
            var middlewarequeue = new MiddlewareQueue <string>(_settings);

            middlewarequeue.AddMiddleware(_deduplicationMiddleware);
            middlewarequeue.AddMiddleware(lastMiddleware);

            middlewarequeue.RunMiddlewaresAsync(
                new BasicDeliverEventArgs
            {
                BasicProperties = new BasicProperties(),
                Body            = value
            },
                null,
                acceptor,
                CancellationToken.None)
            .GetAwaiter().GetResult();

            middlewarequeue.RunMiddlewaresAsync(
                new BasicDeliverEventArgs
            {
                BasicProperties = new BasicProperties(),
                Body            = value
            },
                null,
                acceptor,
                CancellationToken.None)
            .GetAwaiter().GetResult();

            lastMiddleware.Received(1).ProcessAsync(Arg.Any <IEventContext <string> >());
        }
Пример #2
0
        public void SuccessfulPath()
        {
            var acceptor        = Substitute.For <IMessageAcceptor>();
            var middlewarequeue = new MiddlewareQueue <string>(_settings);

            middlewarequeue.AddMiddleware(_middleware);
            middlewarequeue.AddMiddleware(new ActualHandlerMiddleware <string>(_ => Task.CompletedTask));

            middlewarequeue.RunMiddlewaresAsync(null, null, acceptor, CancellationToken.None).GetAwaiter().GetResult();

            acceptor.Received(1).Accept();
        }
Пример #3
0
        public void ShouldResendToNextHandlerOnError()
        {
            var acceptor = Substitute.For <IMessageAcceptor>();
            var rootEventMiddlewareHandler = Substitute.For <IEventMiddleware <string> >();
            var middlewarequeue            = new MiddlewareQueue <string>(_settings);

            middlewarequeue.AddMiddleware(rootEventMiddlewareHandler);
            middlewarequeue.AddMiddleware(_middleware);
            middlewarequeue.AddMiddleware(new ActualHandlerMiddleware <string>(_ => throw new Exception()));

            middlewarequeue.RunMiddlewaresAsync(null, null, acceptor, CancellationToken.None).GetAwaiter().GetResult();

            rootEventMiddlewareHandler.Received(1).ProcessAsync(Arg.Any <IEventContext <string> >());
        }
Пример #4
0
        public RabbitMqSubscriber <TTopicModel> Start()
        {
            if (_messageDeserializer == null)
            {
                throw new InvalidOperationException("Please, specify message deserializer");
            }

            if (_eventHandler == null && _cancellableEventHandler == null)
            {
                throw new InvalidOperationException("Please, specify message handler");
            }

            if (_useAlternativeExchange && !_middlewareQueue.HasMiddleware <InMemoryDeduplicationMiddleware <TTopicModel> >())
            {
                throw new InvalidOperationException("Please, specify deduplicator");
            }

            if (_cancellationTokenSource == null || _cancellationTokenSource.IsCancellationRequested)
            {
                _cancellationTokenSource = new CancellationTokenSource();
            }

            if (_messageReadStrategy == null)
            {
                CreateDefaultBinding();
            }

            if (_thread != null)
            {
                return(this);
            }

            _reconnectionsInARowCount = 0;

            var actualHandlerMiddleware = _eventHandler != null
                ? new ActualHandlerMiddleware <TTopicModel>(_eventHandler)
                : new ActualHandlerMiddleware <TTopicModel>(_cancellableEventHandler);

            _middlewareQueue.AddMiddleware(actualHandlerMiddleware);

            _thread = new Thread(ReadThread);
            _thread.Start(_settings);

            if (_useAlternativeExchange)
            {
                if (_alternateThread != null)
                {
                    return(this);
                }

                // deep clone
                var settings = JsonConvert.DeserializeObject <RabbitMqSubscriptionSettings>(JsonConvert.SerializeObject(_settings));
                // start a new thread which will use 'AlternativeConnectionString'
                settings.ConnectionString = _alternativeExchangeConnString;
                _alternateThread          = new Thread(ReadThread);
                _alternateThread.Start(settings);
            }

            return(this);
        }