public async Task Bind_PushMessages_EachIsConsumedOnce()
        {
            var e1 = new TestEventOne {
                Content = "Test", Id = Guid.NewGuid()
            };
            var e2 = new TestEventTwo {
                Content = "Test", Id = Guid.NewGuid()
            };
            var o1 = new TestOffset("a", "1");
            var o2 = new TestOffset("a", "2");

            _connector.Bind(TestEndpoint.GetDefault());
            _broker.Connect();

            var consumer = _broker.Consumers.First();
            await consumer.TestPush(e1, offset : o1);

            await consumer.TestPush(e2, offset : o2);

            await consumer.TestPush(e1, offset : o1);

            await consumer.TestPush(e2, offset : o2);

            await consumer.TestPush(e1, offset : o1);

            _testSubscriber.ReceivedMessages.Count.Should().Be(2);
        }
        public async Task Bind_PushMessages_WrittenToLog()
        {
            var e1 = new TestEventOne {
                Content = "Test", Id = Guid.NewGuid()
            };
            var e2 = new TestEventTwo {
                Content = "Test", Id = Guid.NewGuid()
            };

            _connector.Bind(TestEndpoint.GetDefault());
            _broker.Connect();

            var consumer = _broker.Consumers.First();
            await consumer.TestPush(e1);

            await consumer.TestPush(e2);

            await consumer.TestPush(e1);

            await consumer.TestPush(e2);

            await consumer.TestPush(e1);

            (await _serviceProvider.GetRequiredService <IInboundLog>().GetLength()).Should().Be(2);
        }
        public async Task Bind_PushMessagesInBatch_OnlyCommittedBatchWrittenToLog()
        {
            var e1 = new TestEventOne {
                Content = "Test", Id = Guid.NewGuid()
            };
            var e2 = new TestEventTwo {
                Content = "Test", Id = Guid.NewGuid()
            };
            var e3 = new TestEventTwo {
                Content = "Test", Id = Guid.NewGuid()
            };
            var e4 = new TestEventTwo {
                Content = "Test", Id = Guid.NewGuid()
            };

            _connector.Bind(TestEndpoint.GetDefault(), settings: new InboundConnectorSettings
            {
                Batch = new Silverback.Messaging.Batch.BatchSettings
                {
                    Size = 2
                }
            });
            _broker.Connect();

            _testSubscriber.FailCondition = m => m is TestEventTwo m2 && m2.Id == e4.Id;

            var consumer = _broker.Consumers.First();

            try { await consumer.TestPush(e1); } catch { }
            try { await consumer.TestPush(e2); } catch { }
            try { await consumer.TestPush(e3); } catch { }
            try { await consumer.TestPush(e4); } catch { }

            (await _serviceProvider.GetRequiredService <IInboundLog>().GetLength()).Should().Be(2);
        }
        public async Task Bind_PushMessagesInBatch_WrittenToLog()
        {
            var e1 = new TestEventOne {
                Content = "Test", Id = Guid.NewGuid()
            };
            var e2 = new TestEventTwo {
                Content = "Test", Id = Guid.NewGuid()
            };

            _connector.Bind(TestEndpoint.GetDefault(), settings: new InboundConnectorSettings
            {
                Batch = new Silverback.Messaging.Batch.BatchSettings
                {
                    Size = 2
                }
            });
            _broker.Connect();

            var consumer = _broker.Consumers.First();
            await consumer.TestPush(e1);

            await consumer.TestPush(e2);

            await consumer.TestPush(e1);

            await consumer.TestPush(e2);

            await consumer.TestPush(e1);

            (await _serviceProvider.GetRequiredService <IInboundLog>().GetLength()).Should().Be(2);
        }
        public async Task Bind_PushMessagesInBatch_EachIsConsumedOnce()
        {
            var e1 = new TestEventOne {
                Content = "Test", Id = Guid.NewGuid()
            };
            var e2 = new TestEventTwo {
                Content = "Test", Id = Guid.NewGuid()
            };

            _connector.Bind(TestEndpoint.GetDefault(), settings: new InboundConnectorSettings
            {
                Batch = new Silverback.Messaging.Batch.BatchSettings
                {
                    Size = 2
                }
            });
            _broker.Connect();

            var consumer = _broker.Consumers.First();
            await consumer.TestPush(e1);

            await consumer.TestPush(e2);

            await consumer.TestPush(e1);

            await consumer.TestPush(e2);

            await consumer.TestPush(e1);

            _testSubscriber.ReceivedMessages.Count.Should().Be(6);
        }
        public async Task Bind_PushMessagesInBatchToMultipleConsumers_OnlyCommittedBatchWrittenToLog()
        {
            var e1 = new TestEventOne {
                Content = "Test", Id = Guid.NewGuid()
            };
            var e2 = new TestEventTwo {
                Content = "Test", Id = Guid.NewGuid()
            };
            var e3 = new TestEventTwo {
                Content = "Test", Id = Guid.NewGuid()
            };
            var e4 = new TestEventTwo {
                Content = "Test", Id = Guid.NewGuid()
            };

            _connector.Bind(TestEndpoint.GetDefault(), settings: new InboundConnectorSettings
            {
                Batch = new Silverback.Messaging.Batch.BatchSettings
                {
                    Size = 2
                },
                Consumers = 2
            });
            _broker.Connect();

            _testSubscriber.FailCondition = m => m is TestEventTwo m2 && m2.Id == e2.Id;

            var consumer1 = _broker.Consumers[0];
            var consumer2 = _broker.Consumers[1];

            var tasks = new[]
            {
                Task.Run(async() =>
                {
                    try
                    {
                        await consumer1.TestPush(e1);
                        await consumer1.TestPush(e2);
                    }
                    catch (Exception)
                    { }
                }),
                Task.Run(async() =>
                {
                    await consumer2.TestPush(e3);
                    await consumer2.TestPush(e4);
                })
            };

            await Task.WhenAll(tasks);

            (await _serviceProvider.GetRequiredService <IInboundLog>().GetLength()).Should().Be(2);
        }
