public void ConsumeTest_CorrectData_ShouldCancel() { var loggerMock = new Mock <ILogger>(); loggerMock.Setup(s => s.LogException((It.IsAny <Exception>()))); var returnValue = new ConsumeResult <Ignore, string>(); var consumerMock = new Mock <IConsumer <Ignore, string> >(); consumerMock .Setup(s => s.Consume(It.IsAny <CancellationToken>())) .Returns(returnValue); consumerMock.Setup(s => s.Dispose()); consumerMock.Setup(s => s.Subscribe(It.IsAny <string>())); var source = new CancellationTokenSource(); var token = source.Token; source.Cancel(); var consumer = new NumberConsumer(consumerMock.Object, loggerMock.Object, "TestTopic"); using (consumer) { consumer.Consume((value) => { }, token); } }
public void ConsumeTest_NullForCallback_ShouldThrowArgumentNullException() { var consumerMock = new Mock <IConsumer <Ignore, string> >(); consumerMock.Setup(s => s.Dispose()); var consumer = new NumberConsumer(consumerMock.Object, null, "TestTopic"); using (consumer) { consumer.Consume(null); } }
static void Main(string[] args) { Console.WriteLine("If you want to stop the application at any given type press the [Escape] key on the keyboard."); Console.WriteLine(); Console.WriteLine("If you have read the above information, please feel free to press any key to continue and start consuming the super secret information..."); Console.ReadLine(); var source = new CancellationTokenSource(); var token = source.Token; Task.Run(() => { while (true) { var key = Console.ReadKey(); if (key.Key == ConsoleKey.Escape) { source.Cancel(); return; } } }); var conf = new ConsumerConfig { GroupId = "test-groupid", BootstrapServers = "localhost:9092", // Note: The AutoOffsetReset property determines the start offset in the event // there are not yet any committed offsets for the consumer group for the // topic/partitions of interest. By default, offsets are committed // automatically, so in this example, consumption will only start from the // earliest message in the topic 'my-topic' the first time you run the program. AutoOffsetReset = AutoOffsetResetType.Earliest }; var logger = new Logger(); var numberConsumer = new NumberConsumer(new Consumer <Ignore, string>(conf), logger, "NumberStorage"); int count = 0; Action <string> callbackAction = (value) => { //If the value is not correct we want to log the exception thus notifying for the error. //However, we want to continue listening in order not to miss any important information(given the information is important) if (!int.TryParse(value, out int number)) { logger.LogException(new FormatException("The number from the consumer is not in a correct format.")); return; } numbersConsumed.Add(number); if (numbersConsumed.Count == 10) { count++; Console.Write($"{string.Join(", ", numbersConsumed)} - "); Console.WriteLine($"[{DateTime.Now}] : {numbersConsumed.Average()}"); numbersConsumed.Clear(); } }; using (numberConsumer) { try { numberConsumer.Consume(callbackAction, token); } catch (OperationCanceledException ex) { logger.LogInfo(ex.Message); } catch (Exception ex) { logger.LogException(ex); Console.ReadLine(); return; } } Console.WriteLine(); Console.WriteLine($"Number of averages calculated {count}"); Console.WriteLine("Press any key to close the application..."); Console.ReadLine(); }