def test_singleton_thread_failover(self):
        listener = _TestListener()
        pool = SingletonThreadPool(pool_size=5,
                                   keyspace='Keyspace1', credentials=_credentials,
                                   listeners=[listener],
                                   server_list=['localhost:9160', 'localhost:9160'])

        conn = pool.get()
        cf = ColumnFamily(conn, 'Standard1')

        for i in range(1,5):
            setattr(cf.client._local.conn.client, 'batch_mutate', _timeout)

            # The first insert attempt should fail, but failover should occur
            # and the insert should succeed
            cf.insert('key', {'col': 'val'})
            assert_equal(listener.failure_count, i)
            cf.get('key')

        pool.dispose()
        listener.reset()

        pool = SingletonThreadPool(pool_size=5,
                                   keyspace='Keyspace1', credentials=_credentials,
                                   listeners=[listener],
                                   server_list=['localhost:9160', 'localhost:9160'])

        threads = []
        args = (pool, 'key', {'col':'val'})
        for j in range(0,5):
            threads.append(threading.Thread(target=_five_tlocal_fails, args=args))
            threads[-1].start()
        for thread in threads:
            thread.join()

        assert_equal(listener.failure_count, 25)
    def test_pool_connection_failure(self):
        listener = _TestListener()
 
        def get_extra():
            """Make failure count adjustments based on whether or not
            the permuted list starts with a good host:port"""
            if listener.serv_list[0] == 'localhost:9160':
                return 0
            else:
                return 1

        pool = QueuePool(pool_size=5, max_overflow=5, recycle=10000,
                         prefill=True,
                         keyspace='Keyspace1', credentials=_credentials,
                         timeout=0.05,
                         listeners=[listener], use_threadlocal=False,
                         server_list=['localhost:9160', 'foobar:1'])

        assert_equal(listener.failure_count, 4 + get_extra()) 

        for i in range(0,7):
            pool.get()

        assert_equal(listener.failure_count, 6 + get_extra())

        pool.dispose()
        listener.reset()

        pool = QueuePool(pool_size=5, max_overflow=5, recycle=10000,
                         prefill=True,
                         keyspace='Keyspace1', credentials=_credentials,
                         timeout=0.05,
                         listeners=[listener], use_threadlocal=True,
                         server_list=['localhost:9160', 'foobar:1'])

        assert_equal(listener.failure_count, 4 + get_extra())

        threads = []
        for i in range(0, 7):
            threads.append(threading.Thread(target=pool.get))
            threads[-1].start()
        for thread in threads:
            thread.join()

        assert_equal(listener.failure_count, 6 + get_extra())

        pool.dispose()
        listener.reset()

        pool = SingletonThreadPool(pool_size=5, keyspace='Keyspace1',
                         credentials=_credentials, listeners=[listener],
                         timeout=0.05,
                         server_list=['localhost:9160', 'foobar:1'])

        threads = []
        for i in range(0, 5):
            threads.append(threading.Thread(target=pool.get))
            threads[-1].start()
        for thread in threads:
            thread.join()

        assert_equal(listener.failure_count, 4 + get_extra())

        pool.dispose()
        listener.reset()

        pool = StaticPool(keyspace='Keyspace1', credentials=_credentials,
                         timeout=0.05, listeners=[listener],
                         server_list=['localhost:9160', 'foobar:1'])

        pool.get()
        assert_equal(listener.failure_count, 0 + get_extra())
        
        pool.dispose()
        listener.reset()

        pool = NullPool(keyspace='Keyspace1', credentials=_credentials,
                         timeout=0.05,
                         listeners=[listener], use_threadlocal=False,
                         server_list=['localhost:9160', 'foobar:1'])
 
        for i in range(0, 5):
            pool.get()

        assert_equal(listener.failure_count, 4 + get_extra()) 

        pool.dispose()
        listener.reset()
       
        pool = AssertionPool(keyspace='Keyspace1', credentials=_credentials,
                             timeout=0.05,
                             listeners=[listener], use_threadlocal=False,
                             server_list=['localhost:9160', 'foobar:1'])
        while True:
            if listener.serv_list[0] == 'foobar:1':
                break
            pool.set_server_list(['localhost:9160', 'foobar:1'])

        conn = pool.get()
        assert_equal(listener.failure_count, 1) 
        
        pool.dispose()
        listener.reset()
 
        pool = AssertionPool(keyspace='Keyspace1', credentials=_credentials,
                             timeout=0.05,
                             listeners=[listener], use_threadlocal=True,
                             server_list=['localhost:9160', 'foobar:1'])
        while True:
            if listener.serv_list[0] == 'foobar:1':
                break
            pool.set_server_list(['localhost:9160', 'foobar:1'])

        conn = pool.get()
        assert_equal(listener.failure_count, 1) 

        pool.dispose()
    def test_singleton_pool_threadlocal(self):
        """Should be the same as non-threadlocal."""
        listener = _TestListener()
        pool = SingletonThreadPool(keyspace='Keyspace1',
                         credentials=_credentials, pool_size=5,
                         listeners=[listener], use_threadlocal=True)

        # Make sure we get the same connection every time
        conn = pool.get()
        assert_equal(pool.get(), conn)
        for i in range(10):
            conn = pool.get()
        assert_equal(listener.connect_count, 1)

        pool.return_conn(conn)

        conn = pool.get()
        assert_equal(pool.get(), conn)
        assert_equal(listener.connect_count, 2)
        pool.return_conn(conn)

        def get_return():
            conn = pool.get()
            print pool.status()

        threads = []
        for i in range(5):
            threads.append(threading.Thread(target=get_return))
            threads[-1].start()
        for thread in threads:
            thread.join()
        assert_equal(listener.connect_count, 7)

        assert_raises(NoConnectionAvailable, pool.get)
        assert_equal(listener.max_count, 1)