示例#1
0
        public async Task ConsumerWithEmptyWhitelistShouldConsumeAllPartition()
        {
            var routerProxy = new BrokerRouterProxy(new MoqMockingKernel());

            var router  = routerProxy.Create();
            var options = CreateOptions(router);

            options.PartitionWhitelist = new List <int>();

            using (var consumer = new Consumer(options))
            {
                var test = consumer.Consume();

                await TaskTest.WaitFor(() => consumer.ConsumerTaskCount > 0);

                await TaskTest.WaitFor(() => routerProxy.BrokerConn0.FetchRequestCallCount > 0);

                await TaskTest.WaitFor(() => routerProxy.BrokerConn1.FetchRequestCallCount > 0);

                Assert.That(consumer.ConsumerTaskCount, Is.EqualTo(2),
                            "Consumer should create one consuming thread for each partition.");
                Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.GreaterThanOrEqualTo(1),
                            "BrokerConn0 not sent FetchRequest");
                Assert.That(routerProxy.BrokerConn1.FetchRequestCallCount, Is.GreaterThanOrEqualTo(1),
                            "BrokerConn1 not sent FetchRequest");
            }
        }
示例#2
0
        public async Task WriteShouldCancelWhileSendingData()
        {
            var writeAttempts = 0;
            var config        = new ConnectionConfiguration(onWriting: (e, payload) => Interlocked.Increment(ref writeAttempts));
            var endpoint      = Endpoint.Resolve(TestConfig.ServerUri(), TestConfig.InfoLog);

            using (var server = new FakeTcpServer(TestConfig.InfoLog, endpoint.IP.Port))
                using (var test = new TcpSocket(endpoint, config, TestConfig.InfoLog))
                    using (var token = new CancellationTokenSource())
                    {
                        var write = test.WriteAsync(new DataPayload(1.ToBytes()), token.Token);

                        await TaskTest.WaitFor(() => server.ConnectionEventcount > 0);

                        await TaskTest.WaitFor(() => writeAttempts > 0);

                        Assert.That(writeAttempts, Is.EqualTo(1), "Socket should have attempted to write.");

                        //create a buffer write that will take a long time
                        var data = Enumerable.Range(0, 100000000).Select(b => (byte)b).ToArray();
                        token.Cancel();
                        var taskResult = test.WriteAsync(
                            new DataPayload(data),
                            token.Token);
                        await Task.WhenAny(taskResult, Task.Delay(TimeSpan.FromSeconds(5))).ConfigureAwait(false);

                        Assert.That(taskResult.IsCanceled, Is.True, "Task should have cancelled.");
                    }
        }
示例#3
0
        public async Task ConsumerWhitelistShouldOnlyConsumeSpecifiedPartition()
        {
            var routerProxy = new BrokerRouterProxy(new MoqMockingKernel());

            routerProxy.BrokerConn0.FetchResponseFunction = async() => { return(new FetchResponse()); };
            var router  = routerProxy.Create();
            var options = CreateOptions(router);

            options.PartitionWhitelist = new List <int> {
                0
            };
            using (var consumer = new Consumer(options))
            {
                var test = consumer.Consume();

                await TaskTest.WaitFor(() => consumer.ConsumerTaskCount > 0);

                await TaskTest.WaitFor(() => routerProxy.BrokerConn0.FetchRequestCallCount > 0);

                Assert.That(consumer.ConsumerTaskCount, Is.EqualTo(1),
                            "Consumer should only create one consuming thread for partition 0.");
                Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.GreaterThanOrEqualTo(1));
                Assert.That(routerProxy.BrokerConn1.FetchRequestCallCount, Is.EqualTo(0));
            }
        }
