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.");
                    }
        }
예제 #2
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));
                    }
        }
        public void WriteShouldHandleLargeVolumeSendAsynchronously()
        {
            AutoResetEvent allEventAreArrived = new AutoResetEvent(false);
            var            write   = new ConcurrentBag <int>();
            int            percent = 0;

            using (var server = new FakeTcpServer(_log, FakeServerPort))
                using (var test = new KafkaTcpSocket(new DefaultTraceLog(LogLevel.Warn), _fakeServerUrl, _maxRetry))
                {
                    int numberOfWrite = 10000;
                    server.OnBytesReceived += data =>
                    {
                        var d = data.Batch(4).Select(x => x.ToArray().ToInt32());
                        foreach (var item in d)
                        {
                            write.Add(item);
                        }

                        if (write.Count % (numberOfWrite / 100) == 0)
                        {
                            Console.WriteLine("*************** done  percent:" + percent);
                            percent++;
                        }
                        if (write.Count == numberOfWrite)
                        {
                            allEventAreArrived.Set();
                        }
                    };
                    var tasks = Enumerable.Range(1, 10000).SelectMany(i => new[] { test.WriteAsync(i.ToBytes().ToPayload()), }).ToArray();

                    Task.WaitAll(tasks);
                    Assert.IsTrue(allEventAreArrived.WaitOne(2000), "not get all event on time");
                    Assert.That(write.OrderBy(x => x), Is.EqualTo(Enumerable.Range(1, numberOfWrite)));
                }
        }
 public void ShouldDisposeWithoutExecptionThrown()
 {
     using (var server = new FakeTcpServer(8999))
     {
         Thread.Sleep(500);
     }
 }
예제 #5
0
 public void ShouldDisposeWithoutExecptionThrown()
 {
     using (var server = new FakeTcpServer(TestConfig.WarnLog, TestConfig.ServerPort()))
     {
         Thread.Sleep(500);
     }
 }
예제 #6
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.");
            }
        }
예제 #7
0
 public void ShouldDisposeWithoutExecptionThrown()
 {
     using (var server = new FakeTcpServer(Ilog, 8999))
     {
         Thread.Sleep(500);
     }
 }
        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 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.");
                    }
        }
예제 #10
0
        public void ReadShouldCancelWhileAwaitingResponse()
        {
            var endpoint = Endpoint.Resolve(TestConfig.ServerUri(), TestConfig.InfoLog);

            using (var server = new FakeTcpServer(TestConfig.InfoLog, endpoint.IP.Port))
                using (var test = new TcpSocket(endpoint, log: TestConfig.InfoLog))
                {
                    var count     = 0;
                    var semaphore = new SemaphoreSlim(0);
                    var token     = new CancellationTokenSource();

                    test.ReadAsync(4, token.Token).ContinueWith(t =>
                    {
                        Interlocked.Increment(ref count);
                        Assert.That(t.IsCanceled, Is.True, "Task should be set to cancelled when disposed.");
                        semaphore.Release();
                    });

                    Thread.Sleep(100);
                    token.Cancel();

                    semaphore.Wait(TimeSpan.FromSeconds(1));
                    Assert.That(count, Is.EqualTo(1), "Read should have cancelled and incremented count.");
                }
        }
예제 #11
0
        public void ReadShouldNotLoseDataFromStreamOverMultipleReads()
        {
            var endpoint = Endpoint.Resolve(TestConfig.ServerUri(), TestConfig.InfoLog);

            using (var server = new FakeTcpServer(TestConfig.InfoLog, endpoint.IP.Port))
                using (var test = new TcpSocket(endpoint, log: TestConfig.InfoLog))
                {
                    const int    firstMessage  = 99;
                    const string secondMessage = "testmessage";
                    var          bytes         = Encoding.UTF8.GetBytes(secondMessage);

                    var payload = new KafkaWriter()
                                  .Write(firstMessage)
                                  .Write(bytes, false);

                    //send the combined payload
                    var send = server.SendDataAsync(payload.ToBytesNoLength());

                    var firstResponse = test.ReadAsync(4, CancellationToken.None).Result.ToInt32();
                    Assert.That(firstResponse, Is.EqualTo(firstMessage));

                    var secondResponse = Encoding.ASCII.GetString(test.ReadAsync(secondMessage.Length, CancellationToken.None).Result);
                    Assert.That(secondResponse, Is.EqualTo(secondMessage));
                }
        }
예제 #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 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>());
                        }
                    }
        }
