Esempio n. 1
0
        public void CheckThreadSafety()
        {
            // arrange
            var w = new SafeDictionaryWrapper <string, string>();

            var count = 3000;

            // act
            var task1 = Task.Factory.StartNew(() =>
            {
                for (var i = 0; i < count; i++)
                {
                    w.GetOrAdd($"a_{i}", k => $"v_{i}");
                }
            });
            var task2 = Task.Factory.StartNew(() =>
            {
                for (var i = 0; i < count; i++)
                {
                    w.GetOrAdd($"b_{i}", k => $"v_{i}");
                }
            });
            var task3 = Task.Factory.StartNew(() =>
            {
                for (var i = 0; i < count; i++)
                {
                    w.GetOrAdd($"c_{i}", k => $"v_{i}");
                }
            });

            Task.WaitAll(task1, task2, task3);

            // assert
            w.Dictonary.Count.Should().Be(3 * count);
        }
Esempio n. 2
0
        public void ClearWorks()
        {
            var w = new SafeDictionaryWrapper <string, string>();

            w.GetOrAdd("a", x => "a");
            w.GetOrAdd("b", x => "b");

            // act
            w.Clear();

            // assert
            w.Dictonary.Count.ShouldBeEquivalentTo(0);
        }
        public KafkaGroupConsumer(KafkaMessageBus messageBus, string group, string[] topics, Func <TopicPartition, IKafkaCommitController, IKafkaTopicPartitionProcessor> processorFactory)
        {
            MessageBus = messageBus ?? throw new ArgumentNullException(nameof(messageBus));
            Group      = group ?? throw new ArgumentNullException(nameof(group));
            Topics     = topics ?? throw new ArgumentNullException(nameof(topics));

            _logger = messageBus.LoggerFactory.CreateLogger <KafkaGroupConsumer>();

            _logger.LogInformation("Creating for group: {0}, topics: {1}", group, string.Join(", ", topics));

            _processors = new SafeDictionaryWrapper <TopicPartition, IKafkaTopicPartitionProcessor>(tp => processorFactory(tp, this));

            _consumer = CreateConsumer(group);
        }
Esempio n. 4
0
        public void CannotMutateDictionary()
        {
            var w = new SafeDictionaryWrapper <string, string>();

            w.GetOrAdd("a", x => "a");
            w.GetOrAdd("b", x => "b");

            // act
            Action clearAction = () => w.Dictonary.Clear();
            Action addAction   = () => w.Dictonary.Add("c", "c");

            // assert
            clearAction.ShouldThrow <NotSupportedException>();
            addAction.ShouldThrow <NotSupportedException>();
        }
Esempio n. 5
0
        public KafkaGroupConsumer(KafkaMessageBus messageBus, string group, string[] topics, Func <TopicPartition, IKafkaCommitController, IKafkaTopicPartitionProcessor> processorFactory)
        {
            Log.InfoFormat(CultureInfo.InvariantCulture, "Creating for group: {0}, topics: {1}", group, string.Join(", ", topics));

            MessageBus = messageBus;
            Group      = group;
            Topics     = topics;

            _processors = new SafeDictionaryWrapper <TopicPartition, IKafkaTopicPartitionProcessor>(tp => processorFactory(tp, this));

            _consumer                       = CreateConsumer(group);
            _consumer.OnMessage            += OnMessage;
            _consumer.OnPartitionsAssigned += OnPartitionAssigned;
            _consumer.OnPartitionsRevoked  += OnPartitionRevoked;
            _consumer.OnPartitionEOF       += OnPartitionEndReached;
            _consumer.OnOffsetsCommitted   += OnOffsetsCommitted;
            _consumer.OnStatistics         += OnStatistics;
        }
