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
130 changes: 43 additions & 87 deletions ldclient/testing/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import pytest

from ldclient.async_client import AsyncLDClient
from ldclient.async_config import AsyncConfig
from ldclient.async_config import AsyncConfig, AsyncDataSystemConfig
from ldclient.context import Context
from ldclient.integrations.test_datav2 import TestDataV2
from ldclient.interfaces import DataStoreMode
from ldclient.testing.mock_async_components import (
MockAsyncEventProcessor,
MockAsyncFeatureStore,
MockAsyncUpdateProcessor
MockAsyncFeatureStore
)
from ldclient.versioned_data_kind import FEATURES

Expand All @@ -24,6 +25,26 @@ def _offline_config(**kwargs):
return AsyncConfig("test-sdk-key", offline=True, **kwargs)


def _test_data_config(td=None, **kwargs):
"""Return an AsyncConfig whose FDv2 data system syncs from TestDataV2."""
if td is None:
td = TestDataV2.data_source()
return AsyncConfig(
"test-sdk-key",
datasystem_config=AsyncDataSystemConfig(synchronizers=[td.async_builder]),
**kwargs,
)


def _daemon_config(store, **kwargs):
"""Return an AsyncConfig whose FDv2 data system reads only from the given store."""
return AsyncConfig(
"test-sdk-key",
datasystem_config=AsyncDataSystemConfig(data_store=store, data_store_mode=DataStoreMode.READ_ONLY),
**kwargs,
)


