public void CachedChannel()
        {
            TestConnectionFactory testConnectionFactory = new TestConnectionFactory();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(testConnectionFactory, "localhost:" + RabbitUtils.DEFAULT_PORT);

            IConnection con1 = cachingConnectionFactory.CreateConnection();
            Assert.AreEqual(1, testConnectionFactory.CreateConnectionCount);

            IModel model1 = con1.CreateModel();
            TestModel testModel = GetTestModel(model1);
            Assert.AreEqual(1, testModel.CreatedCount);
            Assert.AreEqual(0, testModel.CloseCount);
            Assert.AreEqual(1, testConnectionFactory.CreateConnectionCount);

            model1.Close();  // won't close, will put in channel cache.
            Assert.AreEqual(0, testModel.CloseCount);

            IModel model2 = con1.CreateModel();
            TestModel testModel2 = GetTestModel(model2);

            Assert.AreSame(testModel, testModel2);

            Assert.AreEqual(1, testModel.CreatedCount);
            Assert.AreEqual(0, testModel.CloseCount);
            Assert.AreEqual(1, testConnectionFactory.CreateConnectionCount);
        }
        public void CachedModelTwoRequests()
        {
            TestConnectionFactory testConnectionFactory = new TestConnectionFactory();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(testConnectionFactory, "localhost:" + RabbitUtils.DEFAULT_PORT);

            //Create a session
            IConnection con1 = cachingConnectionFactory.CreateConnection();
            Assert.AreEqual(1, testConnectionFactory.CreateConnectionCount);

            //Create a model
            IModel model1 = con1.CreateModel();
            TestModel testModel1 = GetTestModel(model1);
            Assert.AreEqual(1, testModel1.CreatedCount);
            Assert.AreEqual(0, testModel1.CloseCount);

            //will create a new model, not taken from the cache since cache size is 1.
            IModel model2 = con1.CreateModel();
            TestModel testModel2 = GetTestModel(model2);
            Assert.AreEqual(1, testModel2.CreatedCount);
            Assert.AreEqual(0, testModel2.CloseCount);

            Assert.AreNotSame(testModel1, testModel2);
            Assert.AreNotSame(model1, model2);

            model1.Close(); // will put the model in the cache

            // now get a new model, will be taken from the cache
            IModel model3 = con1.CreateModel();
            TestModel testModel3 = GetTestModel(model3);
            Assert.AreSame(testModel1, testModel3);
            Assert.AreSame(model1, model3);
            Assert.AreEqual(1, testModel1.CreatedCount);
            Assert.AreEqual(0, testModel1.CloseCount);

            Assert.AreEqual(1, testConnectionFactory.CreateConnectionCount);
        }
        public void CachedModelTwoRequestsWithCacheSize()
        {
            TestConnectionFactory testConnectionFactory = new TestConnectionFactory();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(testConnectionFactory, "localhost:" + RabbitUtils.DEFAULT_PORT);
            cachingConnectionFactory.ChannelCacheSize = 2;
            cachingConnectionFactory.AfterPropertiesSet();

            //Create a session
            IConnection con1 = cachingConnectionFactory.CreateConnection();
            Assert.AreEqual(1, testConnectionFactory.CreateConnectionCount);

            IModel model1 = con1.CreateModel();
            IModel model2 = con1.CreateModel();

            model1.Close(); // should be ignored, and add last into model cache.
            model2.Close(); // should be ignored, and add last into model cache.

            IModel m1 = con1.CreateModel();
            IModel m2 = con1.CreateModel();

            Assert.AreNotSame(m1, m2);
            Assert.AreSame(m1, model1);
            Assert.AreSame(m2, model2);

            m1.Close();
            m2.Close();

            Assert.AreEqual(2, GetTestConnection(con1).CreateModelCount);

            con1.Close();  // should be ignored.

            Assert.AreEqual(0, GetTestConnection(con1).CloseCount);
            Assert.AreEqual(0, GetTestModel(model1).CloseCount);
            Assert.AreEqual(0, GetTestModel(model2).CloseCount);

            Assert.AreEqual(1, testConnectionFactory.CreateConnectionCount);
        }