public async Task ForcedDefaultBinaryFileDeserializer_ProducedAndConsumed()
        {
            var message = new BinaryFileMessage
            {
                Content     = new MemoryStream(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }),
                ContentType = "application/pdf"
            };

            Host.ConfigureServices(
                services => services
                .AddLogging()
                .AddSilverback()
                .WithConnectionToMessageBroker(options => options.AddMockedKafka())
                .AddEndpoints(
                    endpoints => endpoints
                    .AddOutbound <IBinaryFileMessage>(new KafkaProducerEndpoint("test-e2e"))
                    .AddInbound(
                        new KafkaConsumerEndpoint("test-e2e")
            {
                Serializer = BinaryFileMessageSerializer.Default
            }))
                .AddSingletonBrokerBehavior <SpyBrokerBehavior>())
            .Run();

            var publisher = Host.ScopedServiceProvider.GetRequiredService <IPublisher>();

            await publisher.PublishAsync(message);

            SpyBehavior.OutboundEnvelopes.Should().HaveCount(1);
            SpyBehavior.OutboundEnvelopes[0].RawMessage.Should().BeEquivalentTo(message.Content);
            SpyBehavior.InboundEnvelopes.Should().HaveCount(1);
            SpyBehavior.InboundEnvelopes[0].Message.Should().BeEquivalentTo(message);
            SpyBehavior.InboundEnvelopes[0].Headers.Should().ContainEquivalentOf(
                new MessageHeader("content-type", "application/pdf"));
        }
        public async Task EncryptionAndChunking_EncryptedAndChunkedThenAggregatedAndDecrypted()
        {
            var message = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
                    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
                    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30
                }),
                ContentType = "application/pdf"
            };

            Host.ConfigureServices(
                services => services
                .AddLogging()
                .AddSilverback()
                .WithConnectionToMessageBroker(options => options.AddMockedKafka())
                .AddEndpoints(
                    endpoints => endpoints
                    .AddOutbound <IBinaryFileMessage>(
                        new KafkaProducerEndpoint("test-e2e")
            {
                Chunk = new ChunkSettings
                {
                    Size = 10
                },
                Encryption = new SymmetricEncryptionSettings
                {
                    Key = AesEncryptionKey
                }
            })
                    .AddInbound(
                        new KafkaConsumerEndpoint("test-e2e")
            {
                Encryption = new SymmetricEncryptionSettings
                {
                    Key = AesEncryptionKey
                }
            }))
                .AddSingletonBrokerBehavior <SpyBrokerBehavior>())
            .Run();

            var publisher = Host.ScopedServiceProvider.GetRequiredService <IPublisher>();

            await publisher.PublishAsync(message);

            SpyBehavior.OutboundEnvelopes.Should().HaveCount(5);
            SpyBehavior.OutboundEnvelopes.SelectMany(envelope => envelope.RawMessage.ReadAll()).Should()
            .NotBeEquivalentTo(message.Content.ReadAll());
            SpyBehavior.OutboundEnvelopes.ForEach(
                envelope =>
            {
                envelope.RawMessage.Should().NotBeNull();
                envelope.RawMessage !.Length.Should().BeLessOrEqualTo(30);
            });
            SpyBehavior.InboundEnvelopes.Should().HaveCount(1);
            SpyBehavior.InboundEnvelopes[0].Message.Should().BeEquivalentTo(message);
        }
        public async Task HandleAsync_NonBinaryFileMessage_EnvelopeUntouched()
        {
            var message = new BinaryFileMessage
            {
                Content = new MemoryStream(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 })
            };
            var endpoint = TestProducerEndpoint.GetDefault();

            endpoint.Serializer = new BinaryFileMessageSerializer();
            var envelope = new OutboundEnvelope(message, null, endpoint);

            IOutboundEnvelope?result = null;

            await new BinaryFileHandlerProducerBehavior().HandleAsync(
                new ProducerPipelineContext(
                    envelope,
                    Substitute.For <IProducer>(),
                    Substitute.For <IServiceProvider>()),
                context =>
            {
                result = context.Envelope;
                return(Task.CompletedTask);
            });

            result.Should().NotBeNull();
            result !.Should().BeSameAs(envelope);
        }
        public async Task SerializeAsync_BinaryFileMessage_RawContentProduced()
        {
            var message = new BinaryFileMessage
            {
                Content = new MemoryStream(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 })
            };
            var headers = new MessageHeaderCollection();

            var result = await new BinaryFileMessageSerializer()
                         .SerializeAsync(message, headers, MessageSerializationContext.Empty);

            result.Should().BeEquivalentTo(message.Content);
        }
        public async Task SerializeAsync_Message_TypeHeaderAdded()
        {
            var message = new BinaryFileMessage
            {
                Content = new MemoryStream(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 })
            };
            var headers = new MessageHeaderCollection();

            var serializer = new BinaryFileMessageSerializer();

            await serializer.SerializeAsync(message, headers, MessageSerializationContext.Empty);

            var typeHeaderValue = headers["x-message-type"];

            typeHeaderValue.Should().NotBeNullOrEmpty();
            typeHeaderValue.Should()
            .StartWith("Silverback.Messaging.Messages.BinaryFileMessage, Silverback.Integration,");
        }
