def test_blocks_on_pool(self): waiter = coros.queue(0) def greedy(): self.pool.get() self.pool.get() self.pool.get() self.pool.get() # No one should be waiting yet. self.assertEquals(self.pool.waiting(), 0) # The call to the next get will unschedule this routine. self.pool.get() # So this send should never be called. waiter.send('Failed!') killable = api.spawn(greedy) # no one should be waiting yet. self.assertEquals(self.pool.waiting(), 0) ## Wait for greedy api.sleep(0) ## Greedy should be blocking on the last get self.assertEquals(self.pool.waiting(), 1) ## Send will never be called, so balance should be 0. self.assertFalse(waiter.ready()) api.kill(killable)
def test_killing_dormant(self): DELAY = 0.1 state = [] def test(): try: state.append('start') api.sleep(DELAY) except: state.append('except') # catching GreenletExit pass # when switching to hub, hub makes itself the parent of this greenlet, # thus after the function's done, the control will go to the parent api.sleep(0) state.append('finished') g = api.spawn(test) api.sleep(DELAY / 2) self.assertEqual(state, ['start']) api.kill(g) # will not get there, unless switching is explicitly scheduled by kill self.assertEqual(state, ['start', 'except']) api.sleep(DELAY) self.assertEqual(state, ['start', 'except', 'finished'])
def read_client(self, json_socket): global running while True: api.sleep(0) data = json_socket.recv() if not data: # client disconnected, bail out if self.current: self.current.out_queue.put(["quit"]) break if data[0] == 'connect': if not running: # initial connect command running = GreenletsThread(*data[1:]) self.current = running api.spawn(running.run) for cmd in self.deferred_cmds: running.addCmd(cmd) self.deferred_cmds = [] json_socket.send(["hihi"]) else: running.addCmd(["bootstrap"]) elif self.current: # forward command self.current.addCmd(data) else: if data[0] in ["throttle"]: self.deferred_cmds.append(data) print("exit read client") # exit self.connected = False
def prepare(self, local_uri=None, logger=None): """Start a listening port specified by local_uri if there isn't one on that port/interface already. Add `local_uri' to the list of expected URIs, so that incoming connections featuring this URI won't be rejected. If `logger' is provided use it for this connection instead of the default one. """ if local_uri is None: local_uri = self.generate_local_uri(2855) need_listen = True if local_uri.port: use_tls, listening_port = self.ports.get(local_uri.host, {}).get(local_uri.port, (None, None)) if listening_port is not None: if use_tls==local_uri.use_tls: need_listen = False else: listening_port.stopListening() sleep(0) # make the reactor really stop listening, so that the next listen() call won't fail self.ports.pop(local_uri.host, {}).pop(local_uri.port, None) else: # caller does not care about port number for (use_tls, port) in self.ports[local_uri.host]: if local_uri.use_tls==use_tls: local_uri.port = port.getHost().port need_listen = False if need_listen: port = self._listen(local_uri, self.factory) self.ports.setdefault(local_uri.host, {})[local_uri.port] = (local_uri.use_tls, port) self.expected_local_uris[local_uri] = logger return [local_uri]
def handle_request(self, req): path = req.path_segments() if len(path) > 0 and path[0] == "notexist": req.response(404, body='not found') return api.sleep(0.2) req.write('hello world\n')
def handle_client(self, json_socket): global running global run_main self.connected = True api.spawn(self.read_client, json_socket) if running: json_socket.send(["state", "connected"]) else: json_socket.send(["state", "idle"]) starttime = time.time() while self.connected: if self.current: cmd = self.current.out_queue.get() if cmd[0] == "quit": print("quit on handle client") break; else: json_socket.send(cmd) #for cmd in self.current.queue.get(): # json_socket.send(cmd) #api.sleep(0) api.sleep(0) json_socket.close() if running: running.addCmd(["quit"]) running = None sys.exit()
def runloop(self): while self.keep_going: api.sleep(0.1) ## Only start the number of children we need number = self.num_processes - len(self.child_pipes.keys()) if number > 0: self.log.debug('Should start %d new children' % number) self.spawn_children(number=number) continue if not self.child_pipes.keys(): ## If we don't yet have children, let's loop continue pid, result = None, None try: pid, result = os.wait() except OSError, e: if e.errno != errno.EINTR: raise if pid and self.child_pipes.get(pid): self.child_pipes.pop(pid) if result: signum = os.WTERMSIG(result) exitcode = os.WEXITSTATUS(result) self.log.info('(%s) Child died from signal %s with code %s' % ( pid, signum, exitcode))
def test_ref(self): err = Error() err_ref = weakref.ref(err) with timeout(DELAY*2, err): sleep(DELAY) del err assert not err_ref(), repr(err_ref())
def test_ref(self): err = Error() err_ref = weakref.ref(err) with timeout(DELAY * 2, err): sleep(DELAY) del err assert not err_ref(), repr(err_ref())
def test_timeout_cancel(self): server = eventlet.listen(('0.0.0.0', 0)) bound_port = server.getsockname()[1] done = [False] def client_closer(sock): while True: (conn, addr) = sock.accept() conn.close() def go(): desc = eventlet.connect(('127.0.0.1', bound_port)) try: api.trampoline(desc, read=True, timeout=0.1) except api.TimeoutError: assert False, "Timed out" server.close() desc.close() done[0] = True greenthread.spawn_after_local(0, go) server_coro = api.spawn(client_closer, server) while not done[0]: api.sleep(0) api.kill(server_coro) check_hub()
def test_multiple_waiters(self): # tests that multiple waiters get their results back q = coros.queue() def waiter(q, evt): evt.send(q.wait()) sendings = ['1', '2', '3', '4'] evts = [coros.event() for x in sendings] for i, x in enumerate(sendings): api.spawn(waiter, q, evts[i]) api.sleep(0.01) # get 'em all waiting results = set() def collect_pending_results(): for i, e in enumerate(evts): timer = api.exc_after(0.001, api.TimeoutError) try: x = e.wait() results.add(x) timer.cancel() except api.TimeoutError: pass # no pending result at that event return len(results) q.send(sendings[0]) self.assertEquals(collect_pending_results(), 1) q.send(sendings[1]) self.assertEquals(collect_pending_results(), 2) q.send(sendings[2]) q.send(sendings[3]) self.assertEquals(collect_pending_results(), 4)
def test_multiple(self): self.actor = IncrActor(concurrency=2) total = [0] def received((func, ev, value)): func() total[0] += value ev.send() self.actor.received = received def onemoment(): api.sleep(0.1) evt = coros.event() evt1 = coros.event() self.actor.cast((onemoment, evt, 1)) self.actor.cast((lambda: None, evt1, 2)) evt1.wait() self.assertEqual(total[0], 2) # both coroutines should have been used self.assertEqual(self.actor._pool.current_size, 2) api.sleep(0) self.assertEqual(self.actor._pool.free(), 1) evt.wait() self.assertEqual(total[0], 3) api.sleep(0) self.assertEqual(self.actor._pool.free(), 2)
def cooperative_wait(pobj, check_interval=0.01): """ Waits for a child process to exit, returning the status code. Unlike ``os.wait``, :func:`cooperative_wait` does not block the entire process, only the calling coroutine. If the child process does not die, :func:`cooperative_wait` could wait forever. The argument *check_interval* is the amount of time, in seconds, that :func:`cooperative_wait` will sleep between calls to ``os.waitpid``. """ try: while True: status = pobj.poll() if status >= 0: return status api.sleep(check_interval) except OSError, e: if e.errno == errno.ECHILD: # no child process, this happens if the child process # already died and has been cleaned up, or if you just # called with a random pid value return -1 else: raise
def test_sleeping_during_received(self): # ensure that even if the received method cooperatively # yields, eventually all messages are delivered msgs = [] waiters = [] def received((message, evt)): api.sleep(0) msgs.append(message) evt.send() self.actor.received = received waiters.append(coros.event()) self.actor.cast((1, waiters[-1])) api.sleep(0) waiters.append(coros.event()) self.actor.cast((2, waiters[-1])) waiters.append(coros.event()) self.actor.cast((3, waiters[-1])) api.sleep(0) waiters.append(coros.event()) self.actor.cast((4, waiters[-1])) waiters.append(coros.event()) self.actor.cast((5, waiters[-1])) for evt in waiters: evt.wait() self.assertEqual(msgs, [1, 2, 3, 4, 5])
def release(self): self.__count += 1 if self.coro_queue: ready_event = self.coro_queue.pop() ready_event.send(True) api.sleep(0)
def test_child_process_death(self): prox = saranwrap.wrap({}) pid = saranwrap.getpid(prox) self.assertEqual(os.kill(pid, 0), None) # assert that the process is running del prox # removing all references to the proxy should kill the child process api.sleep(0.1) # need to let the signal handler run self.assertRaises(OSError, os.kill, pid, 0) # raises OSError if pid doesn't exist
def test_killing_dormant(self): state = [] def test(): try: state.append('start') sleep(DELAY) except: state.append('except') # catching GreenletExit pass # when switching to hub, hub makes itself the parent of this greenlet, # thus after the function's done, the control will go to the parent # QQQ why the first sleep is not enough? sleep(0) state.append('finished') g = spawn(test) sleep(DELAY / 2) assert state == ['start'], state kill(g) # will not get there, unless switching is explicitly scheduled by kill assert state == ['start', 'except'], state sleep(DELAY) assert state == ['start', 'except', 'finished'], state
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_sleeping_during_received(self): # ensure that even if the received method cooperatively # yields, eventually all messages are delivered msgs = [] waiters = [] def received( (message, evt) ): api.sleep(0) msgs.append(message) evt.send() self.actor.received = received waiters.append(coros.event()) self.actor.cast( (1, waiters[-1])) api.sleep(0) waiters.append(coros.event()) self.actor.cast( (2, waiters[-1]) ) waiters.append(coros.event()) self.actor.cast( (3, waiters[-1]) ) api.sleep(0) waiters.append(coros.event()) self.actor.cast( (4, waiters[-1]) ) waiters.append(coros.event()) self.actor.cast( (5, waiters[-1]) ) for evt in waiters: evt.wait() self.assertEqual(msgs, [1,2,3,4,5])
def _test_return(self, p, first_time, result, kill_exc_type, action): event, receiver, proc_flag, queue, callback_flag = self.set_links(p, first_time, kill_exc_type) # stuff that will time out because there's no unhandled exception: xxxxx = self.set_links_timeout(p.link_exception) try: sleep(DELAY*2) except kill_exc_type: assert first_time, 'raising here only first time' else: assert not first_time, 'Should not raise LinkedKilled here after first time' assert not p, p self.assertEqual(event.wait(), result) self.assertEqual(queue.wait(), result) self.assertRaises(kill_exc_type, receiver.wait) self.assertRaises(kill_exc_type, proc.waitall, [receiver]) sleep(DELAY) assert not proc_flag, proc_flag assert not callback_flag, callback_flag self.check_timed_out(*xxxxx)
def test_multiple_listeners_error(self): # if there was an error while calling a callback # it should not prevent the other listeners from being called # also, all of the errors should be logged, check the output # manually that they are p = proc.spawn(lambda : 5) results = [] def listener1(*args): results.append(10) 1/0 def listener2(*args): results.append(20) 2/0 def listener3(*args): 3/0 p.link(listener1) p.link(listener2) p.link(listener3) sleep(DELAY*10) assert results in [[10, 20], [20, 10]], results p = proc.spawn(int, 'hello') results = [] p.link(listener1) p.link(listener2) p.link(listener3) sleep(DELAY*10) assert results in [[10, 20], [20, 10]], results
def test_multiple(self): self.actor = IncrActor(concurrency=2) total = [0] def received( (func, ev, value) ): func() total[0] += value ev.send() self.actor.received = received def onemoment(): api.sleep(0.1) evt = coros.event() evt1 = coros.event() self.actor.cast( (onemoment, evt, 1) ) self.actor.cast( (lambda: None, evt1, 2) ) evt1.wait() self.assertEqual(total[0], 2) # both coroutines should have been used self.assertEqual(self.actor._pool.current_size, 2) api.sleep(0) self.assertEqual(self.actor._pool.free(), 1) evt.wait() self.assertEqual(total[0], 3) api.sleep(0) self.assertEqual(self.actor._pool.free(), 2)
def handle_client(self, json_socket): global running global run_main self.connected = True api.spawn(self.read_client, json_socket) if running: json_socket.send(["state", "connected"]) else: json_socket.send(["state", "idle"]) starttime = time.time() while self.connected: if self.current: cmd = self.current.out_queue.get() if cmd[0] == "quit": print("quit on handle client") break else: json_socket.send(cmd) #for cmd in self.current.queue.get(): # json_socket.send(cmd) #api.sleep(0) api.sleep(0) json_socket.close() if running: running.addCmd(["quit"]) running = None sys.exit()
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
def test_execute_async(self): p = self.klass(max_size=2) self.assertEqual(p.free(), 2) r = [] def foo(a): r.append(a) evt = p.execute(foo, 1) self.assertEqual(p.free(), 1) evt.wait() self.assertEqual(r, [1]) api.sleep(0) self.assertEqual(p.free(), 2) #Once the pool is exhausted, calling an execute forces a yield. p.execute_async(foo, 2) self.assertEqual(1, p.free()) self.assertEqual(r, [1]) p.execute_async(foo, 3) self.assertEqual(0, p.free()) self.assertEqual(r, [1]) p.execute_async(foo, 4) self.assertEqual(r, [1,2,3]) api.sleep(0) self.assertEqual(r, [1,2,3,4])
def test_return(self): def return25(): return 25 p = self.p = proc.spawn(return25) self._test_return(p, True, 25, proc.LinkedCompleted, lambda : sleep(0)) # repeating the same with dead process for _ in xrange(3): self._test_return(p, False, 25, proc.LinkedCompleted, lambda : sleep(0))
def test_ordering(self): lst = [] hubs.get_hub().schedule_call_global(DELAY*2, lst.append, 3) hubs.get_hub().schedule_call_global(DELAY, lst.append, 1) hubs.get_hub().schedule_call_global(DELAY, lst.append, 2) while len(lst) < 3: api.sleep(DELAY) self.assertEquals(lst, [1,2,3])
def agent_updater(self): """ Sends AgentUpdate message every so often, for movement purposes. Needs a better name """ while self.connected: self._send_update() eventlet.sleep(1.0/self.settings.AGENT_UPDATES_PER_SECOND)
def test_spawn_is_not_cancelled(self): def func(): spawn(self.lst.pop) # exiting immediatelly, but self.lst.pop must be called spawn(func) sleep(0.1) assert self.lst == [], self.lst
def test_timer_cancelled_upon_greenlet_exit(self): def func(): call_after(0.1, self.lst.pop) spawn(func) assert self.lst == [1], self.lst sleep(0.2) assert self.lst == [1], self.lst
def test_ordering(self): lst = [] hubs.get_hub().schedule_call_global(DELAY * 2, lst.append, 3) hubs.get_hub().schedule_call_global(DELAY, lst.append, 1) hubs.get_hub().schedule_call_global(DELAY, lst.append, 2) while len(lst) < 3: api.sleep(DELAY) self.assertEquals(lst, [1, 2, 3])
def wait(self, time_out=0): start = now = time.time() while self.waiting and now - start <= time_out: api.sleep(0) now = time.time() if now - start > time_out: self.timed_out = True self.waiting = True
def main(): parser = optparse.OptionParser(usage='%prog --port=PORT') parser.add_option('--port', default='8080', dest='login_port', type='int', help='Port to serve on (default 8080)') parser.add_option( '--loginuri', default='https://login.aditi.lindenlab.com/cgi-bin/login.cgi', dest='loginuri', help='Specifies the target loginuri to connect proxy to') parser.add_option( '-v', '--verbose', default=False, dest='verbose', action="store_true", help='enables logging, sets logging level to info, logs names of all \ packets') parser.add_option( '-V', '--verboseverbose', default=False, dest='verboseverbose', action="store_true", help='enables logging, sets logging level to debug, logs contents \ of all packets') options, args = parser.parse_args() # init logging if options.verbose or options.verboseverbose: console = logging.StreamHandler() console.setLevel( logging.DEBUG) # seems to be a no op, set it for the logger formatter = logging.Formatter( '%(asctime)-30s%(name)-30s: %(levelname)-8s %(message)s') console.setFormatter(formatter) logging.getLogger('').addHandler(console) if options.verbose: logging.getLogger('').setLevel(logging.INFO) elif options.verboseverbose: logging.getLogger('').setLevel(logging.DEBUG) else: logging.getLogger('').setLevel(logging.WARNING) # init the viewer proxy viewer_proxy = ViewerProxyApp(options.loginuri, options.login_port) # spawn a coroutine which initially handles the login rpxy, then lives on api.spawn(viewer_proxy.proxy_login) # keep running until we intercept a signal while viewer_proxy._is_running: api.sleep(5)
def test(self): try: sock = socket.socket() api.call_after(0, sock.close) sock.connect(('python.org', 81)) except Exception: api.sleep(0) else: assert False, 'expected an error here'
def test_timer_fired(self): def func(): call_after(0.1, self.lst.pop) sleep(0.2) spawn(func) assert self.lst == [1], self.lst sleep(0.3) assert self.lst == [], self.lst
def test_start_stop_monitors(self): self.message_manager.start_monitors() eventlet.sleep(0) self.assertTrue(self.message_manager._is_running) self.assertTrue(self.message_manager.event_queue._running) self.message_manager.stop_monitors() eventlet.sleep(2) self.assertFalse(self.message_manager._is_running) self.assertTrue(self.message_manager.event_queue.stopped) self.assertFalse(self.message_manager.event_queue._running)
def test_nested_timeout(self): with timeout(DELAY, None): with timeout(DELAY * 2, None): sleep(DELAY * 3) raise AssertionError('should not get there') with timeout(DELAY, _SilentException()): with timeout(DELAY * 2, _SilentException()): sleep(DELAY * 3) raise AssertionError('should not get there')
def test_nested_timeout(self): with timeout(DELAY, None): with timeout(DELAY*2, None): sleep(DELAY*3) raise AssertionError('should not get there') with timeout(DELAY, _SilentException()): with timeout(DELAY*2, _SilentException()): sleep(DELAY*3) raise AssertionError('should not get there')
def test_reader_failed__send(self): client, server = proc.waitall(self.setup_two_endpoints()) client, server = GreenMSRPSession(client), GreenMSRPSession(server) client.reader_job.kill(InjectedError("Killing client's reader_job")) api.sleep(0.1) self.assertRaises(MSRPSessionError, client.send_chunk, self.make_hello(client.msrp)) self.assertRaises(InjectedError, client.receive_chunk) api.sleep(0.1) self.assertRaises(MSRPSessionError, server.send_chunk, self.make_hello(server.msrp)) self.assertRaises(ConnectionClosed, server.receive_chunk)
def killall(procs, *throw_args, **kwargs): if not throw_args: throw_args = (ProcExit, ) wait = kwargs.pop('wait', False) if kwargs: raise TypeError('Invalid keyword argument for proc.killall(): %s' % ', '.join(kwargs.keys())) for g in procs: if not g.dead: hubs.get_hub().schedule_call_global(0, g.throw, *throw_args) if wait and api.getcurrent() is not hubs.get_hub().greenlet: api.sleep(0)
def main(): """ In stand alone mode we will open a port and accept commands. """ server = eventlet.listen(('0.0.0.0', 11112)) #pool = eventlet.GreenPool(1000) while run_main: new_sock, address = server.accept() client_handler = ClientHandler() api.spawn(client_handler.handle_client, JsonSocket(new_sock)) api.sleep(0)