Skip to content

Commit

Permalink
TestSuite (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteant committed Jul 26, 2020
1 parent cdef32c commit 53a2935
Show file tree
Hide file tree
Showing 51 changed files with 6,286 additions and 77 deletions.
12 changes: 7 additions & 5 deletions src/DotNetty.Buffers/ArrayPooledByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ internal void Reuse(ArrayPooledByteBufferAllocator allocator, ArrayPool<byte> ar
{
_allocator = allocator;
_arrayPool = arrayPool;
SetArray(AllocateArray(initialCapacity));

SetMaxCapacity(maxCapacity);
SetArray(AllocateArray(initialCapacity), maxCapacity);

SetReferenceCount(1);
SetIndex0(0, 0);
DiscardMarks();
Expand All @@ -41,9 +42,10 @@ internal void Reuse(ArrayPooledByteBufferAllocator allocator, ArrayPool<byte> ar
{
_allocator = allocator;
_arrayPool = arrayPool;
SetArray(buffer);

SetMaxCapacity(maxCapacity);
SetArray(buffer, maxCapacity);

SetReferenceCount(1);
SetIndex0(0, length);
DiscardMarks();
Expand All @@ -67,10 +69,10 @@ protected virtual void FreeArray(byte[] bytes)
#endif
}

protected void SetArray(byte[] initialArray)
protected void SetArray(byte[] initialArray, int maxCapacity)
{
Memory = initialArray;
_capacity = initialArray.Length;
_capacity = Math.Min(initialArray.Length, maxCapacity);
}

public sealed override IByteBuffer AdjustCapacity(int newCapacity)
Expand All @@ -97,7 +99,7 @@ public sealed override IByteBuffer AdjustCapacity(int newCapacity)
byte[] newArray = AllocateArray(newCapacity);
PlatformDependent.CopyMemory(oldArray, 0, newArray, 0, bytesToCopy);

SetArray(newArray);
SetArray(newArray, MaxCapacity);
FreeArray(oldArray);
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetty.Transport.Libuv/ITcpChannelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace DotNetty.Transport.Libuv
{
public interface ITcpChannelFactory
{
ISocketChannel CreateChannel<TServerChannel>(TServerChannel channel, Tcp tcp) where TServerChannel : IServerChannel;
IChannel CreateChannel<TServerChannel>(TServerChannel channel, Tcp tcp) where TServerChannel : IServerChannel;
}
}
3 changes: 1 addition & 2 deletions src/DotNetty.Transport.Libuv/TcpChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace DotNetty.Transport.Libuv
using System.Net;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Libuv.Native;
using DotNetty.Transport.Channels.Sockets;

public sealed class TcpChannel : TcpChannel<TcpChannel>
{
Expand All @@ -15,7 +14,7 @@ public TcpChannel() : base() { }
internal TcpChannel(IChannel parent, Tcp tcp) : base(parent, tcp) { }
}

public partial class TcpChannel<TChannel> : NativeChannel<TChannel, TcpChannel<TChannel>.TcpChannelUnsafe>, ISocketChannel
public partial class TcpChannel<TChannel> : NativeChannel<TChannel, TcpChannel<TChannel>.TcpChannelUnsafe>
where TChannel : TcpChannel<TChannel>
{
private static readonly ChannelMetadata TcpMetadata = new ChannelMetadata(false);
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetty.Transport.Libuv/TcpChannelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace DotNetty.Transport.Libuv
{
public sealed class TcpChannelFactory : ITcpChannelFactory
{
public ISocketChannel CreateChannel<TServerChannel>(TServerChannel channel, Tcp tcp) where TServerChannel : IServerChannel
public IChannel CreateChannel<TServerChannel>(TServerChannel channel, Tcp tcp) where TServerChannel : IServerChannel
{
return new TcpChannel(channel, tcp);
}
Expand Down
3 changes: 0 additions & 3 deletions src/DotNetty.Transport.Libuv/TcpServerChannelConfig.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

// ReSharper disable ConvertToAutoProperty
// ReSharper disable ConvertToAutoPropertyWithPrivateSetter
// ReSharper disable SuggestBaseTypeForParameter
namespace DotNetty.Transport.Libuv
{
using System;
Expand Down
6 changes: 4 additions & 2 deletions src/DotNetty.Transport/Channels/DefaultChannelPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,11 +1313,13 @@ public void ChannelReadComplete(IChannelHandlerContext ctx)
ReadIfIsAutoRead();
}

[MethodImpl(InlineMethod.AggressiveInlining)]
private void ReadIfIsAutoRead()
{
if (_pipeline._channel.Configuration.IsAutoRead)
var channel = _pipeline._channel;
if (channel.Configuration.IsAutoRead)
{
_ = _pipeline._channel.Read();
_ = channel.Read();
}
}

Expand Down
23 changes: 19 additions & 4 deletions src/DotNetty.Transport/Channels/Groups/DefaultChannelGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ public class DefaultChannelGroup : IChannelGroup, IComparable<IChannelGroup>
private readonly bool _stayClosed;
private int _closed;

public DefaultChannelGroup(IEventExecutor executor)
: this(executor, false)
public DefaultChannelGroup()
: this(false)
{
}

public DefaultChannelGroup(string name, IEventExecutor executor)
: this(name, executor, false)
public DefaultChannelGroup(bool stayClosed)
: this(executor: null, stayClosed)
{
}

public DefaultChannelGroup(IEventExecutor executor)
: this(executor, false)
{
}

Expand All @@ -39,6 +44,16 @@ public DefaultChannelGroup(IEventExecutor executor, bool stayClosed)
{
}

public DefaultChannelGroup(string name, bool stayClosed)
: this(name, null, stayClosed)
{
}

public DefaultChannelGroup(string name, IEventExecutor executor)
: this(name, executor, false)
{
}

public DefaultChannelGroup(string name, IEventExecutor executor, bool stayClosed)
{
if (name is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.name); }
Expand Down
33 changes: 33 additions & 0 deletions src/DotNetty.Transport/Channels/IDuplexChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace DotNetty.Transport.Channels
{
using System.Threading.Tasks;
using DotNetty.Common.Concurrency;

public interface IDuplexChannel : IChannel
{
/// <summary>
/// Returns <c>true</c> if and only if the remote peer shut down its output so that no more
/// data is received from this channel.
/// </summary>
bool IsInputShutdown { get; }

Task ShutdownInputAsync();

/// <summary>
/// Will shutdown the input and notify <see cref="IPromise"/>.
/// </summary>
Task ShutdownInputAsync(IPromise promise);

bool IsOutputShutdown { get; }

Task ShutdownOutputAsync();

Task ShutdownOutputAsync(IPromise promise);

bool IsShutdown { get; }

Task ShutdownAsync();

Task ShutdownAsync(IPromise promise);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public SocketByteChannelUnsafe() //(AbstractSocketByteChannel channel)
void CloseOnRead()
{
var ch = _channel;
if (!ch.IsOpen) { return; }

if (!ch.IsInputShutdown)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected AbstractSocketByteChannel(IChannel parent, Socket socket)
/// <summary>
/// Shutdown the input side of the channel.
/// </summary>
protected abstract Task ShutdownInputAsync();
public abstract Task ShutdownInputAsync();

public virtual bool IsInputShutdown => false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ namespace DotNetty.Transport.Channels.Sockets
/// </summary>
public interface IServerSocketChannel : IServerChannel
{
new IServerSocketChannelConfiguration Configuration { get; }
}
}
3 changes: 2 additions & 1 deletion src/DotNetty.Transport/Channels/Sockets/ISocketChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace DotNetty.Transport.Channels.Sockets
{
public interface ISocketChannel : IChannel
public interface ISocketChannel : IDuplexChannel
{
new ISocketChannelConfiguration Configuration { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public TcpServerSocketChannel(Socket socket)
_channelFactory = new TChannelFactory();
}

IServerSocketChannelConfiguration IServerSocketChannel.Configuration => _config;
public override IChannelConfiguration Configuration => _config;

public override bool IsActive
Expand Down
68 changes: 54 additions & 14 deletions src/DotNetty.Transport/Channels/Sockets/TcpSocketChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public partial class TcpSocketChannel<TChannel> : AbstractSocketByteChannel<TCha
{
private static readonly Action<object, object> ShutdownOutputAction = OnShutdownOutput;
private static readonly Action<object, object> ShutdownInputAction = OnShutdownInput;
private static readonly Action<object, object> ShutdownAction = OnShutdown;

private static readonly ChannelMetadata METADATA = new ChannelMetadata(false, 16);

Expand Down Expand Up @@ -92,6 +93,7 @@ protected TcpSocketChannel(IChannel parent, Socket socket, bool connected)

public override ChannelMetadata Metadata => METADATA;

ISocketChannelConfiguration ISocketChannel.Configuration => _config;
public override IChannelConfiguration Configuration => _config;

protected override EndPoint LocalAddressInternal => Socket.LocalEndPoint;
Expand All @@ -104,19 +106,20 @@ protected TcpSocketChannel(IChannel parent, Socket socket, bool connected)

public bool IsShutdown => (IsInputShutdown && IsOutputShutdown) || !IsActive;

protected override Task ShutdownInputAsync()
public override Task ShutdownInputAsync() => ShutdownInputAsync(NewPromise());

public Task ShutdownInputAsync(IPromise promise)
{
var tcs = NewPromise();
IEventLoop loop = EventLoop;
if (loop.InEventLoop)
{
ShutdownInput0(tcs);
ShutdownInput0(promise);
}
else
{
loop.Execute(ShutdownInputAction, this, tcs);
loop.Execute(ShutdownInputAction, this, promise);
}
return tcs.Task;
return promise.Task;
}

void ShutdownInput0(IPromise promise)
Expand All @@ -125,11 +128,11 @@ void ShutdownInput0(IPromise promise)
{
Socket.Shutdown(SocketShutdown.Receive);
_ = Interlocked.Exchange(ref v_inputShutdown, SharedConstants.True);
promise.Complete();
promise.TryComplete();
}
catch (Exception ex)
{
promise.SetException(ex);
promise.TrySetException(ex);
}
}

Expand All @@ -138,19 +141,20 @@ private static void OnShutdownInput(object channel, object promise)
((TcpSocketChannel<TChannel>)channel).ShutdownInput0((IPromise)promise);
}

public Task ShutdownOutputAsync()
public Task ShutdownOutputAsync() => ShutdownOutputAsync(NewPromise());

public Task ShutdownOutputAsync(IPromise promise)
{
var tcs = NewPromise();
IEventLoop loop = EventLoop;
if (loop.InEventLoop)
{
ShutdownOutput0(tcs);
ShutdownOutput0(promise);
}
else
{
loop.Execute(ShutdownOutputAction, this, tcs);
loop.Execute(ShutdownOutputAction, this, promise);
}
return tcs.Task;
return promise.Task;
}

void ShutdownOutput0(IPromise promise)
Expand All @@ -159,11 +163,11 @@ void ShutdownOutput0(IPromise promise)
{
Socket.Shutdown(SocketShutdown.Send);
_ = Interlocked.Exchange(ref v_outputShutdown, SharedConstants.True);
promise.Complete();
promise.TryComplete();
}
catch (Exception ex)
{
promise.SetException(ex);
promise.TrySetException(ex);
}
}

Expand All @@ -172,6 +176,42 @@ private static void OnShutdownOutput(object channel, object promise)
((TcpSocketChannel<TChannel>)channel).ShutdownOutput0((IPromise)promise);
}

public Task ShutdownAsync() => ShutdownAsync(NewPromise());

public Task ShutdownAsync(IPromise promise)
{
IEventLoop loop = EventLoop;
if (loop.InEventLoop)
{
Shutdown0(promise);
}
else
{
loop.Execute(ShutdownAction, this, promise);
}
return promise.Task;
}

void Shutdown0(IPromise promise)
{
try
{
Socket.Shutdown(SocketShutdown.Both);
_ = Interlocked.Exchange(ref v_inputShutdown, SharedConstants.True);
_ = Interlocked.Exchange(ref v_outputShutdown, SharedConstants.True);
promise.TryComplete();
}
catch (Exception ex)
{
promise.TrySetException(ex);
}
}

private static void OnShutdown(object channel, object promise)
{
((TcpSocketChannel<TChannel>)channel).ShutdownOutput0((IPromise)promise);
}

protected override void DoBind(EndPoint localAddress) => Socket.Bind(localAddress);

protected override bool DoConnect(EndPoint remoteAddress, EndPoint localAddress)
Expand Down
12 changes: 6 additions & 6 deletions test/DotNetty.Buffers.Tests/ByteBufferAllocatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public void Buffer(bool preferDirect)
}

[Theory]
[InlineData(true, 8)]
[InlineData(false, 8)]
[InlineData(true, 16)]
[InlineData(false, 16)]
public void BufferWithCapacity(bool preferDirect, int maxCapacity)
{
IByteBufferAllocator allocator = this.NewAllocator(preferDirect);
Expand Down Expand Up @@ -69,8 +69,8 @@ public void HeapBuffer(bool preferDirect)
protected abstract bool IsDirectExpected(bool preferDirect);

[Theory]
[InlineData(true, 8)]
[InlineData(false, 8)]
[InlineData(true, 16)]
[InlineData(false, 16)]
public void HeapBufferWithCapacity(bool preferDirect, int maxCapacity)
{
IByteBufferAllocator allocator = this.NewAllocator(preferDirect);
Expand Down Expand Up @@ -103,8 +103,8 @@ public void DirectBuffer(bool preferDirect)
}

[Theory]
[InlineData(true, 8)]
[InlineData(false, 8)]
[InlineData(true, 16)]
[InlineData(false, 16)]
public void DirectBufferWithCapacity(bool preferDirect, int maxCapacity)
{
IByteBufferAllocator allocator = this.NewAllocator(preferDirect);
Expand Down
Loading

0 comments on commit 53a2935

Please sign in to comment.