public RabbitMqMessage(
            string message,
            IReadOnlyDictionary<string, string> headers,
            Exchange exchange,
            RoutingKey routingKey,
            MessageId messageId)
        {
            if (string.IsNullOrEmpty(message)) throw new ArgumentNullException(nameof(message));
            if (headers == null) throw new ArgumentNullException(nameof(headers));
            if (exchange == null) throw new ArgumentNullException(nameof(exchange));
            if (routingKey == null) throw new ArgumentNullException(nameof(routingKey));
            if (messageId == null) throw new ArgumentNullException(nameof(messageId));

            Message = message;
            Headers = headers;
            Exchange = exchange;
            RoutingKey = routingKey;
            MessageId = messageId;
        }
        public RabbitMqMessage CreateMessage(IDomainEvent domainEvent)
        {
            var serializedEvent = _eventJsonSerializer.Serialize(
                domainEvent.GetAggregateEvent(),
                domainEvent.Metadata);

            var routingKey = new RoutingKey(string.Format(
                "eventflow.domainevent.{0}.{1}.{2}",
                domainEvent.Metadata[MetadataKeys.AggregateName].ToSlug(),
                domainEvent.Metadata.EventName.ToSlug(),
                domainEvent.Metadata.EventVersion));
            var exchange = new Exchange("eventflow");

            var rabbitMqMessage = new RabbitMqMessage(
                serializedEvent.SerializedData,
                domainEvent.Metadata,
                exchange,
                routingKey,
                new MessageId(domainEvent.Metadata[MetadataKeys.EventId]));

            _log.Verbose("Create RabbitMQ message {0}", rabbitMqMessage);

            return rabbitMqMessage;
        }
Beispiel #3
0
        private static void SendMessages(
            IRabbitMqPublisher rabbitMqPublisher,
            int count,
            Exchange exchange,
            RoutingKey routingKey,
            ConcurrentBag<Exception> exceptions)
        {
            var guid = Guid.NewGuid();

            try
            {
                for (var i = 0; i < count; i++)
                {
                    var rabbitMqMessage = new RabbitMqMessage(
                        $"{guid}-{i}",
                        new Metadata(),
                        exchange,
                        routingKey,
                        new MessageId(Guid.NewGuid().ToString("D")));
                    rabbitMqPublisher.PublishAsync(CancellationToken.None, rabbitMqMessage).Wait();
                }
            }
            catch (Exception e)
            {
                exceptions.Add(e);
            }
        }
Beispiel #4
0
 protected override void OnServiceNotify(Niffle message, RoutingKey routingKey)
 {
     throw new NotImplementedException();
 }
        public void TokenAwarePolicyReturnsLocalReplicasFirst()
        {
            var hostList = new List<Host>
            {
                //5 local nodes and 4 remote
                TestHelper.CreateHost("0.0.0.1", "dc1"),
                TestHelper.CreateHost("0.0.0.2", "dc1"),
                TestHelper.CreateHost("0.0.0.3", "dc2"),
                TestHelper.CreateHost("0.0.0.4", "dc2"),
                TestHelper.CreateHost("0.0.0.5", "dc1"),
                TestHelper.CreateHost("0.0.0.6", "dc1"),
                TestHelper.CreateHost("0.0.0.7", "dc2"),
                TestHelper.CreateHost("0.0.0.8", "dc2"),
                TestHelper.CreateHost("0.0.0.9", "dc1")
            };
            var n = 2;
            var clusterMock = new Mock<ICluster>();
            clusterMock
                .Setup(c => c.AllHosts())
                .Returns(hostList)
                .Verifiable();
            clusterMock
                .Setup(c => c.GetReplicas(It.IsAny<string>(), It.IsAny<byte[]>()))
                .Returns<string, byte[]>((keyspace, key) =>
                {
                    var i = key[0];
                    return hostList.Where(h =>
                    {
                        //The host at with address == k || address == k + n
                        var address = TestHelper.GetLastAddressByte(h);
                        return address == i || address == i + n;
                    }).ToList();
                })
                .Verifiable();

            var policy = new TokenAwarePolicy(new DCAwareRoundRobinPolicy("dc1", 2));
            policy.Initialize(clusterMock.Object);

            //key for host :::1 and :::3
            var k = new RoutingKey { RawRoutingKey = new byte[] { 1 } };
            var hosts = policy.NewQueryPlan(null, new SimpleStatement().SetRoutingKey(k)).ToList();
            //5 local hosts + 2 remote hosts
            Assert.AreEqual(7, hosts.Count);
            //local replica first
            Assert.AreEqual(1, TestHelper.GetLastAddressByte(hosts[0]));
            //remote replica last
            Assert.AreEqual(3, TestHelper.GetLastAddressByte(hosts[6]));
            clusterMock.Verify();

            //key for host :::2 and :::5
            k = new RoutingKey { RawRoutingKey = new byte[] { 2 } };
            n = 3;
            hosts = policy.NewQueryPlan(null, new SimpleStatement().SetRoutingKey(k)).ToList();
            Assert.AreEqual(7, hosts.Count);
            //local replicas first
            Assert.AreEqual(2, TestHelper.GetLastAddressByte(hosts[0]));
            Assert.AreEqual(5, TestHelper.GetLastAddressByte(hosts[1]));
            //next should be local nodes
            Assert.AreEqual("dc1", hosts[2].Datacenter);
            Assert.AreEqual("dc1", hosts[3].Datacenter);
            Assert.AreEqual("dc1", hosts[4].Datacenter);
            clusterMock.Verify();
        }
Beispiel #6
0
        private void CreateQueue(AmazonSQSClient sqsClient, Connection connection, ChannelName queueName, RoutingKey topicName, RegionEndpoint region)
        {
            _logger.Value.Debug($"Queue does not exist, creating queue: {queueName} subscribed to {topicName} on {_awsConnection.Region}");
            _queueUrl = "no queue defined";
            try
            {
                var request = new CreateQueueRequest(queueName)
                {
                    Attributes =
                    {
                        { "VisibilityTimeout",             connection.VisibilityTimeout.ToString()            },
                        { "ReceiveMessageWaitTimeSeconds", ToSecondsAsString(connection.TimeoutInMiliseconds) }
                    },
                    Tags =
                    {
                        { "Source", "Brighter"           },
                        { "Topic",  $"{topicName.Value}" }
                    }
                };
                var response = sqsClient.CreateQueueAsync(request).Result;
                _queueUrl = response.QueueUrl;
                if (!string.IsNullOrEmpty(_queueUrl))
                {
                    _logger.Value.Debug($"Queue created: {_queueUrl}");
                    using (var snsClient = new AmazonSimpleNotificationServiceClient(_awsConnection.Credentials, _awsConnection.Region))
                    {
                        CreateTopic(topicName, sqsClient, snsClient);
                        BindSubscription(sqsClient, snsClient);
                    }
                }
                else
                {
                    throw new InvalidOperationException($"Could not create queue: {queueName} subscribed to {_channelTopicARN} on {_awsConnection.Region}");
                }
            }
            catch (AggregateException ae)
            {
                //TODO: We need some retry semantics here
                //TODO: We need to flatten the ae and handle some of these with ae.Handle((x) => {})
                ae.Handle(ex =>
                {
                    if (ex is QueueDeletedRecentlyException)
                    {
                        //QueueDeletedRecentlyException - wait 30 seconds then retry
                        //Although timeout is 60s, we could be partway through that, so apply Copernican Principle
                        //and assume we are halfway through
                        var error = $"Could not create queue {queueName} because {ae.Message} waiting 60s to retry";
                        _logger.Value.Error(error);
                        Thread.Sleep(TimeSpan.FromSeconds(30));
                        throw new ChannelFailureException(error, ae);
                    }

                    if (ex is AmazonSQSException || ex is HttpErrorResponseException)
                    {
                        var error = $"Could not create queue {_queueUrl} subscribed to topic {topicName} in region {region.DisplayName} because {ae.Message}";
                        _logger.Value.Error(error);
                        throw new InvalidOperationException(error, ex);
                    }

                    return(false);
                });
            }
        }