Beispiel #7
0
        public async Task Encryption_SimpleMessagesNoKeyIdentifier_EncryptedAndDecrypted()
        {
            var message1 = new TestEventOne {
                Content = "Message 1"
            };
            var message2 = new TestEventTwo {
                Content = "Message 2"
            };

            Host.ConfigureServices(
                services => services
                .AddLogging()
                .AddSilverback()
                .UseModel()
                .WithConnectionToMessageBroker(
                    options => options.AddMockedKafka(
                        mockedKafkaOptions => mockedKafkaOptions.WithDefaultPartitionsCount(1)))
                .AddKafkaEndpoints(
                    endpoints => endpoints
                    .Configure(
                        config =>
            {
                config.BootstrapServers = "PLAINTEXT://e2e";
            })
                    .AddOutbound <IIntegrationEvent>(
                        endpoint => endpoint
                        .ProduceTo(DefaultTopicName)
                        .EncryptUsingAes(AesEncryptionKey))
                    .AddInbound(
                        endpoint => endpoint
                        .ConsumeFrom(DefaultTopicName)
                        .DecryptUsingAes(
                            keyIdentifier =>
            {
                return(keyIdentifier switch
                {
                    "another-encryption-key-id" => AesEncryptionKey2,
                    _ => AesEncryptionKey
                });
            })
Beispiel #8
0
        public void Bind_PushMessages_EachIsConsumedOnce()
        {
            var e1 = new TestEventOne {
                Content = "Test", Id = Guid.NewGuid()
            };
            var e2 = new TestEventTwo {
                Content = "Test", Id = Guid.NewGuid()
            };

            _connector.Bind(TestEndpoint.Default);
            _broker.Connect();

            var consumer = _broker.Consumers.First();

            consumer.TestPush(e1);
            consumer.TestPush(e2);
            consumer.TestPush(e1);
            consumer.TestPush(e2);
            consumer.TestPush(e1);

            _testSubscriber.ReceivedMessages.Count.Should().Be(2);
        }
Beispiel #9
0
        public async Task Encryption_SimpleMessagesNoKeyIdentifier_EncryptedAndDecrypted()
        {
            var message1 = new TestEventOne {
                Content = "Message 1"
            };
            var message2 = new TestEventTwo {
                Content = "Message 2"
            };

            Host.ConfigureServices(
                services => services
                .AddLogging()
                .AddSilverback()
                .UseModel()
                .WithConnectionToMessageBroker(options => options.AddMockedMqtt())
                .AddMqttEndpoints(
                    endpoints => endpoints
                    .Configure(
                        config => config
                        .WithClientId("e2e-test")
                        .ConnectViaTcp("e2e-mqtt-broker"))
                    .AddOutbound <IIntegrationEvent>(
                        endpoint => endpoint
                        .ProduceTo(DefaultTopicName)
                        .EncryptUsingAes(AesEncryptionKey))
                    .AddInbound(
                        endpoint => endpoint
                        .ConsumeFrom(DefaultTopicName)
                        .DecryptUsingAes(
                            keyIdentifier =>
            {
                return(keyIdentifier switch
                {
                    "another-encryption-key-id" => AesEncryptionKey2,
                    _ => AesEncryptionKey
                });
            })))
 Task OnMessageReceivedAsync(TestEventTwo message) => throw new Exception("Test");
Beispiel #11
0
        public async Task Encryption_SimpleMessagesWithKeyRotation_EncryptedAndDecrypted()
        {
            var message1 = new TestEventOne {
                Content = "Message 1"
            };
            var message2 = new TestEventTwo {
                Content = "Message 2"
            };
            const string keyIdentifier1 = "my-encryption-key-1";
            const string keyIdentifier2 = "my-encryption-key-2";

            Host.ConfigureServices(
                services => services
                .AddLogging()
                .AddSilverback()
                .UseModel()
                .WithConnectionToMessageBroker(
                    options => options.AddMockedKafka(
                        mockedKafkaOptions => mockedKafkaOptions.WithDefaultPartitionsCount(1)))
                .AddKafkaEndpoints(
                    endpoints => endpoints
                    .Configure(
                        config =>
            {
                config.BootstrapServers = "PLAINTEXT://e2e";
            })
                    .AddOutbound <TestEventOne>(
                        endpoint => endpoint
                        .ProduceTo(DefaultTopicName)
                        .EncryptUsingAes(AesEncryptionKey, keyIdentifier1))
                    .AddOutbound <TestEventTwo>(
                        endpoint => endpoint
                        .ProduceTo(DefaultTopicName)
                        .EncryptUsingAes(AesEncryptionKey2, keyIdentifier2))
                    .AddInbound(
                        endpoint => endpoint
                        .ConsumeFrom(DefaultTopicName)
                        .DecryptUsingAes(
                            keyIdentifier =>
            {
                switch (keyIdentifier)
                {
                case keyIdentifier1:
                    return(AesEncryptionKey);

                default:
                    return(AesEncryptionKey2);
                }
            })
                        .Configure(
                            config =>
            {
                config.GroupId = "consumer1";
            })))
                .AddIntegrationSpyAndSubscriber())
            .Run();

            var publisher = Host.ScopedServiceProvider.GetRequiredService <IEventPublisher>();
            await publisher.PublishAsync(message1);

            await publisher.PublishAsync(message2);

            await Helper.WaitUntilAllMessagesAreConsumedAsync();

            Helper.Spy.OutboundEnvelopes.Should().HaveCount(2);
            Helper.Spy.OutboundEnvelopes[0].RawMessage.Should().BeOfType <SymmetricEncryptStream>();
            Helper.Spy.OutboundEnvelopes[0].Headers.GetValue(DefaultMessageHeaders.EncryptionKeyId).Should()
            .Be(keyIdentifier1);
            Helper.Spy.OutboundEnvelopes[1].RawMessage.Should().BeOfType <SymmetricEncryptStream>();
            Helper.Spy.OutboundEnvelopes[1].Headers.GetValue(DefaultMessageHeaders.EncryptionKeyId).Should()
            .Be(keyIdentifier2);

            Helper.Spy.InboundEnvelopes.Should().HaveCount(2);
            Helper.Spy.InboundEnvelopes[0].Message.Should().BeEquivalentTo(message1);
            Helper.Spy.InboundEnvelopes[0].Headers.GetValue(DefaultMessageHeaders.EncryptionKeyId).Should()
            .Be(keyIdentifier1);
            Helper.Spy.InboundEnvelopes[1].Message.Should().BeEquivalentTo(message2);
            Helper.Spy.InboundEnvelopes[1].Headers.GetValue(DefaultMessageHeaders.EncryptionKeyId).Should()
            .Be(keyIdentifier2);

            DefaultTopic.GetCommittedOffsetsCount("consumer1").Should().Be(2);
        }
 private Task OnMessageReceivedAsync(TestEventTwo message) => throw new InvalidOperationException("Test");
 public void OnEventNameChange(TestEventTwo @event) { }
 void ReceiveTwo(TestEventTwo message) => received++;
Beispiel #15
0
        public async Task PartitionEofCallback_PublishSeveralMessages_HandlerInvokedForRightPartition()
        {
            var messageEventOne = new TestEventOne
            {
                Content = "Hello E2E!"
            };

            var messageEventTwo = new TestEventTwo
            {
                Content = "Hello E2E!"
            };

            Host.ConfigureServices(
                services => services
                .AddLogging()
                .AddSilverback()
                .UseModel()
                .AddSingletonBrokerCallbackHandler <KafkaPartitionEofCallback>()
                .WithConnectionToMessageBroker(
                    options => options
                    .AddMockedKafka(
                        mockedKafkaOptions => mockedKafkaOptions.WithDefaultPartitionsCount(2)))
                .AddKafkaEndpoints(
                    endpoints => endpoints
                    .Configure(
                        config =>
            {
                config.BootstrapServers = "PLAINTEXT://tests";
            })
                    .AddOutbound <TestEventOne>(
                        endpoint => endpoint.ProduceTo(DefaultTopicName, 0))
                    .AddOutbound <TestEventTwo>(
                        endpoint => endpoint.ProduceTo(DefaultTopicName, 1))
                    .AddInbound(
                        endpoint => endpoint
                        .ConsumeFrom(DefaultTopicName)
                        .Configure(
                            config =>
            {
                config.GroupId            = "consumer1";
                config.EnablePartitionEof = true;
            })))
                .AddIntegrationSpyAndSubscriber())
            .Run();

            var kafkaTestingHelper = Host.ServiceProvider.GetRequiredService <IKafkaTestingHelper>();

            var publisher = Host.ScopedServiceProvider.GetRequiredService <IEventPublisher>();
            var callbackHandlerKafkaPartitionEofCallback = (KafkaPartitionEofCallback)Host
                                                           .ScopedServiceProvider
                                                           .GetServices <IBrokerCallback>()
                                                           .First(service => service is KafkaPartitionEofCallback);

            var topicPartition0 = new TopicPartition(DefaultTopicName, new Partition(0));
            var topicPartition1 = new TopicPartition(DefaultTopicName, new Partition(1));

            await kafkaTestingHelper.WaitUntilAllMessagesAreConsumedAsync();

            await AsyncTestingUtil.WaitAsync(
                () => callbackHandlerKafkaPartitionEofCallback.AllPartitionsEofCallbackCount == 2);

            callbackHandlerKafkaPartitionEofCallback.AllPartitionsEofCallbackCount.Should().Be(2);
            callbackHandlerKafkaPartitionEofCallback.GetPartitionEofCallbackCount(topicPartition0).Should()
            .Be(1);
            callbackHandlerKafkaPartitionEofCallback.GetPartitionEofCallbackCount(topicPartition1).Should()
            .Be(1);

            await kafkaTestingHelper.Broker.Consumers[0].DisconnectAsync();

            await publisher.PublishAsync(messageEventOne);

            await kafkaTestingHelper.Broker.Consumers[0].ConnectAsync();

            await kafkaTestingHelper.WaitUntilAllMessagesAreConsumedAsync();

            await AsyncTestingUtil.WaitAsync(
                () => callbackHandlerKafkaPartitionEofCallback.AllPartitionsEofCallbackCount == 4);

            kafkaTestingHelper.Spy.InboundEnvelopes.Should().HaveCount(1);

            callbackHandlerKafkaPartitionEofCallback.AllPartitionsEofCallbackCount.Should().Be(4);
            callbackHandlerKafkaPartitionEofCallback.GetPartitionEofCallbackCount(topicPartition0).Should()
            .Be(2);
            callbackHandlerKafkaPartitionEofCallback.GetPartitionEofCallbackCount(topicPartition1).Should()
            .Be(2);

            await kafkaTestingHelper.Broker.Consumers[0].DisconnectAsync();

            await publisher.PublishAsync(messageEventTwo);

            await kafkaTestingHelper.Broker.Consumers[0].ConnectAsync();

            await kafkaTestingHelper.WaitUntilAllMessagesAreConsumedAsync();

            await AsyncTestingUtil.WaitAsync(
                () => callbackHandlerKafkaPartitionEofCallback.AllPartitionsEofCallbackCount == 6);

            callbackHandlerKafkaPartitionEofCallback.AllPartitionsEofCallbackCount.Should().Be(6);
            callbackHandlerKafkaPartitionEofCallback.GetPartitionEofCallbackCount(topicPartition0).Should()
            .Be(3);
            callbackHandlerKafkaPartitionEofCallback.GetPartitionEofCallbackCount(topicPartition1).Should()
            .Be(3);
        }
 private IEnumerable <ICommand> OnCommandReceived(TestEventTwo message) => new ICommand[]
 {
     new TestCommandOne(), new TestCommandTwo()
 };