Esempio n. 6
0
        public void GetOrAddWorks()
        {
            // arrange
            var w = new SafeDictionaryWrapper <string, string>();
            var v = "2";
            var k = "a";
            var valueFactoryMock = new Mock <Func <string, string> >();

            valueFactoryMock.Setup(x => x(k)).Returns(v);

            // act
            var v1 = w.GetOrAdd(k, valueFactoryMock.Object);
            var v2 = w.GetOrAdd(k, valueFactoryMock.Object);

            // assert
            w.Dictonary.Count.ShouldBeEquivalentTo(1);

            v1.ShouldBeEquivalentTo(v);
            v2.ShouldBeEquivalentTo(v);
            w.Dictonary[k].ShouldBeEquivalentTo(v);

            valueFactoryMock.Verify(x => x(k), Times.Once);
        }
        protected override void Build()
        {
            base.Build();

            _producerByTopic = new SafeDictionaryWrapper <string, EventHubClient>(topic =>
            {
                _logger.LogDebug("Creating EventHubClient for path {0}", topic);
                return(ProviderSettings.EventHubClientFactory(topic));
            });

            _logger.LogInformation("Creating consumers");
            foreach (var consumerSettings in Settings.Consumers)
            {
                _logger.LogInformation("Creating consumer for Topic: {0}, Group: {1}, MessageType: {2}", consumerSettings.Topic, consumerSettings.GetGroup(), consumerSettings.MessageType);
                _consumers.Add(new GroupTopicConsumer(this, consumerSettings));
            }

            if (Settings.RequestResponse != null)
            {
                _logger.LogInformation("Creating response consumer for Topic: {0}, Group: {1}", Settings.RequestResponse.Topic, Settings.RequestResponse.GetGroup());
                _consumers.Add(new GroupTopicConsumer(this, Settings.RequestResponse));
            }
        }
Esempio n. 8
0
        public EventHubMessageBus(MessageBusSettings settings, EventHubMessageBusSettings eventHubSettings)
            : base(settings)
        {
            EventHubSettings = eventHubSettings;

            _producerByTopic = new SafeDictionaryWrapper <string, EventHubClient>(topic =>
            {
                Log.DebugFormat("Creating EventHubClient for path {0}", topic);
                return(EventHubSettings.EventHubClientFactory(topic));
            });

            Log.Info("Creating consumers");
            foreach (var consumerSettings in settings.Consumers)
            {
                Log.InfoFormat("Creating consumer for Topic: {0}, Group: {1}, MessageType: {2}", consumerSettings.Topic, consumerSettings.Group, consumerSettings.MessageType);
                _consumers.Add(new GroupTopicConsumer(this, consumerSettings));
            }

            if (settings.RequestResponse != null)
            {
                Log.InfoFormat("Creating response consumer for Topic: {0}, Group: {1}", settings.RequestResponse.Topic, settings.RequestResponse.Group);
                _consumers.Add(new GroupTopicConsumer(this, settings.RequestResponse));
            }
        }
