├── setup.cfg ├── asyncio_time_travel ├── tests │ ├── __init__.py │ └── test_time_travel_loop.py ├── version.py ├── __init__.py └── time_travel_util.py ├── pytest.ini ├── .gitignore ├── tox.ini ├── .github └── workflows │ ├── python-package.yml │ └── python-publish.yml ├── README.md ├── setup.py └── LICENSE /setup.cfg: -------------------------------------------------------------------------------- 1 | [bdist_wheel] 2 | -------------------------------------------------------------------------------- /asyncio_time_travel/tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /pytest.ini: -------------------------------------------------------------------------------- 1 | [pytest] 2 | addopts = --black 3 | -------------------------------------------------------------------------------- /asyncio_time_travel/version.py: -------------------------------------------------------------------------------- 1 | version = "0.3.0" 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | .cache/ 3 | dist/ 4 | build/ 5 | *.egg-info/ 6 | *.pyc 7 | *.swp 8 | .idea/ 9 | .tox/ 10 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py35,py36,py37,py38,py39 3 | 4 | [testenv] 5 | deps = 6 | pytest 7 | pytest-black 8 | commands = 9 | pytest 10 | -------------------------------------------------------------------------------- /asyncio_time_travel/__init__.py: -------------------------------------------------------------------------------- 1 | from .time_travel_util import TimeTravelLoop 2 | 3 | # Export the TimeTravelLoop class: 4 | __all__ = [ 5 | TimeTravelLoop, 6 | ] 7 | 8 | from .version import version as __version__ 9 | -------------------------------------------------------------------------------- /.github/workflows/python-package.yml: -------------------------------------------------------------------------------- 1 | # This workflow will install Python dependencies, run tests and lint with a variety of Python versions 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions 3 | 4 | name: Python package 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | strategy: 17 | matrix: 18 | python-version: ['3.7', '3.8', '3.9'] 19 | 20 | steps: 21 | - uses: actions/checkout@v2 22 | - name: Set up Python ${{ matrix.python-version }} 23 | uses: actions/setup-python@v2 24 | with: 25 | python-version: ${{ matrix.python-version }} 26 | - name: Install dependencies 27 | run: | 28 | python -m pip install --upgrade pip 29 | python -m pip install pytest pytest-black 30 | - name: Test with pytest 31 | run: | 32 | pytest 33 | -------------------------------------------------------------------------------- /.github/workflows/python-publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will upload a Python Package using Twine when a release is created 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries 3 | 4 | # This workflow uses actions that are not certified by GitHub. 5 | # They are provided by a third-party and are governed by 6 | # separate terms of service, privacy policy, and support 7 | # documentation. 8 | 9 | name: Upload Python Package 10 | 11 | on: 12 | release: 13 | types: [published] 14 | 15 | permissions: 16 | contents: read 17 | 18 | jobs: 19 | deploy: 20 | 21 | runs-on: ubuntu-latest 22 | 23 | steps: 24 | - uses: actions/checkout@v3 25 | - name: Set up Python 26 | uses: actions/setup-python@v3 27 | with: 28 | python-version: '3.x' 29 | - name: Install dependencies 30 | run: | 31 | python -m pip install --upgrade pip 32 | pip install build 33 | - name: Build package 34 | run: python -m build 35 | - name: Publish package 36 | uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 37 | with: 38 | user: __token__ 39 | password: ${{ secrets.PYPI_API_TOKEN }} 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Asyncio Time Travel Loop 2 | ======================== 3 | 4 | Intro 5 | ----- 6 | 7 | Asyncio Time Travel Loop allows you to test asyncio code that waits or sleeps, 8 | without actually waiting or sleeping. 9 | 10 | At the same time, you don't have to bother thinking about how the time 11 | advances. Your code should work exactly the same with TimeTravelLoop as it works 12 | with a normal asyncio events loop. 13 | 14 | Example: Assume that you have a code that waits 1000 seconds, and you want to 15 | tests that code. Instead of actually waiting 1000 seconds, you could use 16 | TimeTravelLoop: 17 | 18 | ```python 19 | import asyncio 20 | from asyncio_time_travel import TimeTravelLoop 21 | 22 | SLEEP_TIME = 1000 23 | 24 | tloop = TimeTravelLoop() 25 | 26 | async def inner_coro(): 27 | # Sleep for a long time: 28 | await asyncio.sleep(SLEEP_TIME, loop=tloop) 29 | 30 | tloop.run_until_complete(inner_coro()) 31 | ``` 32 | 33 | This code completes immediately. 34 | 35 | See some of the [tests](https://github.com/realcr/asyncio_time_travel/blob/master/asyncio_time_travel/tests/test_time_travel_loop.py) for more advanced examples of what TimeTravelLoop can do. 36 | 37 | Installation 38 | ------------ 39 | Run: 40 | ```bash 41 | pip install asyncio-time-travel 42 | ``` 43 | You can also find the package at https://pypi.python.org/pypi/asyncio-time-travel . 44 | 45 | Tests 46 | ----- 47 | 48 | Run (Inside asyncio_time_travel dir): 49 | ```bash 50 | pytest 51 | ``` 52 | 53 | If you haven't yet heard of [pytest](http://pytest.org), it's your lucky day :) 54 | 55 | 56 | How does this work? 57 | ------------------- 58 | 59 | TimeTravelLoop source is based on the source code of 60 | asyncio.test_utils.TestLoop. 61 | 62 | For each _run_once iteration of the loop, the following is done: 63 | - Loop events are executed. 64 | - Time is advanced to the closest scheduled event. 65 | 66 | Using this method your code never waits, and at the same time the events 67 | execute in the correct order. 68 | 69 | 70 | Limitations 71 | ----------- 72 | 73 | TimeTravelLoop is meant to be used with your tests, not for production code. In 74 | particular, if your loop interacts with external events, bending time is not a 75 | good idea (Time will advance differently outside of your loop). 76 | 77 | 78 | Further work 79 | ------------ 80 | 81 | - The code might have bugs. If you find any issues, don't hesitate to fork or 82 | open an issue. 83 | 84 | - It might be possible to integrate this code into asyncio.test_utils in some 85 | way. 86 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # Always prefer setuptools over distutils 2 | from setuptools import setup, find_packages 3 | 4 | # To use a consistent encoding 5 | from codecs import open 6 | from os import path 7 | import sys 8 | 9 | # Get the version 10 | from asyncio_time_travel.version import version 11 | 12 | here = path.abspath(path.dirname(__file__)) 13 | 14 | # Get the long description from the relevant file 15 | with open(path.join(here, "README.md"), encoding="utf-8") as f: 16 | long_description = f.read() 17 | 18 | py_version = sys.version_info[:2] 19 | if py_version < (3, 5): 20 | raise Exception("asyncio_time_travel requires Python >= 3.5.") 21 | 22 | setup( 23 | name="asyncio_time_travel", 24 | # Versions should comply with PEP440. For a discussion on single-sourcing 25 | # the version across setup.py and the project code, see 26 | # https://packaging.python.org/en/latest/single_source_version.html 27 | version=version, 28 | description="Asyncio Time Travel Loop", 29 | long_description=long_description, 30 | long_description_content_type="text/markdown", 31 | # The project's main homepage. 32 | url="https://github.com/realcr/asyncio_time_travel", 33 | # Author details 34 | author="real", 35 | author_email="realcr@gmail.com", 36 | # Choose your license 37 | license="Apache 2.0", 38 | # See https://pypi.python.org/pypi?%3Aaction=list_classifiers 39 | classifiers=[ 40 | # How mature is this project? Common values are 41 | # 3 - Alpha 42 | # 4 - Beta 43 | # 5 - Production/Stable 44 | "Development Status :: 3 - Alpha", 45 | # Indicate who your project is intended for 46 | "Intended Audience :: Developers", 47 | "Topic :: Software Development :: Testing", 48 | # Pick your license as you wish (should match "license" above) 49 | "License :: OSI Approved :: Apache Software License", 50 | # Specify the Python versions you support here. In particular, ensure 51 | # that you indicate whether you support Python 2, Python 3 or both. 52 | "Programming Language :: Python", 53 | "Programming Language :: Python :: 3 :: Only", 54 | "Programming Language :: Python :: 3.5", 55 | "Programming Language :: Python :: 3.6", 56 | "Programming Language :: Python :: 3.7", 57 | "Programming Language :: Python :: 3.8", 58 | "Programming Language :: Python :: 3.9", 59 | ], 60 | keywords="asyncio testing time travel sleep", 61 | # You can just specify the packages manually here if your project is 62 | # simple. Or you can use find_packages(). 63 | packages=find_packages( 64 | exclude=[ 65 | "contrib", 66 | "docs", 67 | "*.tests", 68 | "*.tests.*", 69 | "tests.*", 70 | "tests", 71 | ] 72 | ), 73 | entry_points={ 74 | "console_scripts": [ 75 | "sample=sample:main", 76 | ], 77 | }, 78 | ) 79 | -------------------------------------------------------------------------------- /asyncio_time_travel/time_travel_util.py: -------------------------------------------------------------------------------- 1 | import collections 2 | import heapq 3 | import selectors 4 | from asyncio import base_events, events 5 | 6 | 7 | # A class to manage set of next events: 8 | class NextTimers: 9 | def __init__(self): 10 | # Timers set. Used to check uniqueness: 11 | self._timers_set = set() 12 | # Timers heap. Used to get the closest timer event: 13 | self._timers_heap = [] 14 | 15 | def add(self, when): 16 | """ 17 | Add a timer (Future event). 18 | """ 19 | # We don't add a time twice: 20 | if when in self._timers_set: 21 | return 22 | 23 | # Add to set: 24 | self._timers_set.add(when) 25 | # Add to heap: 26 | heapq.heappush(self._timers_heap, when) 27 | 28 | def is_empty(self): 29 | return len(self._timers_set) == 0 30 | 31 | def pop_closest(self): 32 | """ 33 | Get closest event timer. (The one that will happen the soonest). 34 | """ 35 | if self.is_empty(): 36 | raise IndexError("NextTimers is empty") 37 | 38 | when = heapq.heappop(self._timers_heap) 39 | self._timers_set.remove(when) 40 | 41 | return when 42 | 43 | 44 | # Based on TestSelector from asyncio.test_utils: 45 | class TestSelector(selectors.BaseSelector): 46 | def __init__(self): 47 | self.keys = {} 48 | 49 | def register(self, fileobj, events, data=None): 50 | key = selectors.SelectorKey(fileobj, 0, events, data) 51 | self.keys[fileobj] = key 52 | return key 53 | 54 | def unregister(self, fileobj): 55 | return self.keys.pop(fileobj) 56 | 57 | def select(self, timeout=None): 58 | return [] 59 | 60 | def get_map(self): 61 | return self.keys 62 | 63 | 64 | # Based on TestLoop from asyncio.test_utils: 65 | class TimeTravelLoop(base_events.BaseEventLoop): 66 | """ 67 | Loop for unittests. Passes time without waiting, but makes sure events 68 | happen in the correct order. 69 | """ 70 | 71 | def __init__(self): 72 | super().__init__() 73 | 74 | self._time = 0 75 | self._clock_resolution = 1e-9 76 | self._timers = NextTimers() 77 | self._selector = TestSelector() 78 | 79 | self.readers = {} 80 | self.writers = {} 81 | self.reset_counters() 82 | 83 | def time(self): 84 | return self._time 85 | 86 | def advance_time(self, advance): 87 | """Move test time forward.""" 88 | if advance: 89 | self._time += advance 90 | 91 | def add_reader(self, fd, callback, *args): 92 | self.readers[fd] = events.Handle(callback, args, self) 93 | 94 | def remove_reader(self, fd): 95 | self.remove_reader_count[fd] += 1 96 | if fd in self.readers: 97 | del self.readers[fd] 98 | return True 99 | else: 100 | return False 101 | 102 | def assert_reader(self, fd, callback, *args): 103 | assert fd in self.readers, "fd {} is not registered".format(fd) 104 | handle = self.readers[fd] 105 | assert handle._callback == callback, "{!r} != {!r}".format( 106 | handle._callback, callback 107 | ) 108 | assert handle._args == args, "{!r} != {!r}".format(handle._args, args) 109 | 110 | def add_writer(self, fd, callback, *args): 111 | self.writers[fd] = events.Handle(callback, args, self) 112 | 113 | def remove_writer(self, fd): 114 | self.remove_writer_count[fd] += 1 115 | if fd in self.writers: 116 | del self.writers[fd] 117 | return True 118 | else: 119 | return False 120 | 121 | def assert_writer(self, fd, callback, *args): 122 | assert fd in self.writers, "fd {} is not registered".format(fd) 123 | handle = self.writers[fd] 124 | assert handle._callback == callback, "{!r} != {!r}".format( 125 | handle._callback, callback 126 | ) 127 | assert handle._args == args, "{!r} != {!r}".format(handle._args, args) 128 | 129 | def reset_counters(self): 130 | self.remove_reader_count = collections.defaultdict(int) 131 | self.remove_writer_count = collections.defaultdict(int) 132 | 133 | def _run_once(self): 134 | super()._run_once() 135 | # Advance time only when we finished everything at the present: 136 | if len(self._ready) == 0: 137 | if not self._timers.is_empty(): 138 | self._time = self._timers.pop_closest() 139 | # print('time:',self._time,'timers:',self._timers._timers_set) 140 | 141 | def call_at(self, when, callback, *args, **kwargs): 142 | self._timers.add(when) 143 | return super().call_at(when, callback, *args, **kwargs) 144 | 145 | def _process_events(self, event_list): 146 | return 147 | 148 | def _write_to_self(self): 149 | pass 150 | -------------------------------------------------------------------------------- /asyncio_time_travel/tests/test_time_travel_loop.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import asyncio 3 | 4 | try: 5 | from asyncio.exceptions import TimeoutError 6 | except ImportError: 7 | from asyncio.futures import TimeoutError 8 | 9 | from ..time_travel_util import TimeTravelLoop 10 | 11 | 12 | def run_until_timeout(cor, loop, timeout=None): 13 | """ 14 | Run a given coroutine with timeout. 15 | """ 16 | task_with_timeout = asyncio.wait_for(cor, timeout, loop=loop) 17 | loop.run_until_complete(task_with_timeout) 18 | 19 | 20 | def test_time_travel_loop_basic_timeout(): 21 | """ 22 | Sleep a for a long time. Expect timeout when not enough time is given for 23 | the coroutine. 24 | """ 25 | 26 | SLEEP_TIME = 0x1000 27 | NUM_SLEEPS = 5 28 | 29 | tloop = TimeTravelLoop() 30 | 31 | async def inner_cor(): 32 | for i in range(NUM_SLEEPS): 33 | await asyncio.sleep(SLEEP_TIME, loop=tloop) 34 | 35 | # Expected time for running: 36 | total_time = SLEEP_TIME * NUM_SLEEPS 37 | 38 | # This should work correctly: 39 | tloop.run_until_complete( 40 | asyncio.wait_for(inner_cor(), timeout=total_time + 1, loop=tloop) 41 | ) 42 | 43 | run_until_timeout(inner_cor(), loop=tloop, timeout=total_time + 1) 44 | 45 | with pytest.raises(TimeoutError): 46 | run_until_timeout(inner_cor(), loop=tloop, timeout=total_time - 1) 47 | tloop.close() 48 | 49 | 50 | def test_time_travel_loop_concurrent_sleep(): 51 | """ 52 | Create a few tasks that finish at different times. 53 | Expect the finish times to be of specific order. 54 | """ 55 | 56 | # A long sleeping time: 57 | SLEEP_TIME = 0x1000 58 | 59 | # results list: 60 | res_list = [] 61 | 62 | def add_res(res): 63 | """Add a result""" 64 | res_list.append(res) 65 | 66 | tloop = TimeTravelLoop() 67 | 68 | async def inner_cor(): 69 | # Add result 0: 70 | add_res(0) 71 | 72 | task1 = asyncio.ensure_future( 73 | asyncio.sleep(delay=SLEEP_TIME * 3, loop=tloop), loop=tloop 74 | ) 75 | task2 = asyncio.ensure_future( 76 | asyncio.sleep(delay=SLEEP_TIME * 1, loop=tloop), loop=tloop 77 | ) 78 | task3 = asyncio.ensure_future( 79 | asyncio.sleep(delay=SLEEP_TIME * 4, loop=tloop), loop=tloop 80 | ) 81 | task4 = asyncio.ensure_future( 82 | asyncio.sleep(delay=SLEEP_TIME * 2, loop=tloop), loop=tloop 83 | ) 84 | 85 | task1.add_done_callback(lambda x: add_res(1)) 86 | task2.add_done_callback(lambda x: add_res(2)) 87 | task3.add_done_callback(lambda x: add_res(3)) 88 | task4.add_done_callback(lambda x: add_res(4)) 89 | 90 | tasks = [task1, task2, task3, task4] 91 | 92 | # Wait for all the tasks to complete: 93 | await asyncio.wait(tasks, loop=tloop, timeout=None) 94 | 95 | # Expected time for running: 96 | total_time = (SLEEP_TIME * 4) + 1 97 | 98 | run_until_timeout(inner_cor(), loop=tloop, timeout=total_time) 99 | tloop.close() 100 | 101 | assert res_list == [0, 2, 4, 1, 3] 102 | 103 | 104 | def test_time_travel_loop_complex_order(): 105 | """ 106 | Build different points in time using coroutines that yield to other 107 | coroutines and asyncio.sleep. Make sure that the events are in the expected 108 | order. 109 | """ 110 | 111 | # results list: 112 | res_list = [] 113 | 114 | # List of async_tasks to wait for. 115 | async_tasks = [] 116 | 117 | def add_res(res): 118 | """Add a result""" 119 | res_list.append(res) 120 | 121 | tloop = TimeTravelLoop() 122 | 123 | async def inner_cor(): 124 | add_res(0) 125 | 126 | # Start cor_a: 127 | task = asyncio.ensure_future(cor_a(), loop=tloop) # time=0 128 | 129 | asyncio.ensure_future( 130 | asyncio.sleep(delay=500, loop=tloop), loop=tloop 131 | ).add_done_callback( 132 | lambda x: add_res(2) 133 | ) # time=0 --> 500 134 | 135 | asyncio.ensure_future( 136 | asyncio.sleep(delay=1500, loop=tloop), loop=tloop 137 | ).add_done_callback( 138 | lambda x: add_res(4) 139 | ) # time=0 --> 1500 140 | 141 | # Wait for the longest task to complete: 142 | await asyncio.wait_for(task, timeout=None, loop=tloop) 143 | 144 | async def cor_a(): 145 | add_res(1) # time=0 146 | await asyncio.sleep(1000, loop=tloop) 147 | await cor_b() # time=1000 148 | 149 | async def cor_b(): 150 | add_res(3) # time=1000 151 | await asyncio.sleep(1000, loop=tloop) 152 | add_res(5) # time=2000 153 | asyncio.ensure_future( 154 | asyncio.sleep(delay=500, loop=tloop), loop=tloop 155 | ).add_done_callback( 156 | lambda x: add_res(8) 157 | ) # time=2000 --> 2500 158 | await cor_c() 159 | 160 | async def cor_c(): 161 | add_res(6) # time=2000 162 | await cor_d() 163 | await asyncio.sleep(1000, loop=tloop) 164 | add_res(9) # time=3000 165 | 166 | async def cor_d(): 167 | add_res(7) # time=2000 168 | 169 | # Expected time for running: 170 | total_time = 3000 + 1 171 | 172 | run_until_timeout(inner_cor(), loop=tloop, timeout=total_time) 173 | tloop.close() 174 | 175 | assert res_list == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 176 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------