public void CachedMessageConsumer()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection        connection        = new TestConnection();


            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            mocks.ReplayAll();


            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession            sessionA    = con1.CreateSession(true, SessionMode.SessionTransacted);
            Destination         destination = new Topic("test.dest");
            IMessageConsumer    consumerA   = sessionA.CreateConsumer(destination);
            TestMessageConsumer tmpA        = GetTestMessageConsumer(consumerA);

            sessionA.Close();

            ISession            sessionB  = con1.CreateSession(true, SessionMode.SessionTransacted);
            IMessageConsumer    consumerB = sessionB.CreateConsumer(destination);
            TestMessageConsumer tmpB      = GetTestMessageConsumer(consumerB);

            Assert.AreSame(tmpA, tmpB);

            mocks.VerifyAll();
        }
Beispiel #2
0
        public void CachingConnectionFactory()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection        connection        = (IConnection)mocks.CreateMock(typeof(IConnection));
            ISession           txSession         = (ISession)mocks.CreateMock(typeof(ISession));
            ISession           nonTxSession      = (ISession)mocks.CreateMock(typeof(ISession));

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            Expect.Call(connection.CreateSession(true, SessionMode.SessionTransacted)).Return(txSession).Repeat.Once();
            Expect.Call(txSession.Transacted).Return(true).Repeat.Twice();
            txSession.Rollback();
            LastCall.Repeat.Once();
            txSession.Commit();
            LastCall.Repeat.Once();
            txSession.Close();
            LastCall.Repeat.Once();

            Expect.Call(connection.CreateSession(false, SessionMode.ClientAcknowledge)).Return(nonTxSession).Repeat.Once();
            nonTxSession.Close();
            LastCall.Repeat.Once();
            connection.Start();
            LastCall.Repeat.Twice();
            connection.Stop();
            LastCall.Repeat.Once();
            connection.Close();
            LastCall.Repeat.Once();

            mocks.ReplayAll();

            CachingConnectionFactory scf = new CachingConnectionFactory(connectionFactory);

            scf.ReconnectOnException = false;

            IConnection con1     = scf.CreateConnection();
            ISession    session1 = con1.CreateSession(true, Session.SESSION_TRANSACTED);
            bool        b        = session1.Transacted;

            session1.Close(); // should be ignored
            session1 = con1.CreateSession(false, Session.CLIENT_ACKNOWLEDGE);
            session1.Close(); // should be ignored
            con1.Start();
            con1.Close();     // should be ignored
            IConnection con2     = scf.CreateConnection();
            ISession    session2 = con2.CreateSession(false, Session.CLIENT_ACKNOWLEDGE);

            session2.Close(); // should be ignored
            session2 = con2.CreateSession(true, Session.SESSION_TRANSACTED);
            session2.Commit();
            session2.Close(); // should be ignored
            con2.Start();
            con2.Close();
            scf.Dispose();

            mocks.Verify(connectionFactory);
            mocks.Verify(connection);
            mocks.Verify(txSession);
            mocks.Verify(nonTxSession);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CachedSession"/> class.
 /// </summary>
 /// <param name="targetSession">The target session.</param>
 /// <param name="sessionList">The session list.</param>
 /// <param name="ccf">The CachingConnectionFactory.</param>
 public CachedSession(ISession targetSession, LinkedList sessionList, CachingConnectionFactory ccf)
 {
     target                = targetSession;
     this.sessionList      = sessionList;
     this.sessionCacheSize = ccf.SessionCacheSize;
     shouldCacheProducers  = ccf.CacheProducers;
     shouldCacheConsumers  = ccf.CacheConsumers;
     this.ccf              = ccf;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CachedSession"/> class.
 /// </summary>
 /// <param name="targetSession">The target session.</param>
 /// <param name="sessionList">The session list.</param>
 /// <param name="ccf">The CachingConnectionFactory.</param>
 public CachedSession(ISession targetSession, LinkedList sessionList, CachingConnectionFactory ccf)
 {
     target = targetSession;
     this.sessionList = sessionList;
     this.sessionCacheSize = ccf.SessionCacheSize;
     shouldCacheProducers = ccf.CacheProducers;
     shouldCacheConsumers = ccf.CacheConsumers;
     this.ccf = ccf;
 }
        public void CachedSessionTwoRequests()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection        connection        = new TestConnection();

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            mocks.ReplayAll();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession    session1     = con1.CreateSession(true, SessionMode.SessionTransacted);
            TestSession testSession1 = GetTestSession(session1);

            Assert.AreEqual(1, testSession1.CreatedCount);
            Assert.AreEqual(0, testSession1.CloseCount);


            //will create a new one, not in the cache.
            ISession    session2     = con1.CreateSession(true, SessionMode.SessionTransacted);
            TestSession testSession2 = GetTestSession(session2);

            Assert.AreEqual(1, testSession2.CreatedCount);
            Assert.AreEqual(0, testSession2.CloseCount);

            Assert.AreNotSame(testSession1, testSession2);

            Assert.AreNotSame(session1, session2);

            session1.Close();  // will be put in the cache

            ISession    session3     = con1.CreateSession(true, SessionMode.SessionTransacted);
            TestSession testSession3 = GetTestSession(session3);

            Assert.AreSame(testSession1, testSession3);
            Assert.AreSame(session1, session3);
            Assert.AreEqual(1, testSession1.CreatedCount);
            Assert.AreEqual(0, testSession1.CloseCount);

            mocks.VerifyAll();
        }
        public void CachedSession()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection        connection        = new TestConnection();

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            mocks.ReplayAll();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;

            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession    session1    = con1.CreateSession(true, SessionMode.SessionTransacted);
            TestSession testSession = GetTestSession(session1);

            Assert.AreEqual(1, testSession.CreatedCount);
            Assert.AreEqual(0, testSession.CloseCount);


            session1.Close();  // won't close, will put in session cache.
            Assert.AreEqual(0, testSession.CloseCount);

            ISession session2 = con1.CreateSession(true, SessionMode.SessionTransacted);


            TestSession testSession2 = GetTestSession(session2);


            Assert.AreSame(testSession, testSession2);

            Assert.AreEqual(1, testSession.CreatedCount);
            Assert.AreEqual(0, testSession.CloseCount);

            mocks.VerifyAll();

            //don't explicitly call close on
        }
        public void CachedSession()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection connection = new TestConnection();

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            mocks.ReplayAll();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;

            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession session1 = con1.CreateSession(true, SessionMode.SessionTransacted);        
            TestSession testSession = GetTestSession(session1);
            Assert.AreEqual(1, testSession.CreatedCount);
            Assert.AreEqual(0, testSession.CloseCount);


            session1.Close();  // won't close, will put in session cache.
            Assert.AreEqual(0, testSession.CloseCount);

            ISession session2 = con1.CreateSession(true, SessionMode.SessionTransacted);


            TestSession testSession2 = GetTestSession(session2);
            

            Assert.AreSame(testSession, testSession2);

            Assert.AreEqual(1, testSession.CreatedCount);
            Assert.AreEqual(0, testSession.CloseCount);
           
            mocks.VerifyAll();

            //don't explicitly call close on 
        }
        public void CachedMessageProducerTwoRequests()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection        connection        = new TestConnection();

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            mocks.ReplayAll();


            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession            sessionA  = con1.CreateSession(true, SessionMode.SessionTransacted);
            Topic               dest      = new Topic("test.topic");
            IMessageProducer    producerA = sessionA.CreateProducer(dest);
            TestMessageProducer tmpA      = GetTestMessageProducer(producerA);


            ISession            sessionB  = con1.CreateSession(true, SessionMode.SessionTransacted);
            IMessageProducer    producerB = sessionB.CreateProducer(dest);
            TestMessageProducer tmpB      = GetTestMessageProducer(producerB);

            Assert.AreNotSame(tmpA, tmpB);

            sessionA.Close();

            ISession            sessionC  = con1.CreateSession(true, SessionMode.SessionTransacted);
            IMessageProducer    producerC = sessionC.CreateProducer(dest);
            TestMessageProducer tmpC      = GetTestMessageProducer(producerC);

            Assert.AreSame(tmpA, tmpC);

            mocks.VerifyAll();
        }
        public void CachedSessionTwoRequests()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection connection = new TestConnection();

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            mocks.ReplayAll();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession session1 = con1.CreateSession(true, SessionMode.SessionTransacted);
            TestSession testSession1 = GetTestSession(session1);
            Assert.AreEqual(1, testSession1.CreatedCount);
            Assert.AreEqual(0, testSession1.CloseCount);


            //will create a new one, not in the cache.
            ISession session2 = con1.CreateSession(true, SessionMode.SessionTransacted);
            TestSession testSession2 = GetTestSession(session2);
            Assert.AreEqual(1, testSession2.CreatedCount);
            Assert.AreEqual(0, testSession2.CloseCount);

            Assert.AreNotSame(testSession1, testSession2);

            Assert.AreNotSame(session1, session2);

            session1.Close();  // will be put in the cache

            ISession session3 = con1.CreateSession(true, SessionMode.SessionTransacted);
            TestSession testSession3 = GetTestSession(session3);
            Assert.AreSame(testSession1, testSession3);
            Assert.AreSame(session1, session3);
            Assert.AreEqual(1, testSession1.CreatedCount);
            Assert.AreEqual(0, testSession1.CloseCount);

            mocks.VerifyAll();


        }
        public void CachedMessageConsumer()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection connection = new TestConnection();
            

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            mocks.ReplayAll();


            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession sessionA = con1.CreateSession(true, SessionMode.SessionTransacted);
            Destination destination = new Topic("test.dest");
            IMessageConsumer consumerA = sessionA.CreateConsumer(destination);
            TestMessageConsumer tmpA = GetTestMessageConsumer(consumerA);

            sessionA.Close();

            ISession sessionB = con1.CreateSession(true, SessionMode.SessionTransacted);
            IMessageConsumer consumerB = sessionB.CreateConsumer(destination);
            TestMessageConsumer tmpB = GetTestMessageConsumer(consumerB);

            Assert.AreSame(tmpA, tmpB);

            mocks.VerifyAll();
        }
        public void CachedMessageProducerTwoRequests()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection connection = new TestConnection();

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            mocks.ReplayAll();


            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession sessionA = con1.CreateSession(true, SessionMode.SessionTransacted);
            Topic dest = new Topic("test.topic");
            IMessageProducer producerA = sessionA.CreateProducer(dest);
            TestMessageProducer tmpA = GetTestMessageProducer(producerA);


            ISession sessionB = con1.CreateSession(true, SessionMode.SessionTransacted);
            IMessageProducer producerB = sessionB.CreateProducer(dest);
            TestMessageProducer tmpB = GetTestMessageProducer(producerB);
            
            Assert.AreNotSame(tmpA, tmpB);

            sessionA.Close();

            ISession sessionC = con1.CreateSession(true, SessionMode.SessionTransacted);
            IMessageProducer producerC = sessionC.CreateProducer(dest);
            TestMessageProducer tmpC = GetTestMessageProducer(producerC);

            Assert.AreSame(tmpA, tmpC);

            mocks.VerifyAll();
        }
        public void CachingConnectionFactory()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection connection = (IConnection)mocks.CreateMock(typeof(IConnection));
            ISession txSession = (ISession)mocks.CreateMock(typeof(ISession));
            ISession nonTxSession = (ISession)mocks.CreateMock(typeof(ISession));
            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            Expect.Call(connection.CreateSession(true, SessionMode.SessionTransacted)).Return(txSession).Repeat.Once();
            Expect.Call(txSession.Transacted).Return(true).Repeat.Twice();
            txSession.Rollback();
            LastCall.Repeat.Once();
            txSession.Commit();
            LastCall.Repeat.Once();            
            txSession.Close();
            LastCall.Repeat.Once();

            Expect.Call(connection.CreateSession(false, SessionMode.ClientAcknowledge)).Return(nonTxSession).Repeat.Once();           
            nonTxSession.Close();
            LastCall.Repeat.Once();
            connection.Start();
            LastCall.Repeat.Twice();
            connection.Stop();
            LastCall.Repeat.Once();
            connection.Close();
            LastCall.Repeat.Once();

            mocks.ReplayAll();

            CachingConnectionFactory scf = new CachingConnectionFactory(connectionFactory);
            scf.ReconnectOnException = false;

            IConnection con1 = scf.CreateConnection();
            ISession session1 = con1.CreateSession(true, Session.SESSION_TRANSACTED);
            bool b = session1.Transacted;
            session1.Close();  // should be ignored
            session1 = con1.CreateSession(false, Session.CLIENT_ACKNOWLEDGE);
            session1.Close();  // should be ignored
            con1.Start();
            con1.Close(); // should be ignored
            IConnection con2 = scf.CreateConnection();
            ISession session2 = con2.CreateSession(false, Session.CLIENT_ACKNOWLEDGE);
            session2.Close(); // should be ignored
            session2 = con2.CreateSession(true, Session.SESSION_TRANSACTED);
            session2.Commit();
            session2.Close(); // should be ignored
            con2.Start();
            con2.Close();
            scf.Dispose();

            mocks.Verify(connectionFactory);
            mocks.Verify(connection);
            mocks.Verify(txSession);
            mocks.Verify(nonTxSession);


        }