Exemple #1
0
        public void TestTemporaryLogs()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddRabbitQueue(new Config.Queue("testq.nonDur", false, false, false));
            serviceCollection.AddRabbitQueue(new Config.Queue("testq.ad", true, false, true));
            serviceCollection.AddRabbitQueue(new Config.Queue("testq.excl", true, true, false));
            serviceCollection.AddRabbitQueue(new Config.Queue("testq.all", false, true, true));
            serviceCollection.AddRabbitExchange(new Config.DirectExchange("testex.nonDur", false, false));
            serviceCollection.AddRabbitExchange(new Config.DirectExchange("testex.ad", true, true));
            serviceCollection.AddRabbitExchange(new Config.DirectExchange("testex.all", false, true));
            var connectionFactory  = new SingleConnectionFactory("localhost");
            var applicationContext = GetApplicationContext(serviceCollection);

            var logs       = new List <string>();
            var mockLogger = new Mock <ILogger>();

            mockLogger.Setup((l) => l.Log(It.IsAny <LogLevel>(), It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()))
            .Callback(new InvocationAction(invocation =>
            {
                logs.Add(invocation.Arguments[2].ToString());
            }));
            var rabbitAdmin = new RabbitAdmin(applicationContext, connectionFactory, mockLogger.Object);

            try
            {
                connectionFactory.CreateConnection().Close();
                logs.Sort();
                Assert.NotEmpty(logs);
                Assert.Contains("(testex.ad) durable:True, auto-delete:True", logs[0]);
                Assert.Contains("(testex.all) durable:False, auto-delete:True", logs[1]);
                Assert.Contains("(testex.nonDur) durable:False, auto-delete:False", logs[2]);
                Assert.Contains("(testq.ad) durable:True, auto-delete:True, exclusive:False", logs[3]);
                Assert.Contains("(testq.all) durable:False, auto-delete:True, exclusive:True", logs[4]);
                Assert.Contains("(testq.excl) durable:True, auto-delete:False, exclusive:True", logs[5]);
                Assert.Contains("(testq.nonDur) durable:False, auto-delete:False, exclusive:False", logs[6]);
            }
            finally
            {
                CleanQueuesAndExchanges(rabbitAdmin);
                connectionFactory.Destroy();
            }
        }
Exemple #2
0
        public async Task TestGetQueueProperties()
        {
            var serviceCollection  = new ServiceCollection();
            var connectionFactory  = new SingleConnectionFactory("localhost");
            var applicationContext = GetApplicationContext(serviceCollection);
            var rabbitAdmin        = new RabbitAdmin(applicationContext, connectionFactory);
            var queueName          = "test.properties." + DateTimeOffset.Now.ToUnixTimeMilliseconds();

            try
            {
                rabbitAdmin.DeclareQueue(new Config.Queue(queueName));
                var template = new RabbitTemplate(connectionFactory);
                template.ConvertAndSend(queueName, "foo");
                var n = 0;
                while (n++ < 100 && MessageCount(rabbitAdmin, queueName) == 0)
                {
                    await Task.Delay(100);
                }

                Assert.True(n < 100);
                var channel  = connectionFactory.CreateConnection().CreateChannel(false);
                var consumer = new DefaultBasicConsumer(channel);
                channel.BasicConsume(queueName, true, consumer);
                n = 0;
                while (n++ < 100 && MessageCount(rabbitAdmin, queueName) > 0)
                {
                    await Task.Delay(100);
                }

                Assert.True(n < 100);

                var props = rabbitAdmin.GetQueueProperties(queueName);
                Assert.True(props.TryGetValue(RabbitAdmin.QUEUE_CONSUMER_COUNT, out var consumerCount));
                Assert.Equal(1U, consumerCount);
                channel.Close();
            }
            finally
            {
                rabbitAdmin.DeleteQueue(queueName);
                connectionFactory.Destroy();
            }
        }
Exemple #3
0
        public void SessionCallbackWithinSynchronizedTransaction()
        {
            SingleConnectionFactory scf      = new SingleConnectionFactory(mockConnectionFactory);
            NmsTemplate             template = CreateTemplate();

            template.ConnectionFactory = scf;

            mockConnection.Start();
            LastCall.On(mockConnection).Repeat.Times(2);
            // We're gonna call getTransacted 3 times, i.e. 2 more times.
            Expect.Call(mockSession.Transacted).Return(UseTransactedSession).Repeat.Twice();

            if (UseTransactedTemplate)
            {
                mockSession.Commit();
                LastCall.On(mockSession).Repeat.Once();
            }

            mockSession.Close();
            LastCall.On(mockSession).Repeat.Once();
            mockConnection.Stop();
            LastCall.On(mockConnection).Repeat.Once();
            mockConnection.Close();
            LastCall.On(mockConnection).Repeat.Once();

            mocks.ReplayAll();


            TransactionSynchronizationManager.InitSynchronization();

            try
            {
                template.Execute(session =>
                {
                    bool b = session.Transacted;
                    return(null);
                });
                template.Execute(session =>
                {
                    bool b = session.Transacted;
                    return(null);
                });

                Assert.AreSame(mockSession, ConnectionFactoryUtils.GetTransactionalSession(scf, null, false));
                Assert.AreSame(mockSession,
                               ConnectionFactoryUtils.GetTransactionalSession(scf, scf.CreateConnection(), false));

                //In Java this test was doing 'double-duty' and testing TransactionAwareConnectionFactoryProxy, which has
                //not been implemented in .NET

                template.Execute(session =>
                {
                    bool b = session.Transacted;
                    return(null);
                });

                IList synchs = TransactionSynchronizationManager.Synchronizations;
                Assert.AreEqual(1, synchs.Count);
                ITransactionSynchronization synch = (ITransactionSynchronization)synchs[0];
                synch.BeforeCommit(false);
                synch.BeforeCompletion();
                synch.AfterCommit();
                synch.AfterCompletion(TransactionSynchronizationStatus.Unknown);
            }
            finally
            {
                TransactionSynchronizationManager.ClearSynchronization();
                //Assert.IsTrue(TransactionSynchronizationManager.ResourceDictionary.Count == 0);
                //Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive);
                scf.Dispose();
            }
            Assert.IsTrue(TransactionSynchronizationManager.ResourceDictionary.Count == 0);
            mocks.VerifyAll();
        }