示例#4
0
        public async Task SendAsyncShouldTimeoutMultipleMessagesAtATime()
        {
            var endpoint = Endpoint.Resolve(TestConfig.ServerUri(), TestConfig.InfoLog);

            using (var server = new FakeTcpServer(TestConfig.InfoLog, endpoint.IP.Port))
                using (var socket = new TcpSocket(endpoint, log: TestConfig.InfoLog))
                    using (var conn = new Connection(socket, new ConnectionConfiguration(requestTimeout: TimeSpan.FromMilliseconds(100)), log: TestConfig.InfoLog))
                    {
                        server.HasClientConnected.Wait(TimeSpan.FromSeconds(3));
                        Assert.That(server.ConnectionEventcount, Is.EqualTo(1));

                        var tasks = new[] {
                            conn.SendAsync(new MetadataRequest(), CancellationToken.None),
                            conn.SendAsync(new MetadataRequest(), CancellationToken.None),
                            conn.SendAsync(new MetadataRequest(), CancellationToken.None)
                        };

                        await TaskTest.WaitFor(() => tasks.All(t => t.IsFaulted));

                        foreach (var task in tasks)
                        {
                            Assert.That(task.IsFaulted, Is.True, "Task should have faulted.");
                            Assert.That(task.Exception.InnerException, Is.TypeOf <TimeoutException>(), "Task fault has wrong type.");
                        }
                    }
        }
示例#5
0
        //someTime failed
        public async Task ProducerShouldBlockWhenFullBufferReached()
        {
            int count = 0;
            //with max buffer set below the batch size, this should cause the producer to block until batch delay time.
            var routerProxy = new FakeBrokerRouter();

            routerProxy.BrokerConn0.ProduceResponseFunction = async() => {
                await Task.Delay(200);

                return(new ProduceResponse());
            };
            using (var producer = new Producer(routerProxy.Create(), new ProducerConfiguration(batchSize: 10, batchMaxDelay: TimeSpan.FromMilliseconds(500))))
            {
                var senderTask = Task.Factory.StartNew(async() => {
                    for (int i = 0; i < 3; i++)
                    {
                        await producer.SendMessageAsync(new Message(i.ToString()), FakeBrokerRouter.TestTopic, CancellationToken.None);
                        Console.WriteLine("Buffered: {0}, In Flight: {1}", producer.BufferedMessageCount, producer.InFlightMessageCount);
                        Interlocked.Increment(ref count);
                    }
                });

                await TaskTest.WaitFor(() => count > 0);

                Assert.That(producer.BufferedMessageCount, Is.EqualTo(1));

                Console.WriteLine("Waiting for the rest...");
                senderTask.Wait(TimeSpan.FromSeconds(5));

                Assert.That(senderTask.IsCompleted);
                Assert.That(producer.BufferedMessageCount, Is.EqualTo(1), "One message should be left in the buffer.");

                Console.WriteLine("Unwinding...");
            }
        }
示例#6
0
        public async Task ReadShouldIgnoreMessageWithUnknownCorrelationId()
        {
            const int correlationId = 99;
            var       receivedData  = false;

            var mockLog = new MemoryLog();

            var config   = new ConnectionConfiguration(onRead: (e, buffer, elapsed) => receivedData = true);
            var endpoint = Endpoint.Resolve(TestConfig.ServerUri(), TestConfig.InfoLog);

            using (var server = new FakeTcpServer(TestConfig.InfoLog, endpoint.IP.Port))
                using (var socket = new TcpSocket(endpoint, config, mockLog))
                    using (var conn = new Connection(socket, config, log: mockLog))
                    {
                        //send correlation message
                        server.SendDataAsync(CreateCorrelationMessage(correlationId)).Wait(TimeSpan.FromSeconds(5));

                        //wait for connection
                        await TaskTest.WaitFor(() => server.ConnectionEventcount > 0);

                        Assert.That(server.ConnectionEventcount, Is.EqualTo(1));

                        await TaskTest.WaitFor(() => receivedData);

                        // shortly after receivedData, but still after
                        await TaskTest.WaitFor(() => mockLog.LogEvents.Any(e => e.Item1 == LogLevel.Warn && e.Item2.Message == $"Unexpected response from {endpoint} with correlation id {correlationId} (not in request queue)."));
                    }
        }
示例#7
0
        public async Task FakeShouldBeAbleToReconnect()
        {
            var serverUri = TestConfig.ServerUri();

            using (var server = new FakeTcpServer(TestConfig.WarnLog, serverUri.Port))
            {
                byte[] received = null;
                server.OnBytesReceived += data => received = data;

                var t1 = new TcpClient();
                await t1.ConnectAsync(serverUri.Host, serverUri.Port);

                await TaskTest.WaitFor(() => server.ConnectionEventcount == 1);

                server.DropConnection();
                await TaskTest.WaitFor(() => server.DisconnectionEventCount == 1);

                var t2 = new TcpClient();
                await t2.ConnectAsync(serverUri.Host, serverUri.Port);

                await TaskTest.WaitFor(() => server.ConnectionEventcount == 2);

                t2.GetStream().Write(99.ToBytes(), 0, 4);
                await TaskTest.WaitFor(() => received != null);

                Assert.That(received.ToInt32(), Is.EqualTo(99));
            }
        }
