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 with TestCase and 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 TestCase itself, so the abstract class is not collected.

abstract create_builder()#

Returns the builder under test. Called once per test.

Return type:

ChannelRepositoryBuilder

abstract channel_data()#

Returns the data passed to the built repository’s create to create a channel of the builder’s channel class. Without channel_id: tests that need a given id add it, the other ones need generated ids, so that each call creates a different channel.

Return type:

dict

Channels#

class ChannelContract#

Use cases every channel class has to satisfy: managing connections and delivering messages.

The fixture is a ChannelRepositoryBuilder for 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 a TestCase itself, so the abstract class is not collected.

abstract create_builder()#

Returns the builder under test. Called once per test.

Return type:

ChannelRepositoryBuilder

abstract channel_data()#

Returns the data passed to the built repository’s create, see ChannelRepositoryContract.

Return type:

dict

Error handling#

class ChannelErrorHandlingContract#

Use cases every channel class has to satisfy when queues and listeners fail.

The fixture is a ChannelRepositoryBuilder for 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 TestCase itself, so the abstract class is not collected.

abstract create_builder()#

Returns the builder under test. Called once per scenario.

Return type:

ChannelRepositoryBuilder

abstract channel_data()#

Returns the data passed to the built repository’s create, see ChannelRepositoryContract.

Return type:

dict

Queues#

class QueueContract#

Use cases every Queue implementation has to satisfy.

The fixture is a ChannelRepositoryBuilder creating 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 TestCase itself, so the abstract class is not collected.

abstract create_builder()#

Returns the builder under test. Called once per test.

Return type:

ChannelRepositoryBuilder

abstract channel_data()#

Returns the data passed to the built repository’s create, see ChannelRepositoryContract.

Return type:

dict

Reliable queues#

class ReliableQueueContract#

Use cases every ReliableQueue setup has to satisfy, whatever its inner queue and in-flight storage.

The fixture is a ChannelRepositoryBuilder whose 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 a TestCase itself, so the abstract class is not collected.

abstract create_builder(visibility_timeout)#

Returns the builder under test, creating reliable queues with visibility_timeout.

Parameters:

visibility_timeout (float | None)

Return type:

ChannelRepositoryBuilder

abstract channel_data()#

Returns the data passed to the built repository’s create, see ChannelRepositoryContract.

Return type:

dict

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:

MessageContract

Raises a NoMessagesException if 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:

MessageContract

Raises a NoMessagesException if 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:
handle_pop_error(connection_id, exception)#

Called when popping from the queue of connection connection_id fails. The exception is re-raised afterwards.

Parameters:
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: