public void TestCloseInvalidConnection()
        {
            var mocker = new AutoMoqer();

            var mockConnectionFactory = mocker.GetMock<RabbitMQ.Client.ConnectionFactory>();
            var mockConnection1 = new Mock<RabbitMQ.Client.IConnection>();
            var mockConnection2 = new Mock<RabbitMQ.Client.IConnection>();

            mockConnectionFactory.Setup(c => c.CreateConnection()).ReturnsInOrder(mockConnection1.Object, mockConnection2.Object);

            // simulate a dead connection
            mockConnection1.Setup(c => c.IsOpen).Returns(false);

            var connectionFactory = new SingleConnectionFactory(mockConnectionFactory.Object);

            var connection = connectionFactory.CreateConnection();

            // the dead connection should be discarded
            connection.CreateChannel(false);
            mockConnectionFactory.Verify(c => c.CreateConnection(), Times.Exactly(2));
            mockConnection2.Verify(c => c.CreateModel(), Times.Exactly(1));

            connectionFactory.Dispose();
            mockConnection2.Verify(c => c.Close(), Times.Exactly(1));
        }
        public void TestDestroyBeforeUsed()
        {
            var mocker = new AutoMoqer();

            var mockConnectionFactory = mocker.GetMock<RabbitMQ.Client.ConnectionFactory>();

            var connectionFactory = new SingleConnectionFactory(mockConnectionFactory.Object);
            connectionFactory.Dispose();

            mockConnectionFactory.Verify(c => c.CreateConnection(), Times.Never());
        }
 public void TestNoFailOnStartupWithMissingBroker()
 {
     var connectionFactory = new SingleConnectionFactory("foo");
     connectionFactory.Port = 434343;
     var applicationContext = new GenericApplicationContext();
     applicationContext.ObjectFactory.RegisterSingleton("foo", new Queue("queue"));
     var rabbitAdmin = new RabbitAdmin(connectionFactory);
     rabbitAdmin.ApplicationContext = applicationContext;
     rabbitAdmin.AutoStartup = true;
     rabbitAdmin.AfterPropertiesSet();
 }
Exemple #4
0
        private static void Main(string[] args)
        {
            using (IConnectionFactory connectionFactory = new SingleConnectionFactory())
            {
                IAmqpAdmin amqpAdmin = new RabbitAdmin(connectionFactory);

                var helloWorldQueue = new Queue("hello.world.queue");

                amqpAdmin.DeclareQueue(helloWorldQueue);

                //Each queue is automatically bound to the default direct exchange.

                Console.WriteLine("Queue [hello.world.queue] has been declared.");
                Console.WriteLine("Press 'enter' to exit");
                Console.ReadLine();
            }
        }
Exemple #5
0
        /// <summary>
        /// Mains the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        /// <remarks></remarks>
        public static void Main(string[] args)
        {
            var connectionFactory = new SingleConnectionFactory("localhost");
            connectionFactory.UserName = "******";
            connectionFactory.Password = "******";

            var template = new RabbitTemplate();
            template.ConnectionFactory = connectionFactory;
            template.IsChannelTransacted = true;
            template.AfterPropertiesSet();

            var routingKey = TestConstants.ROUTING_KEY;
            QueueUtils.DeclareTestQueue(template, routingKey);

            // Send message
            SendMessages(template, TestConstants.EXCHANGE_NAME, routingKey, TestConstants.NUM_MESSAGES);
        }