Пример #6
0
        public async Task <IActionResult> ProduceBinaryFile(string filePath, string?contentType)
        {
            // Open specified file stream
            using var fileStream = System.IO.File.OpenRead(filePath);

            // Create a BinaryFileMessage that wraps the file stream
            var binaryFileMessage = new BinaryFileMessage(fileStream);

            if (!string.IsNullOrEmpty(contentType))
            {
                binaryFileMessage.ContentType = contentType;
            }

            // Publish the BinaryFileMessage that will be routed to the outbound endpoint. The FileStream will
            // be read and produced chunk by chunk, without the entire file being loaaded into memory.
            await _publisher.PublishAsync(binaryFileMessage);

            return(NoContent());
        }
        public async Task SerializeDeserializeAsync_Message_CorrectlyDeserialized()
        {
            var message = new BinaryFileMessage
            {
                Content = new MemoryStream(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 })
            };
            var headers = new MessageHeaderCollection();

            var serializer = new BinaryFileMessageSerializer();

            var serialized = await serializer.SerializeAsync(message, headers, MessageSerializationContext.Empty);

            var(deserialized, _) = await serializer
                                   .DeserializeAsync(serialized, headers, MessageSerializationContext.Empty);

            var message2 = deserialized as BinaryFileMessage;

            message2.Should().NotBeNull();
            message2.Should().BeEquivalentTo(message);
        }
        public async Task DefaultSettings_ProducedAndConsumed()
        {
            var message = new BinaryFileMessage
            {
                Content     = new MemoryStream(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }),
                ContentType = "application/pdf"
            };

            var serviceProvider = Host.ConfigureServices(
                services => services
                .AddLogging()
                .AddSilverback()
                .WithConnectionToMessageBroker(options => options.AddMockedKafka())
                .AddEndpoints(
                    endpoints => endpoints
                    .AddOutbound <IBinaryFileMessage>(new KafkaProducerEndpoint("test-e2e"))
                    .AddInbound(new KafkaConsumerEndpoint("test-e2e")))
                .AddSingletonBrokerBehavior <SpyBrokerBehavior>())
                                  .Run();

            var publisher = serviceProvider.GetRequiredService <IPublisher>();
            await publisher.PublishAsync(message);

            SpyBehavior.OutboundEnvelopes.Should().HaveCount(1);
            SpyBehavior.OutboundEnvelopes[0].RawMessage.ReReadAll().Should()
            .BeEquivalentTo(message.Content.ReReadAll());
            SpyBehavior.InboundEnvelopes.Should().HaveCount(1);
            SpyBehavior.InboundEnvelopes[0].Should().BeAssignableTo <IInboundEnvelope <BinaryFileMessage> >();
            SpyBehavior.InboundEnvelopes[0].Headers.Should()
            .ContainEquivalentOf(new MessageHeader("content-type", "application/pdf"));
            var inboundBinaryFile = (IInboundEnvelope <BinaryFileMessage>)SpyBehavior.InboundEnvelopes[0];

            inboundBinaryFile.Message.Should().NotBeNull();
            inboundBinaryFile.Message !.ContentType.Should().BeEquivalentTo(message.ContentType);
            inboundBinaryFile.Message !.Content.ReReadAll().Should().BeEquivalentTo(message.Content.ReReadAll());
        }
