Commit Graph

6 Commits

Author SHA1 Message Date
Alex Waygood 19601efa36
gh-109653: Remove unused imports in the `Lib/` directory (#109803) 2023-09-24 15:07:23 +01:00
Mario Corchero 7e96370a94
gh-61215: threadingmock: Improve test suite to avoid race conditions (#106822)
threadingmock: Improve test suite to avoid race conditions

Simplify tests and split them into multiple tests to prevent assertions
from triggering race conditions.
Additionally, we rely on calling the mocks without delay to validate the
functionality of matching calls.
2023-07-17 18:57:40 +00:00
Chris Withers e6379f72cb
Remove unused branches from mock module (#106617)
* lambda has a name of __none__, but no async lambda so this branch is not needed

* _get_signature_object only returns None for bound builtins. There are no async builtins so this branch isn't needed

* Exclude  a couple of methods from coverage checking in the downstream rolling backport of mock
2023-07-11 08:52:12 +00:00
Mario Corchero 56353b1002
gh-106458: Mark `testthreadingmock.py` with `@requires_working_threading` (GH-106366)
Mark `testthreadingmock.py` with `threading_helper.requires_working_threading`.

Also add longer delays to reduce the change of a race conditions on the tests that validate short timeouts.
2023-07-06 10:54:45 -07:00
Mario Corchero 2dfc7fae78
gh-61215: Rename `wait_until_any_call` to `wait_until_any_call_with` (#106414)
mock: Rename `wait_until_any_call` to `wait_until_any_call_with`

Rename the method to be more explicit that it expects the args and
kwargs to wait for.
2023-07-04 18:34:43 +01:00
Mario Corchero d65b783b69
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>
2023-07-03 07:56:54 +01:00