def setUp(self): self.pool = PSQLPool("host=dev2",3) txnStart()
class Test(unittest.TestCase): def setUp(self): self.pool = PSQLPool("host=dev2",3) txnStart() def tearDown(self): if Server.txn.get_status() in (StatusActive, StatusMarkedRollback): Server.txn.rollback() # Verify that all the connections are back at this point. qsize = self.pool._pool.qsize() assert qsize == 3, "Qsize is %s" % qsize def testGetConnection(self): conn = self.pool.getConnection() assert conn # Verify that we always get the same connection for this # thread conn2 = self.pool.getConnection() assert conn is conn2 def testMultiThreadGetConnection(self): """Verify thread blocks if there's not enough connections.""" try: con1 = Consumer(self.pool) con2 = Consumer(self.pool) con3 = Consumer(self.pool) con1.start() con2.start() con3.start() assert con1.hasConn == False # We sholud be able to grab multiple connectinos in the # main thread and one should still be available for our consumer for x in range(10): self.pool.getConnection() con1.doWait("consume") assert con1.hasConn == True # Now consume the last connection con2.doWait("consume") assert con2.hasConn == True # We should be out of connections at this point so the # next consumer should block con3.doNoWait("consume") assert con3.hasConn == False # Put the connection back con2.doWait("rollback") assert con2.hasConn == False # Con 3 should have the connection now # Sleep long enough for thread 3 to finish sleep(.1) assert con3.hasConn == True finally: con1.stop() con2.stop() con3.stop()