Пример #9
0
        public async Task RetryPolicy_BinaryFileChunkSequenceProcessedAfterSomeTries_RetriedMultipleTimesAndCommitted()
        {
            var message1 = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
                    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
                    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30
                }),
                ContentType = "application/pdf"
            };

            var message2 = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
                    0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x40,
                    0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x50
                }),
                ContentType = "text/plain"
            };

            var tryCount      = 0;
            var receivedFiles = new List <byte[]?>();

            var serviceProvider = Host.ConfigureServices(
                services => services
                .AddLogging()
                .AddSilverback()
                .UseModel()
                .WithConnectionToMessageBroker(
                    options => options.AddMockedKafka(
                        mockedKafkaOptions => mockedKafkaOptions.WithDefaultPartitionsCount(1)))
                .AddEndpoints(
                    endpoints => endpoints
                    .AddOutbound <IBinaryFileMessage>(
                        new KafkaProducerEndpoint(DefaultTopicName)
            {
                Chunk = new ChunkSettings
                {
                    Size = 10
                }
            })
                    .AddInbound(
                        new KafkaConsumerEndpoint(DefaultTopicName)
            {
                Configuration = new KafkaConsumerConfig
                {
                    GroupId          = "consumer1",
                    EnableAutoCommit = false,
                    CommitOffsetEach = 1
                },
                ErrorPolicy = ErrorPolicy.Retry().MaxFailedAttempts(10)
            }))
                .AddDelegateSubscriber(
                    (BinaryFileMessage binaryFile) =>
            {
                if (binaryFile.ContentType != "text/plain")
                {
                    tryCount++;

                    if (tryCount != 2)
                    {
                        // Read only first chunk
                        var buffer = new byte[10];
                        binaryFile.Content !.Read(buffer, 0, 10);
                        throw new InvalidOperationException("Retry!");
                    }
                }

                lock (receivedFiles)
                {
                    receivedFiles.Add(binaryFile.Content.ReadAll());
                }
            })
                .AddSingletonBrokerBehavior <SpyBrokerBehavior>())
                                  .Run();

            var publisher = serviceProvider.GetRequiredService <IPublisher>();

            await publisher.PublishAsync(message1);

            await publisher.PublishAsync(message2);

            await KafkaTestingHelper.WaitUntilAllMessagesAreConsumedAsync();

            tryCount.Should().Be(2);

            SpyBehavior.OutboundEnvelopes.Should().HaveCount(6);
            SpyBehavior.OutboundEnvelopes.ForEach(envelope => envelope.RawMessage.ReReadAll() !.Length.Should().Be(10));
            SpyBehavior.InboundEnvelopes.Should().HaveCount(3);

            SpyBehavior.InboundEnvelopes[0].Message.As <BinaryFileMessage>().ContentType.Should().Be("application/pdf");
            SpyBehavior.InboundEnvelopes[1].Message.As <BinaryFileMessage>().ContentType.Should().Be("application/pdf");
            SpyBehavior.InboundEnvelopes[2].Message.As <BinaryFileMessage>().ContentType.Should().Be("text/plain");

            receivedFiles.Should().HaveCount(2);
            receivedFiles[0].Should().BeEquivalentTo(message1.Content.ReReadAll());
            receivedFiles[1].Should().BeEquivalentTo(message2.Content.ReReadAll());

            DefaultTopic.GetCommittedOffsetsCount("consumer1").Should().Be(6);
        }
