private async Task TestReadPendingIsResetAfterEachRead0(ServerBootstrap sb, Bootstrap cb, bool isLibuvServer, bool isLibuvClient) { IChannel serverChannel = null; IChannel clientChannel = null; try { ReadPendingInitializer serverInitializer = new ReadPendingInitializer(isLibuvServer); ReadPendingInitializer clientInitializer = new ReadPendingInitializer(isLibuvClient); sb.Option(ChannelOption.SoBacklog, 1024) .Option(ChannelOption.AutoRead, true) .ChildOption(ChannelOption.AutoRead, false) // We intend to do 2 reads per read loop wakeup .ChildOption(ChannelOption.RcvbufAllocator, new TestNumReadsRecvByteBufAllocator(2)) .ChildHandler(serverInitializer); serverChannel = await sb.BindAsync(); cb.Option(ChannelOption.AutoRead, false) // We intend to do 2 reads per read loop wakeup .Option(ChannelOption.RcvbufAllocator, new TestNumReadsRecvByteBufAllocator(2)) .Handler(clientInitializer); clientChannel = await cb.ConnectAsync(serverChannel.LocalAddress); // 4 bytes means 2 read loops for TestNumReadsRecvByteBufAllocator await clientChannel.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[4])); // 4 bytes means 2 read loops for TestNumReadsRecvByteBufAllocator Assert.True(serverInitializer._channelInitLatch.Wait(TimeSpan.FromSeconds(5))); await serverInitializer._channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[4])); serverInitializer._channel.Read(); serverInitializer._readPendingHandler.AssertAllRead(); clientChannel.Read(); clientInitializer._readPendingHandler.AssertAllRead(); } finally { if (serverChannel != null) { await serverChannel.CloseAsync(); } if (clientChannel != null) { await clientChannel.CloseAsync(); } Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestSingleCompositeBufferWrite0(ServerBootstrap sb, Bootstrap cb) { IChannel serverChannel = null; IChannel clientChannel = null; try { CountdownEvent latch = new CountdownEvent(1); AtomicReference <object> clientReceived = new AtomicReference <object>(); sb.ChildHandler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(new ChannelInboundHandlerAdapter2()); })); cb.Handler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(new ChannelInboundHandlerAdapter3(latch, clientReceived)); })); serverChannel = await sb.BindAsync(); clientChannel = await cb.ConnectAsync(serverChannel.LocalAddress); IByteBuffer expected = NewCompositeBuffer(clientChannel.Allocator); latch.Wait(); object received = clientReceived.Value; if (received is IByteBuffer actual) { Assert.Equal(expected, actual); expected.Release(); actual.Release(); } else { expected.Release(); throw (Exception)received; } } finally { if (clientChannel != null) { await clientChannel.CloseAsync(); } if (serverChannel != null) { await serverChannel.CloseAsync(); } Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestFixedLengthEchoNotAutoRead0(ServerBootstrap sb, Bootstrap cb) { try { await TestFixedLengthEcho0(sb, cb, false); } finally { Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestGatheringWrite0(ServerBootstrap sb, Bootstrap cb) { try { await TestGatheringWrite0(sb, cb, s_data, false, true); } finally { Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestSimpleEchoWithAdditionalExecutorAndVoidPromise0(ServerBootstrap sb, Bootstrap cb) { try { await TestSimpleEcho0(sb, cb, true, true, true); } finally { Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestStringEcho0(ServerBootstrap sb, Bootstrap cb) { try { await TestStringEcho0(sb, cb, true, Output); } finally { Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private void TestAllDataReadAfterHalfClosure0(ServerBootstrap sb, Bootstrap cb) { try { TestAllDataReadAfterHalfClosure0(true, sb, cb); TestAllDataReadAfterHalfClosure0(false, sb, cb); } finally { Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestAutoReadOffDuringReadOnlyReadsOneTime0(ServerBootstrap sb, Bootstrap cb) { try { await TestAutoReadOffDuringReadOnlyReadsOneTime0(true, sb, cb); await TestAutoReadOffDuringReadOnlyReadsOneTime0(false, sb, cb); } finally { Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestGatheringWriteBig0(ServerBootstrap sb, Bootstrap cb) { byte[] bigData = new byte[1024 * 1024 * 50]; s_random.NextBytes(bigData); try { await TestGatheringWrite0(sb, cb, bigData, false, true); } finally { Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestAutoReadOnDataReadImmediately0(ServerBootstrap sb, Bootstrap cb) { IChannel serverChannel = null; IChannel clientChannel = null; try { sb.Option(ChannelOption.AutoRead, true); sb.ChildOption(ChannelOption.AutoRead, true); cb.Option(ChannelOption.AutoRead, true); CountdownEvent serverReadLatch = new CountdownEvent(1); CountdownEvent clientReadLatch = new CountdownEvent(1); sb.ChildHandler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(new SimpleChannelInboundHandler0(serverReadLatch)); })); cb.Handler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(new SimpleChannelInboundHandler1(clientReadLatch)); })); serverChannel = await sb.BindAsync(); clientChannel = await cb.ConnectAsync(serverChannel.LocalAddress); clientChannel.WriteAndFlushAsync(clientChannel.Allocator.Buffer().WriteZero(1)).Ignore(); serverReadLatch.Wait(); clientReadLatch.Wait(); } finally { if (serverChannel != null) { await serverChannel.CloseAsync(); } if (clientChannel != null) { await clientChannel.CloseAsync(); } Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestConditionalWritability0(ServerBootstrap sb, Bootstrap cb) { IChannel serverChannel = null; IChannel clientChannel = null; try { int expectedBytes = 100 * 1024 * 1024; int maxWriteChunkSize = 16 * 1024; CountdownEvent latch = new CountdownEvent(1); sb.ChildOption(ChannelOption.WriteBufferLowWaterMark, 8 * 1024); sb.ChildOption(ChannelOption.WriteBufferHighWaterMark, 16 * 1024); sb.ChildHandler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(new ChannelDuplexHandler0(expectedBytes, maxWriteChunkSize)); })); serverChannel = await sb.BindAsync(); cb.Handler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(new ChannelInboundHandlerAdapter0(expectedBytes, latch)); })); clientChannel = await cb.ConnectAsync(serverChannel.LocalAddress); latch.Wait(); } finally { if (clientChannel != null) { await clientChannel.CloseAsync(); } if (serverChannel != null) { await serverChannel.CloseAsync(); } Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
public async Task TestSocketReuse() { ServerBootstrap serverBootstrap = new ServerBootstrap(); LocalHandler serverHandler = new LocalHandler("SERVER"); serverBootstrap .Group(new DefaultEventLoopGroup(1), new DefaultEventLoopGroup()) .Channel <LocalServerChannel>() .ChildHandler(serverHandler); Bootstrap clientBootstrap = new Bootstrap(); LocalHandler clientHandler = new LocalHandler("CLIENT"); clientBootstrap .Group(new DefaultEventLoopGroup()) .Channel <LocalChannel>() .RemoteAddress(new LocalAddress(LOCAL_CHANNEL)).Handler(clientHandler); await serverBootstrap.BindAsync(new LocalAddress(LOCAL_CHANNEL)); int count = 100; for (int i = 1; i < count + 1; i++) { var ch = await clientBootstrap.ConnectAsync(); // SPIN until we get what we are looking for. int target = i * c_messageCountPerRun; while (serverHandler.Count.Value != target || clientHandler.Count.Value != target) { Thread.Sleep(50); } Close(ch, clientHandler); } Assert.Equal(count * 2 * c_messageCountPerRun, serverHandler.Count.Value + clientHandler.Count.Value); Task.WaitAll( serverBootstrap.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), serverBootstrap.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), clientBootstrap.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)) ); }
private void TestAutoCloseFalseDoesShutdownOutput0(ServerBootstrap sb, Bootstrap cb, bool supportHalfClosed) { try { TestAutoCloseFalseDoesShutdownOutput0(false, false, sb, cb); TestAutoCloseFalseDoesShutdownOutput0(false, true, sb, cb); if (supportHalfClosed) { TestAutoCloseFalseDoesShutdownOutput0(true, false, sb, cb); TestAutoCloseFalseDoesShutdownOutput0(true, true, sb, cb); } } finally { Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestLocalAddressAfterConnect0(ServerBootstrap sb, Bootstrap cb) { IChannel serverChannel = null; IChannel clientChannel = null; try { TaskCompletionSource <EndPoint> localAddressPromise = new TaskCompletionSource <EndPoint>(); serverChannel = await sb.ChildHandler(new ChannelInboundHandlerAdapter1(localAddressPromise)).BindAsync(); clientChannel = await cb.Handler(new ChannelHandlerAdapter()).RegisterAsync(); if (!(clientChannel is TcpChannel)) { Assert.Null(clientChannel.LocalAddress); Assert.Null(clientChannel.RemoteAddress); } await clientChannel.ConnectAsync(serverChannel.LocalAddress); AssertLocalAddress((IPEndPoint)clientChannel.LocalAddress); Assert.NotNull(clientChannel.RemoteAddress); AssertLocalAddress((IPEndPoint)localAddressPromise.Task.GetAwaiter().GetResult()); } finally { if (clientChannel != null) { await clientChannel.CloseAsync(); } if (serverChannel != null) { await serverChannel.CloseAsync(); } Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestReadPendingIsResetAfterEachRead0(ServerBootstrap sb, Bootstrap cb) { IChannel serverChannel = null; IChannel clientChannel = null; try { MyInitializer serverInitializer = new MyInitializer(); sb.Option(ChannelOption.SoBacklog, 1024); sb.ChildHandler(serverInitializer); serverChannel = await sb.BindAsync(); cb.Handler(new MyInitializer()); clientChannel = await cb.ConnectAsync(serverChannel.LocalAddress); await clientChannel.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[1024])); // We expect to get 2 exceptions (1 from BuggyChannelHandler and 1 from ExceptionHandler). Assert.True(serverInitializer._exceptionHandler._latch1.Wait(TimeSpan.FromSeconds(5))); // After we get the first exception, we should get no more, this is expected to timeout. Assert.False(serverInitializer._exceptionHandler._latch2.Wait(TimeSpan.FromSeconds(1)), "Encountered " + serverInitializer._exceptionHandler._count.Value + " exceptions when 1 was expected"); } finally { if (serverChannel != null) { await serverChannel.CloseAsync(); } if (clientChannel != null) { await clientChannel.CloseAsync(); } Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private void TestHalfClosureOnlyOneEventWhenAutoRead0(ServerBootstrap sb, Bootstrap cb) { IChannel serverChannel = null; try { cb.Option(ChannelOption.AllowHalfClosure, true) .Option(ChannelOption.AutoRead, true); sb.ChildHandler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(new ChannelInboundHandlerAdapter4()); })); AtomicInteger shutdownEventReceivedCounter = new AtomicInteger(); AtomicInteger shutdownReadCompleteEventReceivedCounter = new AtomicInteger(); cb.Handler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(new ChannelInboundHandlerAdapter5(shutdownEventReceivedCounter, shutdownReadCompleteEventReceivedCounter)); })); serverChannel = sb.BindAsync().GetAwaiter().GetResult(); IChannel clientChannel = cb.ConnectAsync(serverChannel.LocalAddress).GetAwaiter().GetResult(); clientChannel.CloseCompletion.GetAwaiter().GetResult(); Assert.Equal(1, shutdownEventReceivedCounter.Value); Assert.Equal(1, shutdownReadCompleteEventReceivedCounter.Value); } finally { if (serverChannel != null) { serverChannel.CloseAsync().GetAwaiter().GetResult(); } Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestChannelEventsFiredWhenClosedDirectly0(ServerBootstrap sb, Bootstrap cb) { BlockingCollection <int> events = new BlockingCollection <int>(); IChannel sc = null; IChannel cc = null; try { sb.ChildHandler(new ChannelHandlerAdapter()); sc = await sb.BindAsync(); cb.Handler(new ChannelInboundHandlerAdapter0(events)); // Connect and directly close again. cc = await cb.ConnectAsync(sc.LocalAddress); await cc.CloseAsync(); Assert.Equal(0, events.Take()); Assert.Equal(1, events.Take()); } finally { if (cc != null) { await cc.CloseAsync(); } if (sc != null) { await sc.CloseAsync(); } Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private void TestWriteBeforeConnect(Bootstrap cb) { TestHandler h = new TestHandler(Output); IChannel ch = null; ServerBootstrap b = DefaultServerBootstrapFactory.Instance.NewInstance().ChildHandler(new ChannelHandlerAdapter()); try { var sch = b.BindAsync(NewSocketAddress()).GetAwaiter().GetResult(); ch = cb.Handler(h).ConnectAsync(sch.LocalAddress).GetAwaiter().GetResult(); ch.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[] { 1 })); } finally { if (ch != null) { ch.CloseAsync(); } Task.WaitAll( b.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), b.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestBufRelease0(ServerBootstrap sb, Bootstrap cb) { try { BufWriterHandler serverHandler = new BufWriterHandler(); BufWriterHandler clientHandler = new BufWriterHandler(); sb.ChildHandler(serverHandler); cb.Handler(clientHandler); IChannel sc = await sb.BindAsync(); IChannel cc = await cb.ConnectAsync(sc.LocalAddress); // Ensure the server socket accepted the client connection *and* initialized pipeline successfully. await serverHandler._channelFuture.Task; // and then close all sockets. await sc.CloseAsync(); await cc.CloseAsync(); serverHandler.Check(); clientHandler.Check(); serverHandler.Release(); clientHandler.Release(); } finally { Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
public override void Shutdown() { Channel.CloseAsync(); bootstrap.Group().ShutdownGracefullyAsync(); bootstrap.ChildGroup().ShutdownGracefullyAsync(); }
private async Task TestAutoReadOffNoDataReadUntilReadCalled0(ServerBootstrap sb, Bootstrap cb, bool isLibuvServer) { IChannel serverChannel = null; IChannel clientChannel = null; const int sleepMs = 100; try { sb.Option(ChannelOption.AutoRead, isLibuvServer); // LibuvServer 不支持 No-AutoRead sb.ChildOption(ChannelOption.AutoRead, false); cb.Option(ChannelOption.AutoRead, false); CountdownEvent serverReadyLatch = new CountdownEvent(1); CountdownEvent acceptorReadLatch = new CountdownEvent(1); CountdownEvent serverReadLatch = new CountdownEvent(1); CountdownEvent clientReadLatch = new CountdownEvent(1); AtomicReference <IChannel> serverConnectedChannelRef = new AtomicReference <IChannel>(); sb.Handler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(new ChannelInboundHandlerAdapter0(acceptorReadLatch)); })); sb.ChildHandler(new ActionChannelInitializer <IChannel>(ch => { serverConnectedChannelRef.Value = ch; ch.Pipeline.AddLast(new SimpleChannelInboundHandler0(serverReadLatch)); serverReadyLatch.SafeSignal(); })); cb.Handler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(new SimpleChannelInboundHandler1(clientReadLatch)); })); serverChannel = await sb.BindAsync(); clientChannel = await cb.ConnectAsync(serverChannel.LocalAddress); await clientChannel.WriteAndFlushAsync(clientChannel.Allocator.Buffer().WriteZero(1)); // The acceptor shouldn't read any data until we call read() below, but give it some time to see if it will. if (!isLibuvServer) { Thread.Sleep(sleepMs); Assert.Equal(1, acceptorReadLatch.CurrentCount); serverChannel.Read(); serverReadyLatch.Wait(); } IChannel serverConnectedChannel = serverConnectedChannelRef.Value; Assert.NotNull(serverConnectedChannel); // Allow some amount of time for the server peer to receive the message (which isn't expected to happen // until we call read() below). Thread.Sleep(sleepMs); Assert.Equal(1, serverReadLatch.CurrentCount); serverConnectedChannel.Read(); serverReadLatch.Wait(); // Allow some amount of time for the client to read the echo. Thread.Sleep(sleepMs); Assert.Equal(1, clientReadLatch.CurrentCount); clientChannel.Read(); clientReadLatch.Wait(); } finally { if (serverChannel != null) { await serverChannel.CloseAsync(); } if (clientChannel != null) { await clientChannel.CloseAsync(); } Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestCompositeBufferPartialWriteDoesNotCorruptData0(ServerBootstrap sb, Bootstrap cb) { // The scenario is the following: // Limit SO_SNDBUF so that a single buffer can be written, and part of a CompositeByteBuf at the same time. // We then write the single buffer, the CompositeByteBuf, and another single buffer and verify the data is not // corrupted when we read it on the other side. IChannel serverChannel = null; IChannel clientChannel = null; try { Random r = new Random(); int soSndBuf = 1024; IByteBufferAllocator alloc = ByteBufferUtil.DefaultAllocator; IByteBuffer expectedContent = alloc.Buffer(soSndBuf * 2); expectedContent.WriteBytes(NewRandomBytes(expectedContent.WritableBytes, r)); CountdownEvent latch = new CountdownEvent(1); AtomicReference <object> clientReceived = new AtomicReference <object>(); sb.ChildOption(ChannelOption.SoSndbuf, soSndBuf) .ChildHandler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(new ChannelInboundHandlerAdapter0(expectedContent, latch, clientReceived, soSndBuf)); })); cb.Handler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(new ChannelInboundHandlerAdapter1(expectedContent, latch, clientReceived)); })); serverChannel = await sb.BindAsync(); clientChannel = await cb.ConnectAsync(serverChannel.LocalAddress); latch.Wait(); object received = clientReceived.Value; if (received is IByteBuffer) { IByteBuffer actual = (IByteBuffer)received; Assert.Equal(expectedContent, actual); expectedContent.Release(); actual.Release(); } else { expectedContent.Release(); throw (Exception)received; } } finally { if (clientChannel != null) { await clientChannel.CloseAsync(); } if (serverChannel != null) { await serverChannel.CloseAsync(); } Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
public async Task ExecutorPreserveOrdering() { var sb = new ServerBootstrap(); sb.Group(new DefaultEventLoopGroup(1), new DefaultEventLoopGroup()); sb.Channel <LocalServerChannel>(); sb.ChildHandler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline .AddLast(new HttpServerCodec()) .AddLast(new HttpObjectAggregator(1024)) .AddLast(/*compressorGroup,*/ new HttpContentCompressor()) .AddLast(new ChannelOutboundHandlerAdapter0()) .AddLast(new ChannelOutboundHandlerAdapter1()); })); var responses = new BlockingCollection <IHttpObject>(); var bs = new Bootstrap(); bs.Group(new DefaultEventLoopGroup()); bs.Channel <LocalChannel>(); bs.Handler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline .AddLast(new HttpClientCodec()) .AddLast(new ChannelInboundHandlerAdapter0(responses)); })); IChannel serverChannel = null; IChannel clientChannel = null; try { serverChannel = await sb.BindAsync(new LocalAddress(Guid.NewGuid().ToString("N"))); clientChannel = await bs.ConnectAsync(serverChannel.LocalAddress); await clientChannel.WriteAndFlushAsync(NewRequest()); var result = responses.TryTake(out var item, TimeSpan.FromSeconds(1)); Assert.True(result); AssertEncodedResponse((IHttpResponse)item); result = responses.TryTake(out item, TimeSpan.FromSeconds(1)); Assert.True(result); IHttpContent c = (IHttpContent)item; Assert.NotNull(c); Assert.Equal($"1f8b08000000000000{Platform}f248cdc9c9d75108cf2fca4901000000ffff", ByteBufferUtil.HexDump(c.Content)); c.Release(); result = responses.TryTake(out item, TimeSpan.FromSeconds(1)); Assert.True(result); c = (IHttpContent)item; Assert.NotNull(c); Assert.Equal("0300c6865b260c000000", ByteBufferUtil.HexDump(c.Content)); c.Release(); result = responses.TryTake(out item, TimeSpan.FromSeconds(1)); Assert.True(result); ILastHttpContent last = (ILastHttpContent)item; Assert.NotNull(last); Assert.Equal(0, last.Content.ReadableBytes); last.Release(); Assert.False(responses.TryTake(out _, TimeSpan.FromSeconds(1))); } finally { if (clientChannel != null) { await clientChannel.CloseAsync(); } if (serverChannel != null) { await serverChannel.CloseAsync(); } await Task.WhenAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), bs.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
private async Task TestCancelWrite0(ServerBootstrap sb, Bootstrap cb) { TestHandler sh = new TestHandler(); TestHandler ch = new TestHandler(); IByteBuffer a = Unpooled.Buffer().WriteByte('a'); IByteBuffer b = Unpooled.Buffer().WriteByte('b'); IByteBuffer c = Unpooled.Buffer().WriteByte('c'); IByteBuffer d = Unpooled.Buffer().WriteByte('d'); IByteBuffer e = Unpooled.Buffer().WriteByte('e'); cb.Handler(ch); sb.ChildHandler(sh); IChannel sc = await sb.BindAsync(); IChannel cc = await cb.ConnectAsync(sc.LocalAddress); var promise = new TaskCompletionSource(); Assert.True(promise.TrySetCanceled()); var f = cc.WriteAsync(a, promise); await cc.WriteAndFlushAsync(b); cc.WriteAsync(c).Ignore(); promise = new TaskCompletionSource(); Assert.True(promise.TrySetCanceled()); var f2 = cc.WriteAsync(d, promise); await cc.WriteAndFlushAsync(e); while (sh._counter < 3) { if (sh._exception.Value != null) { break; } if (ch._exception.Value != null) { break; } Thread.Sleep(50); } await sh._channel.CloseAsync(); await ch._channel.CloseAsync(); await sc.CloseAsync(); Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); if (sh._exception.Value != null && !(sh._exception.Value is SocketException || (sh._exception.Value is ChannelException chexc && chexc.InnerException is OperationException) || sh._exception.Value is OperationException)) { throw sh._exception.Value; } if (sh._exception.Value != null) { throw sh._exception.Value; } if (ch._exception.Value != null && !(ch._exception.Value is SocketException || (sh._exception.Value is ChannelException chexc1 && chexc1.InnerException is OperationException) || sh._exception.Value is OperationException)) { throw ch._exception.Value; } if (ch._exception.Value != null) { throw ch._exception.Value; } Assert.Equal(0, ch._counter); Assert.True(Unpooled.WrappedBuffer(new byte[] { (byte)'b', (byte)'c', (byte)'e' }).Equals(sh._received)); }