public When_an_action_is_performed_on_a_closed_channel_that_then_opens() { persistentConnection = Substitute.For <IPersistentConnection>(); channel = Substitute.For <IModel>(); var eventBus = new EventBus(); var configuration = new ConnectionConfiguration(); var shutdownArgs = new ShutdownEventArgs( ShutdownInitiator.Peer, AmqpException.ConnectionClosed, "connection closed by peer"); var exception = new OperationInterruptedException(shutdownArgs); persistentConnection.CreateModel().Returns(x => { throw exception; }, x => channel, x => channel); var logger = Substitute.For <IEasyNetQLogger>(); persistentChannel = new PersistentChannel(persistentConnection, logger, configuration, eventBus); new Timer(_ => eventBus.Publish(new ConnectionCreatedEvent()), null, 10, Timeout.Infinite); persistentChannel.InvokeChannelAction(x => x.ExchangeDeclare("MyExchange", "direct")); }
public void Can_consume_messages_with_BasicConsumer() { using (IConnection connection = mqFactory.CreateConnection()) using (IModel channel = connection.CreateModel()) { OperationInterruptedException lastEx = null; channel.Close(); ThreadPool.QueueUserWorkItem(_ => { try { PublishHelloRabbit(channel); } catch (Exception ex) { lastEx = ex as OperationInterruptedException; "Caught {0}: {1}".Print(ex.GetType().Name, ex); } }); Thread.Sleep(1000); Assert.That(lastEx, Is.Not.Null); "EOF...".Print(); } }
public When_an_action_is_performed_on_a_closed_channel_that_doesnt_open_again() { persistentConnection = Substitute.For <IPersistentConnection>(); var eventBus = Substitute.For <IEventBus>(); var configuration = new ConnectionConfiguration { Timeout = 1 }; var shutdownArgs = new ShutdownEventArgs( ShutdownInitiator.Peer, AmqpException.ConnectionClosed, "connection closed by peer"); var exception = new OperationInterruptedException(shutdownArgs); persistentConnection.When(x => x.CreateModel()).Do(x => { throw exception; }); var logger = Substitute.For <IEasyNetQLogger>(); persistentChannel = new PersistentChannel(persistentConnection, logger, configuration, eventBus); }
public void SetUp() { persistentConnection = MockRepository.GenerateStub <IPersistentConnection>(); var eventBus = MockRepository.GenerateStub <IEventBus>(); var configuration = new ConnectionConfiguration { Timeout = 1 }; var shutdownArgs = new ShutdownEventArgs( ShutdownInitiator.Peer, AmqpException.ConnectionClosed, "connection closed by peer"); var exception = new OperationInterruptedException(shutdownArgs); persistentConnection.Stub(x => x.CreateModel()).WhenCalled(x => { throw exception; }); var logger = MockRepository.GenerateStub <IEasyNetQLogger>(); persistentChannel = new PersistentChannel(persistentConnection, logger, configuration, eventBus); }
public void SetUp() { persistentConnection = MockRepository.GenerateStub <IPersistentConnection>(); channel = MockRepository.GenerateStub <IModel>(); var eventBus = new EventBus(); var configuration = new ConnectionConfiguration(); var shutdownArgs = new ShutdownEventArgs( ShutdownInitiator.Peer, AmqpException.ConnectionClosed, "connection closed by peer"); var exception = new OperationInterruptedException(shutdownArgs); var first = true; persistentConnection.Stub(x => x.CreateModel()).WhenCalled(x => { if (first) { first = false; throw exception; } x.ReturnValue = channel; }); var logger = MockRepository.GenerateStub <IEasyNetQLogger>(); persistentChannel = new PersistentChannel(persistentConnection, logger, configuration, eventBus); new Timer(_ => eventBus.Publish(new ConnectionCreatedEvent())).Change(10, Timeout.Infinite); persistentChannel.InvokeChannelAction(x => x.ExchangeDeclare("MyExchange", "direct")); }
public void TestBindOnNonExistingExchange() { OperationInterruptedException exception = Assert.Throws <OperationInterruptedException>(() => _model.ExchangeBind("NonExistingQueue", "", string.Empty)); Assert.True(exception.Message.Contains("403"), $"Message doesn't contain the expected 403 part: {exception.Message}"); Assert.False(_model.IsOpen, "Channel should be closed due to the soft error"); Assert.True(_conn.IsOpen, "Connection should still be open due to the soft error only closing the channel"); }
private bool IsQueueMissing(OperationInterruptedException exception) { var reason = exception.ShutdownReason; const int missingQueueReplyCode = 404; return(reason.ReplyCode == missingQueueReplyCode); }
public void TestConsumeOnNonExistingQueue() { OperationInterruptedException exception = Assert.Throws <OperationInterruptedException>(() => { var consumer = new EventingBasicConsumer(_model); _model.BasicConsume("NonExistingQueue", true, consumer); }); Assert.True(exception.Message.Contains("404"), $"Message doesn't contain the expected 404 part: {exception.Message}"); Assert.False(_model.IsOpen, "Channel should be closed due to the soft error"); Assert.True(_conn.IsOpen, "Connection should still be open due to the soft error only closing the channel"); }
/// <summary> /// 显示异常 /// </summary> public string Show(OperationInterruptedException ex) { switch (ex.ShutdownReason.ReplyCode) { case 404: return("队列未找到,请先启动生产者"); default: return(ex.ShutdownReason.ReplyText); } }
private static bool NeedRethrow(OperationInterruptedException exception) { try { var amqpException = AmqpExceptionGrammar.ParseExceptionString(exception.Message); return(amqpException.Code != AmqpException.ConnectionClosed); } catch (ParseException) { return(true); } }
public When_an_action_is_performed_on_a_closed_channel_that_doesnt_open_again() { var persistentConnection = Substitute.For <IPersistentConnection>(); var eventBus = Substitute.For <IEventBus>(); var shutdownArgs = new ShutdownEventArgs( ShutdownInitiator.Peer, AmqpErrorCodes.ConnectionClosed, "connection closed by peer" ); var exception = new OperationInterruptedException(shutdownArgs); persistentConnection.When(x => x.CreateModel()).Do(x => throw exception); persistentChannel = new PersistentChannel(new PersistentChannelOptions(), persistentConnection, eventBus); }
public void Can_consume_messages_with_BasicConsumer() { RabbitMqConfig.UsingChannel(channel => { OperationInterruptedException lastEx = null; channel.Close(); void CallBack() { try { PublishHelloRabbit(channel); } catch (Exception ex) { lastEx = ex as OperationInterruptedException; Console.WriteLine("Caught {0}: {1}", ex.GetType().Name, ex); } } ExecUtils.ExecMultiThreading(1, CallBack); Assert.That(lastEx, Is.Not.Null); }); }
public void QueueDeclare(string exchange, string queue = "", string exchangeType = "fanout", bool durable = false, int messageTtl = 0, int messageMaxLen = 0, int autoDeleteTime = 0, string routingKey = null, string deadExchange = "", string deadRoutingKey = "") { if (string.IsNullOrEmpty(exchange)) { throw new ArgumentException("Exchange名不能为空", nameof(exchange)); } using (IModel channel = GetChannel()) { OperationInterruptedException declareExp = null; try { channel.ExchangeDeclare(exchange, exchangeType, durable); } catch (OperationInterruptedException exp) { // Exchange已经存在,重新定义参数不相同时,会报错,错误信息举例: // "The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=406, // text =\"PRECONDITION_FAILED - inequivalent arg 'type' for exchange 'xxx' in vhost '/': received 'fanout' but current is 'topic'\", // classId =40, methodId=10, cause=" declareExp = exp; } if (!string.IsNullOrEmpty(queue)) { QueueBind(channel, exchange, queue, durable, messageTtl, messageMaxLen, autoDeleteTime, routingKey, deadExchange, deadRoutingKey); } if (declareExp != null) { // Exchange存在时,允许绑定后,再抛出此异常 throw declareExp; } } }
public void SetUp() { persistentConnection = MockRepository.GenerateStub <IPersistentConnection>(); channel = MockRepository.GenerateStub <IModel>(); var eventBus = new EventBus(); var configuration = new ConnectionConfiguration(); var shutdownArgs = new ShutdownEventArgs( ShutdownInitiator.Peer, AmqpException.ConnectionClosed, "connection closed by peer"); var exception = new OperationInterruptedException(shutdownArgs); persistentConnection.Stub(x => x.CreateModel()).Throw(exception).Repeat.Once(); persistentConnection.Stub(x => x.CreateModel()).Return(channel).Repeat.Any(); var logger = MockRepository.GenerateStub <IEasyNetQLogger>(); persistentChannel = new PersistentChannel(persistentConnection, logger, configuration, eventBus); new Timer(_ => eventBus.Publish(new ConnectionCreatedEvent())).Change(10, Timeout.Infinite); persistentChannel.InvokeChannelAction(x => x.ExchangeDeclare("MyExchange", "direct"), DateTime.UtcNow); }
public When_an_action_is_performed_on_a_closed_channel_that_then_opens() { var persistentConnection = Substitute.For <IPersistentConnection>(); channel = Substitute.For <IModel, IRecoverable>(); var eventBus = new EventBus(); var shutdownArgs = new ShutdownEventArgs( ShutdownInitiator.Peer, AmqpErrorCodes.ConnectionClosed, "connection closed by peer" ); var exception = new OperationInterruptedException(shutdownArgs); persistentConnection.CreateModel().Returns( x => throw exception, x => channel, x => channel ); var persistentChannel = new PersistentChannel( new PersistentChannelOptions(), persistentConnection, eventBus ); persistentChannel.InvokeChannelAction(x => x.ExchangeDeclare("MyExchange", "direct")); }
internal static bool Is404(this OperationInterruptedException ex) { return(ex.Message.Contains("code=404")); }
public static bool HasReplyCode(this OperationInterruptedException exception, int code) => exception.ShutdownReason != null && exception.ShutdownReason.ReplyCode == code;