Пример #10
0
        public async Task Encryption_BinaryFile_EncryptedAndDecrypted()
        {
            var message1 = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
                    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
                    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30
                }),
                ContentType = "application/pdf"
            };

            var message2 = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
                    0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x40,
                    0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x50
                }),
                ContentType = "text/plain"
            };

            var receivedFiles = new List <byte[]?>();

            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 <IBinaryFileMessage>(
                        endpoint => endpoint
                        .ProduceTo(DefaultTopicName)
                        .EncryptUsingAes(AesEncryptionKey))
                    .AddInbound(
                        endpoint => endpoint
                        .ConsumeFrom(DefaultTopicName)
                        .DecryptUsingAes(AesEncryptionKey)))
                .AddDelegateSubscriber(
                    (BinaryFileMessage binaryFile) =>
                    receivedFiles.Add(binaryFile.Content.ReadAll()))
                .AddIntegrationSpy();
            })
            .Run();

            var publisher = Host.ScopedServiceProvider.GetRequiredService <IPublisher>();
            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[1].RawMessage.Should().BeOfType <SymmetricEncryptStream>();

            receivedFiles.Should().HaveCount(2);
            receivedFiles.Should().BeEquivalentTo(message1.Content.ReReadAll(), message2.Content.ReReadAll());
        }
Пример #11
0
        public async Task BinaryFile_DefaultSettings_ProducedAndConsumed()
        {
            var message1 = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
                    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
                    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30
                }),
                ContentType = "application/pdf"
            };

            var message2 = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
                    0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x40,
                    0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x50
                }),
                ContentType = "text/plain"
            };

            var receivedFiles = new List <byte[]?>();

            var serviceProvider = Host.ConfigureServices(
                services =>
            {
                services
                .AddLogging()
                .AddSilverback()
                .UseModel()
                .WithConnectionToMessageBroker(options => options.AddMockedKafka())
                .AddEndpoints(
                    endpoints => endpoints
                    .AddOutbound <IBinaryFileMessage>(new KafkaProducerEndpoint(DefaultTopicName))
                    .AddInbound(
                        new KafkaConsumerEndpoint(DefaultTopicName)
                {
                    Configuration = new KafkaConsumerConfig
                    {
                        GroupId = "consumer1",
                        AutoCommitIntervalMs = 100
                    }
                }))
                .AddDelegateSubscriber(
                    (BinaryFileMessage binaryFile) => receivedFiles.Add(binaryFile.Content.ReadAll()))
                .AddSingletonBrokerBehavior <SpyBrokerBehavior>();
            })
                                  .Run();

            var publisher = serviceProvider.GetRequiredService <IPublisher>();
            await publisher.PublishAsync(message1);

            await publisher.PublishAsync(message2);

            await KafkaTestingHelper.WaitUntilAllMessagesAreConsumedAsync();

            SpyBehavior.OutboundEnvelopes.Should().HaveCount(2);
            SpyBehavior.InboundEnvelopes.Should().HaveCount(2);
            SpyBehavior.InboundEnvelopes
            .Select(envelope => envelope.Message.As <BinaryFileMessage>().ContentType)
            .Should().BeEquivalentTo("application/pdf", "text/plain");

            receivedFiles.Should().HaveCount(2);
            receivedFiles.Should().BeEquivalentTo(message1.Content.ReReadAll(), message2.Content.ReReadAll());
        }