예제 #14
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.");
                        }
                    }
        }
예제 #15
0
        public async Task SendAsyncShouldNotAllowResponseToTimeoutWhileAwaitingKafkaToEstableConnection()
        {
            var endpoint = Endpoint.Resolve(TestConfig.ServerUri(), TestConfig.InfoLog);

            using (var socket = new TcpSocket(endpoint, log: TestConfig.InfoLog))
                using (var conn = new Connection(socket, new ConnectionConfiguration(requestTimeout: TimeSpan.FromSeconds(1000)), log: TestConfig.InfoLog))
                {
                    Console.WriteLine("SendAsync blocked by reconnection attempts...");
                    var taskResult = conn.SendAsync(new MetadataRequest(), CancellationToken.None);

                    Console.WriteLine("Task result should be WaitingForActivation...");
                    Assert.That(taskResult.IsFaulted, Is.False);
                    Assert.That(taskResult.Status, Is.EqualTo(TaskStatus.WaitingForActivation));

                    Console.WriteLine("Starting server to establish connection...");
                    using (var server = new FakeTcpServer(TestConfig.InfoLog, endpoint.IP.Port))
                    {
                        server.OnClientConnected += () => Console.WriteLine("Client connected...");
                        server.OnBytesReceived   += b =>
                        {
                            var request = KafkaDecoder.DecodeHeader(b);
                            AsyncContext.Run(async() => await server.SendDataAsync(MessageHelper.CreateMetadataResponse(request.CorrelationId, "Test")));
                        };

                        await Task.WhenAny(taskResult, Task.Delay(TimeSpan.FromSeconds(10)));

                        Assert.That(taskResult.IsFaulted, Is.False);
                        Assert.That(taskResult.IsCanceled, Is.False);
                        await taskResult;
                        Assert.That(taskResult.Status, Is.EqualTo(TaskStatus.RanToCompletion));
                    }
                }
        }
예제 #16
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)."));
                    }
        }
예제 #17
0
        public void WriteAndReadShouldBeAsyncronous()
        {
            var write    = new List <int>();
            var read     = new List <int>();
            var expected = new List <int> {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            };

            using (var server = new FakeTcpServer(FakeServerPort))
            {
                server.OnBytesReceived += data => write.AddRange(data.Batch(4).Select(x => x.ToArray().ToInt32()));

                var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl);

                var tasks = Enumerable.Range(1, 10)
                            .SelectMany(i => new[]
                {
                    test.WriteAsync(i.ToBytes()),
                    test.ReadAsync(4).ContinueWith(t => read.Add(t.Result.ToInt32())),
                    server.SendDataAsync(i.ToBytes())
                }).ToArray();

                Task.WaitAll(tasks);
                Assert.That(write.OrderBy(x => x), Is.EqualTo(expected));
                Assert.That(read.OrderBy(x => x), Is.EqualTo(expected));
            }
        }
예제 #18
0
        public async void SendAsyncShouldNotAllowResponseToTimeoutWhileAwaitingKafkaToEstableConnection()
        {
            using (var socket = new KafkaTcpSocket(_log, _kafkaEndpoint))
                using (var conn = new KafkaConnection(socket, TimeSpan.FromMilliseconds(1000000), log: _log))
                {
                    Console.WriteLine("SendAsync blocked by reconnection attempts...");
                    var taskResult = conn.SendAsync(new MetadataRequest());

                    taskResult.ContinueWith(t => taskResult = t).Wait(TimeSpan.FromSeconds(1));

                    Console.WriteLine("Task result should be WaitingForActivation...");
                    Assert.That(taskResult.IsFaulted, Is.False);
                    Assert.That(taskResult.Status, Is.EqualTo(TaskStatus.WaitingForActivation));

                    Console.WriteLine("Starting server to establish connection...");
                    using (var server = new FakeTcpServer(8999))
                    {
                        server.OnClientConnected += () => Console.WriteLine("Client connected...");
                        server.OnBytesReceived   += (b) =>
                        {
                            server.SendDataAsync(MessageHelper.CreateMetadataResponse(1, "Test"));
                        };

                        await taskResult;

                        Assert.That(taskResult.IsFaulted, Is.False);
                        Assert.That(taskResult.IsCanceled, Is.False);
                        Assert.That(taskResult.Status, Is.EqualTo(TaskStatus.RanToCompletion));
                    }
                }
        }
        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)));
                    }
        }