示例#8
0
        public TestingResultViewModel(TestingResult tr, TaskTest t)
        {
            Score      = tr.Score;
            Commentary = tr.Commentary;
            Result     = tr.ResultCode.ToString();
            TestType   = t.TestType;
            TestName   = TestsNamesConverter.ConvertTypeToName(TestType);

            if (TestsNamesConverter.IsValidType(TestType))
            {
                object obj = null;
                if (tr.TestData != null)
                {
                    switch (TestType)
                    {
                    case "reflectionTest":
                        obj = JsonConvert.DeserializeObject <ReflectionTestResult>(tr.TestData);
                        break;

                    case "functionalTest":
                        obj = JsonConvert.DeserializeObject <FunctionalTestResult>(tr.TestData);
                        break;

                    case "codeStyleTest":
                        obj = JsonConvert.DeserializeObject <CodeStyleTestResult>(tr.TestData);
                        break;
                    }
                }

                Data = obj;
            }
        }
示例#9
0
 public void BenchmarkMethod2(BenchmarkContext context)
 {
     //var b = new byte[1024];
     //_counter.Increment();
     TaskTest u = new TaskTest();
     u.GetTaskByID();
 }
        public async Task ReadShouldIgnoreMessageWithUnknownCorrelationId()
        {
            const int correlationId = 99;

            var mockLog = _kernel.GetMock <IKafkaLog>();

            using (var server = new FakeTcpServer(_log, 8999))
                using (var socket = new KafkaTcpSocket(mockLog.Object, _kafkaEndpoint, _maxRetry))
                    using (var conn = new KafkaConnection(socket, log: mockLog.Object))
                    {
                        var receivedData = false;
                        socket.OnBytesReceived += i => receivedData = true;

                        //send correlation message
                        server.SendDataAsync(CreateCorrelationMessage(correlationId)).Wait(TimeSpan.FromSeconds(5));

                        //wait for connection
                        await TaskTest.WaitFor(() => server.ConnectionEventcount > 0);

                        Assert.That(server.ConnectionEventcount, Is.EqualTo(1));

                        await TaskTest.WaitFor(() => receivedData);

                        //should log a warning and keep going
                        mockLog.Verify(x => x.WarnFormat(It.IsAny <string>(), It.Is <int>(o => o == correlationId)));
                    }
        }
示例#11
0
        public void CancellationShouldInterruptConsumption()
        {
            var routerProxy = new BrokerRouterProxy(_kernel);

            routerProxy.BrokerConn0.FetchResponseFunction = () => { return(new FetchResponse()); };

            var router = routerProxy.Create();

            var options = CreateOptions(router);

            var consumer = new Consumer(options);

            var tokenSrc = new CancellationTokenSource();

            var consumeTask = Task.Run(() => consumer.Consume(tokenSrc.Token).FirstOrDefault());

            //wait until the fake broker is running and requesting fetches
            TaskTest.WaitFor(() => routerProxy.BrokerConn0.FetchRequestCallCount > 10);

            tokenSrc.Cancel();

            Assert.That(
                Assert.Throws <AggregateException>(consumeTask.Wait).InnerException,
                Is.TypeOf <OperationCanceledException>());
        }
示例#12
0
        public void ReadShouldLogDisconnectAndRecover()
        {
            var mockLog = _kernel.GetMock <IKafkaLog>();

            using (var server = new FakeTcpServer(8999))
                using (var socket = new KafkaTcpSocket(mockLog.Object, _kafkaEndpoint))
                    using (var conn = new KafkaConnection(socket, log: mockLog.Object))
                    {
                        TaskTest.WaitFor(() => server.ConnectionEventcount > 0);
                        Assert.That(server.ConnectionEventcount, Is.EqualTo(1));

                        server.DropConnection();
                        TaskTest.WaitFor(() => server.DisconnectionEventCount > 0);
                        Assert.That(server.DisconnectionEventCount, Is.EqualTo(1));

                        //Wait a while for the client to notice the disconnect and log
                        Thread.Sleep(15);

                        //should log an exception and keep going
                        mockLog.Verify(x => x.ErrorFormat(It.IsAny <string>(), It.IsAny <Exception>()));

                        TaskTest.WaitFor(() => server.ConnectionEventcount > 1);
                        Assert.That(server.ConnectionEventcount, Is.EqualTo(2));
                    }
        }