Beispiel #7
0
 protected override void AddListeningRoutingKeys(ref List <RoutingKey> routingKeys)
 {
     //Listen for OnTick
     routingKeys.Add(RoutingKey.Create(Source.WILDCARD, Messaging.RabbitMQ.Action.WILDCARD, Event.ONTICK));
 }
        public void GivenConnectionWhenFailToDeserializeShouldNackAndPublishFailed()
        {
            var          exchange    = Exchange.Create("test", ExchangeType.Direct);
            var          queue       = Queue.Create("test.requested");
            var          routingKey  = RoutingKey.Create("test.route");
            var          body        = Encoding.UTF8.GetBytes("test");
            const ushort deliveryTag = 1;

            _busSerializerMock.Setup(x => x.Deserialize <string>(body))
            .ThrowsAsync(new Exception("Test message"))
            .Verifiable();

            var autoResetEvent = new AutoResetEvent(false);

            // _channelMock.Setup(x => x.QueueDeclare(It.Is))

            _channelMock.Setup(x => x.BasicConsume(
                                   queue.Name.Value,
                                   false,
                                   It.IsAny <string>(),
                                   It.IsAny <bool>(),
                                   It.IsAny <bool>(),
                                   It.IsAny <IDictionary <string, object> >(),
                                   It.IsAny <IBasicConsumer>()))
            .Callback((string queueName, bool autoAck, string consumerTag, bool noLocal, bool exclusive,
                       IDictionary <string, object> _, IBasicConsumer consumer) =>
            {
                ((AsyncEventingBasicConsumer)consumer).HandleBasicDeliver(
                    consumerTag,
                    deliveryTag,
                    false,
                    exchange.Name.Value,
                    routingKey.Value,
                    _basicPropertiesMock.Object,
                    body).Wait();
            })
            .Returns(Guid.NewGuid().ToString());

            _publishBatchMock.Setup(x => x.Publish())
            .Callback(() => autoResetEvent.Set())
            .Verifiable();

            _busConnection.Subscribe <string>(
                exchange,
                queue,
                routingKey,
                10,
                (scope, @event) => Task.CompletedTask);

            autoResetEvent.WaitOne(TimeSpan.FromSeconds(5));

            _loggerMock.Verify(x => x.WriteException(It.IsAny <string>(), It.IsAny <Exception>(),
                                                     It.IsAny <KeyValuePair <string, object>[]>()));
            _channelMock.Verify(x => x.BasicNack(deliveryTag, false, false));

            _publishBatchMock.VerifyAll();

            _channelMock.Verify(x => x.QueueDeclare(
                                    It.Is((string y) => y.EndsWith("-failed")),
                                    true,
                                    false,
                                    false,
                                    It.IsAny <IDictionary <string, object> >()), Times.Once());
            _publishBatchMock.Verify(x => x.Add(
                                         ExchangeName.Default.Value,
                                         It.Is((string y) => y.StartsWith(queue.Name.Value) && y.EndsWith("-failed")),
                                         false,
                                         _basicPropertiesMock.Object,
                                         It.IsAny <byte[]>()), Times.Once());
        }
 protected void query(CcmClusterInfo clusterInfo, int n, bool usePrepared, ConsistencyLevel cl)
 {
     if (usePrepared)
     {
         BoundStatement bs = prepared.Bind(0);
         for (int i = 0; i < n; ++i)
         {
             IPAddress ccord;
             ConsistencyLevel cac;
             var rs = clusterInfo.Session.Execute(bs);
             {
                 ccord = rs.Info.QueriedHost;
                 cac = rs.Info.AchievedConsistency;
             }
             addCoordinator(ccord, cac);
         }
     }
     else
     {
         var routingKey = new RoutingKey();
         routingKey.RawRoutingKey = Enumerable.Repeat((byte) 0x00, 4).ToArray();
         for (int i = 0; i < n; ++i)
         {
             IPAddress ccord;
             ConsistencyLevel cac;
             var rs =
                     clusterInfo.Session.Execute(
                         new SimpleStatement(String.Format("SELECT * FROM {0} WHERE k = 0", TABLE)).SetRoutingKey(routingKey)
                                                                                                   .SetConsistencyLevel(cl));
             {
                 ccord = rs.Info.QueriedHost;
                 cac = rs.Info.AchievedConsistency;
                 Trace.TraceInformation("Query {0} executed by {1} with consistency {2}", i, ccord, cac);
             }
             addCoordinator(ccord, cac);
         }
     }
 }
Beispiel #10
0
 public void Init()
 {
     AutoScaleConsumer = new Consumer(Adapter, Exchange.AUTOSCALEX, RoutingKey.Create(ScalableConsumer.GetQueueName()).ToList());
     AutoScaleConsumer.MessageReceived += OnMessageReceived;
     Adapter.ConsumeAsync(AutoScaleConsumer);
 }