Exemple #6
0
        static void Main(string[] args)
        {
            using (IConnectionFactory connectionFactory = new SingleConnectionFactory())
            {
                IAmqpAdmin amqpAdmin = new RabbitAdmin(connectionFactory);

                var marketDataQueue = new Queue("APP.STOCK.MARKETDATA");
                amqpAdmin.DeclareQueue(marketDataQueue);

                amqpAdmin.DeclareQueue(new Queue("APP.STOCK.REQUEST"));
                amqpAdmin.DeclareQueue(new Queue("APP.STOCK.JOE"));

                //Each queue is automatically bound to the default direct exchange.

                Console.WriteLine("Queues and exchanges have been declared.");
                Console.WriteLine("Press 'enter' to exit");
                Console.ReadLine();
            }
        }
        public void TestFailOnFirstUseWithMissingBroker()
        {
            var connectionFactory = new SingleConnectionFactory("foo");
            connectionFactory.Port = 434343;
            var applicationContext = new GenericApplicationContext();
            applicationContext.ObjectFactory.RegisterSingleton("foo", new Queue("queue"));
            var rabbitAdmin = new RabbitAdmin(connectionFactory);
            rabbitAdmin.ApplicationContext = applicationContext;
            rabbitAdmin.AutoStartup = true;
            rabbitAdmin.AfterPropertiesSet();

            try
            {
                rabbitAdmin.DeclareQueue();
            }
            catch (Exception ex)
            {
                // TODO: Should this be an ArgumentException instead of an AmqpIOException??
                // Assert.True(ex is ArgumentException, "Expecting an ArgumentException");
                Assert.True(ex is AmqpIOException, "Expecting an AmqpIOException");
            }
        }
 public void SetUp()
 {
     connectionFactory = new SingleConnectionFactory();
     brokerAdmin = new RabbitBrokerAdmin(connectionFactory);
 }
        public void TestWithListener()
        {
            var mocker = new AutoMoqer();

            var mockConnectionFactory = mocker.GetMock<RabbitMQ.Client.ConnectionFactory>();
            var mockConnection = mocker.GetMock<RabbitMQ.Client.IConnection>();

            mockConnectionFactory.Setup(c => c.CreateConnection()).Returns(mockConnection.Object);

            var called = new AtomicInteger(0);
            var connectionFactory = new SingleConnectionFactory(mockConnectionFactory.Object);
            var mockConnectionListener = new Mock<IConnectionListener>();
            mockConnectionListener.Setup(m => m.OnCreate(It.IsAny<IConnection>())).Callback((IConnection conn) => called.IncrementValueAndReturn());
            mockConnectionListener.Setup(m => m.OnClose(It.IsAny<IConnection>())).Callback((IConnection conn) => called.DecrementValueAndReturn());

            connectionFactory.ConnectionListeners = new List<IConnectionListener>() { mockConnectionListener.Object };

            var con = connectionFactory.CreateConnection();
            Assert.AreEqual(1, called.Value);

            con.Close();
            Assert.AreEqual(1, called.Value);
            mockConnection.Verify(c => c.Close(), Times.Never());

            connectionFactory.CreateConnection();
            Assert.AreEqual(1, called.Value);

            connectionFactory.Dispose();
            Assert.AreEqual(0, called.Value);
            mockConnection.Verify(c => c.Close(), Times.AtLeastOnce());

            mockConnectionFactory.Verify(c => c.CreateConnection(), Times.Exactly(1));
        }
 public void TestGetQueues()
 {
     SingleConnectionFactory connectionFactory = new SingleConnectionFactory();
     connectionFactory.Port = BrokerTestUtils.GetAdminPort();
     Queue queue = new RabbitAdmin(connectionFactory).DeclareQueue();
     Assert.AreEqual("/", connectionFactory.VirtualHost);
     List<QueueInfo> queues = brokerAdmin.GetQueues();
     Assert.AreEqual(queue.Name, queues[0].Name);
 }
 public CloseSuppressingConnection(SingleConnectionFactory factory, IConnection connection)
 {
     this.target = connection;
     this.singleConnectionFactory = factory;
 }
 /// <summary>
 /// Creates the connection factory.
 /// </summary>
 /// <returns>The connection factory.</returns>
 /// <remarks></remarks>
 protected IConnectionFactory CreateConnectionFactory()
 {
     var connectionFactory = new SingleConnectionFactory();
     connectionFactory.Port = BrokerTestUtils.GetPort();
     return connectionFactory;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SharedConnectionProxy"/> class.
 /// </summary>
 /// <param name="target">
 /// The target.
 /// </param>
 /// <param name="listener">
 /// The listener.
 /// </param>
 /// <param name="outer">
 /// The outer.
 /// </param>
 public SharedConnectionProxy(IConnection target, CompositeConnectionListener listener, SingleConnectionFactory outer)
 {
     this.target = target;
     this.listener = listener;
     this.outer = outer;
 }