Пример #1
0
        public async Task LoggerTest()
        {
            int         counter     = 0;
            KafkaServer kafkaServer = new KafkaServer();

            kafkaServer.Register(services =>
            {
                services.AddLogging(builder =>
                {
                    builder.SetMinimumLevel(LogLevel.Trace);
                });
                services.AddKafkaLogger(options =>
                {
                    options.BootstrapServers = hosts;
                    options.Category         = nameof(KafkaServiceTest);
                    options.InitializeCount  = 10;
                    //options.Key = nameof(KafkaServiceTest);
                    options.MinLevel        = LogLevel.Trace;
                    options.Topic           = $"service.{logger}";
                    options.ApplicationName = nameof(KafkaServiceTest);
                });

                services.AddKafkaConsumer(options =>
                {
                    options.BootstrapServers = hosts;
                    options.EnableAutoCommit = true;
                    options.GroupId          = $"service.{group}.logger";
                    options.Subscribers      = KafkaSubscriber.From($"service.{logger}");
                }).AddListener(result =>
                {
                    WriteLogger(result);
                    if (result.Message.Contains(nameof(KafkaServiceTest)))
                    {
                        counter++;
                    }
                });
            });
            await kafkaServer.StartAsync();

            Thread.Sleep(1000);//等待运行1秒

            var serviceProvider = kafkaServer.ServiceProvider;
            var loggerFactory   = serviceProvider.GetService <ILoggerFactory>();
            var _logger         = loggerFactory.CreateLogger <KafkaServiceTest>();

            _logger.LogTrace("LogTrace");
            _logger.LogDebug("LogDebug");
            _logger.LogInformation("LogInformation");
            _logger.LogWarning("LogWarning");
            _logger.LogError("LogError");
            _logger.LogCritical("LogCritical");

            BlockUntil(() => counter >= 6, 3000);

            Thread.Sleep(3000);//等待运行3秒
            Assert.Equal(6, counter);

            await kafkaServer.StopAsync();
        }
Пример #2
0
        public void ParameterlessConstructor_SetsAdressToEmpty_AndPortToMinus1()
        {
            //Arrange
            KafkaServer server = new KafkaServer();

            //Act

            //Assert
            server.Address.Should().BeEmpty();
            server.Port.Should().Be(-1);
        }
Пример #3
0
        public void Address_CanBeSet()
        {
            //Arrange
            KafkaServer server  = new KafkaServer();
            string      address = "localhost";

            //Act
            server.Address = address;

            //Assert
            server.Address.Should().Be(address);
        }
Пример #4
0
        public async Task ServiceTest()
        {
            int         counter     = 0;
            KafkaServer kafkaServer = new KafkaServer();

            kafkaServer.Register(services =>
            {
                services.AddLogging(builder =>
                {
                    builder.SetMinimumLevel(LogLevel.Trace);
                });
                services.AddKafkaProducer(options =>
                {
                    options.BootstrapServers = hosts;
                    options.InitializeCount  = 3;
                    options.Key   = "kafka";
                    options.Topic = $"service.{topic}";
                });

                services.AddKafkaConsumer(options =>
                {
                    options.BootstrapServers = hosts;
                    options.EnableAutoCommit = false;
                    options.GroupId          = $"service.{group}.kafka";
                    options.Subscribers      = KafkaSubscriber.From($"service.{topic}");
                }).AddListener(result =>
                {
                    Output.WriteLine(JsonSerializer.Serialize(result));
                    counter++;
                    result.Commit();
                });
            });
            await kafkaServer.StartAsync();

            Thread.Sleep(1000);//等待运行1秒

            var serviceProvider = kafkaServer.ServiceProvider;
            var factory         = serviceProvider.GetService <IKafkaProducerFactory>();
            var producer        = factory.Create();

            for (var i = 0; i < 10; i++)
            {
                await producer.PublishAsync($"{nameof(KafkaServiceTest)}{i}", "message" + i);
            }

            BlockUntil(() => counter >= 10, 3000);

            Thread.Sleep(3000);//等待运行3秒
            Assert.Equal(10, counter);

            await kafkaServer.StopAsync();
        }
Пример #5
0
        public void Port_CanBeSet()
        {
            //Arrange
            KafkaServer server = new KafkaServer();
            int         port   = 92;

            //Act
            server.Port = port;

            //Assert

            server.Port.Should().Be(port);
        }
Пример #6
0
        public void ConstructorWithParameters_SetsAdressAndPort()
        {
            //Arrange
            string address = "::1";
            int    port    = 92;

            //Act
            KafkaServer server = new KafkaServer(address, port);

            //Assert
            server.Address.Should().Be(address);
            server.Port.Should().Be(port);
        }
Пример #7
0
        public void ToString_ConcatsAddressAndPort()
        {
            //Arrange
            string address        = "localhost";
            int    port           = 92;
            string expectedString = $"{address}:{port}";
            var    server         = new KafkaServer()
            {
                Address = address, Port = port
            };

            //Act
            string serverString = server.ToString();

            //Assert
            serverString.Should().Be(expectedString);
        }