Пример #1
0
 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 do_receive(q, evt):
     api.exc_after(0, RuntimeError())
     try:
         result = q.wait()
         evt.send(result)
     except RuntimeError:
         evt.send('timed out')
Пример #3
0
    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,"r",0)
        wrap_rfile = greenio.GreenPipe(rfile)
        wfile = os.fdopen(wpipe,"w",0)
        wrap_wfile = greenio.GreenPipe(wfile)

        def sender(evt):
            api.sleep(0.02)
            wrap_wfile.write('hi')
            evt.send('sent via event')

        from eventlet 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.assertEquals(result, 'sent via event')
Пример #4
0
    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, "r", 0)
        wrap_rfile = greenio.GreenPipe(rfile)
        wfile = os.fdopen(wpipe, "w", 0)
        wrap_wfile = greenio.GreenPipe(wfile)

        def sender(evt):
            api.sleep(0.02)
            wrap_wfile.write('hi')
            evt.send('sent via event')

        from eventlet 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.assertEquals(result, 'sent via event')
Пример #5
0
 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)
Пример #6
0
 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)
Пример #7
0
    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, )
Пример #8
0
    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, )
Пример #9
0
    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
Пример #10
0
 def connect(self, db_module, connect_timeout, *args, **kw):
     timeout = api.exc_after(connect_timeout, ConnectTimeout())
     try:
         from eventlet import saranwrap
         return saranwrap.wrap(db_module).connect(*args, **kw)
     finally:
         timeout.cancel()
Пример #11
0
    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() == 'hello\n'
        assert fd.read() == ''

        timer.cancel()
Пример #12
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
Пример #13
0
 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)
Пример #14
0
    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()
Пример #15
0
    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()
Пример #16
0
 def connect(self, db_module, connect_timeout, *args, **kw):
     timeout = api.exc_after(connect_timeout, ConnectTimeout())
     try:
         from eventlet 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()
Пример #17
0
    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()
Пример #18
0
    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()
Пример #19
0
    def test_streaming(self):
        def comet_stream(session, handshake_response):
            event = coros.Channel()
            api.spawn(
                http._make_request,
                event,
                config.base_url + "/comet?is=1&bs=;;;&du=5&s=" + session.key,
                incremental_event=event,
                socket=handshake_response.socket,
                reset_transcript=False,
            )
            buffer = ""
            while True:
                data = event.wait()
                if isinstance(data, http.HTTPResponse):
                    break
                if not data:
                    break
                buffer += data
                while ";;;" in buffer:
                    line, buffer = buffer.split(";;;", 1)
                    packets = json.loads(line[1:-1])
                    for packet in packets:
                        # XXX: use the proper decode function (urldecode, maybe)
                        yield packet[2]

        session, handshake_response = protocol.handshake()
        session.send("foo")
        comet = comet_stream(session, handshake_response)
        if comet.next() != "foo":
            raise CSPException('Invalid comet stream batch: expected "foo"', response)
        response1 = session.send("bar")
        response2 = session.send("baz")
        handshake_response.append_transcript(response2.socket.transcript)
        timer = api.exc_after(3, CSPException("Did not receive a timely comet packet", handshake_response))
        output1 = comet.next()
        output2 = comet.next()
        timer.cancel()
        if output1 != "bar" or output2 != "baz":
            raise CSPException('Invalid comet stream batch: expected "bar" then "baz"', response)
Пример #20
0
 def acquire(self, timeout=None):
     
     if self.__count <= 0:
         ready_event = coros.event()
         self.coro_queue.appendleft(ready_event)
         
         timer = DummyTimer()
         if timeout is not None:
             timer = api.exc_after(timeout, self.Timeout)
         
         try:
             result = ready_event.wait()
         except self.Timeout:
             if ready_event in self.coro_queue:
                 self.coro_queue.remove(ready_event)
             raise
         else:
             timer.cancel()
             
         if not result:
             raise self.WaitCancelled
     
     self.__count -= 1
    def acquire(self, timeout=None):

        if self.__count <= 0:
            ready_event = coros.event()
            self.coro_queue.appendleft(ready_event)

            timer = DummyTimer()
            if timeout is not None:
                timer = api.exc_after(timeout, self.Timeout)

            try:
                result = ready_event.wait()
            except self.Timeout:
                if ready_event in self.coro_queue:
                    self.coro_queue.remove(ready_event)
                raise
            else:
                timer.cancel()

            if not result:
                raise self.WaitCancelled

        self.__count -= 1
Пример #22
0
 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() == 'hello\n'    
     assert fd.read() == ''
     
     timer.cancel()