Beispiel #11
0
        public RMQMessageConsumerRetryDLQTests()
        {
            Guid   correlationId = Guid.NewGuid();
            string contentType   = "text\\plain";
            var    channelName   = $"Requeue-Limit-Tests-{Guid.NewGuid().ToString()}";

            _topicName = $"Requeue-Limit-Tests-{Guid.NewGuid().ToString()}";
            var routingKey = new RoutingKey(_topicName);

            //what do we send
            var myCommand = new MyDeferredCommand {
                Value = "Hello Requeue"
            };

            _message = new Message(
                new MessageHeader(myCommand.Id, _topicName, MessageType.MT_COMMAND, correlationId, "", contentType),
                new MessageBody(JsonSerializer.Serialize((object)myCommand, JsonSerialisationOptions.Options))
                );

            var deadLetterQueueName  = $"{_message.Header.Topic}.DLQ";
            var deadLetterRoutingKey = $"{_message.Header.Topic}.DLQ";

            var subscription = new RmqSubscription <MyCommand>(
                name: new SubscriptionName(channelName),
                channelName: new ChannelName(channelName),
                routingKey: routingKey,
                //after 2 retries, fail and move to the DLQ
                requeueCount: 2,
                //delay before re-queuing
                requeueDelayInMilliseconds: 50,
                deadLetterChannelName: new ChannelName(deadLetterQueueName),
                deadLetterRoutingKey: deadLetterRoutingKey,
                makeChannels: OnMissingChannel.Create
                );

            var rmqConnection = new RmqMessagingGatewayConnection
            {
                AmpqUri            = new AmqpUriSpecification(new Uri("amqp://*****:*****@localhost:5672/%2f")),
                Exchange           = new Exchange("paramore.brighter.exchange"),
                DeadLetterExchange = new Exchange("paramore.brighter.exchange.dlq")
            };

            //how do we send to the queue
            _sender = new RmqMessageProducer(rmqConnection, new RmqPublication());

            //set up our receiver
            _channelFactory = new ChannelFactory(new RmqMessageConsumerFactory(rmqConnection));
            _channel        = _channelFactory.CreateChannel(subscription);

            //how do we handle a command
            IHandleRequests <MyDeferredCommand> handler = new MyDeferredCommandHandler();

            //hook up routing for the command processor
            var subscriberRegistry = new SubscriberRegistry();

            subscriberRegistry.Register <MyDeferredCommand, MyDeferredCommandHandler>();

            //once we read, how do we dispatch to a handler. N.B. we don't use this for reading here
            _commandProcessor = new CommandProcessor(
                subscriberRegistry: subscriberRegistry,
                handlerFactory: new QuickHandlerFactory(() => handler),
                requestContextFactory: new InMemoryRequestContextFactory(),
                policyRegistry: new PolicyRegistry()
                );

            //pump messages from a channel to a handler - in essence we are building our own dispatcher in this test
            IAmAMessageMapper <MyDeferredCommand> mapper = new MyDeferredCommandMessageMapper(_topicName);

            _messagePump = new MessagePumpBlocking <MyDeferredCommand>(_commandProcessor, mapper)
            {
                Channel = _channel, TimeoutInMilliseconds = 5000, RequeueCount = 3
            };

            _deadLetterConsumer = new RmqMessageConsumer(
                connection: rmqConnection,
                queueName: deadLetterQueueName,
                routingKey: deadLetterRoutingKey,
                isDurable: false,
                makeChannels: OnMissingChannel.Assume
                );
        }
Beispiel #12
0
        protected override void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            DateTime   niffleTimeStamp = DateTime.FromBinary(e.Message.TimeStamp);
            RoutingKey routingKey      = new RoutingKey(e.EventArgs.RoutingKey);
            string     action          = routingKey.GetAction();
            string     _event          = routingKey.GetEvent();
            Trade      trade           = e.Message.Trade;

            switch (routingKey.GetActionAsEnum())
            {
            case Messaging.RabbitMQ.Action.TRADEMANAGEMENT:
                //Save the trade to execute against the Linked Trade label
                StateManager.UpdateStateLinkedTradeAsync(trade.LinkedTradeLabel, trade.Order.Label, trade);
                break;
            }

            // Need to have another transforming service if FIX service only publishes raw messages
            // Need to have a service that listens for routing key FIXServiceName.*.*
            // This service will then have to unpack the FIX API message and construct the appropriate msg and routing key

            switch (routingKey.GetEventAsEnum())
            {
            case Messaging.RabbitMQ.Event.ONPOSITIONCLOSED:
                switch (e.Message.Trade.Order.StateChange)
                {
                case Order.Types.StateChange.Filled:         //The stateChange should be used to set the routing key and not needed here.
                    //Check if this is a linked trade
                    if (trade.IsLinkedTrade)
                    {
                        //Save the state of the filled linked trade
                        StateManager.UpdateStateLinkedTradeAsync(trade.LinkedTradeLabel, trade.Order.Label, trade);

                        //Find all other linked trades and cancel them
                        StateManager.FindAllLinkedTradesExcludingAsync(trade.LinkedTradeLabel, trade.Order.Label, CancelTradeOperation);
                    }
                    //This is a masker trade therefore place any linked SL and TP trades
                    else
                    {
                        //Find all linked trades and excute them
                        StateManager.FindAllLinkedTradesAsync(trade.LinkedTradeLabel, trade.Order.PosMaintRptID, ExecuteTradeOperation);
                    }
                    break;

                case Order.Types.StateChange.Canceled:
                    //If this is a masker trade then cancel any linked SL and TP trades
                    if (!trade.IsLinkedTrade)
                    {
                        //Find all linked trades and cancel them
                        StateManager.FindAllLinkedTradesAsync(trade.LinkedTradeLabel, trade.Order.PosMaintRptID, CancelTradeOperation);
                    }
                    break;
                }

                //Order filled
                if (e.Message.Trade.Order.StateChange == Order.Types.StateChange.Filled)
                {
                }
                break;

            default:
                return;
            }
            ;
        }
Beispiel #13
0
        public void GivenRoutingKeyWhenEmptyShouldThrowsArgumentNullException(string routing)
        {
            Func <RoutingKey> action = () => RoutingKey.Create(routing);

            action.Should().Throw <ArgumentNullException>();
        }
 /// <summary>
 ///  Set the routing key for this query. <p> See
 ///  <link>#setRoutingKey(ByteBuffer)</link> for more information. This method is
 ///  a variant for when the query partition key is composite and thus the routing
 ///  key must be built from multiple values.</p>
 /// </summary>
 /// <param name="routingKeyComponents"> the raw (binary) values to compose to
 ///  obtain the routing key. </param>
 ///
 /// <returns>this <code>PreparedStatement</code> object.
 ///  <see>Query#GetRoutingKey</returns>
 public PreparedStatement SetRoutingKey(params RoutingKey[] routingKeyComponents)
 {
     this._routingKey = RoutingKey.Compose(routingKeyComponents); return(this);
 }
 /// <summary>
 ///  Set the routing key for this query. <p> See
 ///  <link>#setRoutingKey(ByteBuffer)</link> for more information. This method is
 ///  a variant for when the query partition key is composite and thus the routing
 ///  key must be built from multiple values.</p>
 /// </summary>
 /// <param name="routingKeyComponents"> the raw (binary) values to compose to
 ///  obtain the routing key. </param>
 /// 
 /// <returns>this <code>PreparedStatement</code> object.
 ///  <see>Query#GetRoutingKey</returns>
 public PreparedStatement SetRoutingKey(params RoutingKey[] routingKeyComponents)
 {
     this._routingKey = RoutingKey.Compose(routingKeyComponents); return this;
 }
