Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Start the project

  • Loading branch information...
commit 90f68239696c530e41fe70e4eac9f9ee9281954b 1 parent ed2d9f1
@ajdavis authored
View
13 LICENSE
@@ -0,0 +1,13 @@
+YieldPoints Copyright (c) 2012 A. Jesse Jiryu Davis
+
+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.
View
9 MANIFEST.in
@@ -0,0 +1,9 @@
+include README.rst
+include LICENSE
+recursive-include doc *.rst
+recursive-include doc *.py
+recursive-include doc *.png
+recursive-include doc Makefile
+prune doc/_build
+recursive-include examples *.py
+
View
4 README.md
@@ -1,4 +0,0 @@
-waitany
-=======
-
-Simple tornado.gen extension, waits until the first of several tasks completes
View
36 README.rst
@@ -0,0 +1,36 @@
+===========
+YieldPoints
+===========
+
+:Info: Synchronization primitives for Tornado coroutines.
+:Author: A\. Jesse Jiryu Davis
+
+About
+=====
+Simple extensions to Tornado's gen_ module.
+
+.. _gen: http://www.tornadoweb.org/documentation/gen.html
+
+Dependencies
+============
+Tornado_ >= version 2.3.
+
+.. _Tornado: http://www.tornadoweb.org/
+
+
+Documentation
+=============
+
+Documentation: http://yieldpoints.readthedocs.org/
+
+You will need Sphinx_ installed to generate the
+documentation. Documentation can be generated like::
+
+ $ sphinx-build doc build
+
+.. _Sphinx: http://sphinx.pocoo.org/
+
+Testing
+=======
+
+Run ``python setup.py nosetests`` in the root directory.
View
3  cover.sh
@@ -0,0 +1,3 @@
+PYTHONPATH=../lib/python2.7/site-packages coverage run --branch --source yieldpoints/__init__.py /Users/emptysquare/.virtualenvs/yieldpoints/bin/nosetests -vs
+rm -rf html
+coverage html -d html
View
153 doc/Makefile
@@ -0,0 +1,153 @@
+# Makefile for Sphinx documentation
+#
+
+# You can set these variables from the command line.
+SPHINXOPTS =
+SPHINXBUILD = sphinx-build
+PAPER =
+BUILDDIR = _build
+
+# Internal variables.
+PAPEROPT_a4 = -D latex_paper_size=a4
+PAPEROPT_letter = -D latex_paper_size=letter
+ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
+# the i18n builder cannot share the environment and doctrees with the others
+I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
+
+.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext
+
+help:
+ @echo "Please use \`make <target>' where <target> is one of"
+ @echo " html to make standalone HTML files"
+ @echo " dirhtml to make HTML files named index.html in directories"
+ @echo " singlehtml to make a single large HTML file"
+ @echo " pickle to make pickle files"
+ @echo " json to make JSON files"
+ @echo " htmlhelp to make HTML files and a HTML help project"
+ @echo " qthelp to make HTML files and a qthelp project"
+ @echo " devhelp to make HTML files and a Devhelp project"
+ @echo " epub to make an epub"
+ @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
+ @echo " latexpdf to make LaTeX files and run them through pdflatex"
+ @echo " text to make text files"
+ @echo " man to make manual pages"
+ @echo " texinfo to make Texinfo files"
+ @echo " info to make Texinfo files and run them through makeinfo"
+ @echo " gettext to make PO message catalogs"
+ @echo " changes to make an overview of all changed/added/deprecated items"
+ @echo " linkcheck to check all external links for integrity"
+ @echo " doctest to run all doctests embedded in the documentation (if enabled)"
+
+clean:
+ -rm -rf $(BUILDDIR)/*
+
+html:
+ $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
+ @echo
+ @echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
+
+dirhtml:
+ $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
+ @echo
+ @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
+
+singlehtml:
+ $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
+ @echo
+ @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
+
+pickle:
+ $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
+ @echo
+ @echo "Build finished; now you can process the pickle files."
+
+json:
+ $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
+ @echo
+ @echo "Build finished; now you can process the JSON files."
+
+htmlhelp:
+ $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
+ @echo
+ @echo "Build finished; now you can run HTML Help Workshop with the" \
+ ".hhp project file in $(BUILDDIR)/htmlhelp."
+
+qthelp:
+ $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
+ @echo
+ @echo "Build finished; now you can run "qcollectiongenerator" with the" \
+ ".qhcp project file in $(BUILDDIR)/qthelp, like this:"
+ @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/YieldPoints.qhcp"
+ @echo "To view the help file:"
+ @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/YieldPoints.qhc"
+
+devhelp:
+ $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
+ @echo
+ @echo "Build finished."
+ @echo "To view the help file:"
+ @echo "# mkdir -p $$HOME/.local/share/devhelp/YieldPoints"
+ @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/YieldPoints"
+ @echo "# devhelp"
+
+epub:
+ $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
+ @echo
+ @echo "Build finished. The epub file is in $(BUILDDIR)/epub."
+
+latex:
+ $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
+ @echo
+ @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
+ @echo "Run \`make' in that directory to run these through (pdf)latex" \
+ "(use \`make latexpdf' here to do that automatically)."
+
+latexpdf:
+ $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
+ @echo "Running LaTeX files through pdflatex..."
+ $(MAKE) -C $(BUILDDIR)/latex all-pdf
+ @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
+
+text:
+ $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
+ @echo
+ @echo "Build finished. The text files are in $(BUILDDIR)/text."
+
+man:
+ $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
+ @echo
+ @echo "Build finished. The manual pages are in $(BUILDDIR)/man."
+
+texinfo:
+ $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
+ @echo
+ @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
+ @echo "Run \`make' in that directory to run these through makeinfo" \
+ "(use \`make info' here to do that automatically)."
+
+info:
+ $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
+ @echo "Running Texinfo files through makeinfo..."
+ make -C $(BUILDDIR)/texinfo info
+ @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
+
+gettext:
+ $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
+ @echo
+ @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
+
+changes:
+ $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
+ @echo
+ @echo "The overview file is in $(BUILDDIR)/changes."
+
+linkcheck:
+ $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
+ @echo
+ @echo "Link check complete; look for any errors in the above output " \
+ "or in $(BUILDDIR)/linkcheck/output.txt."
+
+doctest:
+ $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
+ @echo "Testing of doctests in the sources finished, look at the " \
+ "results in $(BUILDDIR)/doctest/output.txt."
View
7 doc/changelog.rst
@@ -0,0 +1,7 @@
+Changelog
+=========
+
+Version 0.1
+-----------
+
+First release.
View
27 doc/classes.rst
@@ -0,0 +1,27 @@
+:mod:`yieldpoints` Classes
+==========================
+
+.. currentmodule:: yieldpoints
+
+.. contents:: Contents
+ :local:
+
+WaitAny
+-----------
+.. autoclass:: WaitAny
+ :members:
+
+Cancel
+------
+.. autoclass:: Cancel
+ :members:
+
+CancelAll
+---------
+.. autoclass:: CancelAll
+ :members:
+
+Timeout
+-------
+.. autoclass:: Timeout
+ :members:
View
257 doc/conf.py
@@ -0,0 +1,257 @@
+# -*- coding: utf-8 -*-
+#
+# YieldPoints documentation build configuration file
+#
+# This file is execfile()d with the current directory set to its containing dir.
+#
+# Note that not all possible configuration values are present in this
+# autogenerated file.
+#
+# All configuration values have a default; values that are commented out
+# serve to show the default.
+
+# Custom: Sphinx doesn't produce a useful "module index" for YieldPoints
+html_use_modindex = False
+
+doctest_global_setup = """
+from datetime import timedelta
+from functools import partial
+import time
+from tornado import gen
+from tornado.ioloop import IOLoop
+from tornado.httpclient import AsyncHTTPClient
+import yieldpoints
+"""
+
+html_theme_options = {'collapsiblesidebar': True}
+
+import sys, os
+
+# If extensions (or modules to document with autodoc) are in another directory,
+# add these directories to sys.path here. If the directory is relative to the
+# documentation root, use os.path.abspath to make it absolute, like shown here.
+sys.path[0:0] = [os.path.abspath('..')]
+
+import yieldpoints
+
+# -- General configuration -----------------------------------------------------
+
+# If your documentation needs a minimal Sphinx version, state it here.
+#needs_sphinx = '1.0'
+
+# Add any Sphinx extension module names here, as strings. They can be extensions
+# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
+extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.viewcode', 'sphinx.ext.graphviz']
+
+# Add any paths that contain templates here, relative to this directory.
+templates_path = ['_templates']
+
+# The suffix of source filenames.
+source_suffix = '.rst'
+
+# The encoding of source files.
+#source_encoding = 'utf-8-sig'
+
+# The master toctree document.
+master_doc = 'index'
+
+# General information about the project.
+project = u'YieldPoints'
+copyright = u'2012, A. Jesse Jiryu Davis'
+
+# The version info for the project you're documenting, acts as replacement for
+# |version| and |release|, also used in various other places throughout the
+# built documents.
+#
+# The short X.Y version.
+version = yieldpoints.version
+# The full version, including alpha/beta/rc tags.
+release = yieldpoints.version
+
+# The language for content autogenerated by Sphinx. Refer to documentation
+# for a list of supported languages.
+#language = None
+
+# There are two options for replacing |today|: either, you set today to some
+# non-false value, then it is used:
+#today = ''
+# Else, today_fmt is used as the format for a strftime call.
+#today_fmt = '%B %d, %Y'
+
+# List of patterns, relative to source directory, that match files and
+# directories to ignore when looking for source files.
+exclude_patterns = ['_build']
+
+# The reST default role (used for this markup: `text`) to use for all documents.
+#default_role = None
+
+# If true, '()' will be appended to :func: etc. cross-reference text.
+#add_function_parentheses = True
+
+# If true, the current module name will be prepended to all description
+# unit titles (such as .. function::).
+#add_module_names = True
+
+# If true, sectionauthor and moduleauthor directives will be shown in the
+# output. They are ignored by default.
+#show_authors = False
+
+# The name of the Pygments (syntax highlighting) style to use.
+pygments_style = 'sphinx'
+
+# A list of ignored prefixes for module index sorting.
+#modindex_common_prefix = []
+
+# -- Options for HTML output ---------------------------------------------------
+
+# The theme to use for HTML and HTML Help pages. See the documentation for
+# a list of builtin themes.
+html_theme = 'default'
+
+# Theme options are theme-specific and customize the look and feel of a theme
+# further. For a list of options available for each theme, see the
+# documentation.
+#html_theme_options = {}
+
+# Add any paths that contain custom themes here, relative to this directory.
+#html_theme_path = []
+
+# The name for this set of Sphinx documents. If None, it defaults to
+# "<project> v<release> documentation".
+#html_title = None
+
+# A shorter title for the navigation bar. Default is the same as html_title.
+#html_short_title = None
+
+# The name of an image file (relative to this directory) to place at the top
+# of the sidebar.
+#html_logo = None
+
+# The name of an image file (within the static path) to use as favicon of the
+# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
+# pixels large.
+#html_favicon = None
+
+# Add any paths that contain custom static files (such as style sheets) here,
+# relative to this directory. They are copied after the builtin static files,
+# so a file named "default.css" will overwrite the builtin "default.css".
+html_static_path = ['_static']
+
+# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
+# using the given strftime format.
+#html_last_updated_fmt = '%b %d, %Y'
+
+# If true, SmartyPants will be used to convert quotes and dashes to
+# typographically correct entities.
+#html_use_smartypants = True
+
+# Custom sidebar templates, maps document names to template names.
+#html_sidebars = {}
+
+# Additional templates that should be rendered to pages, maps page names to
+# template names.
+#html_additional_pages = {}
+
+# If false, no module index is generated.
+#html_domain_indices = True
+
+# If false, no index is generated.
+#html_use_index = True
+
+# If true, the index is split into individual pages for each letter.
+#html_split_index = False
+
+# If true, links to the reST sources are added to the pages.
+#html_show_sourcelink = True
+
+# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
+#html_show_sphinx = True
+
+# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
+#html_show_copyright = True
+
+# If true, an OpenSearch description file will be output, and all pages will
+# contain a <link> tag referring to it. The value of this option must be the
+# base URL from which the finished HTML is served.
+#html_use_opensearch = ''
+
+# This is the file name suffix for HTML files (e.g. ".xhtml").
+#html_file_suffix = None
+
+# Output file base name for HTML help builder.
+htmlhelp_basename = 'YieldPointdoc'
+
+
+# -- Options for LaTeX output --------------------------------------------------
+
+latex_elements = {
+# The paper size ('letterpaper' or 'a4paper').
+#'papersize': 'letterpaper',
+
+# The font size ('10pt', '11pt' or '12pt').
+#'pointsize': '10pt',
+
+# Additional stuff for the LaTeX preamble.
+#'preamble': '',
+}
+
+# Grouping the document tree into LaTeX files. List of tuples
+# (source start file, target name, title, author, documentclass [howto/manual]).
+latex_documents = [
+ ('index', 'YieldPoints.tex', u'YieldPoints Documentation',
+ u'A. Jesse Jiryu Davis', 'manual'),
+]
+
+# The name of an image file (relative to this directory) to place at the top of
+# the title page.
+#latex_logo = None
+
+# For "manual" documents, if this is true, then toplevel headings are parts,
+# not chapters.
+#latex_use_parts = False
+
+# If true, show page references after internal links.
+#latex_show_pagerefs = False
+
+# If true, show URL addresses after external links.
+#latex_show_urls = False
+
+# Documents to append as an appendix to all manuals.
+#latex_appendices = []
+
+# If false, no module index is generated.
+#latex_domain_indices = True
+
+
+# -- Options for manual page output --------------------------------------------
+
+# One entry per manual page. List of tuples
+# (source start file, name, description, authors, manual section).
+man_pages = [
+ ('index', 'yieldpoints', u'YieldPoints Documentation',
+ [u'A. Jesse Jiryu Davis'], 1)
+]
+
+# If true, show URL addresses after external links.
+#man_show_urls = False
+
+
+# -- Options for Texinfo output ------------------------------------------------
+
+# Grouping the document tree into Texinfo files. List of tuples
+# (source start file, target name, title, author,
+# dir menu entry, description, category)
+texinfo_documents = [
+ ('index', 'YieldPoints', u'YieldPoints Documentation',
+ u'A. Jesse Jiryu Davis', 'YieldPoints', 'One line description of project.',
+ 'Miscellaneous'),
+]
+
+# Documents to append as an appendix to all manuals.
+#texinfo_appendices = []
+
+# If false, no module index is generated.
+#texinfo_domain_indices = True
+
+# How to display URL addresses: 'footnote', 'no', or 'inline'.
+#texinfo_show_urls = 'footnote'
View
6 doc/examples/index.rst
@@ -0,0 +1,6 @@
+Examples
+========
+
+.. todo:: some examples?
+
+.. toctree::
View
7 doc/faq.rst
@@ -0,0 +1,7 @@
+Frequently Asked Questions
+==========================
+
+.. module:: yieldpoints
+
+What's it for?
+--------------
View
91 doc/index.rst
@@ -0,0 +1,91 @@
+=======================================================
+yieldpoints: Synchronization primitives for Tornado coroutines
+=======================================================
+
+.. module:: yieldpoints
+
+Simple extensions to Tornado's gen_ module.
+
+.. _gen: http://www.tornadoweb.org/documentation/gen.html
+
+Examples
+========
+
+Begin two tasks and handle their results in the order completed:
+
+.. doctest::
+
+ >>> @gen.engine
+ ... def f():
+ ... callback0 = yield gen.Callback(0)
+ ... callback1 = yield gen.Callback(1)
+ ...
+ ... # Fire callback1 soon, callback0 later
+ ... IOLoop.instance().add_timeout(
+ ... timedelta(seconds=0.1), partial(callback1, 'foo'))
+ ...
+ ... IOLoop.instance().add_timeout(
+ ... timedelta(seconds=0.2), partial(callback0, 'bar'))
+ ...
+ ... keys = set([0, 1])
+ ... while keys:
+ ... key, result = yield yieldpoints.WaitAny(keys)
+ ... print 'key:', key, ', result:', result
+ ... keys.remove(key)
+ ... IOLoop.instance().stop()
+ ...
+ >>> f()
+ >>> IOLoop.instance().start()
+ key: 1 , result: foo
+ key: 0 , result: bar
+
+Register a timeout and wait for it later on:
+
+.. doctest::
+
+ >>> @gen.engine
+ ... def f():
+ ... start = time.time()
+ ... yield yieldpoints.Timeout(timedelta(seconds=0.1), 'key')
+ ... print 'going to wait'
+ ... yield gen.Wait('key')
+ ... print 'waited, took %.1f seconds' % (time.time() - start)
+ ... IOLoop.instance().stop()
+ ...
+ >>> f()
+ >>> IOLoop.instance().start()
+ going to wait
+ waited, took 0.1 seconds
+
+Begin a task and decline not to wait for it, while avoiding a
+``LeakedCallbackError``:
+
+.. doctest::
+
+ >>> @gen.engine
+ ... def f():
+ ... yield gen.Callback('key') # never called
+ ... yield yieldpoints.Cancel('key')
+ ... IOLoop.instance().stop()
+ ...
+ >>> f()
+ >>> IOLoop.instance().start()
+
+Contents
+========
+.. toctree::
+ examples/index
+ classes
+ faq
+ changelog
+
+Source
+======
+Is on GitHub: https://github.com/ajdavis/yieldpoints
+
+Indices and tables
+==================
+
+* :ref:`genindex`
+* :ref:`search`
+
View
0  examples/__init__.py
No changes.
View
1  requirements.txt
@@ -0,0 +1 @@
+tornado
View
3  setup.cfg
@@ -0,0 +1,3 @@
+[nosetests]
+verbosity = 2
+detailed-errors = 1
View
53 setup.py
@@ -0,0 +1,53 @@
+from setuptools import setup
+import sys
+
+classifiers = """\
+Intended Audience :: Developers
+License :: OSI Approved :: Apache Software License
+Development Status :: 4 - Beta
+Natural Language :: English
+Programming Language :: Python :: 2
+Programming Language :: Python :: 2.5
+Programming Language :: Python :: 2.6
+Programming Language :: Python :: 2.7
+Programming Language :: Python :: 3
+Programming Language :: Python :: 3.1
+Programming Language :: Python :: 3.2
+Programming Language :: Python :: 3.3
+Operating System :: MacOS :: MacOS X
+Operating System :: Unix
+Programming Language :: Python
+Programming Language :: Python :: Implementation :: CPython
+Programming Language :: Python :: Implementation :: PyPy
+"""
+
+description = 'Synchronization primitives for Tornado coroutines.'
+
+long_description = open("README.rst").read()
+
+major, minor = sys.version_info[:2]
+
+kwargs = {}
+if major >= 3:
+ kwargs['use_2to3'] = True
+
+packages = ['yieldpoints']
+if "nosetests" in sys.argv:
+ packages.append('test')
+
+setup(name='yieldpoints',
+ version='0.1',
+ packages=packages,
+ description=description,
+ long_description=long_description,
+ author='A. Jesse Jiryu Davis',
+ author_email='ajdavis@cs.oberlin.edu',
+ url='http://github.com/ajdavis/yieldpoints/',
+ install_requires=['tornado >= 2.4.0'],
+ license='http://www.apache.org/licenses/LICENSE-2.0',
+ classifiers=filter(None, classifiers.split('\n')),
+ keywords='tornado coroutines semaphore mutex queue asynchronous',
+ # use python setup.py nosetests to test
+ setup_requires=['nose'],
+ **kwargs
+)
View
1  test/__init__.py
@@ -0,0 +1 @@
+# This space intentionally commented
View
120 test/async_test_engine.py
@@ -0,0 +1,120 @@
+import functools
+import os
+import time
+import types
+
+from tornado import gen, ioloop
+
+
+def async_test_engine(timeout_sec=5):
+ if not isinstance(timeout_sec, int) and not isinstance(timeout_sec, float):
+ raise TypeError(
+"""Expected int or float, got %s
+Use async_test_engine like:
+ @async_test_engine()
+or:
+ @async_test_engine(timeout_sec=10)""" % (
+ repr(timeout_sec)))
+
+ timeout_sec = max(timeout_sec, float(os.environ.get('TIMEOUT_SEC', 0)))
+ is_done = [False]
+
+ def decorator(func):
+ class AsyncTestRunner(gen.Runner):
+ def __init__(self, gen, timeout):
+ # Tornado 2.3 added a second argument to Runner()
+ super(AsyncTestRunner, self).__init__(gen, lambda: None)
+ self.timeout = timeout
+
+ def run(self):
+ loop = ioloop.IOLoop.instance()
+
+ try:
+ super(AsyncTestRunner, self).run()
+ except Exception:
+ loop.remove_timeout(self.timeout)
+ loop.stop()
+ raise
+
+ if self.finished:
+ loop.remove_timeout(self.timeout)
+ loop.stop()
+
+ def done():
+ is_done[0] = True
+
+ @functools.wraps(func)
+ def _async_test(self):
+ # Uninstall previous loop
+ if hasattr(ioloop.IOLoop, '_instance'):
+ del ioloop.IOLoop._instance
+
+ loop = ioloop.IOLoop.instance()
+
+ def on_timeout():
+ loop.stop()
+ raise AssertionError("%s timed out" % func)
+
+ timeout = loop.add_timeout(time.time() + timeout_sec, on_timeout)
+
+ try:
+ gen = func(self, done)
+ if isinstance(gen, types.GeneratorType):
+ runner = AsyncTestRunner(gen, timeout)
+ runner.run()
+ loop.start()
+
+ if not runner.finished:
+ # Something stopped the loop before func could finish or throw
+ # an exception.
+ raise Exception('%s did not finish' % func)
+
+ if not is_done[0]:
+ raise Exception('%s did not call done()' % func)
+ finally:
+ del ioloop.IOLoop._instance # Uninstall
+
+ return _async_test
+ return decorator
+
+async_test_engine.__test__ = False # Nose otherwise mistakes it for a test
+
+
+class AssertRaises(gen.Task):
+ def __init__(self, exc_type, func, *args, **kwargs):
+ super(AssertRaises, self).__init__(func, *args, **kwargs)
+ if not isinstance(exc_type, type):
+ raise TypeError("%s is not a class" % repr(exc_type))
+
+ if not issubclass(exc_type, Exception):
+ raise TypeError(
+ "%s is not a subclass of Exception" % repr(exc_type))
+ self.exc_type = exc_type
+
+ def get_result(self):
+ (result, error), _ = self.runner.pop_result(self.key)
+ if not isinstance(error, self.exc_type):
+ if error:
+ raise AssertionError("%s raised instead of %s" % (
+ repr(error), self.exc_type.__name__))
+ else:
+ raise AssertionError("%s not raised" % self.exc_type.__name__)
+ return result
+
+
+class AssertEqual(gen.Task):
+ def __init__(self, expected, func, *args, **kwargs):
+ super(AssertEqual, self).__init__(func, *args, **kwargs)
+ self.expected = expected
+
+ def get_result(self):
+ (result, error), _ = self.runner.pop_result(self.key)
+ if error:
+ raise error
+
+
+ if self.expected != result:
+ raise AssertionError("%s returned %s\nnot\n%s" % (
+ self.func, repr(result), repr(self.expected)))
+
+ return result
View
149 test/test_yieldpoints.py
@@ -0,0 +1,149 @@
+"""
+Test YieldPoints.
+"""
+
+from __future__ import with_statement
+
+from datetime import timedelta
+from functools import partial
+import time
+import unittest
+
+from tornado import gen
+from tornado.ioloop import IOLoop
+
+import yieldpoints
+from test.async_test_engine import async_test_engine
+
+
+class TestWaitAny(unittest.TestCase):
+ @async_test_engine()
+ def test_basic(self, done):
+ keys = list(range(3))
+ callbacks = [(yield gen.Callback(key)) for key in keys]
+ loop = IOLoop.instance()
+ loop.add_timeout(timedelta(seconds=0.01), callbacks[1])
+ loop.add_timeout(timedelta(seconds=0.02), callbacks[0])
+ loop.add_timeout(timedelta(seconds=0.03), callbacks[2])
+
+ history = []
+ while keys:
+ key, response = yield yieldpoints.YieldPoints(keys)
+ history.append(key)
+ keys.remove(key)
+
+ self.assertEqual([1, 0, 2], history)
+ done()
+
+ @async_test_engine()
+ def test_timeout(self, done):
+ @gen.engine
+ def test(callback):
+ yield gen.Callback('key') # never called
+ start = time.time()
+ key, result = yield yieldpoints.YieldPoints(
+ ['key'], deadline=timedelta(seconds=0.1))
+ duration = time.time() - start
+ self.assertEqual(None, key)
+ self.assertEqual(None, result)
+ # assertAlmostEquals with 'delta' not available until Python 2.7
+ self.assertTrue(abs(duration - 0.1) < 0.01)
+ callback()
+
+ try:
+ yield gen.Task(test)
+ except gen.LeakedCallbackError:
+ self.fail("LeakedCallbackError was unexpectedly raised")
+ else:
+ # Success
+ done()
+
+ @async_test_engine()
+ def test_timeout_cancel(self, done):
+ @gen.engine
+ def test(callback):
+ (yield gen.Callback('key'))('result') # called immediately
+ start = time.time()
+ key, result = yield yieldpoints.YieldPoints(
+ ['key'], deadline=timedelta(seconds=0.1))
+ duration = time.time() - start
+ self.assertEqual('key', key)
+ self.assertEqual('result', result)
+ self.assertTrue(duration < 0.01)
+ callback()
+
+ try:
+ yield gen.Task(test)
+ except gen.LeakedCallbackError:
+ self.fail("LeakedCallbackError was unexpectedly raised")
+ else:
+ done()
+
+ @async_test_engine()
+ def test_timeout_cancel_with_delay(self, done):
+ @gen.engine
+ def test(callback):
+ callback0 = yield gen.Callback('key') # called soon
+ IOLoop.instance().add_timeout(timedelta(seconds=0.1),
+ partial(callback0, 'result'))
+ start = time.time()
+ key, result = yield yieldpoints.YieldPoints(
+ ['key'], deadline=timedelta(seconds=0.2))
+ duration = time.time() - start
+ self.assertEqual('key', key)
+ self.assertEqual('result', result)
+ self.assertTrue(abs(duration - 0.1) < 0.01)
+ callback()
+
+ try:
+ yield gen.Task(test)
+ except gen.LeakedCallbackError:
+ self.fail("LeakedCallbackError was unexpectedly raised")
+ else:
+ done()
+
+class TestTimeout(unittest.TestCase):
+ @async_test_engine()
+ def test_timeout(self, done):
+ yield yieldpoints.Timeout(timedelta(seconds=0.1), 'key')
+ start = time.time()
+ result = yield gen.Wait('key')
+ duration = time.time() - start
+ self.assertEqual(None, result)
+ self.assertTrue(abs(duration - 0.1) < 0.01)
+ done()
+
+
+class TestCancel(unittest.TestCase):
+ @async_test_engine()
+ def test_timeout(self, done):
+ @gen.engine
+ def test(callback):
+ yield gen.Callback('key') # never called
+ yield yieldpoints.Cancel('key')
+ callback()
+
+ try:
+ yield gen.Task(test)
+ except gen.LeakedCallbackError:
+ self.fail("LeakedCallbackError was unexpectedly raised")
+ else:
+ done()
+
+
+class TestCancelAll(unittest.TestCase):
+ @async_test_engine()
+ def test_timeout(self, done):
+ @gen.engine
+ def test(callback):
+ for i in range(2):
+ yield gen.Callback(i) # never called
+ yield yieldpoints.CancelAll()
+ callback()
+
+ try:
+ yield gen.Task(test)
+ except gen.LeakedCallbackError:
+ self.fail("LeakedCallbackError was unexpectedly raised")
+ else:
+ done()
View
11 tox.ini
@@ -0,0 +1,11 @@
+# Tox (http://tox.testrun.org/) is a tool for running tests in multiple
+# virtualenvs. This configuration file will run the test suite on all supported
+# python versions. To use it, "pip install tox" and then run "tox" from this
+# directory.
+
+[tox]
+envlist = py25, py26, py27, py31, py32, py33, pypy
+
+[testenv]
+commands =
+ {envpython} setup.py nosetests
View
107 yieldpoints/__init__.py
@@ -0,0 +1,107 @@
+from functools import partial
+
+from tornado import gen
+from tornado.gen import UnknownKeyError
+from tornado.ioloop import IOLoop
+
+
+version_tuple = (0, 1)
+
+version = '.'.join(map(str, version_tuple))
+"""Current version of YieldPoints."""
+
+
+__all__ = ['YieldPoints', 'Cancel', 'CancelAll', 'Timeout']
+
+class YieldPointsBase(gen.YieldPoint):
+ def cancel(self, runner, key):
+ try:
+ runner.pending_callbacks.remove(key)
+ except KeyError:
+ raise UnknownKeyError("key %r is not pending" % key)
+
+
+class WaitAny(YieldPointsBase):
+ def __init__(self, keys, deadline=None):
+ self.keys = keys
+ self.deadline = deadline
+ self.expired = False
+ self.timeout = None
+
+ def start(self, runner):
+ self.runner = runner
+ # TODO: configurable loop?
+ if self.deadline is not None:
+ self.timeout = IOLoop.instance().add_timeout(
+ self.deadline, self.expire)
+
+ def is_ready(self):
+ return self.expired or any(
+ self.runner.is_ready(key) for key in self.keys)
+
+ def get_result(self):
+ if self.expired:
+ return None, None
+
+ for key in self.keys:
+ if self.runner.is_ready(key):
+ return key, self.runner.pop_result(key)
+ raise Exception("no results found")
+
+ def expire(self):
+ self.expired = True
+ for key in self.keys:
+ self.cancel(self.runner, key)
+ self.runner.run()
+
+
+class Cancel(YieldPointsBase):
+ def __init__(self, key):
+ self.key = key
+
+ def start(self, runner):
+ self.cancel(runner, self.key)
+
+ def is_ready(self):
+ return True
+
+ def get_result(self):
+ return None
+
+
+class CancelAll(YieldPointsBase):
+ def start(self, runner):
+ # Copy the set, since self.cancel() shrinks it during iteration
+ for key in runner.pending_callbacks.copy():
+ self.cancel(runner, key)
+
+ def is_ready(self):
+ return True
+
+ def get_result(self):
+ return None
+
+
+class Timeout(YieldPointsBase):
+ def __init__(self, deadline, key):
+ self.deadline = deadline
+ self.key = key
+
+ def start(self, runner):
+ self.runner = runner
+ runner.register_callback(self.key)
+ # TODO: configurable loop?
+ IOLoop.instance().add_timeout(
+ self.deadline,
+ partial(self.expire))
+
+ def expire(self):
+ self.runner.set_result(self.key, None)
+ self.runner.run()
+
+ def is_ready(self):
+ # Like gen.Callback: is_ready() returns True so coroutine can proceed
+ return True
+
+ def get_result(self):
+ return None
Please sign in to comment.
Something went wrong with that request. Please try again.