예제 #20
0
        public void ReadShouldStackReadRequestsAndReturnOneAtATime()
        {
            var endpoint = Endpoint.Resolve(TestConfig.ServerUri(), TestConfig.InfoLog);

            using (var server = new FakeTcpServer(TestConfig.InfoLog, endpoint.IP.Port))
            {
                var messages       = new[] { "test1", "test2", "test3", "test4" };
                var expectedLength = "test1".Length;

                var payload = new KafkaWriter().Write(messages);

                using (var socket = new TcpSocket(endpoint, log: TestConfig.WarnLog)) {
                    var tasks = messages.Select(x => socket.ReadAsync(x.Length, CancellationToken.None)).ToArray();

                    var send = server.SendDataAsync(payload.ToBytes());

                    Task.WaitAll(tasks);

                    foreach (var task in tasks)
                    {
                        Assert.That(task.Result.Length, Is.EqualTo(expectedLength));
                    }
                }
            }
        }
        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));
                        }
                    }
        }
예제 #22
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.");
                    }
        }
예제 #23
0
        public void ReadShouldBlockUntilAllBytesRequestedAreReceived()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            {
                var count = 0;

                var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl);

                var resultTask = test.ReadAsync(4).ContinueWith(t =>
                {
                    Interlocked.Increment(ref count);
                    return(t.Result);
                });

                Console.WriteLine("Sending first 3 bytes...");
                var sendInitialBytes = server.SendDataAsync(new byte[] { 0, 0, 0 }).Wait(TimeSpan.FromSeconds(10));
                Assert.That(sendInitialBytes, Is.True, "First 3 bytes should have been sent.");

                Console.WriteLine("Ensuring task blocks...");
                var unblocked = resultTask.Wait(TimeSpan.FromMilliseconds(500));
                Assert.That(unblocked, Is.False, "Wait should return false.");
                Assert.That(resultTask.IsCompleted, Is.False, "Task should still be running, blocking.");
                Assert.That(count, Is.EqualTo(0), "Should still block even though bytes have been received.");

                Console.WriteLine("Sending last byte...");
                var sendLastByte = server.SendDataAsync(new byte[] { 0 }).Wait(TimeSpan.FromSeconds(10));
                Assert.That(sendLastByte, Is.True, "Last byte should have sent.");

                Console.WriteLine("Ensuring task unblocks...");
                resultTask.Wait(TimeSpan.FromMilliseconds(500));
                Assert.That(resultTask.IsCompleted, Is.True, "Task should have completed.");
                Assert.That(count, Is.EqualTo(1), "Task ContinueWith should have executed.");
                Assert.That(resultTask.Result.Length, Is.EqualTo(4), "Result of task should be 4 bytes.");
            }
        }
예제 #24
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));
            }
        }
예제 #25
0
 public void ShouldDisposeEvenWhenTryingToSendWithoutExceptionThrown()
 {
     using (var server = new FakeTcpServer(TestConfig.WarnLog, TestConfig.ServerPort()))
     {
         server.SendDataAsync("test");
         Thread.Sleep(500);
     }
 }
예제 #26
0
 public void ShouldDisposeEvenWhenTryingToSendWithoutExceptionThrown()
 {
     using (var server = new FakeTcpServer(Ilog, 8999))
     {
         server.SendDataAsync("test");
         Thread.Sleep(500);
     }
 }
 public void ShouldDisposeEvenWhenTryingToSendWithoutExceptionThrown()
 {
     using (var server = new FakeTcpServer(8999))
     {
         server.SendDataAsync("test");
         Thread.Sleep(500);
     }
 }
예제 #28
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) { }
     }
 }
예제 #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 async Task ShouldDisposeWithoutExceptionThrown()
        {
            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))
                {
                    var conn = new Connection(socket, log: TestConfig.InfoLog);
                    await TaskTest.WaitFor(() => server.ConnectionEventcount > 0);

                    using (conn) { }
                }
        }
예제 #31
0
        public void WriteAsyncShouldSendData()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            {
                const int testData = 99;
                int       result   = 0;

                var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl);
                server.OnBytesReceived += data => result = data.ToInt32();

                test.WriteAsync(testData.ToBytes()).Wait(TimeSpan.FromSeconds(1));
                TaskTest.WaitFor(() => result > 0);
                Assert.That(result, Is.EqualTo(testData));
            }
        }
