Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions ldclient/impl/datasourcev2/async_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ async def sync(self, ss: SelectorStore) -> AsyncGenerator[Update, None]:
occurs.
"""
log.info("Starting PollingDataSourceV2 synchronizer")
self._interrupt_event.clear()
self._stop.clear()
# The flags are not cleared here. Stop can occur before sync starts, and sync then ends at once.
try:
while self._stop.is_set() is False:
result = await self._fetch(ss)
Expand Down
3 changes: 1 addition & 2 deletions ldclient/impl/datasourcev2/polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ def sync(self, ss: SelectorStore) -> Generator[Update, None, None]:
occurs.
"""
log.info("Starting PollingDataSourceV2 synchronizer")
self._interrupt_event.clear()
self._stop.clear()
# The flags are not cleared here. Stop can occur before sync starts, and sync then ends at once.
while self._stop.is_set() is False:
result = self._fetch(ss)
decision = map_polling_result(result)
Expand Down
9 changes: 9 additions & 0 deletions ldclient/impl/datasystem/async_fdv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ async def stop(self):
# Wait for the coordinator's background work to complete
await self._runner.stop_all(timeout=5.0)

# The data source stops here, whichever synchronizer was active.
self._data_source_status_provider.update_status(
DataSourceState.OFF, self._data_source_status_provider.status.error
)

# Close the store
await self._store.close()

Expand Down Expand Up @@ -549,6 +554,10 @@ async def _synchronizer_loop(self, set_on_ready: AsyncEvent):
synchronizer, set_on_ready, current_index != 0
)

# Stop ends the synchronizer. That is not a failure, so the directive is ignored.
if self._stop_event.is_set():
break

if directive == ConditionDirective.FDV1:
# Abandon all synchronizers and use only fdv1 fallback
log.warning("Falling back to FDv1 protocol")
Expand Down
9 changes: 9 additions & 0 deletions ldclient/impl/datasystem/fdv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ def stop(self):
if thread.is_alive():
log.warning("Thread %s did not terminate in time", thread.name)

# The data source stops here, whichever synchronizer was active.
self._data_source_status_provider.update_status(
DataSourceState.OFF, self._data_source_status_provider.status.error
)

# Close the store
self._store.close()

Expand Down Expand Up @@ -515,6 +520,10 @@ def synchronizer_loop(self: 'FDv2'):
synchronizer, set_on_ready, current_index != 0
)

# Stop ends the synchronizer. That is not a failure, so the directive is ignored.
if self._stop_event.is_set():
break

if directive == ConditionDirective.FDV1:
# Abandon all synchronizers and use only fdv1 fallback
log.warning("Falling back to FDv1 protocol")
Expand Down
14 changes: 14 additions & 0 deletions ldclient/testing/impl/datasourcev2/test_async_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,17 @@ async def close(self) -> None:
assert "connection refused" in interrupted.error.message
assert interrupted.fallback_to_fdv1 is False
assert valid.state == DataSourceState.VALID


@pytest.mark.asyncio
async def test_sync_does_not_poll_when_stopped_before_it_starts():
requester = MockPollingRequester([_Success(value=(_valid_change_set(), {}))])
src = AsyncPollingDataSource(poll_interval=0.01, requester=requester)

await src.stop()

async def collect():
return [update async for update in src.sync(_ss())]

assert await asyncio.wait_for(collect(), timeout=1) == []
assert requester.call_times == []
10 changes: 10 additions & 0 deletions ldclient/testing/impl/datasourcev2/test_polling_synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,13 @@ def test_requester_exception_interrupts_and_recovers():
assert "connection refused" in interrupted.error.message
assert interrupted.fallback_to_fdv1 is False
assert valid.state == DataSourceState.VALID


def test_sync_does_not_poll_when_stopped_before_it_starts():
requester = ListBasedRequester(results=iter([]))
synchronizer = PollingDataSource(poll_interval=0.01, requester=requester)

synchronizer.stop()

# The requester has no results, so a poll would yield an INTERRUPTED update.
assert next(synchronizer.sync(MockSelectorStore(Selector.no_selector())), None) is None
19 changes: 19 additions & 0 deletions ldclient/testing/impl/datasystem/test_async_fdv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ async def test_async_fdv2_basic_start_stop():
await fdv2.stop()


@pytest.mark.asyncio
@pytest.mark.parametrize("synchronizer_count", [1, 2])
async def test_async_fdv2_stop_reports_off_without_a_synchronizer_failure_warning(caplog, synchronizer_count):
td = TestDataV2.data_source()
fdv2 = AsyncFDv2(
AsyncConfig(sdk_key="dummy"),
DataSystemConfig(initializers=None, synchronizers=[td.async_builder] * synchronizer_count),
)
ready_event = asyncio.Event()
fdv2.start(ready_event)
await asyncio.wait_for(ready_event.wait(), timeout=2)

await fdv2.stop()

assert fdv2.data_source_status_provider.status.state == DataSourceState.OFF
# Stop ends the synchronizer. That is not a failure, so it is not reported.
assert not any(record.levelno >= logging.WARNING for record in caplog.records)


@pytest.mark.asyncio
async def test_async_fdv2_synchronizer_receives_updates():
td = TestDataV2.data_source()
Expand Down
19 changes: 19 additions & 0 deletions ldclient/testing/impl/datasystem/test_fdv2_datasystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from threading import Event
from typing import List

import pytest
from mock import Mock

from ldclient.config import (
Expand Down Expand Up @@ -153,6 +154,24 @@ def listener(flag_change: FlagChange):
assert len(changes) == 0


@pytest.mark.parametrize("synchronizer_count", [1, 2])
def test_stop_reports_off_without_a_synchronizer_failure_warning(caplog, synchronizer_count):
td = TestDataV2.data_source()
fdv2 = FDv2(
Config(sdk_key="dummy"),
DataSystemConfig(initializers=None, synchronizers=[td.builder] * synchronizer_count),
)
set_on_ready = Event()
fdv2.start(set_on_ready)
assert set_on_ready.wait(1), "Data system did not become ready in time"

fdv2.stop()

assert fdv2.data_source_status_provider.status.state == DataSourceState.OFF
# Stop ends the synchronizer. That is not a failure, so it is not reported.
assert not any(record.levelno >= logging.WARNING for record in caplog.records)


def test_fdv2_data_availability_is_refreshed_with_data():
td = TestDataV2.data_source()
data_system_config = DataSystemConfig(
Expand Down
Loading