File: //opt/alt/python37/lib/python3.7/site-packages/asynctest/__pycache__/case.cpython-37.pyc
B
�P�e�D � @ s� d Z ddlZddlZddlZddlZddlZddlZddlT ddlZ ddl
Z G dd� dej�ZG dd� dej
�Z
G dd � d e
ej�ZG d
d� de
�Zddd
�ZdS )a�
Module ``case``
---------------
Enhance :class:`unittest.TestCase`:
* a new loop is issued and set as the default loop before each test, and
closed and disposed after,
* if the loop uses a selector, it will be wrapped with
:class:`asynctest.TestSelector`,
* a test method in a TestCase identified as a coroutine function or returning
a coroutine will run on the loop,
* :meth:`~TestCase.setUp()` and :meth:`~TestCase.tearDown()` methods can be
coroutine functions,
* cleanup functions registered with :meth:`~TestCase.addCleanup()` can be
coroutine functions,
* a test fails if the loop did not run during the test.
class-level set-up
~~~~~~~~~~~~~~~~~~
Since each test runs in its own loop, it is not possible to run
:meth:`~TestCase.setUpClass()` and :meth:`~TestCase.tearDownClass()` as
coroutines.
If one needs to perform set-up actions at the class level (meaning
once for all tests in the class), it should be done using a loop created for
this sole purpose and that is not shared with the tests. Ideally, the loop
shall be closed in the method which creates it.
If one really needs to share a loop between tests,
:attr:`TestCase.use_default_loop` can be set to ``True`` (as a class
attribute). The test case will use the loop returned by
:meth:`asyncio.get_event_loop()` instead of creating a new loop for each test.
This way, the event loop or event loop policy can be set during class-level
set-up and tear down.
� N)�*c @ sL e Zd Zdd� Zdd� Zdd� Zdd� Zd d
� Zdd� Zd
d� Z dd� Z
dS )�_Policyc C s || _ || _|| _d | _d S )N)�original_policy�forbid_get_event_loop�loop�watcher)�selfr r r � r �?/opt/alt/python37/lib/python3.7/site-packages/asynctest/case.py�__init__; s z_Policy.__init__c C s* | j rtd��n| jr| jS | j�� S d S )NzSTestCase.forbid_get_event_loop is True, asyncio.get_event_loop() must not be called)r �AssertionErrorr r �get_event_loop)r r r r
r
E s
z_Policy.get_event_loopc C s
| j �� S )N)r �new_event_loop)r r r r
r N s z_Policy.new_event_loopc C s | j �|�}|| _|S )N)r �set_event_loopr )r r �resultr r r
r Q s z_Policy.set_event_loopc C s t td�st�d S )N�SafeChildWatcher)�hasattr�asyncio�NotImplementedError)r r r r
�_check_unixV s
z_Policy._check_unixc C sD | � � | jr6| jd kr0t�� | _| j�| j� | jS | j�� S d S )N)r r r r r �attach_loopr �get_child_watcher)r r r r
r Z s
z_Policy.get_child_watcherc C s4 | � � | jr$| j�|�}|| _|S | j�|�S d S )N)r r r �set_child_watcherr )r r r r r r
r e s z_Policy.set_child_watcherc C s | j r| j �� | j�d � d S )N)r �closer r )r r r r
�
reset_watchern s
z_Policy.reset_watcherN)�__name__�
__module__�__qualname__r r
r r r r r r r r r r
r : s
r c s� e Zd ZdZdZdZdZdd� Zdd� Zdd � Z d
d� Z
dd
� Zd dd�Zdd� Z
dd� Zejdd� �Z� fdd�Zejdd� �Zejdd� �Zejdd� �Zejdd� �Z� ZS )!�TestCaseaJ
A test which is a coroutine function or which returns a coroutine will run
on the loop.
Once the test returned, one or more assertions are checked. For instance,
a test fails if the loop didn't run. These checks can be enabled or
disabled using the :func:`~asynctest.fail_on` decorator.
By default, a new loop is created and is set as the default loop before
each test. Test authors can retrieve this loop with
:attr:`~asynctest.TestCase.loop`.
If :attr:`~asynctest.TestCase.use_default_loop` is set to ``True``, the
current default event loop is used instead. In this case, it is up to the
test author to deal with the state of the loop in each test: the loop might
be closed, callbacks and tasks may be scheduled by previous tests. It is
also up to the test author to close the loop and dispose the related
resources.
If :attr:`~asynctest.TestCase.forbid_get_event_loop` is set to ``True``,
a call to :func:`asyncio.get_event_loop()` will raise an
:exc:`AssertionError`. Since Python 3.6, calling
:func:`asyncio.get_event_loop()` from a callback or a coroutine will return
the running loop (instead of raising an exception).
These behaviors should be configured when defining the test case class::
class With_Reusable_Loop_TestCase(asynctest.TestCase):
use_default_loop = True
forbid_get_event_loop = False
def test_something(self):
pass
If :meth:`setUp()` and :meth:`tearDown()` are coroutine functions, they
will run on the loop. Note that :meth:`setUpClass()` and
:meth:`tearDownClass()` can not be coroutines.
.. versionadded:: 0.5
attribute :attr:`~asynctest.TestCase.use_default_loop`.
.. versionadded:: 0.7
attribute :attr:`~asynctest.TestCase.forbid_get_event_loop`.
In any case, the default loop is now reset to its original state
outside a test function.
.. versionadded:: 0.8
``ignore_loop`` has been deprecated in favor of the extensible
:func:`~asynctest.fail_on` decorator.
FNc C sR | j rt�� | _d }nt�� }| _tt�� || j�}t�|� | � | j�| _d S )N)
�use_default_loopr r
r r r �get_event_loop_policyr �set_event_loop_policy�_patch_loop)r r �policyr r r
�
_init_loop� s
zTestCase._init_loopc C sR t �� }| js<tjdkr*| j�| j�� � | j�� |� � t �
|j� d | _d S )N)� � )r r r �sys�version_infor �run_until_complete�shutdown_asyncgensr r r! r )r r# r r r
�_unset_loop� s
zTestCase._unset_loopc sf t � d�r� S d� _� fdd�}x"dD ]}t� ||t� |��� q&W t� tjj�rbtj �
� j�� _� S )N�_asynctest_ranFc s$ t �� ��� fdd��}t�|��S )Nc s z
�||�S d� _ X d S )NT)r, )r �args�kwargs)r �methodr r
�wrapper� s
z4TestCase._patch_loop.<locals>.wraps.<locals>.wrapper)� functools�wraps�types�
MethodType)r/ r0 )r )r/ r
r2 � s z#TestCase._patch_loop.<locals>.wraps)�run_foreverr) )r r, �setattr�getattr�
isinstancer �selector_events�BaseSelectorEventLoop� asynctest�selectorZTestSelector� _selector)r r r2 r/ r )r r
r"