Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user backends #110

Merged
merged 4 commits into from
Apr 4, 2024

Conversation

alex-oleshkevich
Copy link
Member

@alex-oleshkevich alex-oleshkevich commented Mar 27, 2024

This PR allows usage of custom user backends. Users can build their own backends and not be dependent on the shipped backends.

backend = MemoryBackend("")
async with Broadcast(backend=backend) as broadcast:
    async with broadcast.subscribe("chatroom") as subscriber:
        await broadcast.publish("chatroom", "hello")
        event = await subscriber.get()
        assert event.channel == "chatroom"
        assert event.message == "hello"

Also closes #90

@alex-oleshkevich
Copy link
Member Author

@encode/maintainers anyone?

@@ -1,6 +1,7 @@
import pytest

from broadcaster import Broadcast
from broadcaster._backends.memory import MemoryBackend
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐼 Can we test against public API?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have public API for backends. If we reexport them in __init__.py then they will raise import errors because of missing dependencies. This is quite a big, unrelated change.
Or, alternatively, we can rename _backends -> backends making this public. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah understood.

Given the intent of this PR perhaps the test could be against broadcaster.BroadcastBackend?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

broadcaster.BroadcastBackend is an abstract class. Do you mean extending it for this particular case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.
For httpx we have a policy of only testing against the public API, which seems a good pattern.
We could be following this here also, right?

README.md Outdated Show resolved Hide resolved
Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, thanks.
Couple of suggestions.

Comment on lines +80 to +81
backend = CustomBackend("")
async with Broadcast(backend=backend) as broadcast:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

@alex-oleshkevich alex-oleshkevich merged commit eeb09de into encode:master Apr 4, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot provide my own BroadcastBackend
2 participants