From c013c1d7881c234c40828efba01f0fa598386ccf Mon Sep 17 00:00:00 2001 From: Paul Kotlyar Date: Wed, 1 May 2019 08:55:16 -0500 Subject: [PATCH] fixed null and empty fullbulkstring encoding --- .../Messages/FullBulkStringRedisMessage.cs | 72 +++---------------- .../RedisEncoderTests.cs | 14 ++++ 2 files changed, 23 insertions(+), 63 deletions(-) diff --git a/src/DotNetty.Codecs.Redis/Messages/FullBulkStringRedisMessage.cs b/src/DotNetty.Codecs.Redis/Messages/FullBulkStringRedisMessage.cs index 809473c21..f54ce5633 100644 --- a/src/DotNetty.Codecs.Redis/Messages/FullBulkStringRedisMessage.cs +++ b/src/DotNetty.Codecs.Redis/Messages/FullBulkStringRedisMessage.cs @@ -10,15 +10,21 @@ namespace DotNetty.Codecs.Redis.Messages public sealed class FullBulkStringRedisMessage : DefaultByteBufferHolder, IFullBulkStringRedisMessage { - public static readonly IFullBulkStringRedisMessage Null = new NullFullBulkStringRedisMessage(); - public static readonly IFullBulkStringRedisMessage Empty = new EmptyFullBulkStringRedisMessage(); + public static readonly FullBulkStringRedisMessage Null = new FullBulkStringRedisMessage(true); + public static readonly FullBulkStringRedisMessage Empty = new FullBulkStringRedisMessage(false); public FullBulkStringRedisMessage(IByteBuffer content) : base(content) { } + - public bool IsNull => false; + public FullBulkStringRedisMessage(bool isNull) + : base(Unpooled.Empty) + { + this.IsNull = isNull; + } + public bool IsNull { get; private set; } public override string ToString() => new StringBuilder(StringUtil.SimpleClassName(this)) @@ -28,66 +34,6 @@ public override string ToString() => .Append(']') .ToString(); - sealed class NullFullBulkStringRedisMessage : IFullBulkStringRedisMessage - { - public bool IsNull => true; - - public IByteBuffer Content => Unpooled.Empty; - - public IByteBufferHolder Copy() => this; - - public IByteBufferHolder Duplicate() => this; - - public IByteBufferHolder RetainedDuplicate() => this; - - public int ReferenceCount => 1; - - public IReferenceCounted Retain() => this; - - public IReferenceCounted Retain(int increment) => this; - - - public IByteBufferHolder Replace(IByteBuffer content) => this; - - public IReferenceCounted Touch() => this; - - public IReferenceCounted Touch(object hint) => this; - - public bool Release() => false; - - public bool Release(int decrement) => false; - } - - sealed class EmptyFullBulkStringRedisMessage : IFullBulkStringRedisMessage - { - public bool IsNull => false; - - public IByteBuffer Content => Unpooled.Empty; - - public IByteBufferHolder Copy() => this; - - public IByteBufferHolder Duplicate() => this; - - public IByteBufferHolder RetainedDuplicate() => this; - - public int ReferenceCount => 1; - - public IReferenceCounted Retain() => this; - - public IReferenceCounted Retain(int increment) => this; - - - public IByteBufferHolder Replace(IByteBuffer content) => this; - - public IReferenceCounted Touch() => this; - - public IReferenceCounted Touch(object hint) => this; - - public bool Release() => false; - - public bool Release(int decrement) => false; - } - public override IByteBufferHolder Replace(IByteBuffer content) => new FullBulkStringRedisMessage(content); } } \ No newline at end of file diff --git a/test/DotNetty.Codecs.Redis.Tests/RedisEncoderTests.cs b/test/DotNetty.Codecs.Redis.Tests/RedisEncoderTests.cs index 57ce0678e..50770f372 100644 --- a/test/DotNetty.Codecs.Redis.Tests/RedisEncoderTests.cs +++ b/test/DotNetty.Codecs.Redis.Tests/RedisEncoderTests.cs @@ -93,6 +93,20 @@ public void EncodeFullBulkString() written.Release(); } + [Fact] + public void EncodeNullFullBulkString() + { + + + var msg = FullBulkStringRedisMessage.Null; + Assert.True(this.channel.WriteOutbound(msg)); + + IByteBuffer written = ReadAll(this.channel); + Assert.Equal(BytesOf("$-1\r\n"), BytesOf(written)); + + } + + [Fact] public void EncodeSimpleArray() {