예제 #32
0
        public void WriteAsyncShouldAllowMoreThanOneWrite()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            {
                const int testData = 99;
                var       results  = new List <byte>();

                var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl);
                server.OnBytesReceived += results.AddRange;

                Task.WaitAll(test.WriteAsync(testData.ToBytes()), test.WriteAsync(testData.ToBytes()));
                TaskTest.WaitFor(() => results.Count >= 8);
                Assert.That(results.Count, Is.EqualTo(8));
            }
        }
예제 #33
0
        public async Task ShouldReconnectAfterLosingConnectionAndBeAbleToStartNewRead()
        {
            var log      = TestConfig.InfoLog;
            var endpoint = Endpoint.Resolve(TestConfig.ServerUri(), TestConfig.InfoLog);

            using (var server = new FakeTcpServer(TestConfig.InfoLog, endpoint.IP.Port))
            {
                var disconnects = 0;
                var connects    = 0;
                TaskCompletionSource <int> disconnectEvent = new TaskCompletionSource <int>();

                server.OnClientConnected    += () => Interlocked.Increment(ref connects);
                server.OnClientDisconnected += () => Interlocked.Increment(ref disconnects);
                var config = new ConnectionConfiguration(onDisconnected: (e, exception) => {
                    log.Info(() => LogEvent.Create("inside onDisconnected"));
                    disconnectEvent.TrySetResult(1);
                });
                using (var socket = new TcpSocket(endpoint, config, log)) {
                    //wait till connected
                    await TaskTest.WaitFor(() => connects > 0);

                    log.Info(() => LogEvent.Create("connects > 0"));
                    var readTask = socket.ReadAsync(4, CancellationToken.None);

                    log.Info(() => LogEvent.Create("Connected: Dropping connection..."));
                    server.DropConnection();

                    //wait till Disconnected
                    log.Info(() => LogEvent.Create("Waiting for onDisconnected"));
                    await Task.WhenAny(disconnectEvent.Task, Task.Delay(100000));

                    Assert.IsTrue(disconnectEvent.Task.IsCompleted, "Server should have disconnected the client.");
                    Assert.ThrowsAsync <ConnectionException>(async() => await readTask);
                    await TaskTest.WaitFor(() => connects == 2, 6000);

                    log.Info(() => LogEvent.Create("connects == 2"));
                    Assert.That(connects, Is.EqualTo(2), "Socket should have reconnected.");

                    var readTask2 = socket.ReadAsync(4, CancellationToken.None);

                    log.Info(() => LogEvent.Create("sending data (99)"));
                    await server.SendDataAsync(99.ToBytes());

                    var result = await readTask2;
                    Assert.That(result.ToInt32(), Is.EqualTo(99), "Socket should have received the 4 bytes.");
                }
            }
        }
예제 #34
0
        public void SendAsyncShouldWaitUntilClientIsConnected()
        {
            const int testData = 99;
            using (var server = new FakeTcpServer(Ilog, 8999))
            using (var client = new TcpClient())
            {
                server.SendDataAsync(testData.ToBytes());
                Thread.Sleep(1000);
                client.Connect(_fakeServerUrl.Host, _fakeServerUrl.Port);

                var buffer = new byte[4];
                client.GetStream().ReadAsync(buffer, 0, 4).Wait(TimeSpan.FromSeconds(5));

                Assert.That(buffer.ToInt32(), Is.EqualTo(testData));
            }
        }
예제 #35
0
        public async Task FakeShouldBeAbleToReconnect()
        {
            using (var server = new FakeTcpServer(Ilog, 8999))
            {
                byte[] received = null;
                server.OnBytesReceived += data => received = data;

                var t1 = new TcpClient();
                t1.Connect(_fakeServerUrl.Host, _fakeServerUrl.Port);
                await TaskTest.WaitFor(() => server.ConnectionEventcount == 1);

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

                var t2 = new TcpClient();
                t2.Connect(_fakeServerUrl.Host, _fakeServerUrl.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));
            }
        }
