Module selector¶
Mock of selectors and compatible objects performing asynchronous IO.
This module provides classes to mock objects performing IO (files, sockets,
etc). These mocks are compatible with TestSelector, which
can simulate the behavior of a selector on the mock objects, or forward actual
work to a real selector.
Mocking file-like objects¶
-
class
asynctest.FileMock(*args, **kwargs)[source]¶ Mock a file-like object.
A FileMock is an intelligent mock which can work with TestSelector to simulate IO events during tests.
-
fileno()¶ Return a
FileDescriptorobject.
-
-
class
asynctest.SocketMock(side_effect=None, return_value=sentinel.DEFAULT, wraps=None, name=None, spec_set=None, parent=None, **kwargs)[source]¶ Bases:
asynctest.selector.FileMockMock a socket.
See
FileMock.
-
class
asynctest.SSLSocketMock(side_effect=None, return_value=sentinel.DEFAULT, wraps=None, name=None, spec_set=None, parent=None, **kwargs)[source]¶ Bases:
asynctest.selector.SocketMockMock a socket wrapped by the
sslmodule.See
FileMock.New in version 0.5.
-
class
asynctest.FileDescriptor[source]¶ Bases:
intA subclass of int which allows to identify the virtual file-descriptor of a
FileMock.If
FileDescriptor()without argument, its value will be the value ofnext_fd.When an object is created,
next_fdis set to the highest value for aFileDescriptorobject + 1.-
next_fd= 0¶
-
Helpers¶
-
asynctest.fd(fileobj)[source]¶ Return the
FileDescriptorvalue offileobj.If
fileobjis aFileDescriptor,fileobjis returned, elsefileobj.fileno()is returned instead.Note that if fileobj is an int,
ValueErroris raised.Raises: ValueError – if fileobjis not aFileMock, a file-like object or aFileDescriptor.
-
asynctest.isfilemock(obj)[source]¶ Return
Trueif theobjorobj.fileno()is aasynctest.FileDescriptor.
Mocking the selector¶
-
class
asynctest.TestSelector(selector=None)[source]¶ A selector which supports IOMock objects.
It can wrap an actual implementation of a selector, so the selector will work both with mocks and real file-like objects.
A common use case is to patch the selector loop:
loop._selector = asynctest.TestSelector(loop._selector)
Parameters: selector – optional, if provided, this selector will be used to work with real file-like objects. -
close()[source]¶ Close the selector.
Close the actual selector if supplied, unregister all mocks.
See
selectors.BaseSelector.close().
-
modify(fileobj, events, data=None)[source]¶ Shortcut when calling
TestSelector.unregister()thenTestSelector.register()to update the registration of a an object to the selector.See
selectors.BaseSelector.modify().
-
register(fileobj, events, data=None)[source]¶ Register a file object or a
FileMock.If a real selector object has been supplied to the
TestSelectorobject andfileobjis not aFileMockor aFileDescriptorreturned byFileMock.fileno(), the object will be registered to the real selector.See
selectors.BaseSelector.register().
-
Helpers¶
-
asynctest.set_read_ready(fileobj, loop)[source]¶ Schedule callbacks registered on
loopas if the selector notified that data is ready to be read onfileobj.Parameters: - fileobj – file object or
FileMockon which the event is mocked. - loop –
asyncio.SelectorEventLoopwatching for events onfileobj.
mock = asynctest.SocketMock() mock.recv.return_value = b"Data" def read_ready(sock): print("received:", sock.recv(1024)) loop.add_reader(mock, read_ready, mock) set_read_ready(mock, loop) loop.run_forever() # prints received: b"Data"
New in version 0.4.
- fileobj – file object or
-
asynctest.set_write_ready(fileobj, loop)[source]¶ Schedule callbacks registered on
loopas if the selector notified that data can be written tofileobj.Parameters: - fileobj – file object or
FileMockon which th event is mocked. - loop –
asyncio.SelectorEventLoopwatching for events onfileobj.
New in version 0.4.
- fileobj – file object or