public async Task ShouldUpdateMetadataOnes() { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10); var router = routerProxy.Create(); ProtocolGateway protocolGateway = new ProtocolGateway(router); routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnValidMessage; routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers; int numberOfCall = 1000; Task[] tasks = new Task[numberOfCall]; for (int i = 0; i < numberOfCall / 2; i++) { tasks[i] = protocolGateway.SendProtocolRequest(new FetchRequest(), BrokerRouterProxy.TestTopic, _partitionId); } await Task.Delay(routerProxy._cacheExpiration); await Task.Delay(1); for (int i = 0; i < numberOfCall / 2; i++) { tasks[i + numberOfCall / 2] = protocolGateway.SendProtocolRequest(new FetchRequest(), BrokerRouterProxy.TestTopic, _partitionId); } await Task.WhenAll(tasks); Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(numberOfCall)); Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(1)); }
public async Task ProtocolGateway() { int partitionId = 0; var router = new BrokerRouter(Options); var producer = new Producer(router); string messge1 = Guid.NewGuid().ToString(); var respose = await producer.SendMessageAsync(IntegrationConfig.IntegrationTopic, new[] { new Message(messge1) }, 1, null, MessageCodec.CodecNone, partitionId); var offset = respose.FirstOrDefault().Offset; ProtocolGateway protocolGateway = new ProtocolGateway(IntegrationConfig.IntegrationUri); var fetch = new Fetch { Topic = IntegrationConfig.IntegrationTopic, PartitionId = partitionId, Offset = offset, MaxBytes = 32000, }; var fetches = new List<Fetch> { fetch }; var fetchRequest = new FetchRequest { MaxWaitTime = 1000, MinBytes = 10, Fetches = fetches }; var r = await protocolGateway.SendProtocolRequest(fetchRequest, IntegrationConfig.IntegrationTopic, partitionId); // var r1 = await protocolGateway.SendProtocolRequest(fetchRequest, IntegrationConfig.IntegrationTopic, partitionId); Assert.IsTrue(r.Messages.FirstOrDefault().Value.ToUtf8String() == messge1); }
public async Task ShouldTryToRefreshMataDataIfCanRecoverByRefreshMetadata(ErrorResponseCode code) { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = new TimeSpan(10); var router = routerProxy.Create(); int partitionId = 0; ProtocolGateway protocolGateway = new ProtocolGateway(router); var fetchRequest = new FetchRequest(); bool sendExOnFirstTime = true; Func<Task<FetchResponse>> ShouldReturnErrorAndThenNoError = async () => { Task.Delay(routerProxy._cacheExpiration).Wait(); Task.Delay(1).Wait(); if (sendExOnFirstTime) { sendExOnFirstTime = false; return new FetchResponse() { Error = (short)code }; } return new FetchResponse() { Error = (short)ErrorResponseCode.NoError }; }; routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnErrorAndThenNoError; routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.DefaultMetadataResponse; await protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId); Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2)); Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(2)); }
public async Task ShouldRecoverFromFailerByUpdateMetadataOnce() //Do not debug this test !! { var log = new DefaultTraceLog(); var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(1000); var router = routerProxy.Create(); int partitionId = 0; ProtocolGateway protocolGateway = new ProtocolGateway(router); var fetchRequest = new FetchRequest(); int numberOfCall = 100; long numberOfErrorSend = 0; TaskCompletionSource <int> x = new TaskCompletionSource <int>(); Func <Task <FetchResponse> > ShouldReturnNotLeaderForPartitionAndThenNoError = async() => { log.DebugFormat("FetchResponse Start "); if (!x.Task.IsCompleted) { if (Interlocked.Increment(ref numberOfErrorSend) == numberOfCall) { await Task.Delay(routerProxy._cacheExpiration); await Task.Delay(1); x.TrySetResult(1); log.DebugFormat("all is complete "); } await x.Task; log.DebugFormat("SocketException "); throw new BrokerConnectionException("", new KafkaEndpoint()); } log.DebugFormat("Completed "); return(new FetchResponse() { Error = (short)ErrorResponseCode.NoError }); }; routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnNotLeaderForPartitionAndThenNoError; routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers; Task[] tasks = new Task[numberOfCall]; for (int i = 0; i < numberOfCall; i++) { tasks[i] = protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId); } await Task.WhenAll(tasks); Assert.That(numberOfErrorSend, Is.GreaterThan(1), "numberOfErrorSend"); Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(numberOfCall + numberOfErrorSend), "FetchRequestCallCount"); Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2), "MetadataRequestCallCount"); }
public async Task ShouldThrowFormatExceptionWhenTopicIsInvalid() { var routerProxy = new BrokerRouterProxy(_kernel); var router = routerProxy.Create(); string invalidTopic = " "; var fetchRequest = new FetchRequest(); ProtocolGateway protocolGateway = new ProtocolGateway(router); await protocolGateway.SendProtocolRequest(fetchRequest, invalidTopic, 0); }
public async Task ShouldRecoverFromFailerByUpdateMetadataOnceFullScenario1() { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(0); var router = routerProxy.Create(); int partitionId = 0; ProtocolGateway protocolGateway = new ProtocolGateway(router); var fetchRequest = new FetchRequest(); CreateSuccessfulSendMock(routerProxy); //Send Successful Message await protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId); Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(1), "FetchRequestCallCount"); Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(1), "MetadataRequestCallCount"); Assert.That(routerProxy.BrokerConn1.MetadataRequestCallCount, Is.EqualTo(0), "MetadataRequestCallCount"); routerProxy.BrokerConn0.FetchResponseFunction = FailedInFirstMessageError(ErrorResponseCode.LeaderNotAvailable, TimeSpan.Zero); routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithSingleBroker; routerProxy.BrokerConn1.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithSingleBroker; //Reset variables routerProxy.BrokerConn0.FetchRequestCallCount = 0; routerProxy.BrokerConn1.FetchRequestCallCount = 0; routerProxy.BrokerConn0.MetadataRequestCallCount = 0; routerProxy.BrokerConn1.MetadataRequestCallCount = 0; //Send Successful Message that was recover from exception await protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId); Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(1), "FetchRequestCallCount"); Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(1), "MetadataRequestCallCount"); Assert.That(routerProxy.BrokerConn1.FetchRequestCallCount, Is.EqualTo(1), "FetchRequestCallCount"); Assert.That(routerProxy.BrokerConn1.MetadataRequestCallCount, Is.EqualTo(0), "MetadataRequestCallCount"); }
public async Task SendProtocolRequestShouldNoTryToRefreshMataDataIfCanNotRecoverByRefreshMetadata( ErrorResponseCode code) { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10); var router = routerProxy.Create(); ProtocolGateway protocolGateway = new ProtocolGateway(router); routerProxy.BrokerConn0.FetchResponseFunction = FailedInFirstMessageError(code, routerProxy._cacheExpiration); routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers; await protocolGateway.SendProtocolRequest(new FetchRequest(), BrokerRouterProxy.TestTopic, _partitionId); }
public async Task ShouldRecoverUpdateMetadataForNewTopic() { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10); var router = routerProxy.Create(); ProtocolGateway protocolGateway = new ProtocolGateway(router); var fetchRequest = new FetchRequest(); routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnValidMessage; routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers; int numberOfCall = 1000; Task[] tasks = new Task[numberOfCall]; for (int i = 0; i < numberOfCall / 2; i++) { tasks[i] = protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, _partitionId); } routerProxy.BrokerConn0.MetadataResponseFunction = async() => { var response = await BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers(); response.Topics[0].Name = "test2"; return(response); }; for (int i = 0; i < numberOfCall / 2; i++) { tasks[i + numberOfCall / 2] = protocolGateway.SendProtocolRequest(fetchRequest, "test2", _partitionId); } await Task.WhenAll(tasks); Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(numberOfCall)); Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2)); }
public async Task ShouldTryToRefreshMataDataIfSocketException() { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10); var router = routerProxy.Create(); ProtocolGateway protocolGateway = new ProtocolGateway(router); routerProxy.BrokerConn0.FetchResponseFunction = FailedInFirstMessageException(typeof(SocketException), routerProxy._cacheExpiration); routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers; await protocolGateway.SendProtocolRequest(new FetchRequest(), BrokerRouterProxy.TestTopic, _partitionId); Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2)); Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(2)); }
public async Task ShouldTryToRefreshMataDataIfCanRecoverByRefreshMetadata(ErrorResponseCode code) { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = new TimeSpan(10); var router = routerProxy.Create(); ProtocolGateway protocolGateway = new ProtocolGateway(router); routerProxy.BrokerConn0.FetchResponseFunction = FailedInFirstMessageError(code, routerProxy._cacheExpiration); routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers; await protocolGateway.SendProtocolRequest(new FetchRequest(), BrokerRouterProxy.TestTopic, _partitionId); Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2)); Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(2)); }
public async Task ShouldTryToRefreshMataDataIfSocketException() { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10); var router = routerProxy.Create(); ProtocolGateway protocolGateway = new ProtocolGateway(router); routerProxy.BrokerConn0.FetchResponseFunction = FailedInFirstMessageException(typeof(BrokerConnectionException), routerProxy._cacheExpiration); routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers; await protocolGateway.SendProtocolRequest(new FetchRequest(), BrokerRouterProxy.TestTopic, _partitionId); Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2)); Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(2)); }
public async Task SendProtocolRequestShouldThrowException(Type exceptionType) { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10); var router = routerProxy.Create(); ProtocolGateway protocolGateway = new ProtocolGateway(router); routerProxy.BrokerConn0.FetchResponseFunction = FailedInFirstMessageException(exceptionType, routerProxy._cacheExpiration); routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers; try { await protocolGateway.SendProtocolRequest(new FetchRequest(), BrokerRouterProxy.TestTopic, _partitionId); Assert.IsTrue(false, "Should throw exception"); } catch (Exception ex) { Assert.That(ex.GetType(), Is.EqualTo(exceptionType)); } }
public async Task ProtocolGateway() { int partitionId = 0; var router = new BrokerRouter(Options); var producer = new Producer(router); string messge1 = Guid.NewGuid().ToString(); var respose = await producer.SendMessageAsync(IntegrationConfig.IntegrationTopic, new[] { new Message(messge1) }, 1, null, MessageCodec.CodecNone, partitionId); var offset = respose.FirstOrDefault().Offset; ProtocolGateway protocolGateway = new ProtocolGateway(IntegrationConfig.IntegrationUri); var fetch = new Fetch { Topic = IntegrationConfig.IntegrationTopic, PartitionId = partitionId, Offset = offset, MaxBytes = 32000, }; var fetches = new List <Fetch> { fetch }; var fetchRequest = new FetchRequest { MaxWaitTime = 1000, MinBytes = 10, Fetches = fetches }; var r = await protocolGateway.SendProtocolRequest(fetchRequest, IntegrationConfig.IntegrationTopic, partitionId); // var r1 = await protocolGateway.SendProtocolRequest(fetchRequest, IntegrationConfig.IntegrationTopic, partitionId); Assert.IsTrue(r.Messages.FirstOrDefault().Value.ToUtf8String() == messge1); }
public async Task ShouldRecoverFromFailerByUpdateMetadataOnce() //Do not debug this test !! { var log = new DefaultTraceLog(); var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(1000); var router = routerProxy.Create(); int partitionId = 0; ProtocolGateway protocolGateway = new ProtocolGateway(router); var fetchRequest = new FetchRequest(); int numberOfCall = 100; long numberOfErrorSend = 0; TaskCompletionSource<int> x = new TaskCompletionSource<int>(); Func<Task<FetchResponse>> ShouldReturnNotLeaderForPartitionAndThenNoError = async () => { log.DebugFormat("FetchResponse Start "); if (!x.Task.IsCompleted) { if (Interlocked.Increment(ref numberOfErrorSend) == numberOfCall) { await Task.Delay(routerProxy._cacheExpiration); await Task.Delay(1); x.TrySetResult(1); log.DebugFormat("all is complete "); } await x.Task; log.DebugFormat("SocketException "); throw new BrokerConnectionException("",new KafkaEndpoint()); } log.DebugFormat("Completed "); return new FetchResponse() { Error = (short)ErrorResponseCode.NoError }; }; routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnNotLeaderForPartitionAndThenNoError; routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers; Task[] tasks = new Task[numberOfCall]; for (int i = 0; i < numberOfCall; i++) { tasks[i] = protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId); } await Task.WhenAll(tasks); Assert.That(numberOfErrorSend, Is.GreaterThan(1), "numberOfErrorSend"); Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(numberOfCall + numberOfErrorSend), "FetchRequestCallCount"); Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2), "MetadataRequestCallCount"); }
public async Task ShouldRecoverUpdateMetadataForNewTopic() { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10); var router = routerProxy.Create(); ProtocolGateway protocolGateway = new ProtocolGateway(router); var fetchRequest = new FetchRequest(); routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnValidMessage; routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers; int numberOfCall = 1000; Task[] tasks = new Task[numberOfCall]; for (int i = 0; i < numberOfCall / 2; i++) { tasks[i] = protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, _partitionId); } routerProxy.BrokerConn0.MetadataResponseFunction = async () => { var response = await BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers(); response.Topics[0].Name = "test2"; return response; }; for (int i = 0; i < numberOfCall / 2; i++) { tasks[i + numberOfCall / 2] = protocolGateway.SendProtocolRequest(fetchRequest, "test2", _partitionId); } await Task.WhenAll(tasks); Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(numberOfCall)); Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2)); }
public async Task ShouldRecoverUpdateMetadataForNewTopic() { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10); var router = routerProxy.Create(); int partitionId = 0; ProtocolGateway protocolGateway = new ProtocolGateway(router); var fetchRequest = new FetchRequest(); bool firstTime = true; Func<Task<FetchResponse>> ShouldReturnError = async () => { return new FetchResponse() { Error = (short)ErrorResponseCode.NoError }; }; routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnError; routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.DefaultMetadataResponse; int numberOfCall = 1000; Task[] tasks = new Task[numberOfCall]; for (int i = 0; i < numberOfCall / 2; i++) { tasks[i] = protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId); } //change MetadataResponseFunction routerProxy.BrokerConn0.MetadataResponseFunction = async () => { var response = await BrokerRouterProxy.DefaultMetadataResponse(); response.Topics[0].Name = "test2"; return response; }; for (int i = 0; i < numberOfCall / 2; i++) { tasks[i + numberOfCall / 2] = protocolGateway.SendProtocolRequest(fetchRequest, "test2", partitionId); } await Task.WhenAll(tasks); Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(numberOfCall)); Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2)); }
public async Task SendProtocolRequestShouldNoTryToRefreshMataDataIfCanNotRecoverByRefreshMetadata(ErrorResponseCode code) { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10); var router = routerProxy.Create(); int partitionId = 0; ProtocolGateway protocolGateway = new ProtocolGateway(router); var fetchRequest = new FetchRequest(); bool firstTime = true; Func<Task<FetchResponse>> ShouldReturnError = async () => { if (firstTime) { firstTime = !firstTime; return new FetchResponse() { Error = (short)code }; } return new FetchResponse() { Error = (short)ErrorResponseCode.NoError }; }; routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnError; routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.DefaultMetadataResponse; await protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId); }
public async Task SendProtocolRequestShouldThrowException(Type exceptionType) { var routerProxy = new BrokerRouterProxy(_kernel); routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10); var router = routerProxy.Create(); int partitionId = 0; ProtocolGateway protocolGateway = new ProtocolGateway(router); var fetchRequest = new FetchRequest(); bool firstTime = true; Func<Task<FetchResponse>> ShouldReturnError = async () => { if (firstTime) { firstTime = !firstTime; object[] args = new object[1]; args[0] = "error Test"; throw (Exception)Activator.CreateInstance(exceptionType, args); } return new FetchResponse() { Error = (short)ErrorResponseCode.NoError }; }; routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnError; routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.DefaultMetadataResponse; try { await protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId); Assert.IsTrue(false, "Should throw exception"); } catch (Exception ex) { Assert.That(ex.GetType(), Is.EqualTo(exceptionType)); } }