예제 #36
0
        public async void SendAsyncShouldNotAllowResponseToTimeoutWhileAwaitingKafkaToEstableConnection()
        {
            using (var socket = new KafkaTcpSocket(_log, _kafkaEndpoint))
            using (var conn = new KafkaConnection(socket, TimeSpan.FromMilliseconds(1000000), log: _log))
            {
                Console.WriteLine("SendAsync blocked by reconnection attempts...");
                var taskResult = conn.SendAsync(new MetadataRequest());

                taskResult.ContinueWith(t => taskResult = t).Wait(TimeSpan.FromSeconds(1));

                Console.WriteLine("Task result should be WaitingForActivation...");
                Assert.That(taskResult.IsFaulted, Is.False);
                Assert.That(taskResult.Status, Is.EqualTo(TaskStatus.WaitingForActivation));

                Console.WriteLine("Starting server to establish connection...");
                using (var server = new FakeTcpServer(8999))
                {
                    server.OnClientConnected += () => Console.WriteLine("Client connected...");
                    server.OnBytesReceived += (b) =>
                    {
                        server.SendDataAsync(MessageHelper.CreateMetadataResponse(1, "Test"));
                    };

                    await taskResult;

                    Assert.That(taskResult.IsFaulted, Is.False);
                    Assert.That(taskResult.IsCanceled, Is.False);
                    Assert.That(taskResult.Status, Is.EqualTo(TaskStatus.RanToCompletion));
                }
            }
        }
예제 #37
0
        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 ));
                }

            }
        }
        public void WriteShouldHandleLargeVolumeSendAsynchronously()
        {
            AutoResetEvent allEventAreArrived = new AutoResetEvent(false);
            var write = new ConcurrentBag<int>();
            int percent = 0;
            using (var server = new FakeTcpServer(_log, FakeServerPort))
            using (var test = new KafkaTcpSocket(new DefaultTraceLog(LogLevel.Warn), _fakeServerUrl, _maxRetry))
            {
                int numberOfWrite = 10000;
                server.OnBytesReceived += data =>
                {
                    var d = data.Batch(4).Select(x => x.ToArray().ToInt32());
                    foreach (var item in d)
                    {
                        write.Add(item);
                    }

                    if (write.Count % (numberOfWrite / 100) == 0)
                    {
                        Console.WriteLine("*************** done  percent:" + percent);
                        percent++;
                    }
                    if (write.Count == numberOfWrite)
                    {
                        allEventAreArrived.Set();
                    }
                };
                var tasks = Enumerable.Range(1, 10000).SelectMany(i => new[] { test.WriteAsync(i.ToBytes().ToPayload()), }).ToArray();

                Task.WaitAll(tasks);
                Assert.IsTrue(allEventAreArrived.WaitOne(2000), "not get all event on time");
                Assert.That(write.OrderBy(x => x), Is.EqualTo(Enumerable.Range(1, numberOfWrite)));
            }
        }
        public async Task ShouldReconnectAfterLosingConnectionAndBeAbleToStartNewRead()
        {
            using (var server = new FakeTcpServer(_log, FakeServerPort))
            {
                var disconnects = 0;
                var connects = 0;
                TaskCompletionSource<int> disconnectEvent = new TaskCompletionSource<int>();

                server.OnClientConnected += () => Interlocked.Increment(ref connects);
                server.OnClientDisconnected += () => Interlocked.Increment(ref disconnects);
                var socket = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry);
                socket.OnServerDisconnected += () => disconnectEvent.TrySetResult(1);

                //wait till connected
                await TaskTest.WaitFor(() => connects > 0);
                var readTask = socket.ReadAsync(4);

                server.DropConnection();

                //wait till Disconnected
                await Task.WhenAny(disconnectEvent.Task, Task.Delay(100000));
                Assert.IsTrue(disconnectEvent.Task.IsCompleted, "Server should have disconnected the client.");
                Assert.Throws<ServerDisconnectedException>(async () => await readTask);

                await TaskTest.WaitFor(() => connects == 2, 6000);
                Assert.That(connects, Is.EqualTo(2), "Socket should have reconnected.");

                var readTask2 = socket.ReadAsync(4);

                await server.SendDataAsync(99.ToBytes());
                var result = await readTask2;
                Assert.That(result.ToInt32(), Is.EqualTo(99), "Socket should have received the 4 bytes.");
            }
        }