示例#13
0
        public void AsyncLockShouldAllowOnlyOneThread()
        {
            var block = new SemaphoreSlim(0, 2);
            var count = 0;
            var alock = new AsyncLock();

            var firstCall = Task.Factory.StartNew(async() =>
            {
                using (await alock.LockAsync())
                {
                    Interlocked.Increment(ref count);
                    block.Wait();
                }
                block.Wait();//keep this thread busy
            });

            TaskTest.WaitFor(() => count > 0);

            alock.LockAsync().ContinueWith(t => Interlocked.Increment(ref count));

            Assert.That(count, Is.EqualTo(1), "Only one task should have gotten past lock.");
            Assert.That(firstCall.IsCompleted, Is.False, "Task should still be running.");

            block.Release();
            TaskTest.WaitFor(() => count > 1);
            Assert.That(count, Is.EqualTo(2), "Second call should get past lock.");
            Assert.That(firstCall.IsCompleted, Is.False, "First call should still be busy.");
            block.Release();
        }
示例#14
0
        public void ProducerShouldBlockEvenOnMessagesInTransit()
        {
            //with max buffer set below the batch size, this should cause the producer to block until batch delay time.
            var routerProxy = new FakeBrokerRouter();
            var semaphore   = new SemaphoreSlim(0);

            routerProxy.BrokerConn0.ProduceResponseFunction = async() => { semaphore.Wait(); return(new ProduceResponse()); };
            routerProxy.BrokerConn1.ProduceResponseFunction = async() => { semaphore.Wait(); return(new ProduceResponse()); };

            var producer = new Producer(routerProxy.Create(), maximumMessageBuffer: 5, maximumAsyncRequests: 1)
            {
                BatchSize = 1, BatchDelayTime = TimeSpan.FromMilliseconds(500)
            };

            using (producer)
            {
                var sendTasks = Enumerable.Range(0, 5)
                                .Select(x => producer.SendMessageAsync(FakeBrokerRouter.TestTopic, new[] { new Message(x.ToString()) }))
                                .ToList();

                TaskTest.WaitFor(() => producer.AsyncCount > 0);
                Assert.That(sendTasks.Any(x => x.IsCompleted) == false, "All the async tasks should be blocking or in transit.");
                Assert.That(producer.BufferCount, Is.EqualTo(5), "We sent 5 unfinished messages, they should be counted towards the buffer.");

                semaphore.Release(2);
            }
        }
