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); }
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 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>(); }
protected ProducerSettings GetProducerSettings(Type messageType) { if (!ProducerSettingsByMessageType.TryGetValue(messageType, out var producerSettings)) { var baseMessageType = _messageTypeToBaseType.GetOrAdd(messageType, mt => { var baseType = mt; do { baseType = mt.BaseType; }while (baseType != null && baseType != typeof(object) && !ProducerSettingsByMessageType.ContainsKey(baseType)); if (baseType != null) { _logger.LogDebug("Found a base type of {0} that is configured in the bus: {1}", mt, baseType); } else { _logger.LogDebug("Did not find any base type of {0} that is configured in the bus", mt); } // Note: Nulls are also added to dictionary, so that we don't look them up using reflection next time (cached). return(baseType); }); if (baseMessageType == null) { throw new PublishMessageBusException($"Message of type {messageType} was not registered as a supported publish message. Please check your MessageBus configuration and include this type."); } producerSettings = ProducerSettingsByMessageType[baseMessageType]; } return(producerSettings); }
protected virtual async Task ProduceToTransport(Type messageType, object message, string name, byte[] payload, PathKind kind) { AssertActive(); Log.DebugFormat(CultureInfo.InvariantCulture, "Producing message {0} of type {1} on {2} {3} with size {4}", message, messageType.Name, kind, name, payload.Length); var m = new Message(payload); if (ProducerSettingsByMessageType.TryGetValue(messageType, out var producerSettings)) { try { var messageModifier = producerSettings.GetMessageModifier(); messageModifier(message, m); } catch (Exception e) { Log.WarnFormat(CultureInfo.InvariantCulture, "The configured message modifier failed for message type {0} and message {1}", e, messageType, message); } } if (kind == PathKind.Topic) { var topicProducer = _producerByTopic.GetOrAdd(name); await topicProducer.SendAsync(m).ConfigureAwait(false); } else { var queueProducer = _producerByQueue.GetOrAdd(name); await queueProducer.SendAsync(m).ConfigureAwait(false); } Log.DebugFormat(CultureInfo.InvariantCulture, "Delivered message {0} of type {1} on {2} {3}", message, messageType.Name, kind, name); }
protected virtual void OnPartitionAssigned(object sender, List <TopicPartition> partitions) { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug("Group [{0}]: Assigned partitions: {1}", Group, string.Join(", ", partitions)); } // Ensure processors exist for each assigned topic-partition partitions.ForEach(tp => _processors.GetOrAdd(tp)); _consumer?.Assign(partitions); }
/// <summary> /// /// </summary> /// <param name="messageType"></param> /// <param name="payload"></param> /// <param name="topic"></param> /// <returns></returns> public override async Task PublishToTransport(Type messageType, object message, string topic, byte[] payload) { AssertActive(); Log.DebugFormat("Producing message of type {0} on topic {1} with size {2}", messageType.Name, topic, payload.Length); var producer = _producerByTopic.GetOrAdd(topic); var ev = new EventData(payload); await producer.SendAsync(ev); Log.DebugFormat("Delivered message at offset {0} and sequence {1}", ev.Offset, ev.SequenceNumber); }
protected virtual void OnPartitionAssigned(object sender, List <TopicPartition> partitions) { if (Log.IsDebugEnabled) { Log.DebugFormat(CultureInfo.InvariantCulture, "Group [{0}]: Assigned partitions: {1}", Group, string.Join(", ", partitions)); } // Ensure processors exist for each assigned topic-partition partitions.ForEach(tp => _processors.GetOrAdd(tp)); _consumer?.Assign(partitions); }
/// <summary> /// /// </summary> /// <param name="messageType"></param> /// <param name="payload"></param> /// <param name="topic"></param> /// <returns></returns> public override async Task Publish(Type messageType, byte[] payload, string topic) { Assert.IsFalse(_disposing, () => new MessageBusException("The message bus is disposed at this time")); Log.DebugFormat("Producing message of type {0} on topic {1} with size {2}", messageType.Name, topic, payload.Length); var eventHubClient = _producersByTopic.GetOrAdd(topic); var ev = new EventData(payload); await eventHubClient.SendAsync(ev); Log.DebugFormat("Delivered message at offset {0} and sequence {1}", ev.Offset, ev.SequenceNumber); }
/// <summary> /// /// </summary> /// <param name="messageType"></param> /// <param name="payload"></param> /// <param name="message"></param> /// <param name="name"></param> /// <returns></returns> public override async Task ProduceToTransport(Type messageType, object message, string name, byte[] payload, MessageWithHeaders messageWithHeaders = null) { AssertActive(); _logger.LogDebug("Producing message {0} of type {1} on topic {2} with size {3}", message, messageType.Name, name, payload.Length); var producer = _producerByTopic.GetOrAdd(name); var ev = new EventData(payload); // ToDo: Add support for partition keys await producer.SendAsync(ev).ConfigureAwait(false); _logger.LogDebug("Delivered message {0} of type {1} on topic {2}", message, messageType.Name, name); }
/// <summary> /// /// </summary> /// <param name="messageType"></param> /// <param name="payload"></param> /// <param name="message"></param> /// <param name="name"></param> /// <returns></returns> public override async Task ProduceToTransport(Type messageType, object message, string name, byte[] payload) { AssertActive(); Log.DebugFormat(CultureInfo.InvariantCulture, "Producing message {0} of type {1} on topic {2} with size {3}", message, messageType.Name, name, payload.Length); var producer = _producerByTopic.GetOrAdd(name); var ev = new EventData(payload); // ToDo: Add support for partition keys await producer.SendAsync(ev).ConfigureAwait(false); Log.DebugFormat(CultureInfo.InvariantCulture, "Delivered message {0} of type {1} on topic {2}", message, messageType.Name, name); }
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 virtual void OnPartitionAssigned([NotNull] ICollection <TopicPartition> partitions) { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug("Group [{group}]: Assigned partitions: {partitions}", Group, string.Join(", ", partitions)); } // Ensure processors exist for each assigned topic-partition foreach (var partition in partitions) { _processors.GetOrAdd(partition); } //_consumer?.Assign(partitions); }
protected virtual async Task ProduceToTransport(Type messageType, object message, string name, byte[] payload, PathKind kind) { if (messageType is null) { throw new ArgumentNullException(nameof(messageType)); } if (payload is null) { throw new ArgumentNullException(nameof(payload)); } AssertActive(); _logger.LogDebug("Producing message {0} of type {1} on {2} {3} with size {4}", message, messageType.Name, kind, name, payload.Length); var m = new Message(payload); if (ProducerSettingsByMessageType.TryGetValue(messageType, out var producerSettings)) { try { var messageModifier = producerSettings.GetMessageModifier(); messageModifier(message, m); } catch (Exception e) { _logger.LogWarning(e, "The configured message modifier failed for message type {0} and message {1}", messageType, message); } } if (kind == PathKind.Topic) { var topicProducer = _producerByTopic.GetOrAdd(name); await topicProducer.SendAsync(m).ConfigureAwait(false); } else { var queueProducer = _producerByQueue.GetOrAdd(name); await queueProducer.SendAsync(m).ConfigureAwait(false); } _logger.LogDebug("Delivered message {0} of type {1} on {2} {3}", message, messageType.Name, kind, name); }