def test_pool(self): listener = StatsLogger() pool = ConnectionPool(pool_size=5, max_overflow=5, recycle=10000, prefill=True, pool_timeout=0.1, timeout=1, keyspace='PycassaTestKeyspace', credentials=_credentials, listeners=[listener], use_threadlocal=False) conns = [] for i in range(10): conns.append(pool.get()) assert_equal(listener.stats['created']['success'], 10) assert_equal(listener.stats['created']['failure'], 0) assert_equal(listener.stats['checked_out'], 10) assert_equal(listener.stats['opened'], {'current': 10, 'max': 10}) # Pool is maxed out now assert_raises(NoConnectionAvailable, pool.get) assert_equal(listener.stats['created']['success'], 10) assert_equal(listener.stats['checked_out'], 10) assert_equal(listener.stats['opened'], {'current': 10, 'max': 10}) assert_equal(listener.stats['at_max'], 1) for i in range(0, 5): pool.return_conn(conns[i]) assert_equal(listener.stats['disposed']['success'], 0) assert_equal(listener.stats['checked_in'], 5) assert_equal(listener.stats['opened'], {'current': 5, 'max': 10}) for i in range(5, 10): pool.return_conn(conns[i]) assert_equal(listener.stats['disposed']['success'], 5) assert_equal(listener.stats['checked_in'], 10) conns = [] # These connections should come from the pool for i in range(5): conns.append(pool.get()) assert_equal(listener.stats['created']['success'], 10) assert_equal(listener.stats['checked_out'], 15) # But these will need to be made for i in range(5): conns.append(pool.get()) assert_equal(listener.stats['created']['success'], 15) assert_equal(listener.stats['checked_out'], 20) assert_equal(listener.stats['disposed']['success'], 5) for i in range(10): conns[i].return_to_pool() assert_equal(listener.stats['checked_in'], 20) assert_equal(listener.stats['disposed']['success'], 10) assert_raises(InvalidRequestError, conns[0].return_to_pool) assert_equal(listener.stats['checked_in'], 20) assert_equal(listener.stats['disposed']['success'], 10) print "in test:", id(conns[-1]) assert_raises(InvalidRequestError, conns[-1].return_to_pool) assert_equal(listener.stats['checked_in'], 20) assert_equal(listener.stats['disposed']['success'], 10) pool.dispose()
def setUp(self): super(TestStatsLogger, self).setUp() self.logger = StatsLogger()