示例#15
0
        public void SendAsyncShouldTimeoutMultipleMessagesAtATime()
        {
            using (var server = new FakeTcpServer(8999))
                using (var socket = new KafkaTcpSocket(_log, _kafkaEndpoint))
                    using (var conn = new KafkaConnection(socket, TimeSpan.FromMilliseconds(100), log: _log))
                    {
                        TaskTest.WaitFor(() => server.ConnectionEventcount > 0);
                        Assert.That(server.ConnectionEventcount, Is.EqualTo(1));

                        var tasks = new[]
                        {
                            conn.SendAsync(new MetadataRequest()),
                            conn.SendAsync(new MetadataRequest()),
                            conn.SendAsync(new MetadataRequest())
                        };

                        Task.WhenAll(tasks);

                        TaskTest.WaitFor(() => tasks.Any(t => t.IsFaulted));
                        foreach (var task in tasks)
                        {
                            Assert.That(task.IsFaulted, Is.True);
                            Assert.That(task.Exception.InnerException, Is.TypeOf <ResponseTimeoutException>());
                        }
                    }
        }
        public void WriteShouldCancelWhileSendingData()
        {
            var writeAttempts = 0;

            using (var server = new FakeTcpServer(FakeServerPort))
                using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl))
                    using (var token = new CancellationTokenSource())
                    {
                        test.OnWriteToSocketAttempt += payload => Interlocked.Increment(ref writeAttempts);

                        test.WriteAsync(new KafkaDataPayload {
                            Buffer = 1.ToBytes()
                        }, token.Token);

                        TaskTest.WaitFor(() => server.ConnectionEventcount > 0);
                        TaskTest.WaitFor(() => writeAttempts > 0);

                        Assert.That(writeAttempts, Is.EqualTo(1), "Socket should have attempted to write.");

                        //create a buffer write that will take a long time
                        var taskResult = test.WriteAsync(
                            new KafkaDataPayload {
                            Buffer = Enumerable.Range(0, 1000000).Select(b => (byte)b).ToArray()
                        },
                            token.Token);

                        token.Cancel();

                        taskResult.SafeWait(TimeSpan.FromMilliseconds(5000));

                        Assert.That(taskResult.IsCanceled, Is.True, "Task should have cancelled.");
                    }
        }
        public async Task WriteShouldCancelWhileSendingData()
        {
            var writeAttempts = 0;

            using (var server = new FakeTcpServer(_log, FakeServerPort))
                using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry))
                    using (var token = new CancellationTokenSource())
                    {
                        test.OnWriteToSocketAttempt += payload => Interlocked.Increment(ref writeAttempts);

                        test.WriteAsync(new KafkaDataPayload {
                            Buffer = 1.ToBytes()
                        }, token.Token);

                        await TaskTest.WaitFor(() => server.ConnectionEventcount > 0);

                        await TaskTest.WaitFor(() => writeAttempts > 0);

                        Assert.That(writeAttempts, Is.EqualTo(1), "Socket should have attempted to write.");

                        //create a buffer write that will take a long time
                        var data = Enumerable.Range(0, 100000000).Select(b => (byte)b).ToArray();
                        token.Cancel();
                        var taskResult = test.WriteAsync(
                            new KafkaDataPayload {
                            Buffer = data
                        },
                            token.Token);
                        await Task.WhenAny(taskResult, Task.Delay(TimeSpan.FromSeconds(5))).ConfigureAwait(false);

                        Assert.That(taskResult.IsCanceled, Is.True, "Task should have cancelled.");
                    }
        }
示例#18
0
        public void ReadShouldReconnectAfterLosingConnection()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            {
                var disconnects = 0;
                var connects    = 0;
                server.OnClientConnected    += () => Interlocked.Increment(ref connects);
                server.OnClientDisconnected += () => Interlocked.Increment(ref disconnects);
                var socket = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl);

                var resultTask = ReadFromSocketWithRetry(socket, 4);

                //wait till connected
                TaskTest.WaitFor(() => connects > 0);

                //drop connection
                server.DropConnection();
                TaskTest.WaitFor(() => disconnects > 0);
                Assert.That(disconnects, Is.EqualTo(1), "Server should have disconnected the client.");

                //wait for reconnection
                TaskTest.WaitFor(() => connects > 1);
                Assert.That(connects, Is.EqualTo(2), "Socket should have reconnected.");

                //send data and get result
                server.SendDataAsync(99.ToBytes());
                Assert.That(resultTask.Result.ToInt32(), Is.EqualTo(99), "Socket should have received the 4 bytes.");
            }
        }
        public async Task SendAsyncShouldTimeoutMultipleMessagesAtATime()
        {
            using (var server = new FakeTcpServer(_log, 8999))
                using (var socket = new KafkaTcpSocket(_log, _kafkaEndpoint, _maxRetry))
                    using (var conn = new KafkaConnection(socket, TimeSpan.FromMilliseconds(100), log: _log))
                    {
                        server.HasClientConnected.Wait(TimeSpan.FromSeconds(3));
                        Assert.That(server.ConnectionEventcount, Is.EqualTo(1));

                        var tasks = new[]
                        {
                            conn.SendAsync(new MetadataRequest()),
                            conn.SendAsync(new MetadataRequest()),
                            conn.SendAsync(new MetadataRequest())
                        };

                        Task.WhenAll(tasks);

                        await TaskTest.WaitFor(() => tasks.All(t => t.IsFaulted));

                        foreach (var task in tasks)
                        {
                            Assert.That(task.IsFaulted, Is.True, "Task should have faulted.");
                            Assert.That(task.Exception.InnerException, Is.TypeOf <ResponseTimeoutException>(),
                                        "Task fault should be of type ResponseTimeoutException.");
                        }
                    }
        }
        public async Task KafkaConnectionShouldLogDisconnectAndRecover()
        {
            var mockLog = new Mock <IKafkaLog>();
            var log     = new DefaultTraceLog(LogLevel.Error);

            using (var server = new FakeTcpServer(log, 8999))
                using (var socket = new KafkaTcpSocket(log, _kafkaEndpoint, _maxRetry))
                    using (var conn = new KafkaConnection(socket, log: mockLog.Object))
                    {
                        var disconnected = 0;
                        socket.OnServerDisconnected += () => Interlocked.Increment(ref disconnected);

                        for (int connectionAttempt = 1; connectionAttempt < 4; connectionAttempt++)
                        {
                            await TaskTest.WaitFor(() => server.ConnectionEventcount == connectionAttempt);

                            Assert.That(server.ConnectionEventcount, Is.EqualTo(connectionAttempt));
                            server.SendDataAsync(CreateCorrelationMessage(1)).Wait(TimeSpan.FromSeconds(5));
                            await TaskTest.WaitFor(() => !conn.IsOnErrorState());

                            Assert.IsFalse(conn.IsOnErrorState());
                            mockLog.Verify(x => x.InfoFormat("Polling read thread has recovered: {0}", It.IsAny <object[]>()), Times.Exactly(connectionAttempt - 1));

                            server.DropConnection();
                            await TaskTest.WaitFor(() => conn.IsOnErrorState());

                            Assert.AreEqual(disconnected, connectionAttempt);
                            Assert.IsTrue(conn.IsOnErrorState());

                            mockLog.Verify(x => x.ErrorFormat("Exception occured in polling read thread {0}: {1}", It.IsAny <object[]>()), Times.Exactly(connectionAttempt));
                        }
                    }
        }
