Skip to content
Open
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
2 changes: 2 additions & 0 deletions ldclient/impl/lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def get(self, key):
'''

def put(self, key, value):
if self.capacity <= 0:
return False
found = key in self.cache
if found:
self.cache.move_to_end(key)
Expand Down
25 changes: 25 additions & 0 deletions ldclient/testing/impl/events/test_async_event_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,31 @@ async def test_two_events_for_same_context_only_produce_one_index_event():
assert output[3]['kind'] == 'summary'


@pytest.mark.parametrize(
"event,expected_kinds",
[
pytest.param(EventInputIdentify(timestamp, context), ['identify', 'identify'], id='identify'),
pytest.param(EventInputCustom(timestamp, context, 'eventkey', {'thing': 'stuff'}, 1.5), ['index', 'custom', 'index', 'custom'], id='custom'),
pytest.param(
EventInputEvaluation(timestamp, context, flag.key, flag, 1, 'value', None, 'default', None, True),
['index', 'feature', 'index', 'feature', 'summary'],
id='feature',
),
],
)
async def test_events_are_delivered_with_zero_context_keys_capacity(event, expected_kinds):
mock_http = MockAioHttp()
async with make_processor(mock_http, context_keys_capacity=0) as ep:
ep.send_event(event)
ep.send_event(event)

output = await flush_and_get_events(ep, mock_http)
assert [item['kind'] for item in output] == expected_kinds
for item in output:
if item['kind'] != 'summary':
assert item['context'] == context.to_dict()


async def test_nontracked_events_are_summarized():
mock_http = MockAioHttp()
async with make_processor(mock_http) as ep:
Expand Down
24 changes: 24 additions & 0 deletions ldclient/testing/impl/events/test_event_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,30 @@ def test_two_events_for_same_context_only_produce_one_index_event():
check_summary_event(output[3])


@pytest.mark.parametrize(
"event,expected_kinds",
[
pytest.param(EventInputIdentify(timestamp, context), ['identify', 'identify'], id='identify'),
pytest.param(EventInputCustom(timestamp, context, 'eventkey', {'thing': 'stuff'}, 1.5), ['index', 'custom', 'index', 'custom'], id='custom'),
pytest.param(
EventInputEvaluation(timestamp, context, flag.key, flag, 1, 'value', None, 'default', None, True),
['index', 'feature', 'index', 'feature', 'summary'],
id='feature',
),
],
)
def test_events_are_delivered_with_zero_context_keys_capacity(event, expected_kinds):
with DefaultTestProcessor(context_keys_capacity=0) as ep:
ep.send_event(event)
ep.send_event(event)

output = flush_and_get_events(ep)
assert [item['kind'] for item in output] == expected_kinds
for item in output:
if item['kind'] != 'summary':
assert item['context'] == context.to_dict()


def test_new_index_event_is_added_if_context_cache_has_been_cleared():
with DefaultTestProcessor(context_keys_flush_interval=0.1) as ep:
e0 = EventInputEvaluation(timestamp, context, flag.key, flag, 1, 'value1', None, 'default', None, True)
Expand Down
13 changes: 13 additions & 0 deletions ldclient/testing/impl/test_lru_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import pytest

from ldclient.impl.lru_cache import SimpleLRUCache


@pytest.mark.parametrize("capacity", [0, -1])
def test_non_positive_capacity_does_not_retain_values(capacity):
lru = SimpleLRUCache(capacity)
assert lru.put("a", True) is False
assert lru.get("a") is None
assert lru.put("a", True) is False
assert lru.put("b", True) is False
assert lru.get("a") is None
assert lru.get("b") is None


def test_retains_values_up_to_capacity():
lru = SimpleLRUCache(3)
assert lru.put("a", True) is False
Expand Down
Loading