Beispiel #16
0
        public override Task HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey,
                                                IBasicProperties properties, byte[] body)
        {
            var @event = new BasicDeliverEventArgs(consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body);

            try
            {
                var task = new Task(() =>
                {
                    try
                    {
                        var message = Message <T> .Create(_channel, _options.Exchange, _options.Queue, RoutingKey.Create(routingKey), _options.Serializer, @event,
                                                          (OnDone, OnFail));
                        using var scope = _scopeFactory.CreateScope();
                        try
                        {
                            _options.OnNext(scope, message).GetAwaiter().GetResult();
                            if (_options.AutoAck)
                            {
                                message.Complete();
                            }
                        }
                        catch (Exception exception)
                        {
                            message.Fail(exception);
                        }
                    }
                    catch (Exception exception)
                    {
                        _logger?.WriteException(nameof(Consumer <T>), exception,
                                                new KeyValuePair <string, object>("Event", Encoding.UTF8.GetString(@event.Body)));
                        var failedQueue      = _options.Queue.CreateFailedQueue();
                        var failedRoutingKey = RoutingKey.Create(failedQueue.Name.Value);
                        _connection.Publish(Exchange.Default, failedQueue, failedRoutingKey,
                                            ErrorMessage.Create(@event.Body, @event.BasicProperties));
                        _channel.BasicNack(@event.DeliveryTag, false, false);
                    }
                });
                _tasks.Add(task);
            }
            catch (Exception ex)
            {
                _logger?.WriteException(typeof(T).Name, ex,
                                        new KeyValuePair <string, object>("args", @event));
                _channel.BasicNack(@event.DeliveryTag, false, true);
            }

            return(Task.CompletedTask);
        }
 protected void query(CCMBridge.CCMCluster c, int n, bool usePrepared, ConsistencyLevel cl)
 {
     if (usePrepared)
     {
         BoundStatement bs = prepared.Bind(0);
         for (int i = 0; i < n; ++i)
         {
             IPAddress ccord;
             ConsistencyLevel cac;
             using (var rs = c.Session.Execute(bs))
             {
                 ccord = rs.Info.QueriedHost;
                 cac = rs.Info.AchievedConsistency;
             }
             addCoordinator(ccord, cac);
         }
     }
     else
     {
         RoutingKey routingKey = new RoutingKey();
         routingKey.RawRoutingKey = Enumerable.Repeat((byte)0x00, 4).ToArray();
         for (int i = 0; i < n; ++i)
         {
             IPAddress ccord;
             ConsistencyLevel cac;
             using (var rs = c.Session.Execute(new SimpleStatement(String.Format("SELECT * FROM {0} WHERE k = 0", TABLE)).SetRoutingKey(routingKey).SetConsistencyLevel(cl)))
             {
                 ccord = rs.Info.QueriedHost;
                 cac = rs.Info.AchievedConsistency;
                 Console.WriteLine(string.Format("Query {0} executed by {1} with consistency {2}", i.ToString(), ccord.ToString(), cac.ToString()));
             }
             addCoordinator(ccord, cac);
         }
     }
 }
Beispiel #18
0
 public string GetTypeName()
 {
     string[] result = RoutingKey.Split('.');
     return(result.Length > 1 ? result[0] : RoutingKey);
 }
 protected override List <RoutingKey> SetListeningRoutingKeys()
 {
     return(RoutingKey.Create(Source.WILDCARD, Messaging.RabbitMQ.Action.WILDCARD, Event.ONTICK).ToList());
 }
Beispiel #20
0
        public void TokenAwarePolicyReturnsLocalReplicasFirst()
        {
            var hostList = new List <Host>
            {
                //5 local nodes and 4 remote
                TestHelper.CreateHost("0.0.0.1", "dc1"),
                TestHelper.CreateHost("0.0.0.2", "dc1"),
                TestHelper.CreateHost("0.0.0.3", "dc2"),
                TestHelper.CreateHost("0.0.0.4", "dc2"),
                TestHelper.CreateHost("0.0.0.5", "dc1"),
                TestHelper.CreateHost("0.0.0.6", "dc1"),
                TestHelper.CreateHost("0.0.0.7", "dc2"),
                TestHelper.CreateHost("0.0.0.8", "dc2"),
                TestHelper.CreateHost("0.0.0.9", "dc1")
            };
            var n           = 2;
            var clusterMock = new Mock <ICluster>();

            clusterMock
            .Setup(c => c.AllHosts())
            .Returns(hostList)
            .Verifiable();
            clusterMock
            .Setup(c => c.GetReplicas(It.IsAny <string>(), It.IsAny <byte[]>()))
            .Returns <string, byte[]>((keyspace, key) =>
            {
                var i = key[0];
                return(hostList.Where(h =>
                {
                    //The host at with address == k || address == k + n
                    var address = TestHelper.GetLastAddressByte(h);
                    return address == i || address == i + n;
                }).ToList());
            })
            .Verifiable();

            var policy = new TokenAwarePolicy(new DCAwareRoundRobinPolicy("dc1", 2));

            policy.Initialize(clusterMock.Object);

            //key for host :::1 and :::3
            var k = new RoutingKey {
                RawRoutingKey = new byte[] { 1 }
            };
            var hosts = policy.NewQueryPlan(null, new SimpleStatement().SetRoutingKey(k)).ToList();

            //5 local hosts + 2 remote hosts
            Assert.AreEqual(7, hosts.Count);
            //local replica first
            Assert.AreEqual(1, TestHelper.GetLastAddressByte(hosts[0]));
            clusterMock.Verify();

            //key for host :::2 and :::5
            k = new RoutingKey {
                RawRoutingKey = new byte[] { 2 }
            };
            n     = 3;
            hosts = policy.NewQueryPlan(null, new SimpleStatement().SetRoutingKey(k)).ToList();
            Assert.AreEqual(7, hosts.Count);
            //local replicas first
            CollectionAssert.AreEquivalent(new[] { 2, 5 }, hosts.Take(2).Select(TestHelper.GetLastAddressByte));
            //next should be local nodes
            Assert.AreEqual("dc1", hosts[2].Datacenter);
            Assert.AreEqual("dc1", hosts[3].Datacenter);
            Assert.AreEqual("dc1", hosts[4].Datacenter);
            clusterMock.Verify();
        }
        //Note that we assume here that topic names are globally unique, if not provide the topic ARN directly in the SNSAttributes of the subscription
        private static (bool success, string topicArn) FindTopicByName(RoutingKey topicName, AmazonSimpleNotificationServiceClient snsClient)
        {
            var topic = snsClient.FindTopicAsync(topicName.Value).GetAwaiter().GetResult();

            return(topic != null, topic?.TopicArn);
        }
Beispiel #22
0
        public void TokenAwarePolicyRoundRobinsOnLocalReplicas()
        {
            var hostList = new List <Host>
            {
                //5 local nodes and 4 remote
                TestHelper.CreateHost("0.0.0.1", "dc1"),
                TestHelper.CreateHost("0.0.0.2", "dc1"),
                TestHelper.CreateHost("0.0.0.3", "dc2"),
                TestHelper.CreateHost("0.0.0.4", "dc2"),
                TestHelper.CreateHost("0.0.0.5", "dc1"),
                TestHelper.CreateHost("0.0.0.6", "dc1"),
                TestHelper.CreateHost("0.0.0.7", "dc2"),
                TestHelper.CreateHost("0.0.0.8", "dc2"),
                TestHelper.CreateHost("0.0.0.9", "dc1")
            };
            var clusterMock = new Mock <ICluster>(MockBehavior.Strict);

            clusterMock
            .Setup(c => c.AllHosts())
            .Returns(hostList)
            .Verifiable();
            clusterMock
            .Setup(c => c.GetReplicas(It.IsAny <string>(), It.IsAny <byte[]>()))
            .Returns <string, byte[]>((keyspace, key) =>
            {
                var i = key[0];
                return(hostList.Where(h =>
                {
                    //The host at with address == k and the next one
                    var address = TestHelper.GetLastAddressByte(h);
                    return address == i || address == i + 1;
                }).ToList());
            })
            .Verifiable();

            var policy = new TokenAwarePolicy(new DCAwareRoundRobinPolicy("dc1", 2));

            policy.Initialize(clusterMock.Object);

            var firstHosts = new ConcurrentBag <Host>();
            var k          = new RoutingKey {
                RawRoutingKey = new byte[] { 1 }
            };
            // key for host :::1 and :::2
            const int times  = 10000;
            Action    action = () =>
            {
                var h = policy.NewQueryPlan(null, new SimpleStatement().SetRoutingKey(k)).First();
                firstHosts.Add(h);
            };

            TestHelper.ParallelInvoke(action, times);
            Assert.AreEqual(times, firstHosts.Count);
            double queryPlansWithHost1AsFirst = firstHosts.Count(h => TestHelper.GetLastAddressByte(h) == 1);
            double queryPlansWithHost2AsFirst = firstHosts.Count(h => TestHelper.GetLastAddressByte(h) == 2);

            Assert.AreEqual(times, queryPlansWithHost1AsFirst + queryPlansWithHost2AsFirst);
            // Around half will to one and half to the other
            Assert.That(queryPlansWithHost1AsFirst / times, Is.GreaterThan(0.45).And.LessThan(0.55));
            Assert.That(queryPlansWithHost2AsFirst / times, Is.GreaterThan(0.45).And.LessThan(0.55));
            clusterMock.Verify();
        }
 public bool Matches(string routingKey) => RoutingKey.LastSuffixMatches(routingKey, lastRoutingKeySuffix);