示例#21
0
        [Test, Repeat(1)]///  [Test, Repeat(100)]
        public async Task ReadShouldLogDisconnectAndRecover()
        {
            var mockLog = _kernel.GetMock <IKafkaLog>();

            using (var server = new FakeTcpServer(_log, 8999))
                using (var socket = new KafkaTcpSocket(mockLog.Object, _kafkaEndpoint, _maxRetry))
                    using (var conn = new KafkaConnection(socket, log: mockLog.Object))
                    {
                        var disconnected = false;
                        socket.OnServerDisconnected += () => disconnected = true;

                        await TaskTest.WaitFor(() => server.ConnectionEventcount > 0);

                        Assert.That(server.ConnectionEventcount, Is.EqualTo(1));

                        server.DropConnection();
                        await TaskTest.WaitFor(() => server.DisconnectionEventCount > 0);

                        Assert.That(server.DisconnectionEventCount, Is.EqualTo(1));

                        //Wait a while for the client to notice the disconnect and log
                        await TaskTest.WaitFor(() => disconnected);

                        //should log an exception and keep going
                        mockLog.Verify(x => x.ErrorFormat(It.IsAny <string>(), It.IsAny <Exception>()));

                        await TaskTest.WaitFor(() => server.ConnectionEventcount > 1);

                        Assert.That(server.ConnectionEventcount, Is.EqualTo(2));
                    }
        }
示例#22
0
        public void ProducerShouldReportCorrectAmountOfAsyncRequests()
        {
            var semaphore   = new SemaphoreSlim(0);
            var routerProxy = new FakeBrokerRouter();

            //block the second call returning from send message async
            routerProxy.BrokerConn0.ProduceResponseFunction = () => { semaphore.Wait(); return(new ProduceResponse()); };

            var router = routerProxy.Create();

            using (var producer = new Producer(router, maximumAsyncRequests: 1)
            {
                BatchSize = 1
            })
            {
                var messages = new[] { new Message("1") };

                Assert.That(producer.AsyncCount, Is.EqualTo(0));

                var sendTask = producer.SendMessageAsync(BrokerRouterProxy.TestTopic, messages);

                TaskTest.WaitFor(() => producer.AsyncCount > 0);
                Assert.That(producer.AsyncCount, Is.EqualTo(1), "One async operation should be sending.");

                semaphore.Release();
                sendTask.Wait(TimeSpan.FromMilliseconds(500));

                Assert.That(sendTask.IsCompleted, Is.True, "Send task should be marked as completed.");
                Assert.That(producer.AsyncCount, Is.EqualTo(0), "Async should now show zero count.");
            }
        }
