def do_receive(q, evt): api.exc_after(0, RuntimeError()) try: result = q.wait() evt.send(result) except RuntimeError: evt.send('timed out')
def test_timeout_and_final_write(self): # This test verifies that a write on a socket that we've # stopped listening for doesn't result in an incorrect switch rpipe, wpipe = os.pipe() rfile = os.fdopen(rpipe,"rb",1) wrap_rfile = greenio.GreenPipe(rfile) wfile = os.fdopen(wpipe,"wb",1) wrap_wfile = greenio.GreenPipe(wfile) def sender(evt): api.sleep(0.02) wrap_wfile.write('hi') evt.send('sent via event') from eventlib import coros evt = coros.event() api.spawn(sender, evt) try: # try and get some data off of this pipe # but bail before any is sent api.exc_after(0.01, api.TimeoutError) _c = wrap_rfile.read(1) self.fail() except api.TimeoutError: pass result = evt.wait() self.assertEqual(result, 'sent via event')
def do_receive(tp): api.exc_after(0, RuntimeError()) try: t = tp.get() self.fail("Shouldn't have recieved anything from the pool") except RuntimeError: return 'timed out'
def test_double_exception(self): evt = coros.event() # send an exception through the event evt.send(exc=RuntimeError('from test_double_exception')) self.assertRaises(RuntimeError, evt.wait) evt.reset() # shouldn't see the RuntimeError again api.exc_after(0.001, api.TimeoutError('from test_double_exception')) self.assertRaises(api.TimeoutError, evt.wait)
def test_send(self): event1 = event() event2 = event() spawn(event1.send, 'hello event1') exc_after(0, ValueError('interrupted')) try: result = event1.wait() except ValueError: X = object() result = with_timeout(DELAY, event2.wait, timeout_value=X) assert result is X, 'Nobody sent anything to event2 yet it received %r' % ( result, )
def connect(self, db_module, connect_timeout, *args, **kw): timeout = api.exc_after(connect_timeout, ConnectTimeout()) try: from eventlib import saranwrap return saranwrap.wrap(db_module).connect(*args, **kw) finally: timeout.cancel()
def test_del_closes_socket(self): timer = api.exc_after(0.5, api.TimeoutError) def accept_once(listener): # delete/overwrite the original conn # object, only keeping the file object around # closing the file object should close everything try: conn, addr = listener.accept() conn = conn.makeGreenFile() conn.write('hello\n') conn.close() self.assertRaises(socket.error, conn.write, 'a') finally: listener.close() server = api.tcp_listener(('0.0.0.0', 0)) killer = api.spawn(accept_once, server) client = api.connect_tcp(('127.0.0.1', server.getsockname()[1])) fd = client.makeGreenFile() client.close() assert fd.read() == b'hello\n' assert fd.read() == '' timer.cancel()
def test_waiters_get_woken(self): # verify that when there's someone waiting on an empty pool # and someone puts an immediately-closed connection back in # the pool that the waiter gets woken self.pool = self.create_pool(max_size=1, max_age=0) conn = self.pool.get() self.assertEqual(self.pool.free(), 0) self.assertEqual(self.pool.waiting(), 0) e = coros.event() def retrieve(pool, ev): c = pool.get() ev.send(c) api.spawn(retrieve, self.pool, e) api.sleep(0) # these two sleeps should advance the retrieve api.sleep(0) # coroutine until it's waiting in get() self.assertEqual(self.pool.free(), 0) self.assertEqual(self.pool.waiting(), 1) self.pool.put(conn) timer = api.exc_after(0.3, api.TimeoutError) conn = e.wait() timer.cancel() self.assertEqual(self.pool.free(), 0) self.assertEqual(self.pool.waiting(), 0)
def handle(self): self.close_connection = 0 timeout = DEFAULT_TIMEOUT while not self.close_connection: if timeout == 0: break cancel = api.exc_after(timeout, Timeout) try: self.raw_requestline = self.rfile.readline(MAX_REQUEST_LINE) if self.raw_requestline is not None: if len(self.raw_requestline) == MAX_REQUEST_LINE: # Someone sent a request line which is too # large. Be helpful and tell them. self.write_bad_request(414, 'Request-URI Too Long') self.close_connection = True continue except socket.error, e: if e[0] in CONNECTION_CLOSED: self.close_connection = True cancel.cancel() continue except Timeout: self.close_connection = True continue
def test_003_passing_non_int_to_read(self): # This should go in greenio_test sock = api.connect_tcp(('127.0.0.1', 12346)) fd = sock.makeGreenFile() fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n') cancel = api.exc_after(1, RuntimeError) self.assertRaises(TypeError, fd.read, "This shouldn't work") cancel.cancel() fd.close()
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)
def connect(self, db_module, connect_timeout, *args, **kw): timeout = api.exc_after(connect_timeout, ConnectTimeout()) try: from eventlib import tpool try: # *FIX: this is a huge hack that will probably only work for MySQLdb autowrap = (db_module.cursors.DictCursor, ) except: autowrap = () conn = tpool.execute(db_module.connect, *args, **kw) return tpool.Proxy(conn, autowrap=autowrap) finally: timeout.cancel()
def test_putting_to_queue(self): timer = api.exc_after(0.1, api.TimeoutError) size = 2 self.pool = IntPool(min_size=0, max_size=size) queue = coros.queue() results = [] def just_put(pool_item, index): self.pool.put(pool_item) queue.send(index) for index in xrange(size + 1): pool_item = self.pool.get() api.spawn(just_put, pool_item, index) while results != range(size + 1): x = queue.wait() results.append(x) timer.cancel()
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 setUp(self): # raise an exception if we're waiting forever self._cancel_timeout = api.exc_after( 1, RuntimeError('test takes too long'))
def handle(self): self.close_connection = 0 timeout = DEFAULT_TIMEOUT while not self.close_connection: if timeout == 0: break cancel = api.exc_after(timeout, Timeout) try: self.raw_requestline = self.rfile.readline(MAX_REQUEST_LINE) if self.raw_requestline is not None: if len(self.raw_requestline) == MAX_REQUEST_LINE: # Someone sent a request line which is too # large. Be helpful and tell them. self.write_bad_request(414, 'Request-URI Too Long') self.close_connection = True continue except socket.error as e: if e[0] in CONNECTION_CLOSED: self.close_connection = True cancel.cancel() continue except Timeout: self.close_connection = True continue except Exception as e: try: if e[0][0][0].startswith('SSL'): print("SSL Error:", e[0][0]) self.close_connection = True cancel.cancel() continue except Exception as f: print("Exception in ssl test:", f) pass raise e cancel.cancel() if not self.raw_requestline or not self.parse_request(): self.close_connection = True continue self.set_response_code(None, 200, None) request = Request(self, self.command, self.path, self.headers) request.set_header('Server', self.version_string()) request.set_header('Date', self.date_time_string()) try: timeout = int(request.get_header('keep-alive', timeout)) except TypeError as ValueError: pass try: try: try: self.server.site.handle_request(request) except ErrorResponse as err: request.response(code=err.code, reason_phrase=err.reason, headers=err.headers, body=err.body) finally: # clean up any timers that might have been left around by the handling code #api.get_hub().cancel_timers(api.getcurrent()) pass # throw an exception if it failed to write a body if not request.response_written(): raise NotImplementedError( "Handler failed to write response to request: %s" % request) if not hasattr(self, '_cached_body'): try: request.read_body() ## read & discard body except: pass except socket.error as e: # Broken pipe, connection reset by peer if e[0] in CONNECTION_CLOSED: #print "Remote host closed connection before response could be sent" pass else: raise except Exception as e: self.server.log_message( "Exception caught in HttpRequest.handle():\n") self.server.log_exception(*sys.exc_info()) if not request.response_written(): request.response(500) request.write('Internal Server Error') self.close() raise e # can't do a plain raise since exc_info might have been cleared self.close()
def setUp(self): from eventlib import api self.timer = api.exc_after(1, RuntimeError('test is taking too long'))
def setUp(self): print '\n%s.%s' % (self.__class__.__name__, self._testMethodName) self.timer = api.exc_after(self.PER_TEST_TIMEOUT, api.TimeoutError('per test timeout expired'))
def test_timeout(self): import time api.exc_after(0.1, api.TimeoutError()) self.assertRaises(api.TimeoutError, tpool.execute, time.sleep, 0.3)
def setUp(self): self.timer = api.exc_after(1, TestTookTooLong()) self.pool = IntPool(max_size=2)
def setUp(self): print('\n%s.%s' % (self.__class__.__name__, self._testMethodName)) self.timer = api.exc_after( self.PER_TEST_TIMEOUT, api.TimeoutError('per test timeout expired'))
def waiter(q): timer = api.exc_after(0.1, api.TimeoutError) self.assertEqual(q.wait(), 'hi2') timer.cancel()
def connect(self, db_module, connect_timeout, *args, **kw): timeout = api.exc_after(connect_timeout, ConnectTimeout()) try: return db_module.connect(*args, **kw) finally: timeout.cancel()
def setUp(self): # raise an exception if we're waiting forever self._cancel_timeout = api.exc_after(1, api.TimeoutError()) self.actor = IncrActor()