Пример #12
0
        public async Task Encryption_ChunkedBinaryFile_EncryptedAndDecrypted()
        {
            var message1 = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
                    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
                    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30
                }),
                ContentType = "application/pdf"
            };

            var message2 = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
                    0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x40,
                    0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x50
                }),
                ContentType = "text/plain"
            };

            var receivedFiles = new List <byte[]?>();

            var serviceProvider = Host.ConfigureServices(
                services =>
            {
                services
                .AddLogging()
                .AddSilverback()
                .UseModel()
                .WithConnectionToMessageBroker(
                    options => options.AddMockedKafka(
                        mockedKafkaOptions => mockedKafkaOptions.WithDefaultPartitionsCount(1)))
                .AddEndpoints(
                    endpoints => endpoints
                    .AddOutbound <IBinaryFileMessage>(
                        new KafkaProducerEndpoint(DefaultTopicName)
                {
                    Chunk = new ChunkSettings
                    {
                        Size = 10
                    },
                    Encryption = new SymmetricEncryptionSettings
                    {
                        Key = AesEncryptionKey
                    }
                })
                    .AddInbound(
                        new KafkaConsumerEndpoint(DefaultTopicName)
                {
                    Configuration = new KafkaConsumerConfig
                    {
                        GroupId = "consumer1",
                        AutoCommitIntervalMs = 100
                    },
                    Encryption = new SymmetricEncryptionSettings
                    {
                        Key = AesEncryptionKey
                    }
                }))
                .AddDelegateSubscriber(
                    (BinaryFileMessage binaryFile) => receivedFiles.Add(binaryFile.Content.ReadAll()))
                .AddSingletonBrokerBehavior <SpyBrokerBehavior>();
            })
                                  .Run();

            var publisher = serviceProvider.GetRequiredService <IPublisher>();
            await publisher.PublishAsync(message1);

            await publisher.PublishAsync(message2);

            await KafkaTestingHelper.WaitUntilAllMessagesAreConsumedAsync();

            SpyBehavior.OutboundEnvelopes.Should().HaveCount(12);

            for (int i = 0; i < 6; i++)
            {
                SpyBehavior.OutboundEnvelopes[i].RawMessage.Should().NotBeNull();
                SpyBehavior.OutboundEnvelopes[i].RawMessage !.Length.Should().BeLessOrEqualTo(10);
                SpyBehavior.OutboundEnvelopes[i].RawMessage.ReReadAll().Should()
                .NotBeEquivalentTo(message1.Content.ReReadAll() !.Skip(i * 10).Take(10));
            }

            for (int i = 0; i < 6; i++)
            {
                SpyBehavior.OutboundEnvelopes[i + 6].RawMessage.Should().NotBeNull();
                SpyBehavior.OutboundEnvelopes[i + 6].RawMessage !.Length.Should().BeLessOrEqualTo(10);
                SpyBehavior.OutboundEnvelopes[i + 6].RawMessage.ReReadAll().Should()
                .NotBeEquivalentTo(message2.Content.ReReadAll() !.Skip(i * 10).Take(10));
            }

            receivedFiles.Should().HaveCount(2);
            receivedFiles.Should().BeEquivalentTo(message1.Content.ReReadAll(), message2.Content.ReReadAll());
        }
