Skip to content

TestKafkaBroker

faststream.confluent.testing.TestKafkaBroker #

TestKafkaBroker(
    broker: KafkaBroker,
    /,
    *,
    with_real: bool = False,
    connect_only: bool | None = None,
)
TestKafkaBroker(
    *brokers: KafkaBroker,
    with_real: bool = False,
    connect_only: bool | None = None,
)
TestKafkaBroker(
    *brokers: KafkaBroker,
    with_real: bool = False,
    connect_only: bool | None = None,
)

Bases: TestBroker[KafkaBroker, EnterType]

A class to test Kafka brokers.

Source code in faststream/confluent/testing.py
64
65
66
67
68
69
70
71
72
73
74
def __init__(
    self,
    *brokers: KafkaBroker,
    with_real: bool = False,
    connect_only: bool | None = None,
) -> None:
    super().__init__(
        *brokers,
        with_real=with_real,
        connect_only=connect_only,
    )

with_real instance-attribute #

with_real = with_real

brokers instance-attribute #

brokers = brokers

connect_only instance-attribute #

connect_only = connect_only

create_publisher_fake_subscriber #

create_publisher_fake_subscriber(
    broker: KafkaBroker, publisher: LogicPublisher
) -> tuple[LogicSubscriber[Any], bool]
Source code in faststream/confluent/testing.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def create_publisher_fake_subscriber(
    self,
    broker: KafkaBroker,
    publisher: "LogicPublisher",
) -> tuple["LogicSubscriber[Any]", bool]:
    sub: LogicSubscriber[Any] | None = None
    for handler in (s for b in self.brokers for s in b.subscribers):
        handler = cast("LogicSubscriber[Any]", handler)
        if _is_handler_matches(
            handler,
            topic=publisher.topic,
            partition=publisher.partition,
        ):
            sub = handler
            break

    if sub is None:
        is_real = False

        topic_name = publisher.topic

        if publisher.partition:
            tp = TopicPartition(
                topic=topic_name,
                partition=publisher.partition,
            )
            sub = broker.subscriber(
                partitions=[tp],
                batch=isinstance(publisher, BatchPublisher),
                auto_offset_reset="earliest",
                persistent=False,
            )
        else:
            sub = broker.subscriber(
                topic_name,
                batch=isinstance(publisher, BatchPublisher),
                auto_offset_reset="earliest",
                persistent=False,
            )
    else:
        is_real = True

    return sub, is_real