예제 #40
0
        public void KafkaTcpSocketShouldDisposeEvenWhileAwaitingReadAndThrowException()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl))
            {
                int readSize = 0;

                test.OnReadFromSocketAttempt += i => readSize = i;

                var taskResult = test.ReadAsync(4);

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

                using (test) { }

                taskResult.ContinueWith(t => taskResult = t).Wait(TimeSpan.FromSeconds(1));

                Assert.That(taskResult.IsFaulted, Is.True);
                Assert.That(taskResult.Exception.InnerException, Is.TypeOf<ObjectDisposedException>());
            }
        }
        public void WriteAndReadShouldBeAsynchronous()
        {
            //     for (int j = 0; j < 1000; j++)
            //     {
            var expected = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var write = new ConcurrentBag<int>();
            var read = new ConcurrentBag<int>();

            AutoResetEvent allEventAreArrived = new AutoResetEvent(false);
            AutoResetEvent allrReadEventAreArrived = new AutoResetEvent(false);
            using (var server = new FakeTcpServer(_log, FakeServerPort))
            using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry))
            {
                server.OnBytesReceived += data =>
                {
                    var d = data.Batch(4).Select(x => x.ToArray().ToInt32());
                    foreach (var item in d)
                    {
                        write.Add(item);
                    }

                    if (expected.Count == write.Count)
                    {
                        allEventAreArrived.Set();
                    }
                };
                var tasks = Enumerable.Range(1, 10)
                    .SelectMany(i => new[]
                        {
                            test.WriteAsync(i.ToBytes().ToPayload()),
                            test.ReadAsync(4).ContinueWith(t =>
                            {
                                read.Add(t.Result.ToInt32());
                                if (read.Count == expected.Count)
                                {
                                    allrReadEventAreArrived.Set();
                                }
                            }),
                            server.SendDataAsync(i.ToBytes())
                        }).ToArray();

                Task.WaitAll(tasks);
                Assert.IsTrue(allEventAreArrived.WaitOne(3000), "not Get all write event in time");
                Assert.IsTrue(allrReadEventAreArrived.WaitOne(3000), "not Get all  read event in time");
                var w = write.OrderBy(x => x);
                var r = read.OrderBy(x => x);

                for (int i = 0; i < expected.Count; i++)
                {
                    //  _log.InfoFormat("write was {0}  expected {1}", w.ElementAt(i), expected[i]);
                    Assert.That(w.ElementAt(i), Is.EqualTo(expected[i]));
                }
                for (int i = 0; i < expected.Count; i++)
                {
                    //     _log.InfoFormat("read was {0}  expected {1}", r.ElementAt(i), expected[i]);
                    Assert.That(r.ElementAt(i), Is.EqualTo(expected[i]));
                }
            }
            //   }
        }
        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.");
            }
        }
예제 #43
0
        public void ReadShouldBeAbleToReceiveMoreThanOnce()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl))
            {
                const int firstMessage = 99;
                const string secondMessage = "testmessage";

                Console.WriteLine("Sending first message to receive...");
                server.SendDataAsync(firstMessage.ToBytes());

                var firstResponse = test.ReadAsync(4).Result.ToInt32();
                Assert.That(firstResponse, Is.EqualTo(firstMessage));

                Console.WriteLine("Sending second message to receive...");
                server.SendDataAsync(secondMessage);

                var secondResponse = Encoding.ASCII.GetString(test.ReadAsync(secondMessage.Length).Result);
                Assert.That(secondResponse, Is.EqualTo(secondMessage));
            }
        }
예제 #44
0
        public void ReadShouldIgnoreMessageWithUnknownCorrelationId()
        {
            const int correlationId = 99;

            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))
            {
                var receivedData = false;
                socket.OnBytesReceived += i => receivedData = true;

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

                //wait for connection
                TaskTest.WaitFor(() => server.ConnectionEventcount > 0);
                Assert.That(server.ConnectionEventcount, Is.EqualTo(1));

                TaskTest.WaitFor(() => receivedData);

                //should log a warning and keep going
                mockLog.Verify(x => x.WarnFormat(It.IsAny<string>(), It.Is<int>(o => o == correlationId)));
            }
        }
예제 #45
0
        public void ReadShouldBeAbleToReceiveMoreThanOnceAsyncronously()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl))
            {
                const int firstMessage = 99;
                const int secondMessage = 100;

                Console.WriteLine("Sending first message to receive...");
                server.SendDataAsync(firstMessage.ToBytes());
                var firstResponseTask = test.ReadAsync(4);

                Console.WriteLine("Sending second message to receive...");
                server.SendDataAsync(secondMessage.ToBytes());
                var secondResponseTask = test.ReadAsync(4);

                Assert.That(firstResponseTask.Result.ToInt32(), Is.EqualTo(firstMessage));
                Assert.That(secondResponseTask.Result.ToInt32(), Is.EqualTo(secondMessage));
            }
        }
예제 #46
0
        public void WriteAsyncShouldSendData()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl))
            {
                const int testData = 99;
                int result = 0;

                server.OnBytesReceived += data => result = data.ToInt32();

                test.WriteAsync(testData.ToBytes().ToPayload()).Wait(TimeSpan.FromSeconds(1));
                TaskTest.WaitFor(() => result > 0);
                Assert.That(result, Is.EqualTo(testData));
            }
        }
