Skip to content

Add ReadOnlyMemory overload to BasicPublish #788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion projects/Apigen/apigen/Apigen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,10 +1108,14 @@ public void MaybeEmitModelMethod(MethodInfo method)

public string SanitisedFullName(Type t)
{
if (t.FullName.StartsWith("System.Collections.Generic.IDictionary`2[[System.String"))
if (t.Equals(typeof(IDictionary<string, object>)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous approach had a potential bug in that it would treat any IDictionary<string, T> as IDictionary<string, object>, so this is definitely better.

{
return "IDictionary<string, object>";
}
if (t.Equals(typeof(ReadOnlyMemory<byte>)))
{
return "ReadOnlyMemory<byte>";
}

switch (t.FullName)
{
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/api/IModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ string BasicConsume(
/// </remarks>
[AmqpMethodDoNotImplement(null)]
void BasicPublish(string exchange, string routingKey, bool mandatory,
IBasicProperties basicProperties, byte[] body);
IBasicProperties basicProperties, ReadOnlyMemory<byte> body);

/// <summary>
/// Configures QoS parameters of the Basic content-class.
Expand Down
7 changes: 4 additions & 3 deletions projects/RabbitMQ.Client/client/api/IModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
//---------------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace RabbitMQ.Client
Expand Down Expand Up @@ -88,7 +89,7 @@ public static string BasicConsume(this IModel model, string queue,
/// <remarks>
/// The publication occurs with mandatory=false and immediate=false.
/// </remarks>
public static void BasicPublish(this IModel model, PublicationAddress addr, IBasicProperties basicProperties, byte[] body)
public static void BasicPublish(this IModel model, PublicationAddress addr, IBasicProperties basicProperties, ReadOnlyMemory<byte> body)
{
model.BasicPublish(addr.ExchangeName, addr.RoutingKey, basicProperties: basicProperties, body: body);
}
Expand All @@ -99,15 +100,15 @@ public static void BasicPublish(this IModel model, PublicationAddress addr, IBas
/// <remarks>
/// The publication occurs with mandatory=false
/// </remarks>
public static void BasicPublish(this IModel model, string exchange, string routingKey, IBasicProperties basicProperties, byte[] body)
public static void BasicPublish(this IModel model, string exchange, string routingKey, IBasicProperties basicProperties, ReadOnlyMemory<byte> body)
{
model.BasicPublish(exchange, routingKey, false, basicProperties, body);
}

/// <summary>
/// (Spec method) Convenience overload of BasicPublish.
/// </summary>
public static void BasicPublish(this IModel model, string exchange, string routingKey, bool mandatory = false, IBasicProperties basicProperties = null, byte[] body = null)
public static void BasicPublish(this IModel model, string exchange, string routingKey, bool mandatory = false, IBasicProperties basicProperties = null, ReadOnlyMemory<byte> body = default)
{
model.BasicPublish(exchange, routingKey, mandatory, basicProperties, body);
}
Expand Down
4 changes: 2 additions & 2 deletions projects/RabbitMQ.Client/client/impl/AutorecoveringModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ public void _Private_BasicPublish(string exchange,
string routingKey,
bool mandatory,
IBasicProperties basicProperties,
byte[] body)
ReadOnlyMemory<byte> body)
{
if (routingKey == null)
{
Expand Down Expand Up @@ -806,7 +806,7 @@ public void BasicPublish(string exchange,
string routingKey,
bool mandatory,
IBasicProperties basicProperties,
byte[] body)
ReadOnlyMemory<byte> body)
{
if (routingKey == null)
{
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/IFullModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ void _Private_BasicPublish(string exchange,
string routingKey,
bool mandatory,
[AmqpContentHeaderMapping] IBasicProperties basicProperties,
[AmqpContentBodyMapping] byte[] body);
[AmqpContentBodyMapping] ReadOnlyMemory<byte> body);

[AmqpForceOneWay]
[AmqpMethodMapping(null, "basic", "recover")]
Expand Down
6 changes: 3 additions & 3 deletions projects/RabbitMQ.Client/client/impl/ModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public MethodBase ModelRpc(MethodBase method, ContentHeaderBase header, byte[] b
}
}

public void ModelSend(MethodBase method, ContentHeaderBase header, byte[] body)
public void ModelSend(MethodBase method, ContentHeaderBase header, ReadOnlyMemory<byte> body)
{
if (method.HasContent)
{
Expand Down Expand Up @@ -905,7 +905,7 @@ public abstract void _Private_BasicPublish(string exchange,
string routingKey,
bool mandatory,
IBasicProperties basicProperties,
byte[] body);
ReadOnlyMemory<byte> body);

public abstract void _Private_BasicRecover(bool requeue);

Expand Down Expand Up @@ -1100,7 +1100,7 @@ public void BasicPublish(string exchange,
string routingKey,
bool mandatory,
IBasicProperties basicProperties,
byte[] body)
ReadOnlyMemory<byte> body)
{
if (routingKey == null)
{
Expand Down
8 changes: 4 additions & 4 deletions projects/Unit/APIApproval.Approve.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ namespace RabbitMQ.Client
string BasicConsume(string queue, bool autoAck, string consumerTag, bool noLocal, bool exclusive, System.Collections.Generic.IDictionary<string, object> arguments, RabbitMQ.Client.IBasicConsumer consumer);
RabbitMQ.Client.BasicGetResult BasicGet(string queue, bool autoAck);
void BasicNack(ulong deliveryTag, bool multiple, bool requeue);
void BasicPublish(string exchange, string routingKey, bool mandatory, RabbitMQ.Client.IBasicProperties basicProperties, byte[] body);
void BasicPublish(string exchange, string routingKey, bool mandatory, RabbitMQ.Client.IBasicProperties basicProperties, System.ReadOnlyMemory<byte> body);
void BasicQos(uint prefetchSize, ushort prefetchCount, bool global);
void BasicRecover(bool requeue);
void BasicRecoverAsync(bool requeue);
Expand Down Expand Up @@ -398,9 +398,9 @@ namespace RabbitMQ.Client
public static string BasicConsume(this RabbitMQ.Client.IModel model, string queue, bool autoAck, string consumerTag, RabbitMQ.Client.IBasicConsumer consumer) { }
public static string BasicConsume(this RabbitMQ.Client.IModel model, string queue, bool autoAck, string consumerTag, System.Collections.Generic.IDictionary<string, object> arguments, RabbitMQ.Client.IBasicConsumer consumer) { }
public static string BasicConsume(this RabbitMQ.Client.IModel model, RabbitMQ.Client.IBasicConsumer consumer, string queue, bool autoAck = false, string consumerTag = "", bool noLocal = false, bool exclusive = false, System.Collections.Generic.IDictionary<string, object> arguments = null) { }
public static void BasicPublish(this RabbitMQ.Client.IModel model, RabbitMQ.Client.PublicationAddress addr, RabbitMQ.Client.IBasicProperties basicProperties, byte[] body) { }
public static void BasicPublish(this RabbitMQ.Client.IModel model, string exchange, string routingKey, RabbitMQ.Client.IBasicProperties basicProperties, byte[] body) { }
public static void BasicPublish(this RabbitMQ.Client.IModel model, string exchange, string routingKey, bool mandatory = false, RabbitMQ.Client.IBasicProperties basicProperties = null, byte[] body = null) { }
public static void BasicPublish(this RabbitMQ.Client.IModel model, RabbitMQ.Client.PublicationAddress addr, RabbitMQ.Client.IBasicProperties basicProperties, System.ReadOnlyMemory<byte> body) { }
public static void BasicPublish(this RabbitMQ.Client.IModel model, string exchange, string routingKey, RabbitMQ.Client.IBasicProperties basicProperties, System.ReadOnlyMemory<byte> body) { }
public static void BasicPublish(this RabbitMQ.Client.IModel model, string exchange, string routingKey, bool mandatory = false, RabbitMQ.Client.IBasicProperties basicProperties = null, System.ReadOnlyMemory<byte> body = default) { }
public static void ExchangeBind(this RabbitMQ.Client.IModel model, string destination, string source, string routingKey, System.Collections.Generic.IDictionary<string, object> arguments = null) { }
public static void ExchangeBindNoWait(this RabbitMQ.Client.IModel model, string destination, string source, string routingKey, System.Collections.Generic.IDictionary<string, object> arguments = null) { }
public static void ExchangeDeclare(this RabbitMQ.Client.IModel model, string exchange, string type, bool durable = false, bool autoDelete = false, System.Collections.Generic.IDictionary<string, object> arguments = null) { }
Expand Down
76 changes: 76 additions & 0 deletions projects/Unit/TestBasicPublish.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using RabbitMQ.Client.Events;

namespace RabbitMQ.Client.Unit
{
[TestFixture]
public class TestBasicPublish
{
[Test]
public void TestBasicRoundtripArray()
{
var cf = new ConnectionFactory();
using(IConnection c = cf.CreateConnection())
using(IModel m = c.CreateModel())
{
QueueDeclareOk q = m.QueueDeclare();
IBasicProperties bp = m.CreateBasicProperties();
byte[] sendBody = System.Text.Encoding.UTF8.GetBytes("hi");
byte[] consumeBody = null;
var consumer = new EventingBasicConsumer(m);
var are = new AutoResetEvent(false);
consumer.Received += async (o, a) =>
{
consumeBody = a.Body.ToArray();
are.Set();
await Task.Yield();
};
string tag = m.BasicConsume(q.QueueName, true, consumer);


m.BasicPublish("", q.QueueName, bp, sendBody);
bool waitResFalse = are.WaitOne(2000);
m.BasicCancel(tag);


Assert.IsTrue(waitResFalse);
Assert.AreEqual(sendBody, consumeBody);
}
}

[Test]
public void TestBasicRoundtripReadOnlyMemory()
{
var cf = new ConnectionFactory();
using(IConnection c = cf.CreateConnection())
using(IModel m = c.CreateModel())
{
QueueDeclareOk q = m.QueueDeclare();
IBasicProperties bp = m.CreateBasicProperties();
byte[] sendBody = System.Text.Encoding.UTF8.GetBytes("hi");
byte[] consumeBody = null;
var consumer = new EventingBasicConsumer(m);
var are = new AutoResetEvent(false);
consumer.Received += async (o, a) =>
{
consumeBody = a.Body.ToArray();
are.Set();
await Task.Yield();
};
string tag = m.BasicConsume(q.QueueName, true, consumer);


m.BasicPublish("", q.QueueName, bp, new ReadOnlyMemory<byte>(sendBody));
bool waitResFalse = are.WaitOne(2000);
m.BasicCancel(tag);


Assert.IsTrue(waitResFalse);
Assert.AreEqual(sendBody, consumeBody);
}
}
}
}