Skip to content

Commit 617dfd0

Browse files
committed
* Ensure AmqpString equality or comparison does not trigger string init
1 parent c608789 commit 617dfd0

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

projects/RabbitMQ.Client/client/api/AmqpString.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ public override bool Equals(object obj)
156156

157157
public bool Equals(AmqpString other)
158158
{
159-
if (_value == null)
159+
if (_value is null || other._value is null)
160160
{
161-
return _stringBytes.Equals(other._stringBytes);
161+
return GetHashCode().Equals(other.GetHashCode());
162162
}
163163
else
164164
{
@@ -168,19 +168,12 @@ public bool Equals(AmqpString other)
168168

169169
public override int GetHashCode()
170170
{
171-
if (_value == null)
172-
{
173-
return ReadOnlyMemoryOfByteEqualityComparer.CalculateHashCode(_stringBytes);
174-
}
175-
else
176-
{
177-
return _value.GetHashCode();
178-
}
171+
return ReadOnlyMemoryOfByteEqualityComparer.CalculateHashCode(_stringBytes);
179172
}
180173

181174
public int CompareTo(AmqpString other)
182175
{
183-
if (_value == null)
176+
if (_value is null || other._value is null)
184177
{
185178
return GetHashCode().CompareTo(other.GetHashCode());
186179
}

projects/RabbitMQ.Client/client/api/ExchangeType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
namespace RabbitMQ.Client
3737
{
3838
/// <summary>
39-
/// Convenience struct providing compile-time names for standard exchange types.
39+
/// Convenience class providing compile-time names for standard exchange types.
4040
/// </summary>
4141
/// <remarks>
4242
/// Use the static members of this class as values for the

projects/Test/Unit/TestAmqpString.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,19 @@ public void TestEqualityWhenUsingReadOnlyMemoryOfByte()
140140
var ex2 = new ExchangeName(b2);
141141
Assert.Equal(ex1, ex2);
142142
}
143+
144+
[Fact]
145+
public void TestEqualityWhenUsingMixOfReadOnlyMemoryOfByteAndString()
146+
{
147+
ReadOnlyMemory<byte> b1 = new byte[] { (byte)'f', (byte)'o', (byte)'o' };
148+
var ex1 = new ExchangeName(b1);
149+
150+
string b2str = "foo";
151+
var ex2 = new ExchangeName(b2str);
152+
153+
Assert.Equal(ex1, ex2);
154+
Assert.Equal(ex1, b2str);
155+
Assert.Equal(ex2, b2str);
156+
}
143157
}
144158
}

0 commit comments

Comments
 (0)