Skip to content

Expose nowait version of BasicCancel #759

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 1 commit into from
Mar 17, 2020
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: 6 additions & 0 deletions projects/client/RabbitMQ.Client/src/client/api/IModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ public interface IModel : IDisposable
[AmqpMethodDoNotImplement(null)]
void BasicCancel(string consumerTag);

/// <summary>
/// Same as BasicCancel but sets nowait to true and returns void (as there
/// will be no response from the server).
/// </summary>
void BasicCancelNoWait(string consumerTag);

/// <summary>Start a Basic content-class consumer.</summary>
[AmqpMethodDoNotImplement(null)]
string BasicConsume(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,16 @@ public void BasicCancel(string consumerTag)
_delegate.BasicCancel(consumerTag);
}

public void BasicCancelNoWait(string consumerTag)
{
RecordedConsumer cons = _connection.DeleteRecordedConsumer(consumerTag);
if (cons != null)
{
_connection.MaybeDeleteRecordedAutoDeleteQueue(cons.Queue);
}
_delegate.BasicCancelNoWait(consumerTag);
}

public string BasicConsume(
string queue,
bool autoAck,
Expand Down
13 changes: 12 additions & 1 deletion projects/client/RabbitMQ.Client/src/client/impl/ModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,7 @@ public void BasicCancel(string consumerTag)
_Private_BasicCancel(consumerTag, false);
k.GetReply(ContinuationTimeout);
}

lock (m_consumers)
{
m_consumers.Remove(consumerTag);
Expand All @@ -1021,6 +1022,16 @@ public void BasicCancel(string consumerTag)
ModelShutdown -= k.m_consumer.HandleModelShutdown;
}

public void BasicCancelNoWait(string consumerTag)
{
_Private_BasicCancel(consumerTag, true);

lock (m_consumers)
{
m_consumers.Remove(consumerTag);
}
}

public string BasicConsume(string queue,
bool autoAck,
string consumerTag,
Expand Down Expand Up @@ -1224,7 +1235,7 @@ public void ExchangeDelete(string exchange,
public void ExchangeDeleteNoWait(string exchange,
bool ifUnused)
{
_Private_ExchangeDelete(exchange, ifUnused, false);
_Private_ExchangeDelete(exchange, ifUnused, true);
}

public void ExchangeUnbind(string destination,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ namespace RabbitMQ.Client
void Abort(ushort replyCode, string replyText);
void BasicAck(ulong deliveryTag, bool multiple);
void BasicCancel(string consumerTag);
void BasicCancelNoWait(string consumerTag);
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);
Expand Down
35 changes: 33 additions & 2 deletions projects/client/Unit/src/unit/TestAsyncConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ namespace RabbitMQ.Client.Unit
[TestFixture]
public class TestAsyncConsumer
{

[Test]
public void TestBasicRoundtrip()
{
Expand Down Expand Up @@ -82,6 +81,38 @@ public void TestBasicRoundtrip()
}
}

[Test]
public void TestBasicRoundtripNoWait()
{
var cf = new ConnectionFactory{ DispatchConsumersAsync = true };
using (IConnection c = cf.CreateConnection())
{
using (IModel m = c.CreateModel())
{
QueueDeclareOk q = m.QueueDeclare();
IBasicProperties bp = m.CreateBasicProperties();
byte[] body = System.Text.Encoding.UTF8.GetBytes("async-hi");
m.BasicPublish("", q.QueueName, bp, body);
var consumer = new AsyncEventingBasicConsumer(m);
var are = new AutoResetEvent(false);
consumer.Received += async (o, a) =>
{
are.Set();
await Task.Yield();
};
string tag = m.BasicConsume(q.QueueName, true, consumer);
// ensure we get a delivery
bool waitRes = are.WaitOne(2000);
Assert.IsTrue(waitRes);
// unsubscribe and ensure no further deliveries
m.BasicCancelNoWait(tag);
m.BasicPublish("", q.QueueName, bp, body);
bool waitResFalse = are.WaitOne(2000);
Assert.IsFalse(waitResFalse);
}
}
}

[Test]
public void NonAsyncConsumerShouldThrowInvalidOperationException()
{
Expand All @@ -98,4 +129,4 @@ public void NonAsyncConsumerShouldThrowInvalidOperationException()
}
}
}
}
}