Пример #13
0
        public async Task Encryption_ChunkedBinaryFile_EncryptedAndDecrypted()
        {
            var message1 = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
                    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
                    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30
                }),
                ContentType = "application/pdf"
            };

            var message2 = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
                    0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x40,
                    0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x50
                }),
                ContentType = "text/plain"
            };

            var receivedFiles = new List <byte[]?>();

            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 <IBinaryFileMessage>(
                        endpoint => endpoint
                        .ProduceTo(DefaultTopicName)
                        .EnableChunking(10)
                        .EncryptUsingAes(AesEncryptionKey))
                    .AddInbound(
                        endpoint => endpoint
                        .ConsumeFrom(DefaultTopicName)
                        .DecryptUsingAes(AesEncryptionKey)
                        .Configure(
                            config =>
                {
                    config.GroupId = "consumer1";
                })))
                .AddDelegateSubscriber(
                    (BinaryFileMessage binaryFile) => receivedFiles.Add(binaryFile.Content.ReadAll()))
                .AddIntegrationSpy();
            })
            .Run();

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

            await publisher.PublishAsync(message2);

            await Helper.WaitUntilAllMessagesAreConsumedAsync();

            Helper.Spy.RawOutboundEnvelopes.Should().HaveCount(12);

            for (int i = 0; i < 6; i++)
            {
                Helper.Spy.RawOutboundEnvelopes[i].RawMessage.Should().NotBeNull();
                Helper.Spy.RawOutboundEnvelopes[i].RawMessage !.Length.Should().BeLessOrEqualTo(10);
                Helper.Spy.RawOutboundEnvelopes[i].RawMessage.ReReadAll().Should()
                .NotBeEquivalentTo(message1.Content.ReReadAll() !.Skip(i * 10).Take(10));
            }

            for (int i = 0; i < 6; i++)
            {
                Helper.Spy.RawOutboundEnvelopes[i + 6].RawMessage.Should().NotBeNull();
                Helper.Spy.RawOutboundEnvelopes[i + 6].RawMessage !.Length.Should().BeLessOrEqualTo(10);
                Helper.Spy.RawOutboundEnvelopes[i + 6].RawMessage.ReReadAll().Should()
                .NotBeEquivalentTo(message2.Content.ReReadAll() !.Skip(i * 10).Take(10));
            }

            receivedFiles.Should().HaveCount(2);
            receivedFiles.Should().BeEquivalentTo(message1.Content.ReReadAll(), message2.Content.ReReadAll());
        }
Пример #14
0
        public async Task BinaryFile_DefaultSettings_ProducedAndConsumed()
        {
            var message1 = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
                    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
                    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30
                }),
                ContentType = "application/pdf"
            };

            var message2 = new BinaryFileMessage
            {
                Content = new MemoryStream(
                    new byte[]
                {
                    0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
                    0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x40,
                    0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x50
                }),
                ContentType = "text/plain"
            };

            var receivedFiles = new List <byte[]?>();

            Host.ConfigureServices(
                services =>
            {
                services
                .AddLogging()
                .AddSilverback()
                .UseModel()
                .WithConnectionToMessageBroker(options => options.AddMockedKafka())
                .AddKafkaEndpoints(
                    endpoints => endpoints
                    .Configure(
                        config =>
                {
                    config.BootstrapServers = "PLAINTEXT://e2e";
                })
                    .AddOutbound <IBinaryFileMessage>(
                        endpoint => endpoint.ProduceTo(DefaultTopicName))
                    .AddInbound(
                        endpoint => endpoint
                        .ConsumeFrom(DefaultTopicName)
                        .Configure(
                            config =>
                {
                    config.GroupId = "consumer1";
                })))
                .AddDelegateSubscriber(
                    (BinaryFileMessage binaryFile) =>
                {
                    lock (receivedFiles)
                    {
                        receivedFiles.Add(binaryFile.Content.ReadAll());
                    }
                })
                .AddIntegrationSpy();
            })
            .Run();

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

            await publisher.PublishAsync(message2);

            await Helper.WaitUntilAllMessagesAreConsumedAsync();

            Helper.Spy.OutboundEnvelopes.Should().HaveCount(2);
            Helper.Spy.InboundEnvelopes.Should().HaveCount(2);
            Helper.Spy.InboundEnvelopes
            .Select(envelope => envelope.Message.As <BinaryFileMessage>().ContentType)
            .Should().BeEquivalentTo("application/pdf", "text/plain");

            receivedFiles.Should().HaveCount(2);
            receivedFiles.Should().BeEquivalentTo(
                new[]
            {
                message1.Content.ReReadAll(),
                message2.Content.ReReadAll()
            });
        }