예제 #47
0
        public void ReadShouldStackReadRequestsAndReturnOneAtATime()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            {
                var messages = new[] { "test1", "test2", "test3", "test4" };
                var expectedLength = "test1".Length;

                var payload = new KafkaMessagePacker().Pack(messages);

                var socket = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl);

                var tasks = messages.Select(x => socket.ReadAsync(x.Length)).ToArray();

                server.SendDataAsync(payload.Payload());

                Task.WaitAll(tasks);

                foreach (var task in tasks)
                {
                    Assert.That(task.Result.Length, Is.EqualTo(expectedLength));
                }
            }
        }
예제 #48
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.");
            }

        }
예제 #49
0
        public void ReadShouldThrowServerDisconnectedExceptionWhenDisconnected()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            {
                var socket = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl);

                var resultTask = socket.ReadAsync(4);

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

                server.DropConnection();

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

                resultTask.ContinueWith(t => resultTask = t).Wait(TimeSpan.FromSeconds(1));

                Assert.That(resultTask.IsFaulted, Is.True);
                Assert.That(resultTask.Exception.InnerException, Is.TypeOf<ServerDisconnectedException>());
            }

        }
예제 #50
0
        public void ReadShouldNotLoseDataFromStreamOverMultipleReads()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl))
            {
                const int firstMessage = 99;
                const string secondMessage = "testmessage";

                var payload = new KafkaMessagePacker()
                    .Pack(firstMessage)
                    .Pack(secondMessage, StringPrefixEncoding.None);

                //send the combined payload
                server.SendDataAsync(payload.PayloadNoLength());

                var firstResponse = test.ReadAsync(4).Result.ToInt32();
                Assert.That(firstResponse, Is.EqualTo(firstMessage));

                var secondResponse = Encoding.ASCII.GetString(test.ReadAsync(secondMessage.Length).Result);
                Assert.That(secondResponse, Is.EqualTo(secondMessage));
            }
        }
예제 #51
0
        public async Task ReadShouldThrowServerDisconnectedExceptionWhenDisconnected()
        {
            using (var server = new FakeTcpServer(_log, FakeServerPort))
            {
                var socket = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry);

                var resultTask = socket.ReadAsync(4);

                //wait till connected
                await TaskTest.WaitFor(() => server.ConnectionEventcount > 0);

                server.DropConnection();

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

                resultTask.ContinueWith(t => resultTask = t).Wait(TimeSpan.FromSeconds(1));

                Assert.That(resultTask.IsFaulted, Is.True);
                Assert.That(resultTask.Exception.InnerException, Is.TypeOf<BrokerConnectionException>());
            }
        }
예제 #52
0
        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.");
            }
        }
예제 #53
0
        public void WriteAsyncShouldAllowMoreThanOneWrite()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl))
            {
                const int testData = 99;
                var results = new List<byte>();

                server.OnBytesReceived += results.AddRange;

                Task.WaitAll(test.WriteAsync(testData.ToBytes().ToPayload()), test.WriteAsync(testData.ToBytes().ToPayload()));
                TaskTest.WaitFor(() => results.Count >= 8);
                Assert.That(results.Count, Is.EqualTo(8));
            }
        }
예제 #54
0
        public void ReadShouldBlockUntilAllBytesRequestedAreReceived()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl))
            {
                var sendCompleted = 0;
                var bytesReceived = 0;

                test.OnBytesReceived += i => Interlocked.Add(ref bytesReceived, i);

                var resultTask = test.ReadAsync(4).ContinueWith(t =>
                    {
                        Interlocked.Increment(ref sendCompleted);
                        return t.Result;
                    });

                Console.WriteLine("Sending first 3 bytes...");
                server.HasClientConnected.Wait(TimeSpan.FromMilliseconds(1000));
                var sendInitialBytes = server.SendDataAsync(new byte[] { 0, 0, 0 }).Wait(TimeSpan.FromSeconds(10));
                Assert.That(sendInitialBytes, Is.True, "First 3 bytes should have been sent.");

                Console.WriteLine("Ensuring task blocks...");
                TaskTest.WaitFor(() => bytesReceived > 0);
                Assert.That(resultTask.IsCompleted, Is.False, "Task should still be running, blocking.");
                Assert.That(sendCompleted, Is.EqualTo(0), "Should still block even though bytes have been received.");
                Assert.That(bytesReceived, Is.EqualTo(3), "Three bytes should have been received and we are waiting on the last byte.");

                Console.WriteLine("Sending last byte...");
                var sendLastByte = server.SendDataAsync(new byte[] { 0 }).Wait(TimeSpan.FromSeconds(10));
                Assert.That(sendLastByte, Is.True, "Last byte should have sent.");

                Console.WriteLine("Ensuring task unblocks...");
                TaskTest.WaitFor(() => resultTask.IsCompleted);
                Assert.That(bytesReceived, Is.EqualTo(4), "Should have received 4 bytes.");
                Assert.That(resultTask.IsCompleted, Is.True, "Task should have completed.");
                Assert.That(sendCompleted, Is.EqualTo(1), "Task ContinueWith should have executed.");
                Assert.That(resultTask.Result.Length, Is.EqualTo(4), "Result of task should be 4 bytes.");
            }
        }
