this provides gevent-like api using eventlet. Signed-off-by: YAMAMOTO Takashi <[email protected]> --- ryu/lib/hub.py | 90 ++++++++++++++++++++++++++++++++++++++++++ ryu/tests/unit/lib/test_hub.py | 55 ++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 ryu/lib/hub.py create mode 100644 ryu/tests/unit/lib/test_hub.py
diff --git a/ryu/lib/hub.py b/ryu/lib/hub.py new file mode 100644 index 0000000..dfdf217 --- /dev/null +++ b/ryu/lib/hub.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Nippon Telegraph and Telephone Corporation. +# Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + + +# we don't bother to use cfg.py because monkey patch needs to be +# called very early. instead, we use an environment variable to +# select the type of hub. +HUB_TYPE = os.getenv('RYU_HUB_TYPE', 'eventlet') + +if HUB_TYPE == 'eventlet': + import eventlet + import eventlet.event + import eventlet.queue + import eventlet.timeout + import eventlet.wsgi + import ssl + + spawn = eventlet.spawn + getcurrent = eventlet.getcurrent + patch = eventlet.monkey_patch + sleep = eventlet.sleep + + def kill(thread): + thread.kill() + + def joinall(threads): + for t in threads: + try: + t.wait() + except: + pass + + Queue = eventlet.queue.Queue + QueueEmpty = eventlet.queue.Empty + + class StreamServer(object): + def __init__(self, listen_info, handle=None, backlog=None, + spawn='default', **ssl_args): + assert backlog is None + assert spawn == 'default' + self.server = eventlet.listen(listen_info) + if ssl_args: + def wrap_and_handle(sock, addr): + ssl_args.setdefault('server_side', True) + handle(ssl.wrap_socket(sock, **ssl_args), addr) + + self.handle = wrap_and_handle + else: + self.handle = handle + + def serve_forever(self): + while True: + sock, addr = self.server.accept() + spawn(self.handle, sock, addr) + + class WSGIServer(StreamServer): + def serve_forever(self): + eventlet.wsgi.server(self.server, self.handle) + + Timeout = eventlet.timeout.Timeout + + class Event(object): + def __init__(self): + self._ev = eventlet.event.Event() + + def set(self): + self._ev.send() + + def wait(self, timeout=None): + if timeout is None: + self._ev.wait() + with Timeout(timeout): + self._ev.wait() diff --git a/ryu/tests/unit/lib/test_hub.py b/ryu/tests/unit/lib/test_hub.py new file mode 100644 index 0000000..02cccfb --- /dev/null +++ b/ryu/tests/unit/lib/test_hub.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Nippon Telegraph and Telephone Corporation. +# Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import time +import unittest +from nose.tools import raises + +from ryu.lib import hub +hub.patch() + + +class MyException(BaseException): + pass + + +class Test_hub(unittest.TestCase): + """ Test case for ryu.lib.hub + """ + + def setUp(self): + pass + + def tearDown(self): + pass + + @raises(hub.Timeout) + def test_timeout1(self): + with hub.Timeout(0.1): + hub.sleep(1) + + @raises(MyException) + def test_timeout2(self): + with hub.Timeout(0.1, MyException): + hub.sleep(1) + + def test_timeout3(self): + with hub.Timeout(1): + hub.sleep(0.1) + # sleep some more to ensure timer cancelation + hub.sleep(2) -- 1.8.0.1 ------------------------------------------------------------------------------ Minimize network downtime and maximize team effectiveness. Reduce network management and security costs.Learn how to hire the most talented Cisco Certified professionals. Visit the Employer Resources Portal http://www.cisco.com/web/learning/employer_resources/index.html _______________________________________________ Ryu-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ryu-devel
