def test_many_hubs(self): # Create and close a Hub in many threads. If the hub does not garbage # collect, then this will run out of file descriptors. for i in range(100): hub = gruvi.Hub() with gruvi.switch_back(timeout=0, hub=hub): self.assertRaises(gruvi.Timeout, hub.switch) hub.close() hub.switch() gc.collect()
def test_callback_order(self): # Callbacks run with run_callback() should be run in the order they # were added. hub = gruvi.Hub() result = [] for i in range(100): hub.run_callback(result.append, i) hub.close() hub.switch() self.assertEqual(len(result), 100) self.assertEqual(result, list(range(100)))
def test_close_from_thread(self): # Calling hub.close() from a thread should cause the hub to exit. def close_hub(): time.sleep(0.01) hub.close() t1 = threading.Thread(target=close_hub) t1.start() hub = gruvi.Hub() self.assertIsNone(hub.switch()) self.assertFalse(hub.is_alive()) t1.join()
def test_cleanup(self): # After we close() a Hub, it should be garbage collectable, including # its event loop. hub = gruvi.Hub() gruvi.sleep(0) ref1 = weakref.ref(hub) ref2 = weakref.ref(hub.loop) hub.close() hub.switch() del hub gc.collect() self.assertIsNone(ref1()) self.assertIsNone(ref2())
def mem_hub(self): self.add_result(sizeof(gruvi.Hub(), exclude=('_log', '_thread')))
def test_close(self): # Calling hub.close() should cause the hub to exit. hub = gruvi.Hub() hub.run_callback(hub.close) self.assertIsNone(hub.switch()) self.assertFalse(hub.is_alive())