예제 #55
0
        public void SendAsyncShouldTimeoutWhenSendAsyncTakesTooLong()
        {
            using (var server = new FakeTcpServer(8999))
            using (var socket = new KafkaTcpSocket(_log, _kafkaEndpoint))
            using (var conn = new KafkaConnection(socket, TimeSpan.FromMilliseconds(1), log: _log))
            {
                TaskTest.WaitFor(() => server.ConnectionEventcount > 0);
                Assert.That(server.ConnectionEventcount, Is.EqualTo(1));

                var taskResult = conn.SendAsync(new MetadataRequest());

                taskResult.ContinueWith(t => taskResult = t).Wait(TimeSpan.FromMilliseconds(100));

                Assert.That(taskResult.IsFaulted, Is.True, "Task should have reported an exception.");
                Assert.That(taskResult.Exception.InnerException, Is.TypeOf<ResponseTimeoutException>());
            }
        }
예제 #56
0
        public void WriteAndReadShouldBeAsynchronous()
        {
            var write = new List<int>();
            var read = new List<int>();
            var expected = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            using (var server = new FakeTcpServer(FakeServerPort))
            using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl))
            {
                server.OnBytesReceived += data => write.AddRange(data.Batch(4).Select(x => x.ToArray().ToInt32()));

                var tasks = Enumerable.Range(1, 10)
                    .SelectMany(i => new[]
                    {
                        test.WriteAsync(i.ToBytes().ToPayload()),
                        test.ReadAsync(4).ContinueWith(t => read.Add(t.Result.ToInt32())),
                        server.SendDataAsync(i.ToBytes())
                    }).ToArray();

                Task.WaitAll(tasks);
                Assert.That(write.OrderBy(x => x), Is.EqualTo(expected));
                Assert.That(read.OrderBy(x => x), Is.EqualTo(expected));
            }
        }
예제 #57
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))
            {
                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);

                TaskTest.WaitFor(() => tasks.Any(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.");
                }
            }
        }
예제 #58
0
        public void WriteShouldHandleLargeVolumeSendAsynchronously()
        {
            var write = new List<int>();

            using (var server = new FakeTcpServer(FakeServerPort))
            using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl))
            {
                server.OnBytesReceived += data => write.AddRange(data.Batch(4).Select(x => x.ToArray().ToInt32()));

                var tasks = Enumerable.Range(1, 10000)
                    .SelectMany(i => new[]
                    {
                        test.WriteAsync(i.ToBytes().ToPayload()),
                    }).ToArray();

                Task.WaitAll(tasks);

                Assert.That(write.OrderBy(x => x), Is.EqualTo(Enumerable.Range(1, 10000)));
            }
        }
예제 #59
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))
            {
                var disconnected = false;
                socket.OnServerDisconnected += () => disconnected = true;

                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
                TaskTest.WaitFor(() => disconnected);
                

                //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));
            }
        }
예제 #60
0
        public void ReadShouldCancelWhileAwaitingResponse()
        {
            using (var server = new FakeTcpServer(FakeServerPort))
            using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl))
            {
                var count = 0;
                var semaphore = new SemaphoreSlim(0);
                var token = new CancellationTokenSource();

                test.ReadAsync(4, token.Token).ContinueWith(t =>
                    {
                        Interlocked.Increment(ref count);
                        Assert.That(t.IsCanceled, Is.True, "Task should be set to cancelled when disposed.");
                        semaphore.Release();
                    });

                Thread.Sleep(100);
                token.Cancel();

                semaphore.Wait(TimeSpan.FromSeconds(1));
                Assert.That(count, Is.EqualTo(1), "Read should have cancelled and incremented count.");
            }
        }