public void TcpHappyPathTest() { foreach (bool async in new[] { false, true }) { var sender = new MyTcpNetworkSender("tcp://hostname:123", AddressFamily.Unspecified) { Async = async, }; sender.Initialize(); byte[] buffer = Encoding.UTF8.GetBytes("quick brown fox jumps over the lazy dog"); var exceptions = new List <Exception>(); for (int i = 1; i < 8; i *= 2) { sender.Send( buffer, 0, i, ex => { lock (exceptions) exceptions.Add(ex); }); } var mre = new ManualResetEvent(false); sender.FlushAsync(ex => { lock (exceptions) { exceptions.Add(ex); } mre.Set(); }); mre.WaitOne(); var actual = sender.Log.ToString(); Assert.True(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1); Assert.True(actual.IndexOf("create socket 10000 Stream Tcp") != -1); Assert.True(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1); Assert.True(actual.IndexOf("send async 0 1 'q'") != -1); Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1); Assert.True(actual.IndexOf("send async 0 4 'quic'") != -1); mre.Reset(); for (int i = 1; i < 8; i *= 2) { sender.Send( buffer, 0, i, ex => { lock (exceptions) exceptions.Add(ex); }); } sender.Close(ex => { lock (exceptions) { exceptions.Add(ex); } mre.Set(); }); mre.WaitOne(); actual = sender.Log.ToString(); Assert.True(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1); Assert.True(actual.IndexOf("create socket 10000 Stream Tcp") != -1); Assert.True(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1); Assert.True(actual.IndexOf("send async 0 1 'q'") != -1); Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1); Assert.True(actual.IndexOf("send async 0 4 'quic'") != -1); Assert.True(actual.IndexOf("send async 0 1 'q'") != -1); Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1); Assert.True(actual.IndexOf("send async 0 4 'quic'") != -1); Assert.True(actual.IndexOf("close") != -1); foreach (var ex in exceptions) { Assert.Null(ex); } } }
public void TcpHappyPathTest() { foreach (bool async in new[] { false, true }) { var sender = new MyTcpNetworkSender("tcp://hostname:123", AddressFamily.Unspecified) { Async = async, }; sender.Initialize(); byte[] buffer = Encoding.UTF8.GetBytes("quick brown fox jumps over the lazy dog"); var exceptions = new List<Exception>(); for (int i = 1; i < 8; i *= 2) { sender.Send( buffer, 0, i, ex => { lock (exceptions) exceptions.Add(ex); }); } var mre = new ManualResetEvent(false); sender.FlushAsync(ex => { lock (exceptions) { exceptions.Add(ex); } mre.Set(); }); mre.WaitOne(); var actual = sender.Log.ToString(); Assert.True(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1); Assert.True(actual.IndexOf("create socket 10000 Stream Tcp") != -1); Assert.True(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1); Assert.True(actual.IndexOf("send async 0 1 'q'") != -1); Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1); Assert.True(actual.IndexOf("send async 0 4 'quic'") != -1); mre.Reset(); for (int i = 1; i < 8; i *= 2) { sender.Send( buffer, 0, i, ex => { lock (exceptions) exceptions.Add(ex); }); } sender.Close(ex => { lock (exceptions) { exceptions.Add(ex); } mre.Set(); }); mre.WaitOne(); actual = sender.Log.ToString(); Assert.True(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1); Assert.True(actual.IndexOf("create socket 10000 Stream Tcp") != -1); Assert.True(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1); Assert.True(actual.IndexOf("send async 0 1 'q'") != -1); Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1); Assert.True(actual.IndexOf("send async 0 4 'quic'") != -1); Assert.True(actual.IndexOf("send async 0 1 'q'") != -1); Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1); Assert.True(actual.IndexOf("send async 0 4 'quic'") != -1); Assert.True(actual.IndexOf("close") != -1); foreach (var ex in exceptions) { Assert.Null(ex); } } }
public void TcpSendFailureTest() { var sender = new MyTcpNetworkSender("tcp://hostname:123", AddressFamily.Unspecified) { SendFailureIn = 3, // will cause failure on 3rd send Async = true, }; sender.Initialize(); byte[] buffer = Encoding.UTF8.GetBytes("quick brown fox jumps over the lazy dog"); var exceptions = new Exception[9]; var writeFinished = new ManualResetEvent(false); int remaining = exceptions.Length; for (int i = 1; i < 10; i++) { int pos = i - 1; sender.Send( buffer, 0, i, ex => { lock (exceptions) { exceptions[pos] = ex; if (--remaining == 0) { writeFinished.Set(); } } }); } var mre = new ManualResetEvent(false); writeFinished.WaitOne(); sender.Close(ex => mre.Set()); mre.WaitOne(); var actual = sender.Log.ToString(); Assert.True(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1); Assert.True(actual.IndexOf("create socket 10000 Stream Tcp") != -1); Assert.True(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1); Assert.True(actual.IndexOf("send async 0 1 'q'") != -1); Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1); Assert.True(actual.IndexOf("send async 0 3 'qui'") != -1); Assert.True(actual.IndexOf("failed") != -1); Assert.True(actual.IndexOf("close") != -1); for (int i = 0; i < exceptions.Length; ++i) { if (i < 2) { Assert.Null(exceptions[i]); } else { Assert.NotNull(exceptions[i]); } } }