Example #1
0
 def test_persisted_offset(self, *, store):
     with pytest.raises(NotImplementedError):
         store.persisted_offset(TP("foo", 0))
Example #2
0
 def _new_topicpartition(self, *args, **kwargs):
     return TP(*args, **kwargs)
Example #3
0
 def test_is_changelog_tp(self, *, app, consumer):
     app.tables = Mock(name="tables", autospec=TableManager)
     app.tables.changelog_topics = {"foo", "bar"}
     assert consumer._is_changelog_tp(TP("foo", 31))
     assert consumer._is_changelog_tp(TP("bar", 0))
     assert not consumer._is_changelog_tp(TP("baz", 3))
Example #4
0
 def test_persisted_offset(self, *, store):
     assert store.persisted_offset(TP("foo", 0)) is None
Example #5
0
def ensure_TP(tp: Any) -> TP:
    return tp if isinstance(tp, TP) else TP(tp.topic, tp.partition)
Example #6
0
from collections import Counter
import pytest
from faust.tables.recovery import RebalanceAgain, Recovery, ServiceStopped
from faust.types import TP
from mode.utils.mocks import AsyncMock, Mock

TP1 = TP('foo', 6)
TP2 = TP('bar', 3)
TP3 = TP('baz', 1)
TP4 = TP('xuz', 0)


@pytest.fixture()
def tables():
    return Mock(name='tables')


@pytest.fixture()
def recovery(*, tables, app):
    return Recovery(app, tables)


class test_Recovery:
    @pytest.fixture()
    def table(self):
        return Mock(name='table')

    def test_init(self, *, recovery, tables):
        assert recovery.tables is tables
        assert recovery.signal_recovery_start
        assert recovery.signal_recovery_end
Example #7
0
import asyncio
import operator
from copy import copy

import pytest
from mode import label, shortlabel
from mode.utils.mocks import AsyncMock, Mock, call, patch

from faust import Event, Record, Stream, Topic, joins
from faust.exceptions import PartitionsMismatch
from faust.stores.base import Store
from faust.tables.base import Collection
from faust.types import TP
from faust.windows import Window

TP1 = TP("foo", 0)


class User(Record):
    id: str
    name: str


class MyTable(Collection):
    def __post_init__(self, *args, **kwargs):
        self.datas = {}

    def _has_key(self, key):
        return key in self.datas

    def _get_key(self, key):
Example #8
0
 async def test_on_partitions_assigned__isolated(self, *, isolated_agent):
     assigned = {TP('foo', 0)}
     i = isolated_agent.on_isolated_partitions_assigned = AsyncMock()
     await isolated_agent.on_partitions_assigned(assigned)
     i.assert_called_once_with(assigned)
Example #9
0
 async def test_on_isolated_partitions_assigned(self, *, agent):
     agent._assign_isolated_partition = AsyncMock(name='aip')
     await agent.on_isolated_partitions_assigned({TP('foo', 0)})
     agent._assign_isolated_partition.assert_called_once_with(TP('foo', 0))
Example #10
0
 async def test_on_partitions_revoked__isolated(self, *, isolated_agent):
     revoked = {TP('foo', 0)}
     i = isolated_agent.on_isolated_partitions_revoked = AsyncMock(name='i')
     await isolated_agent.on_partitions_revoked(revoked)
     i.assert_called_once_with(revoked)
Example #11
0
 async def test_on_partitions_assigned(self, *, agent):
     assigned = {TP('foo', 0)}
     agent.on_shared_partitions_assigned = AsyncMock(name='ospr')
     await agent.on_partitions_assigned(assigned)
     agent.on_shared_partitions_assigned.assert_called_once_with(assigned)
Example #12
0
 def assignment(self) -> Set[TP]:
     return {
         TP(t, 0)
         for t in cast(Transport, self.transport)._subscription
     }
Example #13
0
 async def test_need_active_standby_for(self, *, store):
     assert await store.need_active_standby_for(TP("foo", 0))
Example #14
0
 def test_set_persisted_offset(self, *, store):
     store.set_persisted_offset(TP("foo", 0), 30303)
Example #15
0
 async def test_on_isolated_partition_assigned(self, *, actor):
     await actor.on_isolated_partition_assigned(TP('foo', 0))
Example #16
0
 async def test_start_isolated(self, *, agent):
     agent._start_for_partitions = AsyncMock(
         name='agent._start_for_partitions', )
     ret = await agent._start_isolated(TP('foo', 0))
     agent._start_for_partitions.assert_called_once_with({TP('foo', 0)})
     assert ret is agent._start_for_partitions.coro()
Example #17
0
import asyncio
import pytest
from faust import App, Channel, Topic
from faust.transport.consumer import Consumer
from faust.transport.conductor import Conductor
from faust.types import Message, TP
from mode import label, shortlabel
from mode.utils.futures import done_future
from mode.utils.mocks import AsyncMock, Mock

TP1 = TP('foo', 0)
TP2 = TP('foo', 1)


