Exemple #1
0
            protected override Task GivenAsync()
            {
                _testServer = KafkaTestFramework.WithSocket();

                _testServer.On <ApiVersionsRequest, ApiVersionsResponse>(
                    request => request.Respond()
                    .WithAllApiKeys());

                _testServer.On <MetadataRequest, MetadataResponse>(
                    request => request.Respond()
                    .WithTopicsCollection(
                        request.TopicsCollection?.Select(topic =>
                                                         new Func <MetadataResponse.MetadataResponseTopic,
                                                                   MetadataResponse.MetadataResponseTopic>(
                                                             responseTopic =>
                                                             responseTopic
                                                             .WithName(topic.Name)
                                                             .WithPartitionsCollection(partition =>
                                                                                       partition
                                                                                       .WithLeaderId(Int32.From(0))
                                                                                       .WithPartitionIndex(Int32.From(0))
                                                                                       .WithReplicaNodesCollection(new[] { Int32.From(0) }))))
                        .ToArray() ?? new Func <MetadataResponse.MetadataResponseTopic, MetadataResponse.MetadataResponseTopic> [0])
                    .WithControllerId(Int32.From(0))
                    .WithClusterId(String.From("test"))
                    .WithBrokersCollection(broker => broker
                                           .WithRack(String.From("testrack"))
                                           .WithNodeId(Int32.From(0))
                                           .WithHost(String.From("localhost"))
                                           .WithPort(Int32.From(_testServer.Port)))
                    );

                _testServer.On <ProduceRequest, ProduceResponse>(async request => (await request
                                                                                   .WithActionAsync(produceRequest => Task.Run(async() =>
                {
                    _records = await produceRequest
                               .ExtractRecordsAsync(CancellationToken.None)
                               .ToListAsync()
                               .ConfigureAwait(false);
                }))
                                                                                   .ConfigureAwait(false))
                                                                 .Respond()
                                                                 .WithResponsesCollection(request.TopicsCollection.Select(topicProduceData =>
                                                                                                                          new Func <ProduceResponse.TopicProduceResponse,
                                                                                                                                    ProduceResponse.TopicProduceResponse>(
                                                                                                                              topicProduceResponse =>
                                                                                                                              topicProduceResponse
                                                                                                                              .WithName(topicProduceData.Name)
                                                                                                                              .WithPartitionsCollection(topicProduceData.PartitionsCollection.Select(partitionProduceData =>
                                                                                                                                                                                                     new Func <ProduceResponse.TopicProduceResponse.PartitionProduceResponse,
                                                                                                                                                                                                               ProduceResponse.TopicProduceResponse.PartitionProduceResponse>(
                                                                                                                                                                                                         partitionProduceResponse =>
                                                                                                                                                                                                         partitionProduceResponse
                                                                                                                                                                                                         .WithPartitionIndex(partitionProduceData.PartitionIndex)
                                                                                                                                                                                                         .WithLogAppendTimeMs(Int64.From(-1))))
                                                                                                                                                        .ToArray())))
                                                                                          .ToArray()));

                return(Task.CompletedTask);
            }
Exemple #2
0
        public async ValueTask SendAsync(
            TSendPayload payload,
            CancellationToken cancellationToken = default)
        {
            var buffer = new MemoryStream();

            await using var _ = buffer.ConfigureAwait(false);
            var writer = new KafkaWriter(buffer);

            await using (writer.ConfigureAwait(false))
            {
                await payload
                .WriteToAsync(writer, cancellationToken)
                .ConfigureAwait(false);
            }

            var lengthBuffer = new MemoryStream();

            await using (lengthBuffer
                         .ConfigureAwait(false))
            {
                var lengthWriter = new KafkaWriter(lengthBuffer);
                await using (lengthWriter.ConfigureAwait(false))
                {
                    await lengthWriter
                    .WriteInt32Async(Int32.From((int)buffer.Length), cancellationToken)
                    .ConfigureAwait(false);
                }

                await _networkClient.SendAsync(
                    lengthBuffer
                    .GetBuffer()
                    .AsMemory()
                    .Slice(0, (int)lengthBuffer.Length),
                    cancellationToken)
                .ConfigureAwait(false);
            }

            await _networkClient.SendAsync(
                buffer.GetBuffer().AsMemory()
                .Slice(0, (int)buffer.Length),
                cancellationToken)
            .ConfigureAwait(false);
        }