Skip to content
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

incorrect implementation of RabbitMqQueueOptionsBuilder.SetAutoDelete() #57

Closed
wants to merge 1 commit into from
Closed
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
124 changes: 87 additions & 37 deletions Rebus.RabbitMq.Tests/RabbitMqCreateQueueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,119 @@ namespace Rebus.RabbitMq.Tests
[TestFixture]
public class RabbitMqCreateQueueTest : FixtureBase
{
readonly string _testQueue1 = TestConfig.GetName("test_queue_1");

protected override void SetUp()
[Test]
public void Test_CreateQueue_WHEN_InputQueueOptions_AutoDelete_False_AND_TTL0_THEN_BusCanStart_()
{
RabbitMqTransportFactory.DeleteQueue(_testQueue1);
using (var testScope = new QeueuNameTestScope())
{
var activator = Using(new BuiltinHandlerActivator());
var configurer = Configure.With(activator)
.Transport(t =>
{
t.UseRabbitMq(RabbitMqTransportFactory.ConnectionString, testScope.QeueuName)
.InputQueueOptions(o => o.SetAutoDelete(false))
.AddClientProperties(new Dictionary<string, string> {
{ "description", "CreateQueue_With_AutoDelete test in RabbitMqCreateQueueTest.cs" }
});
});

using (var bus = configurer.Start())
{
Assert.IsTrue(bus.Advanced.Workers.Count > 0);
}

Thread.Sleep(5000);
Assert.IsTrue(RabbitMqTransportFactory.QueueExists(testScope.QeueuName));
}
}

[Test]
public void Test_CreateQueue_WHEN_InputQueueOptions_AutoDelete_False_AND_TTL0_THEN_BusCanStart()
public void Test_CreateQueue_WHEN_InputQueueOptions_AutoDelete_True_THEN_BusCanStart()
{
var activator = Using(new BuiltinHandlerActivator());
var configurer = Configure.With(activator)
.Transport(t =>
{
t.UseRabbitMq(RabbitMqTransportFactory.ConnectionString, _testQueue1)
.InputQueueOptions(o => o.SetAutoDelete(false))
.AddClientProperties(new Dictionary<string, string> {
using (var testScope = new QeueuNameTestScope())
{
var activator = Using(new BuiltinHandlerActivator());
var configurer = Configure.With(activator)
.Transport(t =>
{
t.UseRabbitMq(RabbitMqTransportFactory.ConnectionString, testScope.QeueuName)
.InputQueueOptions(o => o.SetAutoDelete(true))
.AddClientProperties(new Dictionary<string, string> {
{ "description", "CreateQueue_With_AutoDelete test in RabbitMqCreateQueueTest.cs" }
});
});
var bus = configurer.Start();
});
});

Assert.IsTrue(bus.Advanced.Workers.Count > 0);
using (var bus = configurer.Start())
{
Assert.IsTrue(bus.Advanced.Workers.Count > 0);
}
}
}


[Test]
public void Test_CreateQueue_WHEN_InputQueueOptions_AutoDelete_True_AND_TTL_Positive_THEN_BusCanStart()
public void Test_CreateQueue_WHEN_InputQueueOptions_SetQueueTTL_0_THEN_ArgumentException()
{
var activator = Using(new BuiltinHandlerActivator());
var configurer = Configure.With(activator)
.Transport(t =>
{
t.UseRabbitMq(RabbitMqTransportFactory.ConnectionString, _testQueue1)
.InputQueueOptions(o => o.SetAutoDelete(true, 1))
.AddClientProperties(new Dictionary<string, string> {
using (var testScope = new QeueuNameTestScope())
{
var activator = Using(new BuiltinHandlerActivator());

Assert.Throws<ArgumentException>(() =>
{
var configurer = Configure.With(activator)
.Transport(t =>
{
t.UseRabbitMq(RabbitMqTransportFactory.ConnectionString, testScope.QeueuName)
.InputQueueOptions(o => o.SetQueueTTL(0).SetDurable(false))
.AddClientProperties(new Dictionary<string, string> {
{ "description", "CreateQueue_With_AutoDelete test in RabbitMqCreateQueueTest.cs" }
});
});
});

var bus = configurer.Start();

Assert.IsTrue(bus.Advanced.Workers.Count > 0);
}
, "Time must be in milliseconds and greater than 0");
}
}

[Test]
public void Test_CreateQueue_WHEN_InputQueueOptions_AutoDelete_True_AND_TTL_0_THEN_ArgumentExceptionThrown()
public void Test_CreateQueue_WHEN_InputQueueOptions_SetQueueTTL_5000_THEN_QueueIsDeleted_WHEN_5000msAfterConnectionClosed()
{
var activator = Using(new BuiltinHandlerActivator());

Assert.Throws<ArgumentException>(() =>
using (var testScope = new QeueuNameTestScope())
{
var activator = Using(new BuiltinHandlerActivator());

var configurer = Configure.With(activator)
.Transport(t =>
{
t.UseRabbitMq(RabbitMqTransportFactory.ConnectionString, _testQueue1)
.InputQueueOptions(o => o.SetAutoDelete(true, 0))
t.UseRabbitMq(RabbitMqTransportFactory.ConnectionString, testScope.QeueuName)
.InputQueueOptions(o => o.SetQueueTTL(100))
.AddClientProperties(new Dictionary<string, string> {
{ "description", "CreateQueue_With_AutoDelete test in RabbitMqCreateQueueTest.cs" }
});
});

using (var bus = configurer.Start())
{
Assert.IsTrue(bus.Advanced.Workers.Count > 0);
}

Thread.Sleep(5000);
Assert.IsFalse(RabbitMqTransportFactory.QueueExists(testScope.QeueuName));
}
}

class QeueuNameTestScope : IDisposable
{
public string QeueuName { get; }

public QeueuNameTestScope()
{
QeueuName = Guid.NewGuid().ToString();
}

public void Dispose()
{
RabbitMqTransportFactory.DeleteQueue(QeueuName);
}
, "Time must be in milliseconds and greater than 0");
}

}
}
22 changes: 20 additions & 2 deletions Rebus.RabbitMq.Tests/RabbitMqTransportFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ public static void DeleteQueue(string queueName)
}
}