Beispiel #24
0
        internal static void Bind(this Queue queue, IModel channel, Exchange exchange, RoutingKey routingKey)
        {
            if (queue == null)
            {
                throw new ArgumentNullException(nameof(queue));
            }
            if (channel == null)
            {
                throw new ArgumentNullException(nameof(channel));
            }
            if (exchange == null)
            {
                throw new ArgumentNullException(nameof(exchange));
            }
            if (routingKey == null)
            {
                throw new ArgumentNullException(nameof(routingKey));
            }

            if (exchange.IsDefault)
            {
                return;
            }
            var arguments = queue.Arguments.ToDictionary(x => x.Key, x => x.Value);

            channel.QueueBind(queue.Name.Value, exchange.Name.Value, routingKey.Value, arguments);
        }
Beispiel #25
0
 override protected void OnServiceNotify(Niffle message, RoutingKey routingKey)
 {
     //Nothing requried
 }
Beispiel #26
0
 abstract protected void OnServiceNotify(Niffle message, RoutingKey routingKey);
Beispiel #27
0
 public Connection(ConnectionName name, IAmAChannelFactory channelFactory, Type dataType, ChannelName channelName, RoutingKey routingKey, int noOfPerformers = 1, int timeoutInMilliseconds = 300, int requeueCount = -1, int requeueDelayInMilliseconds = 0, int unacceptableMessageLimit = 0, bool isDurable = false, bool isAsync = false)
     : this(dataType, name, channelName, routingKey, noOfPerformers, timeoutInMilliseconds, requeueCount, requeueDelayInMilliseconds, unacceptableMessageLimit, isDurable, isAsync, channelFactory)
 {
 }
        public KafkaMessageConsumer(
            KafkaMessagingGatewayConfiguration configuration,
            RoutingKey routingKey,
            string groupId,
            AutoOffsetReset offsetDefault         = AutoOffsetReset.Earliest,
            int sessionTimeoutMs                  = 10000,
            int maxPollIntervalMs                 = 10000,
            IsolationLevel isolationLevel         = IsolationLevel.ReadCommitted,
            long commitBatchSize                  = 10,
            int sweepUncommittedOffsetsIntervalMs = 30000,
            int readCommittedOffsetsTimeoutMs     = 5000,
            int numPartitions             = 1,
            short replicationFactor       = 1,
            int topicFindTimeoutMs        = 10000,
            OnMissingChannel makeChannels = OnMissingChannel.Create
            )
        {
            if (configuration is null)
            {
                throw new ConfigurationException("You must set a KafkaMessaginGatewayConfiguration to connect to a broker");
            }

            if (routingKey is null)
            {
                throw new ConfigurationException("You must set a RoutingKey as the Topic for the consumer");
            }

            if (groupId is null)
            {
                throw new ConfigurationException("You must set a GroupId for the consumer");
            }

            Topic = routingKey;

            _clientConfig = new ClientConfig
            {
                BootstrapServers = string.Join(",", configuration.BootStrapServers),
                ClientId         = configuration.Name,
                Debug            = configuration.Debug,
                SaslMechanism    = configuration.SaslMechanisms.HasValue ? (Confluent.Kafka.SaslMechanism?)((int)configuration.SaslMechanisms.Value) : null,
                                       SaslKerberosPrincipal = configuration.SaslKerberosPrincipal,
                                       SaslUsername          = configuration.SaslUsername,
                                       SaslPassword          = configuration.SaslPassword,
                                       SecurityProtocol      = configuration.SecurityProtocol.HasValue ? (Confluent.Kafka.SecurityProtocol?)((int)configuration.SecurityProtocol.Value) : null,
                                                                   SslCaLocation = configuration.SslCaLocation
            };
            _consumerConfig = new ConsumerConfig(_clientConfig)
            {
                GroupId               = groupId,
                ClientId              = configuration.Name,
                AutoOffsetReset       = offsetDefault,
                BootstrapServers      = string.Join(",", configuration.BootStrapServers),
                SessionTimeoutMs      = sessionTimeoutMs,
                MaxPollIntervalMs     = maxPollIntervalMs,
                EnablePartitionEof    = true,
                AllowAutoCreateTopics = false, //We will do this explicit always so as to allow us to set parameters for the topic
                IsolationLevel        = isolationLevel,
                //We commit the last offset for acknowledged requests when a batch of records has been processed.
                EnableAutoOffsetStore = false,
                EnableAutoCommit      = false,
                // https://www.confluent.io/blog/cooperative-rebalancing-in-kafka-streams-consumer-ksqldb/
                PartitionAssignmentStrategy = PartitionAssignmentStrategy.CooperativeSticky,
            };

            _maxBatchSize                  = commitBatchSize;
            _sweepUncommittedInterval      = TimeSpan.FromMilliseconds(sweepUncommittedOffsetsIntervalMs);
            _readCommittedOffsetsTimeoutMs = readCommittedOffsetsTimeoutMs;

            _consumer = new ConsumerBuilder <string, string>(_consumerConfig)
                        .SetPartitionsAssignedHandler((consumer, list) =>
            {
                var partitions = list.Select(p => $"{p.Topic} : {p.Partition.Value}");

                s_logger.LogInformation("Partition Added {Channels}", String.Join(",", partitions));

                _partitions.AddRange(list);
            })
                        .SetPartitionsRevokedHandler((consumer, list) =>
            {
                _consumer.Commit(list);
                var revokedPartitions = list.Select(tpo => $"{tpo.Topic} : {tpo.Partition}").ToList();

                s_logger.LogInformation("Partitions for consumer revoked {Channels}", string.Join(",", revokedPartitions));

                _partitions = _partitions.Where(tp => list.All(tpo => tpo.TopicPartition != tp)).ToList();
            })
                        .SetPartitionsLostHandler((consumer, list) =>
            {
                var lostPartitions = list.Select(tpo => $"{tpo.Topic} : {tpo.Partition}").ToList();

                s_logger.LogInformation("Partitions for consumer lost {Channels}", string.Join(",", lostPartitions));

                _partitions = _partitions.Where(tp => list.All(tpo => tpo.TopicPartition != tp)).ToList();
            })
                        .SetErrorHandler((consumer, error) =>
            {
                s_logger.LogError("Code: {ErrorCode}, Reason: {ErrorMessage}, Fatal: {FatalError}", error.Code,
                                  error.Reason, error.IsFatal);
            })
                        .Build();

            s_logger.LogInformation("Kakfa consumer subscribing to {Topic}", Topic);
            _consumer.Subscribe(new [] { Topic.Value });

            _creator = new KafkaMessageCreator();

            MakeChannels       = makeChannels;
            Topic              = routingKey;
            NumPartitions      = numPartitions;
            ReplicationFactor  = replicationFactor;
            TopicFindTimeoutMs = topicFindTimeoutMs;

            EnsureTopic();
        }
