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.");
                    }
        }
Beispiel #3
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));
            }
        }
        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 WriteAsyncShouldAllowMoreThanOneWrite()
        {
            using (var server = new FakeTcpServer(_log, FakeServerPort))
                using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry))
                {
                    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()));
                    await TaskTest.WaitFor(() => results.Count >= 8);

                    Assert.That(results.Count, Is.EqualTo(8));
                }
        }
Beispiel #6
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));
            }
        }
 public void ConnectionShouldAttemptMultipleTimesWhenConnectionFails()
 {
     var count = 0;
     using (var test = new KafkaTcpSocket(new DefaultTraceLog(), _badServerUrl))
     {
         test.WriteAsync(1.ToBytes()); //will force a connection
         test.OnReconnectionAttempt += x => Interlocked.Increment(ref count);
         TaskTest.WaitFor(() => count > 1, 10000);
         Assert.That(count, Is.GreaterThan(1));
     }
 }
        public async Task ConnectionShouldAttemptMultipleTimesWhenConnectionFails()
        {
            var count = 0;

            using (var test = new KafkaTcpSocket(_log, _badServerUrl, _maxRetry))
            {
                test.WriteAsync(1.ToBytes().ToPayload()); //will force a connection
                test.OnReconnectionAttempt += x => Interlocked.Increment(ref count);
                await TaskTest.WaitFor(() => count > 1, 10000);

                Assert.That(count, Is.GreaterThan(1));
            }
        }
Beispiel #9
0
        public void KafkaTcpSocketShouldDisposeEvenWhileWriting()
        {
            var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl);

            var taskResult = test.WriteAsync(4.ToBytes());

            using (test) { } //allow the sockets to set

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

            Assert.That(taskResult.IsCompleted, Is.True);
            Assert.That(taskResult.IsFaulted, Is.True, "Task should result indicate a fault.");
            Assert.That(taskResult.Exception.InnerException, Is.TypeOf <ObjectDisposedException>(), "Exception should be a disposed exception.");
        }
Beispiel #10
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));
            }
        }
        public async Task WriteAsyncShouldSendData()
        {
            using (var server = new FakeTcpServer(_log, FakeServerPort))
                using (var test = new KafkaTcpSocket(new DefaultTraceLog(LogLevel.Warn), _fakeServerUrl, _maxRetry))
                {
                    const int testData = 99;
                    int       result   = 0;

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

                    test.WriteAsync(testData.ToBytes().ToPayload()).Wait(TimeSpan.FromSeconds(1));
                    await TaskTest.WaitFor(() => result > 0);

                    Assert.That(result, Is.EqualTo(testData));
                }
        }
        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)));
                }
        }
        public void WriteShouldCancelWhileAwaitingReconnection()
        {
            int connectionAttempt = 0;

            using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry))
                using (var token = new CancellationTokenSource())
                {
                    test.OnReconnectionAttempt += i => connectionAttempt = i;

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

                    TaskTest.WaitFor(() => connectionAttempt > 1);

                    token.Cancel();

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

                    Assert.That(taskResult.IsCanceled, Is.True);
                }
        }
        public void KafkaTcpSocketShouldDisposeEvenWhileWriting()
        {
            using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry))
            {
                int writeSize = 0;
                test.OnWriteToSocketAttempt += payload => writeSize = payload.Buffer.Length;

                var taskResult = test.WriteAsync(4.ToBytes().ToPayload());

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

                using (test)
                {
                } //allow the sockets to set

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

                Assert.That(taskResult.IsCompleted, Is.True);
                Assert.That(taskResult.IsFaulted, Is.True, "Task should result indicate a fault.");
                Assert.That(taskResult.Exception.InnerException, Is.TypeOf <ObjectDisposedException>(),
                            "Exception should be a disposed exception.");
            }
        }
        public async Task SocketShouldReconnectEvenAfterCancelledWrite()
        {
            int connectionAttempt = 0;

            using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry))
                using (var token = new CancellationTokenSource())
                {
                    test.OnReconnectionAttempt += i => Interlocked.Exchange(ref connectionAttempt, i);

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

                    TaskTest.WaitFor(() => connectionAttempt > 1);

                    var attemptsMadeSoFar = connectionAttempt;

                    token.Cancel();

                    await TaskTest.WaitFor(() => connectionAttempt > attemptsMadeSoFar);

                    Assert.That(connectionAttempt, Is.GreaterThan(attemptsMadeSoFar));
                }
        }
        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]));
                    }
                }
            //   }
        }