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 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."); } }
private async Task<byte[]> ReadFromSocketWithRetry(KafkaTcpSocket socket, int readSize) { byte[] buffer; try { buffer = await socket.ReadAsync(readSize); return buffer; } catch (Exception ex) { Assert.That(ex, Is.TypeOf<ServerDisconnectedException>()); } buffer = await socket.ReadAsync(4); return buffer; }
public async Task WhenNoConnectionThrowSocketException() { var socket = new KafkaTcpSocket(_log, new KafkaEndpoint() { ServeUri = new Uri("http://not.com") }, _maxRetry); var resultTask = socket.ReadAsync(4); await resultTask; }
public void ReadShouldBeAbleToReceiveMoreThanOnceAsyncronously() { using (var server = new FakeTcpServer(_log, FakeServerPort)) using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry)) { 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)); } }
public void ReadShouldBeAbleToReceiveMoreThanOnce() { using (var server = new FakeTcpServer(_log, FakeServerPort)) using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry)) { 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)); } }
public void ReadShouldNotLoseDataFromStreamOverMultipleReads() { using (var server = new FakeTcpServer(_log, FakeServerPort)) using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry)) { 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)); } }
public void KafkaTcpSocketShouldDisposeEvenWhilePollingToReconnect() { var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl); var taskResult = test.ReadAsync(4); 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 ReadShouldNotLoseDataFromStreamOverMultipleReads() { using (var server = new FakeTcpServer(FakeServerPort)) { const int firstMessage = 99; const string secondMessage = "testmessage"; var payload = new WriteByteStream(); payload.Pack(firstMessage.ToBytes(), secondMessage.ToBytes()); var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl); //send the combined payload server.SendDataAsync(payload.Payload()); 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)); } }
public void ReadShouldBeAbleToReceiveMoreThanOnce() { using (var server = new FakeTcpServer(8999)) { const int firstMessage = 99; const string secondMessage = "testmessage"; var test = new KafkaTcpSocket(new DefaultTraceLog(), _fakeServerUrl); Console.WriteLine("Sending first message to receive..."); server.SendDataAsync(firstMessage.ToBytes()).Wait(TimeSpan.FromSeconds(2)); var firstResponse = test.ReadAsync(4).Result.ToInt32(); Assert.That(firstResponse, Is.EqualTo(firstMessage)); Console.WriteLine("Sending second message to receive..."); server.SendDataAsync(secondMessage).Wait(TimeSpan.FromSeconds(2)); var secondResponse = Encoding.ASCII.GetString(test.ReadAsync(secondMessage.Length).Result); Assert.That(secondResponse, Is.EqualTo(secondMessage)); } }
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."); } }
public async Task WhenNoConnectionThrowSocketExceptionAfterMaxRetry() { var reconnectionAttempt = 0; var socket = new KafkaTcpSocket(_log, new KafkaEndpoint() { ServeUri = new Uri("http://not.com") }, _maxRetry); socket.OnReconnectionAttempt += (x) => Interlocked.Increment(ref reconnectionAttempt); var resultTask = socket.ReadAsync(4); await resultTask; Assert.Equals(reconnectionAttempt, _maxRetry); }
public async Task KafkaTcpSocketShouldDisposeEvenWhilePollingToReconnect() { int connectionAttempt = 0; using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry)) { test.OnReconnectionAttempt += i => connectionAttempt = i; var taskResult = test.ReadAsync(4); await TaskTest.WaitFor(() => connectionAttempt > 1); test.Dispose(); await Task.WhenAny(taskResult, Task.Delay(1000)).ConfigureAwait(false); Assert.That(taskResult.IsFaulted, Is.True); Assert.That(taskResult.Exception.InnerException, Is.TypeOf <ObjectDisposedException>()); } }
public async Task ReadShouldCancelWhileAwaitingReconnection() { int connectionAttempt = 0; using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry)) using (var token = new CancellationTokenSource()) { test.OnReconnectionAttempt += i => connectionAttempt = i; var taskResult = test.ReadAsync(4, token.Token); await TaskTest.WaitFor(() => connectionAttempt > 1); token.Cancel(); taskResult.SafeWait(TimeSpan.FromMilliseconds(1000)); Assert.That(taskResult.IsCanceled, Is.True); } }
public async Task ReadShouldBlockUntilAllBytesRequestedAreReceived() { using (var server = new FakeTcpServer(_log, FakeServerPort)) using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry)) { 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..."); await 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..."); await 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."); } }
public async Task KafkaTcpSocketShouldDisposeEvenWhileAwaitingReadAndThrowException() { using (var server = new FakeTcpServer(_log, FakeServerPort)) using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry)) { int readSize = 0; test.OnReadFromSocketAttempt += i => readSize = i; var taskResult = test.ReadAsync(4); await 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 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 <ServerDisconnectedException>()); } }
public void ReadShouldThrowServerDisconnectedExceptionWhenDisconnected() { using (var server = new FakeTcpServer(8999)) { 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>()); } }
public async Task SocketShouldReconnectEvenAfterCancelledRead() { 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.ReadAsync(4, token.Token); await TaskTest.WaitFor(() => connectionAttempt > 1); var attemptsMadeSoFar = connectionAttempt; token.Cancel(); await TaskTest.WaitFor(() => connectionAttempt > attemptsMadeSoFar); Assert.That(connectionAttempt, Is.GreaterThan(attemptsMadeSoFar)); } }
public void ReadShouldStackReadRequestsAndReturnOneAtATime() { using (var server = new FakeTcpServer(_log, FakeServerPort)) { var messages = new[] { "test1", "test2", "test3", "test4" }; var expectedLength = "test1".Length; var payload = new KafkaMessagePacker().Pack(messages); var socket = new KafkaTcpSocket(new DefaultTraceLog(LogLevel.Warn), _fakeServerUrl, _maxRetry); 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)); } } }
public void ReadShouldCancelWhileAwaitingResponse() { using (var server = new FakeTcpServer(_log, FakeServerPort)) using (var test = new KafkaTcpSocket(_log, _fakeServerUrl, _maxRetry)) { 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."); } }
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])); } } // } }