Beispiel #1
0
        public async Task ShouldUpdateMetadataOnce()
        {
            var scenario        = new RoutingScenario();
            var cacheExpiration = TimeSpan.FromMilliseconds(100);
            var router          = scenario.CreateRouter(cacheExpiration);

            scenario.Connection1.Add(ApiKey.Fetch, ShouldReturnValidMessage);
            scenario.Connection1.Add(ApiKey.Metadata, async _ => await RoutingScenario.DefaultMetadataResponse());
            int numberOfCall = 1000;

            Task[] tasks = new Task[numberOfCall];
            for (int i = 0; i < numberOfCall / 2; i++)
            {
                tasks[i] = router.SendAsync(new FetchRequest(), RoutingScenario.TestTopic, 0, CancellationToken.None);
            }
            await Task.Delay(cacheExpiration);

            await Task.Delay(1);

            for (int i = 0; i < numberOfCall / 2; i++)
            {
                tasks[i + numberOfCall / 2] = router.SendAsync(new FetchRequest(), RoutingScenario.TestTopic, 0, CancellationToken.None);
            }

            await Task.WhenAll(tasks);

            Assert.That(scenario.Connection1[ApiKey.Fetch], Is.EqualTo(numberOfCall));
            Assert.That(scenario.Connection1[ApiKey.Metadata], Is.EqualTo(1));
        }
Beispiel #2
0
 private void CreateSuccessfulSendMock(RoutingScenario router)
 {
     router.Connection1.Add(ApiKey.Fetch, ShouldReturnValidMessage);
     router.Connection1.Add(ApiKey.Metadata, async _ => await RoutingScenario.DefaultMetadataResponse());
     router.Connection2.Add(ApiKey.Fetch, ShouldReturnValidMessage);
     router.Connection2.Add(ApiKey.Metadata, async _ => await RoutingScenario.DefaultMetadataResponse());
 }
Beispiel #3
0
        public async Task ShouldRecoverUpdateMetadataForNewTopic()
        {
            var scenario        = new RoutingScenario();
            var cacheExpiration = TimeSpan.FromMilliseconds(100);
            var router          = scenario.CreateRouter(cacheExpiration);

            var fetchRequest = new FetchRequest();

            scenario.Connection1.Add(ApiKey.Fetch, ShouldReturnValidMessage);
            scenario.Connection1.Add(ApiKey.Metadata, async _ => await RoutingScenario.DefaultMetadataResponse());
            int numberOfCall = 100;

            Task[] tasks = new Task[numberOfCall];
            for (int i = 0; i < numberOfCall / 2; i++)
            {
                tasks[i] = router.SendAsync(fetchRequest, RoutingScenario.TestTopic, 0, CancellationToken.None);
            }

            scenario.Connection1.Add(ApiKey.Metadata, async _ => {
                var response = await RoutingScenario.DefaultMetadataResponse();
                return(new MetadataResponse(response.brokers, response.topic_metadata.Select(t => new MetadataResponse.Topic("test2", t.topic_error_code, t.partition_metadata))));
            });

            for (int i = 0; i < numberOfCall / 2; i++)
            {
                tasks[i + numberOfCall / 2] = router.SendAsync(fetchRequest, "test2", 0, CancellationToken.None);
            }

            await Task.WhenAll(tasks);

            Assert.That(scenario.Connection1[ApiKey.Fetch], Is.EqualTo(numberOfCall));
            Assert.That(scenario.Connection1[ApiKey.Metadata], Is.EqualTo(2));
        }
Beispiel #4
0
        public async Task SendProtocolRequestShouldThrowException(Type exceptionType)
        {
            var scenario        = new RoutingScenario();
            var cacheExpiration = TimeSpan.FromMilliseconds(10);
            var router          = scenario.CreateRouter(cacheExpiration);

            scenario.Connection1.Add(ApiKey.Fetch, FailedInFirstMessageException(exceptionType, cacheExpiration));
            scenario.Connection1.Add(ApiKey.Metadata, async _ => await RoutingScenario.DefaultMetadataResponse());
            Assert.ThrowsAsync(exceptionType, async() => await router.SendAsync(new FetchRequest(), RoutingScenario.TestTopic, 0, CancellationToken.None));
        }
Beispiel #5
0
        public async Task SendProtocolRequestShouldNotTryToRefreshMataDataIfCanNotRecoverByRefreshMetadata(
            ErrorCode code)
        {
            var scenario        = new RoutingScenario();
            var cacheExpiration = TimeSpan.FromMilliseconds(10);
            var router          = scenario.CreateRouter(cacheExpiration);

            scenario.Connection1.Add(ApiKey.Fetch, FailedInFirstMessageError(code, cacheExpiration));
            scenario.Connection1.Add(ApiKey.Metadata, async _ => await RoutingScenario.DefaultMetadataResponse());
            Assert.ThrowsAsync <RequestException>(async() => await router.SendAsync(new FetchRequest(), RoutingScenario.TestTopic, 0, CancellationToken.None));
        }