Beispiel #29
0
 protected override void OnStateUpdate(Niffle message, RoutingKey routingKey)
 {
     throw new NotImplementedException();
 }
Beispiel #30
0
        protected override void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            if (!IsInitialised)
            {
                return;
            }
            DateTime   niffleTimeStamp = DateTime.FromBinary(e.Message.TimeStamp);
            RoutingKey routingKey      = new RoutingKey(e.EventArgs.RoutingKey);
            string     source          = routingKey.GetSource();
            string     action          = routingKey.GetAction();
            string     _event          = routingKey.GetEvent();

            //Service messages received will be notify
            if (e.Message.Type == Niffle.Types.Type.Service)
            {
                if (e.Message.Service != null)
                {
                    if (e.Message.Service.Success)
                    {
                        ReportExecution(source, action, _event, Utils.FormatDateTimeWithSeparators(niffleTimeStamp));

                        //If Strategy ended then report.
                        if (source == nameof(OnTerminateTime))
                        {
                            Report();
                            //Reset();
                        }
                    }
                }
            }

            //Position message will be for Position Opened or Closed
            if (e.Message.Type == Niffle.Types.Type.Position)
            {
                if (e.Message.Position != null)
                {
                    if (routingKey.GetEventAsEnum() == Event.ONPOSITIONCLOSED)
                    {
                        if (e.Message.Position.StateChange == Messaging.Protobuf.Position.Types.StateChange.Closed)
                        {
                            ReportPositionClosed(e.Message.Position);
                        }
                    }

                    if (routingKey.GetEventAsEnum() == Event.ONPOSITIONOPENED)
                    {
                        if (e.Message.Position.StateChange == Messaging.Protobuf.Position.Types.StateChange.Opened)
                        {
                            ReportPositionOpened(e.Message.Position);
                        }
                    }

                    if (routingKey.GetEventAsEnum() == Event.ONPOSITIONMODIFIED)
                    {
                        if (e.Message.Position.StateChange == Messaging.Protobuf.Position.Types.StateChange.Modified)
                        {
                            ReportPositionModified(e.Message.Position, Utils.FormatDateTimeWithSeparators(niffleTimeStamp));
                        }
                    }
                }
            }

            //Orders messages will be for Orders Placed, Cancelled or Modified
            if (e.Message.Type == Niffle.Types.Type.Order)
            {
                if (e.Message.Order != null)
                {
                    if (routingKey.GetEventAsEnum() == Event.ONORDERPLACED)
                    {
                        if (e.Message.Order.StateChange == Messaging.Protobuf.Order.Types.StateChange.Filled)
                        {
                            ReportOrderPlaced(e.Message.Order);
                        }
                    }

                    if (routingKey.GetEventAsEnum() == Event.ONORDERCANCELLED)
                    {
                        if (e.Message.Order.StateChange == Messaging.Protobuf.Order.Types.StateChange.Canceled)
                        {
                            ReportOrderCancelled(e.Message.Order, Utils.FormatDateTimeWithSeparators(niffleTimeStamp));
                        }
                    }

                    //Need to work out how to determine if order has been modified
                    if (routingKey.GetEventAsEnum() == Event.ONORDERMODIFIED)
                    {
                        if (e.Message.Order.StateChange == Messaging.Protobuf.Order.Types.StateChange.Filled)
                        {
                            ReportOrderModified(e.Message.Order, Utils.FormatDateTimeWithSeparators(niffleTimeStamp));
                        }
                    }
                }
            }

            //Error messages
            if (e.Message.Type == Niffle.Types.Type.Error)
            {
                if (e.Message.Error != null)
                {
                    ReportError(source, e.Message.Error, Utils.FormatDateTimeWithSeparators(niffleTimeStamp));
                }
            }
        }
        public void TokenAwarePolicyRoundRobinsOnLocalReplicas()
        {
            var hostList = new List<Host>
            {
                //5 local nodes and 4 remote
                TestHelper.CreateHost("0.0.0.1", "dc1"),
                TestHelper.CreateHost("0.0.0.2", "dc1"),
                TestHelper.CreateHost("0.0.0.3", "dc2"),
                TestHelper.CreateHost("0.0.0.4", "dc2"),
                TestHelper.CreateHost("0.0.0.5", "dc1"),
                TestHelper.CreateHost("0.0.0.6", "dc1"),
                TestHelper.CreateHost("0.0.0.7", "dc2"),
                TestHelper.CreateHost("0.0.0.8", "dc2"),
                TestHelper.CreateHost("0.0.0.9", "dc1")
            };
            var clusterMock = new Mock<ICluster>(MockBehavior.Strict);
            clusterMock
                .Setup(c => c.AllHosts())
                .Returns(hostList)
                .Verifiable();
            clusterMock
                .Setup(c => c.GetReplicas(It.IsAny<string>(), It.IsAny<byte[]>()))
                .Returns<string, byte[]>((keyspace, key) =>
                {
                    var i = key[0];
                    return hostList.Where(h =>
                    {
                        //The host at with address == k and the next one
                        var address = TestHelper.GetLastAddressByte(h);
                        return address == i || address == i + 1;
                    }).ToList();
                })
                .Verifiable();

            var policy = new TokenAwarePolicy(new DCAwareRoundRobinPolicy("dc1", 2));
            policy.Initialize(clusterMock.Object);

            var firstHosts = new ConcurrentBag<Host>();
            var k = new RoutingKey { RawRoutingKey = new byte[] { 1 } };
            //key for host :::1 and :::2
            var actions = new List<Action>();
            const int times = 100;
            for (var i = 0; i < times; i++)
            {
                actions.Add(() =>
                {
                    var h = policy.NewQueryPlan(null, new SimpleStatement().SetRoutingKey(k)).First();
                    firstHosts.Add(h);
                });
            }
            

            var parallelOptions = new ParallelOptions();
            parallelOptions.TaskScheduler = new ThreadPerTaskScheduler();
            parallelOptions.MaxDegreeOfParallelism = 1000;

            Parallel.Invoke(parallelOptions, actions.ToArray());
            Assert.AreEqual(times, firstHosts.Count);
            //Half the times
            Assert.AreEqual(times / 2, firstHosts.Count(h => TestHelper.GetLastAddressByte(h) == 1));
            Assert.AreEqual(times / 2, firstHosts.Count(h => TestHelper.GetLastAddressByte(h) == 2));

            clusterMock.Verify();
        }
 public void Query(ITestCluster testCluster, int numberOfQueriesToExecute, bool usePrepared, ConsistencyLevel consistencyLevel)
 {
     if (usePrepared)
     {
         BoundStatement bs = PreparedStatement.Bind(0);
         for (int i = 0; i < numberOfQueriesToExecute; ++i)
         {
             ConsistencyLevel cac;
             var rs = testCluster.Session.Execute(bs);
             {
                 string queriedHost = rs.Info.QueriedHost.ToString();
                 cac = rs.Info.AchievedConsistency;
                 AddCoordinator(queriedHost, cac);
             }
         }
     }
     else
     {
         var routingKey = new RoutingKey();
         routingKey.RawRoutingKey = Enumerable.Repeat((byte) 0x00, 4).ToArray();
         for (int i = 0; i < numberOfQueriesToExecute; ++i)
         {
             string hostQueried;
             ConsistencyLevel achievedConsistency;
             var rs = testCluster.Session.Execute(
                         new SimpleStatement(String.Format("SELECT * FROM {0} WHERE k = 0", TableName)).SetRoutingKey(routingKey)
                                                                                                   .SetConsistencyLevel(consistencyLevel));
             {
                 hostQueried = rs.Info.QueriedHost.ToString();
                 achievedConsistency = rs.Info.AchievedConsistency;
                 Trace.TraceInformation("Query {0} executed by {1} with consistency {2}", i, hostQueried, achievedConsistency);
             }
             AddCoordinator(hostQueried, achievedConsistency);
         }
     }
 }
