def test_When_stressed_Then_dont_blow_up(self): try: reap = 1 max = 4 num = 100 rs = [] p = EvictingPool(WidgetFactory(), maxAgeSecs=max, reapEverySecs=reap) for i in xrange(num): rs.append(p.checkout()) for r in rs: p.checkin(r) cnt = 0 while p.available() > 0: time.sleep(1) cnt += 1 # TODO: Likes to fail on mac if cnt > 20: self.fail( 'Failed: expected available to go down to zero. Available = %d' % p.available()) self.assertEquals(0, p.size()) finally: p.shutdown()
def test_When_many_lt_maxAge_Then_none_evicted(self): try: p = EvictingPool(WidgetFactory(), maxAgeSecs=10, reapEverySecs=1) r1 = p.checkout() r2 = p.checkout() p.checkin(r1) time.sleep(1) p.checkin(r2) self.assertEquals(2, p.available()) finally: p.shutdown()
def test_When_one_lt_and_one_gt_maxAge_Then_evict_only_gt(self): try: max = 5 p = EvictingPool(WidgetFactory(), maxAgeSecs=max, reapEverySecs=1) r1 = p.checkout() r2 = p.checkout() p.checkin(r1) time.sleep(max + 1) # induce eviction of r1 p.checkin(r2) # induce retention of r2 self.assertEquals(1, p.available()) time.sleep(max+1) # induce eviction of r2 self.assertEquals(0, p.available()) finally: p.shutdown()
def test_When_many_gt_maxAge_Then_all_evicted(self): try: max = 5 p = EvictingPool(WidgetFactory(), maxAgeSecs=max, reapEverySecs=1) r1 = p.checkout() r2 = p.checkout() p.checkin(r1) time.sleep(1) p.checkin(r2) self.assertEquals(2, p.available()) time.sleep(max+1) # induce eviction self.assertEquals(0, p.available()) finally: p.shutdown()
def test_When_one_lt_and_one_gt_maxAge_Then_evict_only_gt(self): try: max = 5 p = EvictingPool(WidgetFactory(), maxAgeSecs=max, reapEverySecs=1) r1 = p.checkout() r2 = p.checkout() p.checkin(r1) time.sleep(max + 1) # induce eviction of r1 p.checkin(r2) # induce retention of r2 self.assertEquals(1, p.available()) time.sleep(max + 1) # induce eviction of r2 self.assertEquals(0, p.available()) finally: p.shutdown()
def test_When_many_gt_maxAge_Then_all_evicted(self): try: max = 5 p = EvictingPool(WidgetFactory(), maxAgeSecs=max, reapEverySecs=1) r1 = p.checkout() r2 = p.checkout() p.checkin(r1) time.sleep(1) p.checkin(r2) self.assertEquals(2, p.available()) time.sleep(max + 1) # induce eviction self.assertEquals(0, p.available()) finally: p.shutdown()
def test_When_stressed_Then_dont_blow_up(self): try: reap = 1 max = 4 num = 100 rs = [] p = EvictingPool(WidgetFactory(), maxAgeSecs=max, reapEverySecs=reap) for i in xrange(num): rs.append(p.checkout()) for r in rs: p.checkin(r) cnt = 0 while p.available() > 0: time.sleep(1) cnt += 1 # TODO: Likes to fail on mac if cnt > 20: self.fail('Failed: expected available to go down to zero. Available = %d' % p.available()) self.assertEquals(0, p.size()) finally: p.shutdown()