Skip to content

Commit 9c6e4dd

Browse files
committed
* Get lazy function of AmqpString working and start to test it.
1 parent bd0a742 commit 9c6e4dd

File tree

4 files changed

+62
-26
lines changed

4 files changed

+62
-26
lines changed

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

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,25 @@
3535

3636
namespace RabbitMQ.Client
3737
{
38-
// TODO lazy string value
3938
public abstract class AmqpString : IEquatable<AmqpString>, IComparable<AmqpString>
4039
{
41-
private readonly string _value;
40+
private string _value;
41+
private readonly Encoding _encoding;
4242
private readonly ReadOnlyMemory<byte> _stringBytes;
4343
private readonly int _byteCount;
4444

4545
protected AmqpString()
4646
{
4747
_value = string.Empty;
48+
_encoding = Encoding.UTF8;
4849
_stringBytes = ReadOnlyMemory<byte>.Empty;
50+
_byteCount = 0;
4951
}
5052

5153
public AmqpString(ReadOnlyMemory<byte> stringBytes)
5254
{
55+
_value = null;
56+
_encoding = Encoding.UTF8;
5357
_stringBytes = stringBytes;
5458
_byteCount = _stringBytes.Length;
5559
}
@@ -63,6 +67,8 @@ public AmqpString(string value, ushort maxLen, Encoding encoding,
6367
public AmqpString(string value, ushort maxLen, Encoding encoding, string validatorRegex,
6468
bool strictValidation = false)
6569
{
70+
_encoding = encoding;
71+
6672
/*
6773
* Note:
6874
* RabbitMQ does hardly any validation for names, only stripping off CR/LF
@@ -100,6 +106,8 @@ public AmqpString(string value, ushort maxLen, Encoding encoding, string validat
100106

101107
public int ByteCount => _byteCount;
102108

109+
internal bool HasString => _value != null;
110+
103111
public bool IsEmpty
104112
{
105113
get
@@ -117,25 +125,17 @@ public bool IsEmpty
117125

118126
public bool Contains(string value)
119127
{
120-
if (_value is null)
121-
{
122-
// TODO
123-
return false;
124-
}
125-
else
126-
{
127-
return _value.Contains(value);
128-
}
128+
return Value.Contains(value);
129129
}
130130

131131
public override string ToString()
132132
{
133-
return _value;
133+
return Value;
134134
}
135135

136136
public static implicit operator string(AmqpString amqpString)
137137
{
138-
return amqpString._value;
138+
return amqpString.ToString();
139139
}
140140

141141
public static implicit operator ReadOnlyMemory<byte>(AmqpString amqpString)
@@ -171,26 +171,33 @@ public override bool Equals(object obj)
171171

172172
public bool Equals(AmqpString other)
173173
{
174-
return _value == other._value;
174+
if (_value == null)
175+
{
176+
throw new InvalidOperationException("TODO");
177+
}
178+
else
179+
{
180+
return _value == other._value;
181+
}
175182
}
176183

177184
public override int GetHashCode()
178185
{
179-
return _value.GetHashCode();
180-
}
181-
182-
public int CompareTo(AmqpString other)
183-
{
184-
if (_value is null)
186+
if (_value == null)
185187
{
186-
throw new InvalidOperationException("[CRITICAL] should not see this");
188+
throw new InvalidOperationException("TODO");
187189
}
188190
else
189191
{
190-
return _value.CompareTo(other._value);
192+
return _value.GetHashCode();
191193
}
192194
}
193195

196+
public int CompareTo(AmqpString other)
197+
{
198+
return Value.CompareTo(other.Value);
199+
}
200+
194201
public static bool operator ==(AmqpString amqpString1, AmqpString amqpString2)
195202
{
196203
if (amqpString1 is null || amqpString2 is null)
@@ -216,10 +223,23 @@ protected virtual string FixUp(string value)
216223
return value;
217224
}
218225

226+
// TODO remove
219227
private static bool isAscii(string value)
220228
{
221229
return Encoding.UTF8.GetByteCount(value) == value.Length;
222230
}
231+
232+
private string Value
233+
{
234+
get
235+
{
236+
if (_value == null)
237+
{
238+
_value = _encoding.GetString(_stringBytes.ToArray());
239+
}
240+
return _value;
241+
}
242+
}
223243
}
224244

225245
/*

projects/RabbitMQ.Client/client/impl/RabbitMQActivitySource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ internal static Activity Deliver(BasicDeliverEventArgs deliverEventArgs)
155155
if (activity != null && activity.IsAllDataRequested)
156156
{
157157
PopulateMessagingTags("deliver",
158-
routingKey,
158+
deliverEventArgs.RoutingKey,
159159
deliverEventArgs.Exchange,
160160
deliverEventArgs.DeliveryTag,
161161
deliverEventArgs.BasicProperties,

projects/Test/SequentialIntegration/TestActivitySource.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ public async Task TestPublisherAndBasicGetActivityTags(bool useRoutingKeyAsOpera
163163
using (ActivityListener activityListener = StartActivityListener(activities))
164164
{
165165
await Task.Delay(500);
166-
string queue = $"queue-{Guid.NewGuid()}";
166+
QueueName queue = new QueueName($"queue-{Guid.NewGuid()}");
167167
const string msg = "for basic.get";
168168

169169
try
170170
{
171171
await _channel.QueueDeclareAsync(queue, false, false, false, null);
172-
await _channel.BasicPublishAsync(ExchangeName.Empty, queue, Encoding.UTF8.GetBytes(msg), mandatory: true);
172+
await _channel.BasicPublishAsync(ExchangeName.Empty, (RoutingKey)queue, Encoding.UTF8.GetBytes(msg), mandatory: true);
173173
await _channel.WaitForConfirmsOrDieAsync();
174174
QueueDeclareOk ok = await _channel.QueueDeclarePassiveAsync(queue);
175175
Assert.Equal(1u, ok.MessageCount);
@@ -201,7 +201,7 @@ private static ActivityListener StartActivityListener(List<Activity> activities)
201201
return activityListener;
202202
}
203203

204-
private void AssertActivityData(bool useRoutingKeyAsOperationName, string queueName,
204+
private void AssertActivityData(bool useRoutingKeyAsOperationName, QueueName queueName,
205205
List<Activity> activityList, bool isDeliver = false)
206206
{
207207
string childName = isDeliver ? "deliver" : "receive";

projects/Test/Unit/TestAmqpString.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
//---------------------------------------------------------------------------
3131

3232
using System;
33+
using System.Text;
3334
using RabbitMQ.Client;
3435
using Xunit;
3536

@@ -108,5 +109,20 @@ public void TestInvalidConsumerTagThrows(string arg)
108109
{
109110
Assert.Throws<ArgumentOutOfRangeException>(() => new ConsumerTag(arg, strictValidation: true));
110111
}
112+
113+
[Theory]
114+
[InlineData("exchange-ABC:123.abc_456")]
115+
[InlineData("6PiY80XbjBKnY39R947i2s03cAg261412IS1FzS4uEoJJ6cWZ50P0SJ3S4yqvzx0n4TN4NsROlWyEwaUG4I5Glrj1mI2N28QGbkf5t8Kyo7EavaqME5TrvhPxtJGY1p")]
116+
[InlineData("foo_bar_baz")]
117+
[InlineData("exchangename-Евгений")]
118+
public void TestExchangeName_InitializeFromMemoryIsLazy(string arg)
119+
{
120+
byte[] mem = Encoding.UTF8.GetBytes(arg);
121+
var e = new ExchangeName(mem);
122+
Assert.False(e.HasString);
123+
string estr = (string)e;
124+
Assert.True(e.HasString);
125+
Assert.Equal(arg, estr);
126+
}
111127
}
112128
}

0 commit comments

Comments
 (0)