Beispiel #33
0
        public void PublisherPerformance()
        {
            var exchange = new Exchange("eventflow");
            var routingKey = new RoutingKey("performance");
            var exceptions = new ConcurrentBag<Exception>();
            const int threadCount = 100;
            const int messagesPrThread = 200;

            using (var consumer = new RabbitMqConsumer(_uri, "eventflow", new[] {"#"}))
            using (var resolver = BuildResolver(o => o.RegisterServices(sr => sr.Register<ILog, NullLog>())))
            {
                var rabbitMqPublisher = resolver.Resolve<IRabbitMqPublisher>();
                var threads = Enumerable.Range(0, threadCount)
                    .Select(_ =>
                        {
                            var thread = new Thread(o => SendMessages(rabbitMqPublisher, messagesPrThread, exchange, routingKey, exceptions));
                            thread.Start();
                            return thread;
                        })
                    .ToList();

                foreach (var thread in threads)
                {
                    thread.Join();
                }

                var rabbitMqMessages = consumer.GetMessages(threadCount * messagesPrThread);
                rabbitMqMessages.Should().HaveCount(threadCount*messagesPrThread);
                exceptions.Should().BeEmpty();
            }
        }
        public void TokenAwarePolicyRoundRobinsOnLocalReplicas()
        {
            var hostList = new List <Host>
            {
                //5 local nodes and 4 remote
                TestHelper.CreateHost("0.0.0.1", "dc1"),
                TestHelper.CreateHost("0.0.0.2", "dc1"),
                TestHelper.CreateHost("0.0.0.3", "dc2"),
                TestHelper.CreateHost("0.0.0.4", "dc2"),
                TestHelper.CreateHost("0.0.0.5", "dc1"),
                TestHelper.CreateHost("0.0.0.6", "dc1"),
                TestHelper.CreateHost("0.0.0.7", "dc2"),
                TestHelper.CreateHost("0.0.0.8", "dc2"),
                TestHelper.CreateHost("0.0.0.9", "dc1")
            };
            var clusterMock = new Mock <ICluster>(MockBehavior.Strict);

            clusterMock
            .Setup(c => c.AllHosts())
            .Returns(hostList)
            .Verifiable();
            clusterMock
            .Setup(c => c.GetReplicas(It.IsAny <string>(), It.IsAny <byte[]>()))
            .Returns <string, byte[]>((keyspace, key) =>
            {
                var i = key[0];
                return(hostList.Where(h =>
                {
                    //The host at with address == k and the next one
                    var address = TestHelper.GetLastAddressByte(h);
                    return address == i || address == i + 1;
                }).ToList());
            })
            .Verifiable();

            var policy = new TokenAwarePolicy(new DCAwareRoundRobinPolicy("dc1", 2));

            policy.Initialize(clusterMock.Object);

            var firstHosts = new ConcurrentBag <Host>();
            var k          = new RoutingKey {
                RawRoutingKey = new byte[] { 1 }
            };
            //key for host :::1 and :::2
            var       actions = new List <Action>();
            const int times   = 100;

            for (var i = 0; i < times; i++)
            {
                actions.Add(() =>
                {
                    var h = policy.NewQueryPlan(null, new SimpleStatement().SetRoutingKey(k)).First();
                    firstHosts.Add(h);
                });
            }


            var parallelOptions = new ParallelOptions();

            parallelOptions.TaskScheduler          = new ThreadPerTaskScheduler();
            parallelOptions.MaxDegreeOfParallelism = 1000;

            Parallel.Invoke(parallelOptions, actions.ToArray());
            Assert.AreEqual(times, firstHosts.Count);
            //Half the times
            Assert.AreEqual(times / 2, firstHosts.Count(h => TestHelper.GetLastAddressByte(h) == 1));
            Assert.AreEqual(times / 2, firstHosts.Count(h => TestHelper.GetLastAddressByte(h) == 2));

            clusterMock.Verify();
        }
        public void GivenConnectionWhenSubscribeShouldExecuteCallbackAndAckOnSuccess()
        {
            var          exchange    = Exchange.Create("test", ExchangeType.Direct);
            var          queue       = Queue.Create("test.requested");
            var          routingKey  = RoutingKey.Create("test.route");
            var          body        = Encoding.UTF8.GetBytes("test");
            const ushort deliveryTag = 1;

            _busSerializerMock.Setup(x => x.Deserialize <string>(body))
            .ReturnsAsync("test")
            .Verifiable();

            _channelMock.Setup(x => x.BasicConsume(
                                   queue.Name.Value,
                                   false,
                                   It.IsAny <string>(),
                                   It.IsAny <bool>(),
                                   It.IsAny <bool>(),
                                   It.IsAny <IDictionary <string, object> >(),
                                   It.IsAny <IBasicConsumer>()))
            .Callback((string queueName, bool autoAck, string consumerTag, bool noLocal, bool exclusive,
                       IDictionary <string, object> _, IBasicConsumer consumer) =>
            {
                ((AsyncEventingBasicConsumer)consumer).HandleBasicDeliver(
                    consumerTag,
                    deliveryTag,
                    false,
                    exchange.Name.Value,
                    routingKey.Value,
                    _basicPropertiesMock.Object,
                    body).Wait();
            })
            .Returns(Guid.NewGuid().ToString());

            var isExecuted     = false;
            var autoResetEvent = new AutoResetEvent(false);

            _busConnection.Subscribe <string>(exchange, queue, routingKey, 10, (scope, @event) =>
            {
                isExecuted = true;
                return(Task.CompletedTask);
            });

            _channelMock.Setup(x => x.BasicAck(1, false))
            .Callback((ulong tag, bool multiple) => autoResetEvent.Set())
            .Verifiable();

            autoResetEvent.WaitOne(TimeSpan.FromSeconds(5));

            isExecuted.Should().BeTrue();
            _channelMock.Verify(x => x.BasicQos(0, 10, false), Times.Once());
            _channelMock.Verify(x => x.BasicConsume(
                                    queue.Name.Value,
                                    false,
                                    It.IsAny <string>(),
                                    It.IsAny <bool>(),
                                    It.IsAny <bool>(),
                                    It.IsAny <IDictionary <string, object> >(),
                                    It.IsAny <IBasicConsumer>()), Times.Once());
            _channelMock.Verify(x => x.BasicAck(deliveryTag, false), Times.Once());
        }