Beispiel #6
0
        public async Task ShouldRecoverFromFailureByUpdateMetadataOnce() //Do not debug this test !!
        {
            var scenario        = new RoutingScenario();
            var cacheExpiration = TimeSpan.FromMilliseconds(1000);
            var router          = scenario.CreateRouter(cacheExpiration);

            int partitionId  = 0;
            var fetchRequest = new FetchRequest();

            int  numberOfCall            = 100;
            long numberOfErrorSend       = 0;
            TaskCompletionSource <int> x = new TaskCompletionSource <int>();
            Func <IRequestContext, Task <IResponse> > ShouldReturnNotLeaderForPartitionAndThenNoError = async _ =>
            {
                var log = TestConfig.Log;
                log.Debug(() => LogEvent.Create("FetchResponse Start "));
                if (!x.Task.IsCompleted)
                {
                    if (Interlocked.Increment(ref numberOfErrorSend) == numberOfCall)
                    {
                        await Task.Delay(cacheExpiration);

                        await Task.Delay(1);

                        x.TrySetResult(1);
                        log.Debug(() => LogEvent.Create("all is complete "));
                    }

                    await x.Task;
                    log.Debug(() => LogEvent.Create("SocketException "));
                    throw new ConnectionException(scenario.Connection1.Endpoint);
                }
                log.Debug(() => LogEvent.Create("Completed "));

                return(new FetchResponse());
            };

            scenario.Connection1.Add(ApiKey.Fetch, ShouldReturnNotLeaderForPartitionAndThenNoError);
            scenario.Connection1.Add(ApiKey.Metadata, async _ => await RoutingScenario.DefaultMetadataResponse());

            Task[] tasks = new Task[numberOfCall];

            for (int i = 0; i < numberOfCall; i++)
            {
                tasks[i] = router.SendAsync(fetchRequest, RoutingScenario.TestTopic, partitionId, CancellationToken.None);
            }

            await Task.WhenAll(tasks);

            Assert.That(numberOfErrorSend, Is.GreaterThan(1), "numberOfErrorSend");
            Assert.That(scenario.Connection1[ApiKey.Fetch], Is.EqualTo(numberOfCall + numberOfErrorSend),
                        "RequestCallCount(ApiKey.Fetch)");
            Assert.That(scenario.Connection1[ApiKey.Metadata], Is.EqualTo(2), "RequestCallCount(ApiKey.Metadata)");
        }
Beispiel #7
0
        public async Task SelectExactPartitionShouldThrowWhenTopicsCollectionIsEmpty()
        {
            var metadataResponse = await RoutingScenario.DefaultMetadataResponse();

            metadataResponse.topic_metadata.Clear();

            var scenario = new RoutingScenario();

#pragma warning disable 1998
            scenario.Connection1.Add(ApiKey.Metadata, async _ => metadataResponse);
#pragma warning restore 1998

            Assert.Throws <RoutingException>(() => scenario.CreateRouter().GetTopicConnection(RoutingScenario.TestTopic, 1));
        }
Beispiel #8
0
        public async Task ShouldTryToRefreshMataDataIfOnExceptions(Type exceptionType)
        {
            var scenario        = new RoutingScenario();
            var cacheExpiration = TimeSpan.FromMilliseconds(10);
            var router          = scenario.CreateRouter(cacheExpiration);

            scenario.Connection1.Add(ApiKey.Fetch, FailedInFirstMessageException(exceptionType, cacheExpiration));
            scenario.Connection1.Add(ApiKey.Metadata, async _ => await RoutingScenario.DefaultMetadataResponse());

            await router.SendAsync(new FetchRequest(), RoutingScenario.TestTopic, 0, CancellationToken.None);

            Assert.That(scenario.Connection1[ApiKey.Metadata], Is.EqualTo(2));
            Assert.That(scenario.Connection1[ApiKey.Fetch], Is.EqualTo(2));
        }
Beispiel #9
0
        public async Task SelectExactPartitionShouldThrowWhenServerCollectionIsEmpty()
        {
            var metadataResponse = await RoutingScenario.DefaultMetadataResponse();

            metadataResponse = new MetadataResponse(topics: metadataResponse.topic_metadata);

            var scenario = new RoutingScenario();

#pragma warning disable 1998
            scenario.Connection1.Add(ApiKey.Metadata, async _ => metadataResponse);
#pragma warning restore 1998
            var router    = scenario.CreateRouter();
            var testTopic = RoutingScenario.TestTopic;
            await router.GetTopicMetadataAsync(testTopic, CancellationToken.None);

            Assert.Throws <RoutingException>(() => router.GetTopicConnection(testTopic, 1));
        }
Beispiel #10
0
        public async Task UsesFactoryToAddNewServers()
        {
            // Arrange
            var connections = CreateConnections(2);

            foreach (var connection in connections)
            {
                connection
                .SendAsync(Arg.Any <IRequest <MetadataResponse> >(), Arg.Any <CancellationToken>(), Arg.Any <IRequestContext>())
                .Returns(_ => RoutingScenario.DefaultMetadataResponse());
            }
            var factory = CreateFactory(connections);
            var router  = new Router(new Endpoint(new IPEndPoint(IPAddress.Loopback, 1)), factory);

            // Act
            var testTopic = RoutingScenario.TestTopic;
            await router.GetTopicMetadataAsync(testTopic, CancellationToken.None);

            var topics = router.GetTopicMetadata(testTopic);

            // Assert
            factory.Received()
            .Create(Arg.Is <Endpoint>(e => e.Ip.Port == 2), Arg.Any <IConnectionConfiguration>(), Arg.Any <ILog>());
        }