Contracts#
Repositories#
- class ChannelRepositoryContract#
Use cases every channel repository has to satisfy, whatever its storages or custom repositories.
The fixture is a
ChannelRepositoryBuilder: to check a custom part, subclass this together withTestCaseand return a builder configured with it:class RedisStorageTestCase(ChannelRepositoryContract, TestCase): def create_builder(self): return ChannelRepositoryBuilder(SSEChannel, RedisStorage()) def channel_data(self): return {'retry_timeout_milliseconds': 10}
Not a
TestCaseitself, so the abstract class is not collected.- abstract create_builder()#
Returns the builder under test. Called once per test.
- Return type:
Channels#
- class ChannelContract#
Use cases every channel class has to satisfy: managing connections and delivering messages.
The fixture is a
ChannelRepositoryBuilderfor the channel class:class MyChannelTestCase(ChannelContract, IsolatedAsyncioTestCase): def create_builder(self): return ChannelRepositoryBuilder(MyChannel) def channel_data(self): return {}
Streamed events are compared with the channel’s own
adapt, as their format is up to each channel. Not aTestCaseitself, so the abstract class is not collected.- abstract create_builder()#
Returns the builder under test. Called once per test.
- Return type:
Error handling#
- class ChannelErrorHandlingContract#
Use cases every channel class has to satisfy when queues and listeners fail.
The fixture is a
ChannelRepositoryBuilderfor the channel class. Tests replace its listeners and queues with failing ones where needed:class MyChannelTestCase(ChannelErrorHandlingContract, IsolatedAsyncioTestCase): def create_builder(self): return ChannelRepositoryBuilder(MyChannel) def channel_data(self): return {}
Not a
TestCaseitself, so the abstract class is not collected.- abstract create_builder()#
Returns the builder under test. Called once per scenario.
- Return type:
Queues#
- class QueueContract#
Use cases every
Queueimplementation has to satisfy.The fixture is a
ChannelRepositoryBuildercreating queues of the class under test:class RedisQueueTestCase(QueueContract, IsolatedAsyncioTestCase): def create_builder(self): return ChannelRepositoryBuilder(SSEChannel).queues(RedisQueue) def channel_data(self): return {}
Not a
TestCaseitself, so the abstract class is not collected.- abstract create_builder()#
Returns the builder under test. Called once per test.
- Return type:
Reliable queues#
- class ReliableQueueContract#
Use cases every
ReliableQueuesetup has to satisfy, whatever its inner queue and in-flight storage.The fixture is a
ChannelRepositoryBuilderwhose queues are reliable queues with the given visibility timeout:class RedisReliableQueueTestCase(ReliableQueueContract, IsolatedAsyncioTestCase): def create_builder(self, visibility_timeout): return ChannelRepositoryBuilder(SSEChannel, RedisStorage()).queues( lambda: ReliableQueue(RedisQueue(), visibility_timeout=visibility_timeout, in_flight=RedisStorage()) ) def channel_data(self): return {}
Concrete classes must be
IsolatedAsyncioTestCase, as some use cases are async. Not aTestCaseitself, so the abstract class is not collected.
Test doubles#
Test doubles used by contracts, e.g. parts that always fail and error handlers recording what they handle.
- class BrokenQueue#
Fails on every operation. Holds no state, so serialising storages can persist it.
- pop(timeout=0)#
Next message from the queue.
- Parameters:
timeout (float) – Seconds to wait for a message. With 0 it returns immediately.
- Return type:
Raises a
NoMessagesExceptionif no message is available, after waiting timeout seconds.
- async wait_pop(timeout)#
Waits for the next message without blocking the event loop.
The default implementation runs
pop()with timeout in a worker thread, so it takes one thread per waiting call. Override it with a native async wait when possible.- Parameters:
timeout (float) – Seconds to wait for a message.
- Return type:
Raises a
NoMessagesExceptionif no message arrives within timeout.
- class BrokenListener#
- on_message(msg)#
Event handler. It executes when a message is delivered to client
- Parameters:
msg (MessageContract)
- Return type:
None
- class RecordingQueuingErrorHandler#
- __init__()#
- handle_push_error(connection_id, msg, exception)#
Called when pushing msg to the queue of connection connection_id fails. The exception is re-raised afterwards.
- Parameters:
connection_id (str)
msg (MessageContract)
exception (Exception)
- class RecordingListenerErrorHandler#
- __init__()#
- handle_on_message_error(connection_id, msg, exception)#
Called when
on_message()of connection connection_id fails. The exception is not re-raised: msg is delivered anyway.- Parameters:
connection_id (str)
msg (MessageContract)
exception (Exception)