Skip to content
This repository
Browse code

Added tests to assert catching an exception when a queue is declared …

…(this will most likely have to be rolled back).
  • Loading branch information...
commit f62c70bacd0b144b129a90e6ebed5934231adefe 1 parent d6ce6c5
Oliver, Jonathan authored
35 src/proj/NanoMessageBus.RabbitChannel/RabbitChannelGroupConfiguration.cs
@@ -22,29 +22,31 @@ public virtual void ConfigureChannel(IModel channel)
22 22 protected virtual void DeclareSystemExchange(IModel channel, PublicationAddress address)
23 23 {
24 24 if (this.DispatchOnly || address == null)
25   - return;
26   -
27   - TryDeclareExchange(channel, address.ExchangeName, address.ExchangeType);
28   - channel.QueueDeclare(address.ExchangeName, true, false, false, null);
  25 + return;
  26 +
  27 + TryDeclare(address.ExchangeName, name => channel.ExchangeDeclare(
  28 + name, address.ExchangeType, true, true, null));
  29 + TryDeclare(address.ExchangeName, name => channel.QueueDeclare(
  30 + name, true, false, false, null));
29 31 channel.QueueBind(address.ExchangeName, address.ExchangeName, address.RoutingKey, null);
30 32 }
31 33 protected virtual void DeclareExchanges(IModel channel)
32 34 {
33 35 foreach (var name in this.MessageTypes.Select(x => x.FullName.NormalizeName()))
34   - TryDeclareExchange(channel, name, ExchangeType.Fanout);
  36 + TryDeclare(name, x => channel.ExchangeDeclare(x, ExchangeType.Fanout, true, true, null));
35 37 }
36   - private static void TryDeclareExchange(IModel channel, string name, string exchangeType)
  38 + private static void TryDeclare(string resource, Action<string> callback)
37 39 {
38 40 try
39 41 {
40   - channel.ExchangeDeclare(name, exchangeType, true, true, null);
  42 + callback(resource);
41 43 }
42 44 catch (OperationInterruptedException e)
43 45 {
44   - if (e.ShutdownReason.ReplyCode != ExchangeRedeclaration)
  46 + if (e.ShutdownReason.ReplyCode != RedeclarationFailed)
45 47 throw;
46 48
47   - Log.Info("The exchange '{0}' has already been declared using different parameters.", name);
  49 + Log.Info("The resource '{0}' has already been declared using different parameters.", resource);
48 50 }
49 51 }
50 52 protected virtual void DeclareQueue(IModel channel)
@@ -53,11 +55,14 @@ protected virtual void DeclareQueue(IModel channel)
53 55 return;
54 56
55 57 var declarationArgs = new Hashtable();
  58 + declarationArgs["x-expires"] = TimeSpan.FromDays(7).TotalMilliseconds;
  59 +
56 60 if (this.DeadLetterExchange != null)
57   - declarationArgs[DeadLetterExchangeDeclaration] = this.DeadLetterExchange.ExchangeName;
58   -
59   - var declaration = channel.QueueDeclare(
60   - this.InputQueue, this.DurableQueue, this.ExclusiveQueue, this.AutoDelete, declarationArgs);
  61 + declarationArgs[DeadLetterExchangeDeclaration] = this.DeadLetterExchange.ExchangeName;
  62 +
  63 + QueueDeclareOk declaration = null;
  64 + TryDeclare(this.InputQueue, x => declaration = channel.QueueDeclare(
  65 + x, this.DurableQueue, this.ExclusiveQueue, this.AutoDelete, declarationArgs));
61 66
62 67 if (declaration != null)
63 68 this.InputQueue = declaration.QueueName;
@@ -69,7 +74,7 @@ protected virtual void DeclareQueue(IModel channel)
69 74 channel.QueuePurge(this.InputQueue);
70 75
71 76 channel.BasicQos(0, (ushort)this.ChannelBuffer, false);
72   - }
  77 + }
73 78 protected virtual void BindQueue(IModel channel)
74 79 {
75 80 if (!this.DispatchOnly)
@@ -328,7 +333,7 @@ public RabbitChannelGroupConfiguration()
328 333 private const int DefaultWorkerCount = 1;
329 334 private const int DefaultMaxAttempts = 3;
330 335 private const int DefaultChannelBuffer = 1024;
331   - private const int ExchangeRedeclaration = 406;
  336 + private const int RedeclarationFailed = 406;
332 337 private const string DefaultGroupName = "unnamed-group";
333 338 private const string DefaultReturnAddressFormat = "direct://default/{0}";
334 339 private const string DefaultPoisonMessageExchange = "poison-messages";
42 src/tests/NanoMessageBus.RabbitChannel.UnitTests/RabbitChannelGroupConfigurationTests.cs
@@ -535,10 +535,17 @@ public class when_no_serializer_is_specified : using_channel_config
535 535 [Subject(typeof(RabbitChannelGroupConfiguration))]
536 536 public class when_configuring_a_new_receive_channel : using_channel_config
537 537 {
538   - Establish context = () => config
539   - .WithInputQueue("some-queue")
540   - .WithPoisonMessageExchange("poison-msgs")
541   - .WithDeadLetterExchange("dead-letters");
  538 + Establish context = () =>
  539 + {
  540 + mockChannel
  541 + .Setup(x => x.QueueDeclare("some-queue", true, false, false, Moq.It.IsAny<IDictionary>()))
  542 + .Callback<string, bool, bool, bool, IDictionary>((a, b, c, d, e) => queueDeclarationArgs = e);
  543 +
  544 + config
  545 + .WithInputQueue("some-queue")
  546 + .WithPoisonMessageExchange("poison-msgs")
  547 + .WithDeadLetterExchange("dead-letters");
  548 + };
542 549
543 550 Because of = () => Configure();
544 551
@@ -562,6 +569,33 @@ public class when_configuring_a_new_receive_channel : using_channel_config
562 569
563 570 It should_bind_dead_letter_exchange_and_queue = () =>
564 571 mockChannel.Verify(x => x.QueueBind("dead-letters", "dead-letters", string.Empty, null));
  572 +
  573 + It should_set_the_queue_expiration_to_7_days = () =>
  574 + queueDeclarationArgs["x-expires"].ShouldEqual(TimeSpan.FromDays(7).TotalMilliseconds);
  575 +
  576 + static IDictionary queueDeclarationArgs;
  577 + }
  578 +
  579 + [Subject(typeof(RabbitChannelGroupConfiguration))]
  580 + public class when_redeclaring_the_setting_for_a_queue : using_channel_config
  581 + {
  582 + Establish context = () =>
  583 + {
  584 + const int Redeclaration = 406;
  585 + var reason = new ShutdownEventArgs(ShutdownInitiator.Peer, Redeclaration, string.Empty);
  586 + var exception = new OperationInterruptedException(reason);
  587 +
  588 + config.WithInputQueue("some-queue");
  589 + mockChannel
  590 + .Setup(x => x.QueueDeclare("some-queue", true, false, false, Moq.It.IsAny<IDictionary>()))
  591 + .Throws(exception);
  592 + };
  593 +
  594 + Because of = () =>
  595 + Try(Configure);
  596 +
  597 + It should_NOT_throw_an_exception = () =>
  598 + thrown.ShouldBeNull();
565 599 }
566 600
567 601 [Subject(typeof(RabbitChannelGroupConfiguration))]

0 comments on commit f62c70b

Please sign in to comment.
Something went wrong with that request. Please try again.