public void TestFlushAfterGatheredFlush() { var group = new MultithreadEventLoopGroup(1); var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); IChannel sc = null; try { ServerBootstrap sb = new ServerBootstrap(); sb.Group(group) .Channel <TcpServerSocketChannel>() .ChildHandler(new ChannelInboundHandlerAdapter0()); sc = sb.BindAsync(new IPEndPoint(IPAddress.Loopback, 0)).GetAwaiter().GetResult(); socket.ConnectAsync(sc.LocalAddress).GetAwaiter().GetResult(); byte[] tempArea = new byte[3]; Span <byte> buf = tempArea; var totalCount = 0; do { var byteCount = socket.Receive(buf); if (byteCount < 0) { break; } totalCount += byteCount; if (totalCount < 3) { buf = buf.Slice(totalCount); } } while (totalCount < 3); Assert.Equal("abc", Encoding.ASCII.GetString(tempArea)); } finally { sc?.CloseAsync().GetAwaiter().GetResult(); SocketEx.SafeClose(socket); group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)).GetAwaiter().GetResult(); } }
public void TestFlushCloseReentrance() { var group = new MultithreadEventLoopGroup(1); var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); IChannel sc = null; try { var futures = new BlockingCollection <Task>(); ServerBootstrap sb = new ServerBootstrap(); sb.Group(group) .Channel <TcpServerSocketChannel>() .Option(ChannelOption.SoSndbuf, 1024) .ChildHandler(new ChannelInboundHandlerAdapter1(futures)); sc = sb.BindAsync(new IPEndPoint(IPAddress.Loopback, 0)).GetAwaiter().GetResult(); #if NET452 socket.Connect(sc.LocalAddress); #else socket.ConnectAsync(sc.LocalAddress).GetAwaiter().GetResult(); #endif byte[] tempArea = new byte[8192]; #if NETCOREAPP_2_0_GREATER || NETSTANDARD_2_0_GREATER Span <byte> buf = tempArea; #endif while (true) { #if NETCOREAPP_2_0_GREATER || NETSTANDARD_2_0_GREATER var byteCount = socket.Receive(buf); #else var byteCount = socket.Receive(tempArea); #endif if (byteCount <= 0) { break; } // Wait a little bit so that the write attempts are split into multiple flush attempts. Thread.Sleep(10); } SocketEx.SafeClose(socket); Assert.Equal(3, futures.Count); var f1 = futures.Take(); var f2 = futures.Take(); var f3 = futures.Take(); Assert.True(f1.IsSuccess()); Assert.True(f1.IsCompleted); Assert.False(f2.IsSuccess()); Assert.True(f2.IsCompleted); Assert.IsType <ClosedChannelException>(TaskUtil.Unwrap(f2.Exception)); Assert.False(f3.IsSuccess()); Assert.True(f3.IsCompleted); Assert.IsType <ClosedChannelException>(TaskUtil.Unwrap(f3.Exception)); } finally { SocketEx.SafeClose(socket); sc?.CloseAsync().GetAwaiter().GetResult(); group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)).GetAwaiter().GetResult(); } }
protected override void Close(Socket s) { SocketEx.SafeClose(s); }