Пример #23
0
 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.assertEquals(self.pool.free(), 0)
     self.assertEquals(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.assertEquals(self.pool.free(), 0)
     self.assertEquals(self.pool.waiting(), 1)
     self.pool.put(conn)
     timer = api.exc_after(0.3, api.TimeoutError)
     conn = e.wait()
     timer.cancel()
     self.assertEquals(self.pool.free(), 0)
     self.assertEquals(self.pool.waiting(), 0)
Пример #24
0
 def setUp(self):
     self.timer = api.exc_after(1, TestTookTooLong())
     self.pool = IntPool(max_size=2)
Пример #25
0
 def waiter(q):
     timer = api.exc_after(0.1, api.TimeoutError)
     self.assertEquals(q.wait(), 'hi2')
     timer.cancel()
Пример #26
0
 def __enter__(self):
     if self.seconds is None:
         self.timer = FakeTimer()
     else:
         self.timer = exc_after(self.seconds, *self.throw_args)
     return self.timer
Пример #27
0
 def setUp(self):
     self.timer = api.exc_after(1, TestTookTooLong())
     self.pool = IntPool(max_size=2)
Пример #28
0
 def test_timeout(self):
     import time
     api.exc_after(0.1, api.TimeoutError())
     self.assertRaises(api.TimeoutError,
                       tpool.execute, time.sleep, 0.3)
Пример #29
0
 def test_timeout(self):
     import time
     api.exc_after(0.1, api.TimeoutError())
     self.assertRaises(api.TimeoutError, tpool.execute, time.sleep, 0.3)
Пример #30
0
def _make_request(e, url, **kwargs):
    timeout = kwargs.pop('timeout', None)
    method = kwargs.pop('method', 'GET')
    body = kwargs.pop('body', "")
    version = kwargs.pop('version', "1.1")
    socket = kwargs.pop('socket', None)
    reset_transcript = kwargs.pop('reset_transcript', True)
    incremental_event = kwargs.pop('incremental_event', None)
    previous_transcript = []
    if socket and not socket.open:
        previous_transcript = socket.transcript
        socket = None
    # TODO: make headers the right case
    headers = kwargs
    parsed = urlparse(url)
    path = quote(parsed.path or '/')
    if parsed.query:
        path += '?' + encode_query(parsed.query)
    host_header = parsed.hostname + ((parsed.port == 80) and "" or ":" + str(parsed.port))
    if not socket:
        socket = StructuredSocket(parsed.hostname, parsed.port or 80)
        if not reset_transcript and previous_transcript:
            socket.transcript = previous_transcript
    if reset_transcript:
        socket.start_transcript()
    response = HTTPResponse(socket)
    timer = None
    if timeout:
        timer = api.exc_after(timeout, error.HTTPProtocolError("timeout", response))
    socket.send("%s %s HTTP/%s\r\n" % (method.upper(), path, version))
    socket.send("Host: %s\r\n" % (host_header,))
    for key, val in headers.items():
        socket.send('%s: %s\r\n' % (key, val))
    if method.lower() == 'post' and body:
        socket.send("Content-Length: %s\r\n" % (len(body),))
    socket.send('\r\n')
    socket.send(body)
    try:
        response.protocol, response.code, response.status = socket.read_line().split(' ', 2)
        response.code = int(response.code)
        while True:            
            header_line = socket.read_line()
            if not header_line: break
            key, val = header_line.split(': ')
            response.headers[key] = val
        if response.get_content_length():
            response.body = socket.read_bytes(response.get_content_length())
        elif incremental_event != None:
            if response.protocol.lower() == 'http/1.0':
                while True: # HTTP 1.0, read streaming HTTP response
                    data = socket.read_stream()
                    incremental_event.send(data)
                    incremental_event.reset()
                    if not data:
                        break
            elif response.protocol.lower() == 'http/1.1':
                while True:
                    chunk_description = socket.read_line()
#                    print 'chunk description', chunk_description
                    chunk_size = int(chunk_description, 16)
                    if not chunk_size:
                        incremental_event.send('')
                        break
                    chunk = socket.read_bytes(chunk_size)
                    skip_this = socket.read_line()
#                    print 'SKIPPING', repr(skip_this)
#                    print 'send back', chunk
                    incremental_event.send(chunk)
            else:
                print 'wha?'
        e.send(response)
    except Exception, err:
#        raise
        socket.close()
        e.send_exception(error.HTTPProtocolError("Protocol Error", response))
Пример #31
0
 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'))
Пример #32
0
 def setUp(self):
     # raise an exception if we're waiting forever
     self._cancel_timeout = api.exc_after(1, api.TimeoutError())
     self.actor = IncrActor()
Пример #33
0
 def setUp(self):
     from eventlet import api
     self.timer = api.exc_after(1, RuntimeError('test is taking too long'))
Пример #34
0
 def __enter__(self):
     if self.seconds is None:
         self.timer = FakeTimer()
     else:
         self.timer = exc_after(self.seconds, *self.throw_args)
     return self.timer
Пример #35
0
 def setUp(self):
     # raise an exception if we're waiting forever
     self._cancel_timeout = api.exc_after(1, api.TimeoutError())
     self.actor = IncrActor()
Пример #36
0
 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()
Пример #37
0
 def setUp(self):
     # raise an exception if we're waiting forever
     self._cancel_timeout = api.exc_after(1, RuntimeError('test takes too long'))
Пример #38
0
 def setUp(self):
     # raise an exception if we're waiting forever
     self._cancel_timeout = api.exc_after(
         1, RuntimeError('test takes too long'))