def _make_flag(key: str, value, version: int = 1) -> dict:
"""Build a minimal feature flag dict usable with AsyncInMemoryFeatureStore."""
return {
Expand Down Expand Up @@ -54,27 +75,17 @@ async def test_default_variation_returns_default_when_not_started():

@pytest.mark.asyncio
async def test_variation_returns_flag_value_when_initialized():
"""After start(), variation() returns the stored flag value.
"""After start(), variation() returns the flag value from the data source.

We use update_processor_class=MockAsyncUpdateProcessor so that the client
considers itself initialized (store.initialized becomes True once the
NullUpdateProcessor fires ready) without any network connection.
TestDataV2 supplies the flag without any network connection, so the
client initializes as soon as the synchronizer delivers its basis.
"""
store = MockAsyncFeatureStore()
flag = _make_flag('my-flag', 'hello')
await store.force_set(FEATURES, flag)
# Pre-initialize the store so is_initialized() returns True
store._initialized = True
td = TestDataV2.data_source()
td.update(td.flag('my-flag').value_for_all('hello'))

# Use MockAsyncUpdateProcessor which sets ready immediately
config = AsyncConfig(
"test-sdk-key",
feature_store=store,
update_processor_class=MockAsyncUpdateProcessor,
send_events=False,
)
client = AsyncLDClient(config)
client = AsyncLDClient(_test_data_config(td, send_events=False))
await client.start(start_wait=1.0)
assert await client.is_initialized()

context = Context.create('user-1')
result = await client.variation('my-flag', context, 'default')
Expand Down Expand Up @@ -117,12 +128,7 @@ async def test_context_manager():
@pytest.mark.asyncio
async def test_flush_delegates_to_event_processor():
"""flush() calls flush() on the underlying event processor."""
config = AsyncConfig(
"test-sdk-key",
update_processor_class=MockAsyncUpdateProcessor,
send_events=False,
)
client = AsyncLDClient(config)
client = AsyncLDClient(_test_data_config(send_events=False))
await client.start()

# Replace the event processor with a mock that tracks flush calls
Expand Down Expand Up @@ -243,18 +249,10 @@ async def test_flag_tracker_available_before_start():
@pytest.mark.asyncio
async def test_variation_detail_returns_reason():
"""variation_detail() returns an EvaluationDetail with a non-None reason."""
store = MockAsyncFeatureStore()
flag = _make_flag('detail-flag', 'hello')
await store.force_set(FEATURES, flag)
store._initialized = True
td = TestDataV2.data_source()
td.update(td.flag('detail-flag').value_for_all('hello'))

config = AsyncConfig(
"test-sdk-key",
feature_store=store,
update_processor_class=MockAsyncUpdateProcessor,
send_events=False,
)
async with AsyncLDClient(config) as client:
async with AsyncLDClient(_test_data_config(td, send_events=False)) as client:
context = Context.create('user-1')
detail = await client.variation_detail('detail-flag', context, 'fallback')

Expand All @@ -266,14 +264,8 @@ async def test_variation_detail_returns_reason():
@pytest.mark.asyncio
async def test_track_sends_event():
"""track() sends a custom event to the event processor."""
store = MockAsyncFeatureStore()
store._initialized = True

mock_ep = MockAsyncEventProcessor()
config = AsyncConfig(
"test-sdk-key",
feature_store=store,
update_processor_class=MockAsyncUpdateProcessor,
config = _test_data_config(
event_processor_class=lambda _cfg: mock_ep,
send_events=True,
)
Expand All @@ -292,16 +284,7 @@ async def test_track_sends_event():
@pytest.mark.asyncio
async def test_data_source_status_provider_accessible():
"""data_source_status_provider is not None after start()."""
store = MockAsyncFeatureStore()
store._initialized = True

config = AsyncConfig(
"test-sdk-key",
feature_store=store,
update_processor_class=MockAsyncUpdateProcessor,
send_events=False,
)
async with AsyncLDClient(config) as client:
async with AsyncLDClient(_test_data_config(send_events=False)) as client:
assert client.data_source_status_provider is not None


Expand All @@ -311,12 +294,7 @@ async def test_is_offline_reflects_config():
async with AsyncLDClient(_offline_config()) as client:
assert client.is_offline() is True

config = AsyncConfig(
"test-sdk-key",
update_processor_class=MockAsyncUpdateProcessor,
send_events=False,
)
async with AsyncLDClient(config) as client:
async with AsyncLDClient(_test_data_config(send_events=False)) as client:
assert client.is_offline() is False


Expand Down Expand Up @@ -438,13 +416,7 @@ async def test_all_flags_state_returns_flag_values():
'flag-b': _make_flag('flag-b', 'value-b'),
}})

config = AsyncConfig(
"test-sdk-key",
feature_store=store,
update_processor_class=MockAsyncUpdateProcessor,
send_events=False,
)
async with AsyncLDClient(config) as client:
async with AsyncLDClient(_daemon_config(store, send_events=False)) as client:
context = Context.create('user-1')
state = await client.all_flags_state(context)

Expand All @@ -469,13 +441,7 @@ async def test_all_flags_state_degrades_gracefully_when_evaluator_raises():
'flag-bad-last': _make_flag('flag-bad-last', 'y'),
}})

config = AsyncConfig(
"test-sdk-key",
feature_store=store,
update_processor_class=MockAsyncUpdateProcessor,
send_events=False,
)
async with AsyncLDClient(config) as client:
async with AsyncLDClient(_daemon_config(store, send_events=False)) as client:
async def fake_evaluate(flag, context, event_factory):
if flag['key'].startswith('flag-bad'):
raise RuntimeError("boom")
Expand Down Expand Up @@ -503,25 +469,15 @@ async def fake_evaluate(flag, context, event_factory):
assert 'prerequisites' not in flags_state['flag-bad-first']


class UninitializedAsyncUpdateProcessor(MockAsyncUpdateProcessor):
def initialized(self) -> bool:
return False


@pytest.mark.asyncio
async def test_cached_data_warnings_are_logged_once_per_client(caplog):
caplog.set_level(logging.WARNING, logger='ldclient.util')
store = MockAsyncFeatureStore()
await store.force_set(FEATURES, _make_flag('my-flag', 'hello'))
store._initialized = True
config = AsyncConfig(
"test-sdk-key",
feature_store=store,
update_processor_class=UninitializedAsyncUpdateProcessor,
send_events=False,
)
client = AsyncLDClient(config)
# Daemon mode reports cached data, so every evaluation reads last known values.
client = AsyncLDClient(_daemon_config(store, send_events=False))
await client.start(start_wait=1.0)
assert await client.is_initialized()
context = Context.create('user-1')

assert await client.variation('my-flag', context, 'default') == 'hello'
Expand Down
122 changes: 122 additions & 0 deletions ldclient/testing/test_async_ldclient_end_to_end_fdv2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"""
End-to-end tests of the async client on the FDv2 data system, run against a
mock HTTP server.
"""

import time

import pytest

from ldclient.async_client import AsyncLDClient
from ldclient.async_config import AsyncConfig, AsyncDataSystemConfig
from ldclient.client import Context
from ldclient.impl.datasourcev2.async_polling import (
AsyncPollingDataSourceBuilder
)
from ldclient.impl.datasourcev2.async_streaming import (
AsyncStreamingDataSourceBuilder
)
from ldclient.testing.http_util import (
BasicResponse,
CauseNetworkError,
SequentialHandler,
start_server
)
from ldclient.testing.test_ldclient_end_to_end_fdv2 import (
always_true_flag,
poll_response,
sdk_key,
stream_response
)

user = Context.from_dict({'key': 'userkey', 'kind': 'user'})


def make_config(uri, synchronizer):
return AsyncConfig(
sdk_key,
base_uri=uri,
stream_uri=uri,
send_events=False,
datasystem_config=AsyncDataSystemConfig(synchronizers=[synchronizer]),
)


@pytest.mark.asyncio
async def test_async_client_starts_in_streaming_mode():
with start_server() as server:
with stream_response([always_true_flag]) as stream:
server.for_path('/sdk/stream', stream)

async with AsyncLDClient(make_config(server.uri, AsyncStreamingDataSourceBuilder())) as client:
assert await client.is_initialized()
assert await client.variation(always_true_flag['key'], user, False) is True

r = server.await_request()
assert r.path == '/sdk/stream'
assert r.headers['Authorization'] == sdk_key


@pytest.mark.asyncio
async def test_async_client_starts_in_polling_mode():
with start_server() as server:
server.for_path('/sdk/poll', poll_response([always_true_flag]))

async with AsyncLDClient(make_config(server.uri, AsyncPollingDataSourceBuilder())) as client:
assert await client.is_initialized()
assert await client.variation(always_true_flag['key'], user, False) is True

r = server.await_request()
assert r.path == '/sdk/poll'
assert r.headers['Authorization'] == sdk_key


@pytest.mark.asyncio
async def test_async_client_fails_fast_in_streaming_mode_with_401_error():
with start_server() as server:
server.for_path('/sdk/stream', BasicResponse(401))

start_wait = 10
client = AsyncLDClient(make_config(server.uri, AsyncStreamingDataSourceBuilder()))
try:
started = time.monotonic()
await client.start(start_wait=start_wait)
# A 401 turns the data source off, which releases start() early.
assert time.monotonic() - started < start_wait / 2
assert not await client.is_initialized()
assert await client.variation(always_true_flag['key'], user, False) is False
finally:
await client.close()


@pytest.mark.asyncio
async def test_async_client_fails_fast_in_polling_mode_with_401_error():
with start_server() as server:
server.for_path('/sdk/poll', BasicResponse(401))

start_wait = 10
client = AsyncLDClient(make_config(server.uri, AsyncPollingDataSourceBuilder()))
try:
started = time.monotonic()
await client.start(start_wait=start_wait)
assert time.monotonic() - started < start_wait / 2
assert not await client.is_initialized()
assert await client.variation(always_true_flag['key'], user, False) is False
finally:
await client.close()


@pytest.mark.xfail(
reason='AsyncPollingDataSource.sync lets a requester exception escape, so a network error removes the synchronizer',
strict=True,
)
@pytest.mark.asyncio
async def test_async_client_retries_poll_after_network_error():
with start_server() as server:
server.for_path('/sdk/poll', SequentialHandler(
CauseNetworkError(), CauseNetworkError(), CauseNetworkError(), poll_response([always_true_flag])))
builder = AsyncPollingDataSourceBuilder().poll_interval(0.05)

async with AsyncLDClient(make_config(server.uri, builder)) as client:
assert await client.is_initialized()
assert await client.variation(always_true_flag['key'], user, False) is True
Loading
Loading