示例#23
0
        public void ProducerShouldBlockWhenFullBufferReached()
        {
            int count = 0;
            //with max buffer set below the batch size, this should cause the producer to block until batch delay time.
            var routerProxy = new FakeBrokerRouter();
            var producer    = new Producer(routerProxy.Create(), maximumMessageBuffer: 1)
            {
                BatchSize = 10, BatchDelayTime = TimeSpan.FromMilliseconds(500)
            };

            using (producer)
            {
                var senderTask = Task.Factory.StartNew(async() =>
                {
                    for (int i = 0; i < 3; i++)
                    {
                        await producer.SendMessageAsync(FakeBrokerRouter.TestTopic, new[] { new Message(i.ToString()) });
                        Console.WriteLine("Await: {0}", producer.BufferCount);
                        Interlocked.Increment(ref count);
                    }
                });

                TaskTest.WaitFor(() => count > 0);
                Assert.That(producer.BufferCount, Is.EqualTo(1));

                Console.WriteLine("Waiting for the rest...");
                senderTask.Wait(TimeSpan.FromSeconds(5));

                Assert.That(senderTask.IsCompleted);
                Assert.That(producer.BufferCount, Is.EqualTo(1), "One message should be left in the buffer.");

                Console.WriteLine("Unwinding...");
            }
        }
示例#24
0
        public void SendAsyncShouldBlockWhenMaximumAsyncQueueReached()
        {
            int count       = 0;
            var semaphore   = new SemaphoreSlim(0);
            var routerProxy = new BrokerRouterProxy(_kernel);

            //block the second call returning from send message async
            routerProxy.BrokerConn0.ProduceResponseFunction = () => { semaphore.Wait(); return(new ProduceResponse()); };

            var router = routerProxy.Create();

            using (var producer = new Producer(router, 1))
            {
                var messages = new List <Message>
                {
                    new Message("1"), new Message("2")
                };

                Task.Factory.StartNew(() =>
                {
                    producer.SendMessageAsync(BrokerRouterProxy.TestTopic, messages);
                    count++;
                    producer.SendMessageAsync(BrokerRouterProxy.TestTopic, messages);
                    count++;
                });

                TaskTest.WaitFor(() => count > 0);
                Assert.That(count, Is.EqualTo(1), "Only one SendMessageAsync should continue.");
                Assert.That(producer.ActiveCount, Is.EqualTo(1), "One async call shoud be active.");
                semaphore.Release();
                TaskTest.WaitFor(() => count > 1);
                Assert.That(count, Is.EqualTo(2), "The second SendMessageAsync should continue after semaphore is released.");
            }
        }
示例#25
0
    public void BenchmarkMethod(BenchmarkContext context)
    {
        //var b = new byte[1024];
        //_counter.Increment();
        TaskTest u = new TaskTest();

        u.AddTaskwithParent();
    }
示例#26
0
        public ActionResult DeleteConfirmed(int id)
        {
            TaskTest taskTest = db.TaskTests.Find(id);

            db.TaskTests.Remove(taskTest);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
示例#27
0
 public void ShouldStartReadPollingOnConstruction()
 {
     using (var socket = new KafkaTcpSocket(_log, _kafkaEndpoint))
         using (var conn = new KafkaConnection(socket, log: _log))
         {
             TaskTest.WaitFor(() => conn.ReadPolling);
             Assert.That(conn.ReadPolling, Is.True);
         }
 }
 public void ShouldStartReadPollingOnConstruction()
 {
     using (var socket = new KafkaTcpSocket(_log, new Uri("http://localhost:8999")))
         using (var conn = new KafkaConnection(socket, log: _log))
         {
             TaskTest.WaitFor(() => conn.ReadPolling);
             Assert.That(conn.ReadPolling, Is.True);
         }
 }
示例#29
0
 public void ShouldDisposeWithoutExceptionThrown()
 {
     using (var server = new FakeTcpServer(8999))
         using (var socket = new KafkaTcpSocket(_log, _kafkaEndpoint))
         {
             var conn = new KafkaConnection(socket, log: _log);
             TaskTest.WaitFor(() => server.ConnectionEventcount > 0);
             using (conn) { }
         }
 }
示例#30
0
        public void ConnectionNotShouldAttemptOnConstruction()
        {
            var count = 0;

            using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl))
            {
                test.OnReconnectionAttempt += x => Interlocked.Increment(ref count);
                TaskTest.WaitFor(() => count > 0);
                Assert.That(count, Is.EqualTo(0));
            }
        }