in csharp/AdapterTest/SocketWrapperTest.cs [24:150]
private void SocketTest(ISocketWrapper serverSocket)
{
serverSocket.Listen();
if (serverSocket is RioSocketWrapper)
{
// Do nothing for second listen operation.
Assert.DoesNotThrow(() => serverSocket.Listen(int.MaxValue));
}
var port = ((IPEndPoint)serverSocket.LocalEndPoint).Port;
var clientMsg = "Hello Message from client";
var clientMsgBytes = Encoding.UTF8.GetBytes(clientMsg);
Task.Run(() =>
{
var bytes = new byte[1024];
using (var socket = serverSocket.Accept())
{
using (var s = socket.GetStream())
{
// Receive data
var bytesRec = s.Read(bytes, 0, bytes.Length);
// send echo message.
s.Write(bytes, 0, bytesRec);
s.Flush();
// Receive one byte
var oneByte = s.ReadByte();
// Send echo one byte
byte[] oneBytes = { (byte)oneByte };
s.Write(oneBytes, 0, oneBytes.Length);
s.Flush();
Thread.SpinWait(0);
// Send more bytes to test ReadByte() do not cause failures
s.Write(bytes, 0, bytesRec);
s.Flush();
// Keep sending to ensure no memory leak
var longBytes = Encoding.UTF8.GetBytes(new string('x', 66536));
for (int i = 0; i < 1000; i++)
{
s.Write(longBytes, 0, longBytes.Length);
s.Flush();
}
byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
s.Write(msg, 0, msg.Length);
s.Flush();
// Receive echo byte.
s.ReadByte();
}
}
});
var clientSock = SocketFactory.CreateSocket();
// Valid invalid operation
Assert.Throws<InvalidOperationException>(() => clientSock.GetStream());
Assert.Throws<InvalidOperationException>(() => clientSock.Receive());
Assert.Throws<InvalidOperationException>(() => clientSock.Send(null));
Assert.Throws<SocketException>(() => clientSock.Connect(IPAddress.Any, 1024, null));
clientSock.Connect(IPAddress.Loopback, port, null);
// Valid invalid operation
var byteBuf = ByteBufPool.Default.Allocate();
Assert.Throws<ArgumentException>(() => clientSock.Send(byteBuf));
byteBuf.Release();
Assert.Throws<SocketException>(() => clientSock.Listen());
if (clientSock is RioSocketWrapper)
{
Assert.Throws<InvalidOperationException>(() => clientSock.Accept());
}
using (var ins = clientSock.GetInputStream())
using (var outs = clientSock.GetOutputStream())
{
// Send message
outs.Write(clientMsgBytes, 0, clientMsgBytes.Length);
outs.Flush();
// Receive echo message
var bytes = new byte[1024];
var bytesRec = ins.Read(bytes, 0, bytes.Length);
Assert.AreEqual(clientMsgBytes.Length, bytesRec);
var recvStr = Encoding.UTF8.GetString(bytes, 0, bytesRec);
Assert.AreEqual(clientMsg, recvStr);
// Send one byte
byte[] oneBytes = { 1 };
outs.Write(oneBytes, 0, oneBytes.Length);
outs.Flush();
// Receive echo message
var oneByte = ins.ReadByte();
Assert.AreEqual((byte)1, oneByte);
// Receive more message to test ReadByte do not cause failures.
bytesRec = ins.Read(bytes, 0, bytes.Length);
Assert.AreNotEqual(0, bytesRec);
// Keep receiving to ensure no memory leak.
while (true)
{
bytesRec = ins.Read(bytes, 0, bytes.Length);
recvStr = Encoding.UTF8.GetString(bytes, 0, bytesRec);
if (recvStr.IndexOf("<EOF>", StringComparison.OrdinalIgnoreCase) > -1)
{
break;
}
}
// send echo bytes
outs.Write(oneBytes, 0, oneBytes.Length);
outs.Flush();
}
clientSock.Close();
// Verify invalid operation
Assert.Throws<ObjectDisposedException>(() => clientSock.Receive());
serverSocket.Close();
}