public static bool QueueExists(string queueName)
{
var connectionFactory = new ConnectionFactory { Uri = new Uri(ConnectionString) };
using (var connection = connectionFactory.CreateConnection())
using (var model = connection.CreateModel())
{
try
{
model.QueueDeclarePassive(queueName);
return true;
}
catch (RabbitMQ.Client.Exceptions.OperationInterruptedException)
{
return false;
}
}
}

public static void DeleteExchange(string exchangeName)
{
var connectionFactory = new ConnectionFactory { Uri = new Uri(ConnectionString) };
Expand Down Expand Up @@ -109,9 +127,9 @@ public static bool ExchangeExists(string exchangeName)
}
}
}

protected virtual RabbitMqTransport CreateRabbitMqTransport(string inputQueueAddress)
{
{
return new RabbitMqTransport(ConnectionString, inputQueueAddress, new ConsoleLoggerFactory(false));
}
}
Expand Down
38 changes: 31 additions & 7 deletions Rebus.RabbitMq/Config/RabbitMqQueueOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,43 @@ public RabbitMqQueueOptionsBuilder SetExclusive(bool exclusive)
}

/// <summary>
/// Set auto delete, when last consumer disconnects
/// <param name="autoDelete">Whether queue should be deleted</param>
/// <param name="ttlInMs">Time to live (in milliseconds) after last subscriber disconnects</param>
/// Set auto-delete propery when declaring the queue
/// <param name="autoDelete">Whether queue should be deleted when the last consumer unsubscribes</param>
/// </summary>
public RabbitMqQueueOptionsBuilder SetAutoDelete(bool autoDelete, long ttlInMs = 0)
public RabbitMqQueueOptionsBuilder SetAutoDelete(bool autoDelete)
{
AutoDelete = autoDelete;
return this;
}

if (AutoDelete && ttlInMs <= 0)
/// <summary>
/// Configure for how long a queue can be unused before it is automatically deleted by setting x-expires argument
/// </summary>
/// <param name="ttlInMs">expiration period in milliseconds, </param>
/// <exception cref="ArgumentException">if the argumnet value is 0 or less</exception>
public RabbitMqQueueOptionsBuilder SetQueueTTL(long ttlInMs)
{
if (ttlInMs <= 0)
throw new ArgumentException("Time must be in milliseconds and greater than 0", nameof(ttlInMs));

if (AutoDelete)
Arguments.Add("x-expires", ttlInMs);
Arguments.Add("x-expires", ttlInMs);

return this;
}


/// <summary>
/// Set auto delete, when last consumer disconnects and/or how long queue can stay unused until it is deleted as expired.
/// Zero or negative values of ttlInMs are ignored (no queue expiration).
/// <param name="autoDelete">Whether queue should be deleted</param>
/// <param name="ttlInMs">Time to live (in milliseconds) after last subscriber disconnects</param>
/// </summary>
public RabbitMqQueueOptionsBuilder SetAutoDelete(bool autoDelete, long ttlInMs = 0)
{
SetAutoDelete(autoDelete);

if (ttlInMs > 0)
SetQueueTTL(ttlInMs);

return this;
}
Expand Down