Esempio n. 9
0
        protected override void Build()
        {
            base.Build();

            _producerByTopic = new SafeDictionaryWrapper <string, ITopicClient>(topic =>
            {
                Log.DebugFormat(CultureInfo.InvariantCulture, "Creating {0} for name {1}", nameof(ITopicClient), topic);
                return(ProviderSettings.TopicClientFactory(topic));
            });

            _producerByQueue = new SafeDictionaryWrapper <string, IQueueClient>(queue =>
            {
                Log.DebugFormat(CultureInfo.InvariantCulture, "Creating {0} for name {1}", nameof(IQueueClient), queue);
                return(ProviderSettings.QueueClientFactory(queue));
            });

            foreach (var producerSettings in Settings.Producers)
            {
                var      producerKind = producerSettings.GetKind();
                PathKind existingKind;

                var topic = producerSettings.DefaultTopic;
                if (topic != null)
                {
                    if (_kindByTopic.TryGetValue(topic, out existingKind))
                    {
                        if (existingKind != producerKind)
                        {
                            throw new ConfigurationMessageBusException($"The same name '{topic}' was used for queue and topic. You cannot share one name for a topic and queue. Please fix your configuration.");
                        }
                    }
                    else
                    {
                        _kindByTopic.Add(topic, producerKind);
                    }
                }

                if (_kindByMessageType.TryGetValue(producerSettings.MessageType, out existingKind))
                {
                    if (existingKind != producerKind)
                    {
                        throw new ConfigurationMessageBusException($"The same message type '{producerSettings.MessageType}' was used for queue and topic. You cannot share one message type for a topic and queue. Please fix your configuration.");
                    }
                }
                else
                {
                    _kindByMessageType.Add(producerSettings.MessageType, producerKind);
                }
            }

            byte[] getPayload(Message m) => m.Body;
            void initConsumerContext(Message m, ConsumerContext ctx) => ctx.SetTransportMessage(m);

            Log.Info("Creating consumers");
            foreach (var consumerSettings in Settings.Consumers)
            {
                Log.InfoFormat(CultureInfo.InvariantCulture, "Creating consumer for {0}", consumerSettings.FormatIf(Log.IsInfoEnabled));

                var messageProcessor = new ConsumerInstancePoolMessageProcessor <Message>(consumerSettings, this, getPayload, initConsumerContext);
                AddConsumer(consumerSettings, messageProcessor);
            }

            if (Settings.RequestResponse != null)
            {
                Log.InfoFormat(CultureInfo.InvariantCulture, "Creating response consumer for {0}", Settings.RequestResponse.FormatIf(Log.IsInfoEnabled));

                var messageProcessor = new ResponseMessageProcessor <Message>(Settings.RequestResponse, this, getPayload);
                AddConsumer(Settings.RequestResponse, messageProcessor);
            }
        }
        public ServiceBusMessageBus(MessageBusSettings settings, ServiceBusMessageBusSettings serviceBusSettings) : base(settings)
        {
            ServiceBusSettings = serviceBusSettings;

            _producerByTopic = new SafeDictionaryWrapper <string, ITopicClient>(topic =>
            {
                Log.DebugFormat(CultureInfo.InvariantCulture, $"Creating {nameof(ITopicClient)} for name {0}", topic);
                return(ServiceBusSettings.TopicClientFactory(topic));
            });

            _producerByQueue = new SafeDictionaryWrapper <string, IQueueClient>(queue =>
            {
                Log.DebugFormat(CultureInfo.InvariantCulture, $"Creating {nameof(IQueueClient)} for name {0}", queue);
                return(ServiceBusSettings.QueueClientFactory(queue));
            });

            foreach (var producerSettings in settings.Producers)
            {
                var      producerKind = producerSettings.GetKind();
                PathKind existingKind;

                var topic = producerSettings.DefaultTopic;
                if (topic != null)
                {
                    if (_kindByTopic.TryGetValue(topic, out existingKind))
                    {
                        if (existingKind != producerKind)
                        {
                            throw new InvalidConfigurationMessageBusException($"The same name '{topic}' was used for queue and topic. You cannot share one name for a topic and queue. Please fix your configuration.");
                        }
                    }
                    else
                    {
                        _kindByTopic.Add(topic, producerKind);
                    }
                }

                if (_kindByMessageType.TryGetValue(producerSettings.MessageType, out existingKind))
                {
                    if (existingKind != producerKind)
                    {
                        throw new InvalidConfigurationMessageBusException($"The same message type '{producerSettings.MessageType}' was used for queue and topic. You cannot share one message type for a topic and queue. Please fix your configuration.");
                    }
                }
                else
                {
                    _kindByMessageType.Add(producerSettings.MessageType, producerKind);
                }
            }

            Log.Info("Creating consumers");
            foreach (var consumerSettings in settings.Consumers)
            {
                Log.InfoFormat(CultureInfo.InvariantCulture, "Creating consumer for {0}", consumerSettings.FormatIf(Log.IsInfoEnabled));

                var messageProcessor = new ConsumerInstancePool <Message>(consumerSettings, this, m => m.Body);
                AddConsumer(consumerSettings, messageProcessor);
            }

            if (settings.RequestResponse != null)
            {
                Log.InfoFormat(CultureInfo.InvariantCulture, "Creating response consumer for {0}", settings.RequestResponse.FormatIf(Log.IsInfoEnabled));

                var messageProcessor = new ResponseMessageProcessor <Message>(settings.RequestResponse, this, m => m.Body);
                AddConsumer(settings.RequestResponse, messageProcessor);
            }
        }