def test_thread_safety(self): event = gruvi.Event() result = [] def run_test(): timeout = random.choice((None, 0.1)) result.append(event.wait(timeout=timeout)) def run_thread(): fibers = [] for i in range(self.nfibers): fibers.append(gruvi.spawn(run_test)) for fib in fibers: fib.join() threads = [] for i in range(self.nthreads): thread = threading.Thread(target=run_thread) thread.start() threads.append(thread) gruvi.sleep(0.5) event.set() for thread in threads: thread.join() timeouts = result.count(False) notified = result.count(True) self.assertEqual(timeouts + notified, self.nthreads * self.nfibers) self.assertGreater(timeouts, 0) self.assertGreater(notified, 0) self.assertFalse(event._callbacks)
def test_basic(self): # Ensure that an event can be set and cleared event = gruvi.Event() self.assertFalse(event.is_set()) event.set() self.assertTrue(event.is_set()) event.clear() self.assertFalse(event.is_set())
def test_wait(self): event = gruvi.Event() done = [] def waiter(): done.append(False) done.append(event.wait()) gruvi.spawn(waiter) gruvi.sleep(0) self.assertEqual(done, [False]) event.set() gruvi.sleep(0) self.assertEqual(done, [False, True])
parser = argparse.ArgumentParser() parser.add_argument('hostname') parser.add_argument('port', type=int) parser.add_argument('--ssl', action='store_true') args = parser.parse_args() remote = gruvi.StreamClient() remote.connect((args.hostname, args.port), ssl=args.ssl) stdin = gruvi.StreamClient() stdin.connect(sys.stdin.fileno(), mode='r') stdout = gruvi.StreamClient() stdout.connect(sys.stdout.fileno(), mode='w') done = gruvi.Event() def stdin_reader(): while True: buf = stdin.read1() if not buf: print('Got EOF on stdin') break remote.write(buf) done.set() def remote_reader(): while True: buf = remote.read1() if not buf: print('Got EOF from remote')
def mem_event(self): self.add_result(sizeof(gruvi.Event()))
def test_wait_timeout(self): event = gruvi.Event() self.assertFalse(event.wait(timeout=0.01)) self.assertFalse(event._callbacks)