Beispiel #36
0
        protected override List <RoutingKey> SetListeningRoutingKeys()
        {
            List <RoutingKey> routingKeys = RoutingKey.Create(Niffler.Messaging.RabbitMQ.Action.TRADEOPERATION).ToList();

            return(routingKeys);
        }
        public void GivenConnectionWhenSubscribeShouldExecuteCallbackAndRetryOnFailure()
        {
            var          exchange    = Exchange.Create("test", ExchangeType.Direct);
            var          queue       = Queue.Create("test.requested");
            var          routingKey  = RoutingKey.Create("test.route");
            var          body        = Encoding.UTF8.GetBytes("test");
            const ushort deliveryTag = 1;

            var loggerMock = new Mock <IBusLogger>();

            _busSerializerMock.Setup(x => x.Deserialize <string>(body))
            .ReturnsAsync("test")
            .Verifiable();
            _serviceProviderMock.Setup(x => x.GetService(typeof(IBusLogger)))
            .Returns(loggerMock.Object)
            .Verifiable();

            var autoResetEvent = new AutoResetEvent(false);

            _channelMock.Setup(x => x.BasicConsume(
                                   queue.Name.Value,
                                   false,
                                   It.IsAny <string>(),
                                   It.IsAny <bool>(),
                                   It.IsAny <bool>(),
                                   It.IsAny <IDictionary <string, object> >(),
                                   It.IsAny <IBasicConsumer>()))
            .Callback((string queueName, bool autoAck, string consumerTag, bool noLocal, bool exclusive,
                       IDictionary <string, object> _, IBasicConsumer consumer) =>
            {
                ((AsyncEventingBasicConsumer)consumer).HandleBasicDeliver(
                    consumerTag,
                    deliveryTag,
                    false,
                    exchange.Name.Value,
                    routingKey.Value,
                    _basicPropertiesMock.Object,
                    body).Wait();
            })
            .Returns(Guid.NewGuid().ToString());

            _publishBatchMock.Setup(x => x.Publish())
            .Callback(() => autoResetEvent.Set())
            .Verifiable();

            _busConnection.Subscribe <string>(
                exchange,
                queue,
                routingKey,
                10,
                (scope, @event) => throw new Exception());

            autoResetEvent.WaitOne(TimeSpan.FromSeconds(5));

            _publishBatchMock.VerifyAll();
            _channelMock.Verify(x => x.QueueDeclare(
                                    It.Is((string y) => y.EndsWith("-retry")),
                                    true,
                                    false,
                                    false,
                                    It.Is <IDictionary <string, object> >(args =>
                                                                          args["x-dead-letter-exchange"].Equals(ExchangeName.Default.Value) &&
                                                                          args["x-dead-letter-routing-key"].Equals(queue.Name.Value))), Times.Once());
            _publishBatchMock.Verify(x => x.Add(
                                         Exchange.Default.Name.Value,
                                         It.Is((string y) => y.StartsWith(queue.Name.Value) && y.EndsWith("-retry")),
                                         false,
                                         _basicPropertiesMock.Object,
                                         It.IsAny <byte[]>()), Times.Once());
        }
        public SnsReDrivePolicySDlqTests()
        {
            Guid   correlationId = Guid.NewGuid();
            string replyTo       = "http:\\queueUrl";
            string contentType   = "text\\plain";
            var    channelName   = $"Redrive-Tests-{Guid.NewGuid().ToString()}".Truncate(45);

            _dlqChannelName = $"Redrive-DLQ-Tests-{Guid.NewGuid().ToString()}".Truncate(45);
            _topicName      = $"Redrive-Tests-{Guid.NewGuid().ToString()}".Truncate(45);
            var routingKey = new RoutingKey(_topicName);

            //how are we consuming
            var subscription = new SqsSubscription <MyCommand>(
                name: new SubscriptionName(channelName),
                channelName: new ChannelName(channelName),
                routingKey: routingKey,
                //don't block the redrive policy from owning retry management
                requeueCount: -1,
                //delay before requeuing
                requeueDelayInMs: 50,
                //we want our SNS subscription to manage requeue limits using the DLQ for 'too many requeues'
                redrivePolicy: new RedrivePolicy
                (
                    deadLetterQueueName: new ChannelName(_dlqChannelName),
                    maxReceiveCount: 2
                ));

            //what do we send
            var myCommand = new MyDeferredCommand {
                Value = "Hello Redrive"
            };

            _message = new Message(
                new MessageHeader(myCommand.Id, _topicName, MessageType.MT_COMMAND, correlationId, replyTo, contentType),
                new MessageBody(JsonSerializer.Serialize((object)myCommand, JsonSerialisationOptions.Options))
                );

            //Must have credentials stored in the SDK Credentials store or shared credentials file
            (AWSCredentials credentials, RegionEndpoint region) = CredentialsChain.GetAwsCredentials();
            _awsConnection = new AWSMessagingGatewayConnection(credentials, region);

            //how do we send to the queue
            _sender = new SqsMessageProducer(_awsConnection, new SnsPublication {
                MakeChannels = OnMissingChannel.Create
            });

            //We need to do this manually in a test - will create the channel from subscriber parameters
            _channelFactory = new ChannelFactory(_awsConnection);
            _channel        = _channelFactory.CreateChannel(subscription);

            //how do we handle a command
            IHandleRequests <MyDeferredCommand> handler = new MyDeferredCommandHandler();

            //hook up routing for the command processor
            var subscriberRegistry = new SubscriberRegistry();

            subscriberRegistry.Register <MyDeferredCommand, MyDeferredCommandHandler>();

            //once we read, how do we dispatch to a handler. N.B. we don't use this for reading here
            _commandProcessor = new CommandProcessor(
                subscriberRegistry: subscriberRegistry,
                handlerFactory: new QuickHandlerFactory(() => handler),
                requestContextFactory: new InMemoryRequestContextFactory(),
                policyRegistry: new PolicyRegistry()
                );

            //pump messages from a channel to a handler - in essence we are building our own dispatcher in this test
            IAmAMessageMapper <MyDeferredCommand> mapper = new MyDeferredCommandMessageMapper(_topicName);

            _messagePump = new MessagePumpBlocking <MyDeferredCommand>(_commandProcessor, mapper)
            {
                Channel = _channel, TimeoutInMilliseconds = 5000, RequeueCount = 3
            };
        }