class test_Conductor:
    @pytest.fixture
    def con(self, *, app):
        return Conductor(app)

    def test_constructor(self, *, con):
        assert con._topics == set()
        assert con._topic_name_index == {}
        assert con._tp_index == {}
        assert con._tp_to_callback == {}
        assert con._acking_topics == set()
        assert con._subscription_changed is None
        assert con._subscription_done is None
        assert con._compiler
        assert con.on_message

    @pytest.mark.asyncio
Example #18
0
 def test_stream__active_partitions(self, *, agent):
     assert agent.stream(active_partitions={TP('foo', 0)})
Example #19
0
import pytest
from faust.exceptions import ImproperlyConfigured
from faust.sensors.datadog import DatadogMonitor, DatadogStatsClient
from faust.types import TP
from mode.utils.mocks import ANY, Mock, call

TP1 = TP('foo', 3)


@pytest.fixture()
def dogstatsd(*, monkeypatch):
    dogstatsd = Mock(name='dogstatsd')
    monkeypatch.setattr('faust.sensors.datadog.DogStatsd', dogstatsd)
    return dogstatsd


@pytest.fixture()
def statsd(*, monkeypatch):
    statsd = Mock(name='datadog')
    monkeypatch.setattr('faust.sensors.datadog.datadog', statsd)
    return statsd


class test_DatadogStatsClient:

    @pytest.fixture()
    def client(self, *, dogstatsd, statsd):
        return DatadogStatsClient()

    def test_incr(self, *, client):
        client.incr('metric', count=3)
Example #20
0
 def assignment(self) -> Set[TP]:
     """Return the current assignment."""
     return {
         TP(t, 0)
         for t in cast(Transport, self.transport)._subscription
     }
Example #21
0
 async def on_message(message: Message) -> None:
     tp = TP(topic=message.topic, partition=0)
     return await get_callback_for_tp(tp)(message)
Example #22
0
 def _new_topicpartition(self, topic: str, partition: int) -> TP:
     return TP(topic, partition)
Example #23
0
 def _leader_tp(self) -> TP:
     return TP(self._leader_topic_name, 0)
Example #24
0
import pytest
from faust import Event, Stream, Table, Topic
from faust.assignor import PartitionAssignor
from faust.sensors import Sensor
from faust.transport.consumer import Consumer
from faust.transport.producer import Producer
from faust.types import Message, TP
from mode.utils.mocks import Mock

TP1 = TP('foo', 0)


@pytest.fixture
def message():
    return Mock(name='message', autospec=Message)


@pytest.fixture
def stream():
    return Mock(name='stream', autospec=Stream)


@pytest.fixture
def event():
    return Mock(name='event', autospec=Event)


@pytest.fixture
def topic():
    return Mock(name='topic', autospec=Topic)
Example #25
0
from faust.app._attached import Attachments
from faust.exceptions import AlreadyConfiguredWarning
from faust.tables.manager import TableManager
from faust.transport.base import Producer, Transport
from faust.transport.conductor import Conductor
from faust.transport.consumer import (
    Consumer,
    ConsumerThread,
    Fetcher,
    ProducerSendError,
    ThreadDelegateConsumer,
    TransactionManager,
)
from faust.types import TP, Message

TP1 = TP("foo", 0)
TP2 = TP("foo", 1)
TP3 = TP("bar", 3)


class TestFetcher:
    @pytest.fixture
    def consumer(self):
        return Mock(
            name="consumer",
            autospec=Consumer,
            _drain_messages=AsyncMock(),
        )

    @pytest.fixture
    def fetcher(self, *, app, consumer):
Example #26
0
 def test_track_tp_end_offsets(self, *, mon):
     mon.track_tp_end_offset(TP('foo', 0), 4004)
     mon.client.gauge.assert_called_once_with(
         'end_offset.foo.0',
         4004,
     )
Example #27
0
 def _new_topicpartition(self, topic, partition) -> TP:
     return TP(topic, partition)
Example #28
0
 async def test_on_isolated_partition_revoked(self, *, actor):
     actor.cancel = Mock(name='cancel')
     actor.stop = AsyncMock(name='stop')
     await actor.on_isolated_partition_revoked(TP('foo', 0))
     actor.cancel.assert_called_once_with()
     actor.stop.assert_called_once_with()
Example #29
0
    async def test_on_partitions_revoked(self, *, consumer):
        consumer._on_partitions_revoked = AsyncMock(name="opr")
        tps = {TP("foo", 0), TP("bar", 2)}
        await consumer.on_partitions_revoked(tps)

        consumer._on_partitions_revoked.assert_called_once_with(tps)
Example #30
0
 async def test_on_client_only_start(self, *, con, app):
     topic = app.topic('foo', 'bar')
     con.add(topic)
     await con.on_client_only_start()
     assert con._tp_index[TP(topic='foo', partition=0)] == {topic}
     assert con._tp_index[TP(topic='bar', partition=0)] == {topic}