summaryrefslogtreecommitdiffstats
path: root/Doc/library/unittest.mock.rst
diff options
context:
space:
mode:
authorMario Corchero <mcorcherojim@bloomberg.net>2023-07-03 06:56:54 (GMT)
committerGitHub <noreply@github.com>2023-07-03 06:56:54 (GMT)
commitd65b783b6966d233467a48ef633afb4aff9d5df8 (patch)
tree705e48278f6729282516cdc7491ec51860b5c0fb /Doc/library/unittest.mock.rst
parent0355625d94a50f4b816770bad946420d005900b8 (diff)
downloadcpython-d65b783b6966d233467a48ef633afb4aff9d5df8.zip
cpython-d65b783b6966d233467a48ef633afb4aff9d5df8.tar.gz
cpython-d65b783b6966d233467a48ef633afb4aff9d5df8.tar.bz2
gh-61215: New mock to wait for multi-threaded events to happen (#16094)
mock: Add `ThreadingMock` class Add a new class that allows to wait for a call to happen by using `Event` objects. This mock class can be used to test and validate expectations of multithreading code. It uses two attributes for events to distinguish calls with any argument and calls with specific arguments. The calls with specific arguments need a lock to prevent two calls in parallel from creating the same event twice. The timeout is configured at class and constructor level to allow users to set a timeout, we considered passing it as an argument to the function but it could collide with a function parameter. Alternatively we also considered passing it as positional only but from an API caller perspective it was unclear what the first number meant on the function call, think `mock.wait_until_called(1, "arg1", "arg2")`, where 1 is the timeout. Lastly we also considered adding the new attributes to magic mock directly rather than having a custom mock class for multi threading scenarios, but we preferred to have specialised class that can be composed if necessary. Additionally, having added it to `MagicMock` directly would have resulted in `AsyncMock` having this logic, which would not work as expected, since when if user "waits" on a coroutine does not have the same meaning as waiting on a standard call. Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
Diffstat (limited to 'Doc/library/unittest.mock.rst')
-rw-r--r--Doc/library/unittest.mock.rst47
1 files changed, 47 insertions, 0 deletions
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index 6c4d801..756422b 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -205,8 +205,10 @@ The Mock Class
import asyncio
import inspect
import unittest
+ import threading
from unittest.mock import sentinel, DEFAULT, ANY
from unittest.mock import patch, call, Mock, MagicMock, PropertyMock, AsyncMock
+ from unittest.mock import ThreadingMock
from unittest.mock import mock_open
:class:`Mock` is a flexible mock object intended to replace the use of stubs and
@@ -1099,6 +1101,51 @@ object::
[call('foo'), call('bar')]
+.. class:: ThreadingMock(spec=None, side_effect=None, return_value=DEFAULT, wraps=None, name=None, spec_set=None, unsafe=False, *, timeout=UNSET, **kwargs)
+
+ A version of :class:`MagicMock` for multithreading tests. The
+ :class:`ThreadingMock` object provides extra methods to wait for a call to
+ be invoked, rather than assert on it immediately.
+
+ The default timeout is specified by the ``timeout`` argument, or if unset by the
+ :attr:`ThreadingMock.DEFAULT_TIMEOUT` attribute, which defaults to blocking (``None``).
+
+ You can configure the global default timeout by setting :attr:`ThreadingMock.DEFAULT_TIMEOUT`.
+
+ .. method:: wait_until_called(*, timeout=UNSET)
+
+ Waits until the mock is called.
+
+ If a timeout was passed at the creation of the mock or if a timeout
+ argument is passed to this function, the function raises an
+ :exc:`AssertionError` if the call is not performed in time.
+
+ >>> mock = ThreadingMock()
+ >>> thread = threading.Thread(target=mock)
+ >>> thread.start()
+ >>> mock.wait_until_called(timeout=1)
+ >>> thread.join()
+
+ .. method:: wait_until_any_call(*args, **kwargs)
+
+ Waits until the the mock is called with the specified arguments.
+
+ If a timeout was passed at the creation of the mock
+ the function raises an :exc:`AssertionError` if the call is not performed in time.
+
+ >>> mock = ThreadingMock()
+ >>> thread = threading.Thread(target=mock, args=("arg1", "arg2",), kwargs={"arg": "thing"})
+ >>> thread.start()
+ >>> mock.wait_until_any_call("arg1", "arg2", arg="thing")
+ >>> thread.join()
+
+ .. attribute:: DEFAULT_TIMEOUT
+
+ Global default timeout in seconds to create instances of :class:`ThreadingMock`.
+
+ .. versionadded:: 3.13
+
+
Calling
~~~~~~~