public void GetAllTopics_given_registered_topics_returns_expected_topics() { // Arrange var topic1 = "build.capabilities"; var topic2 = "build.dummy"; var sut = new DomainEventRegistry() .Register <CapabilityCreatedDomainEvent>( eventName: "eventDummyName1", topicName: topic1) .Register <MemberJoinedCapabilityDomainEvent>( eventName: "eventDummyName2", topicName: topic2); // Act var actualTopic = sut.GetAllTopics(); // Assert Assert.Equal(2, actualTopic.Count()); Assert.Contains(topic1, actualTopic); Assert.Contains(topic2, actualTopic); }
public Task StartAsync(CancellationToken cancellationToken) { _executingTask = Task.Factory.StartNew(async() => { using (var consumer = _consumerFactory.Create()) { var topics = _eventRegistry.GetAllTopics(); _logger.LogInformation($"Event consumer started. Listening to topics: {string.Join(",", topics)}"); consumer.Subscribe(topics); // consume loop while (!_cancellationTokenSource.IsCancellationRequested) { ConsumeResult <string, string> msg; try { msg = consumer.Consume(cancellationToken); } catch (Exception ex) { _logger.LogError($"Consumption of event failed, reason: {ex}"); continue; } using (var scope = _serviceProvider.CreateScope()) { _logger.LogInformation($"Received event: Topic: {msg.Topic} Partition: {msg.Partition}, Offset: {msg.Offset} {msg.Value}"); try { var eventDispatcher = scope.ServiceProvider.GetRequiredService <IEventDispatcher>(); await eventDispatcher.Send(msg.Value, scope); await Task.Run(() => consumer.Commit(msg)); } catch (Exception ex) when(ex is EventTypeNotFoundException || ex is EventHandlerNotFoundException) { _logger.LogWarning($"Message skipped. Exception message: {ex.Message}", ex); await Task.Run(() => consumer.Commit(msg)); } catch (EventMessageIncomprehensible ex) { _logger.LogWarning(ex, $"Encountered a message that was irrecoverably incomprehensible. Skipping. Raw message included {msg} with value '{msg.Value}'"); await Task.Run(() => consumer.Commit(msg)); } catch (Exception ex) { _logger.LogError(ex, "Error consuming event."); } } } } }, _cancellationTokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default) .ContinueWith(task => { if (task.IsFaulted) { _logger.LogError(task.Exception, "Event loop crashed"); } }, cancellationToken); return(Task.CompletedTask); }