public void WhenAskForReadableConnectionAndNoUpConnectionIsAvailable_ThenGiveFailOverConnection()
        {
            var session = new Session();
            var activeServers = new Server[]{
                new Server("active1"){ IsUp = false, IsWritable = true },
                new Server("active2"){ IsUp = false, IsWritable = false },
            };
            var failoverServers = new Server[]{
                new Server("failover"){ IsUp = true, IsWritable = true },
                new Server("failover"){ IsUp = true, IsWritable = false },
            };
            var cnxManager = this.CreateCnxManager(session, activeServers, failoverServers);

            var dbCnx = cnxManager.GetReadableConnection();

            Assert.Equal("failover", dbCnx.ConnectionString);
        }
        public void WhenAskForReadableConnectionAndNoUpConnectionIsAvailable_ThenThrowException()
        {
            var session = new Session();
            var activeServers = new Server[]{
                new Server("active1"){ IsUp = false, IsWritable = true },
                new Server("active2"){ IsUp = false, IsWritable = false },
            };
            var failoverServers = new Server[]{
                new Server("failover"){ IsUp = false, IsWritable = true },
                new Server("failover"){ IsUp = false, IsWritable = false },
            };
            var cnxManager = this.CreateCnxManager(session, activeServers, failoverServers);

            Assert.Throws<Exception>(() =>
            {
                var dbCnx = cnxManager.GetWritableConnection();
            });
        }
        public void WhenAskForReadableConnectionAndNoNotDelayConnectionIsAvailable_ThenThrow()
        {
            var session = new Session();
            var activeServers = new Server[]{
                new Server("active1"){ IsUp = true, IsWritable = true, DelayFromMaster = TimeSpan.FromMinutes(1) },
                new Server("active2"){ IsUp = true, IsWritable = false, DelayFromMaster = TimeSpan.FromMinutes(1) },
            };
            var failoverServers = new Server[]{
                new Server("failover"){ IsUp = true, IsWritable = true, DelayFromMaster = TimeSpan.FromMinutes(1) },
                new Server("failover"){ IsUp = true, IsWritable = false, DelayFromMaster = TimeSpan.FromMinutes(1) },
            };
            var cnxManager = this.CreateCnxManager(session, activeServers, failoverServers);

            Assert.Throws<Exception>(() =>
            {
                var dbCnx = cnxManager.GetReadableConnection();
            });
        }
        public void WhenAskForReadableConnectionAndSessionWasInWriteMode_ThenGetWritableConnection()
        {
            var session = new Session();
            var activeServers = new Server[]{
                new Server("active1"){ IsUp = true, IsWritable = true },
                new Server("active2"){ IsUp = false, IsWritable = false },
                new Server("active2"){ IsUp = false, IsWritable = false },
                new Server("active2"){ IsUp = false, IsWritable = false },
                new Server("active2"){ IsUp = false, IsWritable = false },
            };
            var cnxManager = this.CreateCnxManager(session,
                activeServers, Enumerable.Empty<Server>());

            session.SetWritableMode();

            var dbCnx = cnxManager.GetReadableConnection();

            Assert.Equal("active1", dbCnx.ConnectionString);
        }
        public void WhenAskForWritableConnection_ThenSessionGoesWritable()
        {
            var session = new Session();
            var sessionManager = new Mock<ISessionManager>(MockBehavior.Loose);
            sessionManager.Setup(c => c.SetSessionOnWritable()).Verifiable();
            var activeServers = new Server[]{
                new Server("active1"){ IsUp = true, IsWritable = true },
            };
            var cnxManager = this.CreateCnxManager(session, sessionManager,
                activeServers, Enumerable.Empty<Server>());

            Assert.False(session.IsInWritableMode());

            var dbCnx = cnxManager.GetWritableConnection();

            sessionManager.Verify(c => c.SetSessionOnWritable(), Times.Once(), "set session write not called");
        }