def test_resize(self): pool = self.klass(max_size=2) evt = _event.Event() def wait_long_time(e): e.wait() pool.execute(wait_long_time, evt) pool.execute(wait_long_time, evt) self.assertEqual(pool.free(), 0) self.assert_pool_has_free(pool, 0) # verify that the pool discards excess items put into it pool.resize(1) # cause the wait_long_time functions to return, which will # trigger puts to the pool evt.send(None) api.sleep(0) api.sleep(0) self.assertEqual(pool.free(), 1) self.assert_pool_has_free(pool, 1) # resize larger and assert that there are more free items pool.resize(2) self.assertEqual(pool.free(), 2) self.assert_pool_has_free(pool, 2)
def test_stderr_raising(self): # testing that really egregious errors in the error handling code # (that prints tracebacks to stderr) don't cause the pool to lose # any members import sys pool = self.klass(min_size=1, max_size=1) def crash(*args, **kw): raise RuntimeError("Whoa") class FakeFile(object): write = crash # we're going to do this by causing the traceback.print_exc in # safe_apply to raise an exception and thus exit _main_loop normal_err = sys.stderr try: sys.stderr = FakeFile() waiter = pool.execute(crash) self.assertRaises(RuntimeError, waiter.wait) # the pool should have something free at this point since the # waiter returned # pool.Pool change: if an exception is raised during execution of a link, # the rest of the links are scheduled to be executed on the next hub iteration # this introduces a delay in updating pool.sem which makes pool.free() report 0 # therefore, sleep: api.sleep(0) self.assertEqual(pool.free(), 1) # shouldn't block when trying to get t = api.exc_after(0.1, api.TimeoutError) try: pool.execute(api.sleep, 1) finally: t.cancel() finally: sys.stderr = normal_err