Skip to content

Commit f20fffe

Browse files
authored
chore: Remove dead code, clarify names, other cleanup (#398)
1 parent c73ad14 commit f20fffe

File tree

10 files changed

+54
-24
lines changed

10 files changed

+54
-24
lines changed

ldclient/impl/datasource/feature_requester.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from ldclient.interfaces import FeatureRequester
1414
from ldclient.versioned_data_kind import FEATURES, SEGMENTS
1515

16-
LATEST_ALL_URI = '/sdk/latest-all'
16+
FDV1_POLLING_ENDPOINT = '/sdk/latest-all'
1717

1818

1919
CacheEntry = namedtuple('CacheEntry', ['data', 'etag'])
@@ -24,7 +24,7 @@ def __init__(self, config):
2424
self._cache = dict()
2525
self._http = _http_factory(config).create_pool_manager(1, config.base_uri)
2626
self._config = config
27-
self._poll_uri = config.base_uri + LATEST_ALL_URI
27+
self._poll_uri = config.base_uri + FDV1_POLLING_ENDPOINT
2828
if config.payload_filter_key is not None:
2929
self._poll_uri += '?%s' % parse.urlencode({'filter': config.payload_filter_key})
3030

ldclient/impl/datasourcev2/polling.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
import urllib3
1515

1616
from ldclient.config import Config, DataSourceBuilder, HTTPConfig
17-
from ldclient.impl.datasource.feature_requester import LATEST_ALL_URI
17+
from ldclient.impl.datasource.feature_requester import FDV1_POLLING_ENDPOINT
1818
from ldclient.impl.datasystem.protocolv2 import (
1919
DeleteObject,
2020
EventName,
2121
PutObject
2222
)
2323
from ldclient.impl.http import HTTPFactory, _base_headers
24-
from ldclient.impl.repeating_task import RepeatingTask
2524
from ldclient.impl.util import (
2625
_LD_ENVID_HEADER,
2726
_LD_FD_FALLBACK_HEADER,
@@ -52,7 +51,7 @@
5251
Update
5352
)
5453

55-
POLLING_ENDPOINT = "/sdk/poll"
54+
FDV2_POLLING_ENDPOINT = "/sdk/poll"
5655

5756

5857
PollingResult = _Result[Tuple[ChangeSet, Mapping], str]
@@ -95,9 +94,6 @@ def __init__(
9594
self._poll_interval = poll_interval
9695
self._interrupt_event = Event()
9796
self._stop = Event()
98-
self._task = RepeatingTask(
99-
"ldclient.datasource.polling", poll_interval, 0, self._poll
100-
)
10197

10298
@property
10399
def name(self) -> str:
@@ -193,7 +189,6 @@ def stop(self):
193189
"""Stops the synchronizer."""
194190
log.info("Stopping PollingDataSourceV2 synchronizer")
195191
self._interrupt_event.set()
196-
self._task.stop()
197192
self._stop.set()
198193

199194
def _poll(self, ss: SelectorStore) -> BasisResult:
@@ -226,7 +221,7 @@ def _poll(self, ss: SelectorStore) -> BasisResult:
226221

227222
basis = Basis(
228223
change_set=change_set,
229-
persist=change_set.selector is not None,
224+
persist=change_set.selector is not None and change_set.selector.is_defined(),
230225
environment_id=env_id,
231226
)
232227

@@ -251,7 +246,7 @@ def __init__(self, config: Config, base_uri: str, http_options: HTTPConfig):
251246
self._http = factory.create_pool_manager(1, base_uri)
252247
self._http_options = http_options
253248
self._config = config
254-
self._poll_uri = base_uri + POLLING_ENDPOINT
249+
self._poll_uri = base_uri + FDV2_POLLING_ENDPOINT
255250

256251
def fetch(self, selector: Optional[Selector]) -> PollingResult:
257252
"""
@@ -482,7 +477,7 @@ def __init__(self, config: Config, base_uri: str, http_options: HTTPConfig):
482477
)
483478
self._http_options = http_options
484479
self._config = config
485-
self._poll_uri = base_uri + LATEST_ALL_URI
480+
self._poll_uri = base_uri + FDV1_POLLING_ENDPOINT
486481

487482
def fetch(self, selector: Optional[Selector]) -> PollingResult:
488483
"""

ldclient/impl/datasourcev2/streaming.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ def _handle_error(self, error: Exception, envid: Optional[str]) -> Tuple[Optiona
380380
revert_to_fdv1=True,
381381
environment_id=envid,
382382
)
383+
self.stop()
383384
return (update, False)
384385

385386
http_error_message_result = http_error_message(

ldclient/impl/datasystem/store.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ def apply_delta(self, collections: Collections) -> bool:
111111
with self._lock.write():
112112
for kind, kind_data in all_decoded.items():
113113
items_of_kind = self._items[kind]
114-
kind_data = all_decoded[kind]
115114
for key, item in kind_data.items():
116115
items_of_kind[key] = item
117116
log.debug(

ldclient/integrations/test_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def __init__(self, key: str):
153153
# consider it part of the public API, but it is still called from TestData.
154154
def _copy(self) -> 'FlagBuilder':
155155
"""Creates a deep copy of the flag builder. Subsequent updates to the
156-
original ``FlagBuilder`` object will not update the copy and vise versa.
156+
original ``FlagBuilder`` object will not update the copy and vice versa.
157157
158158
:return: a copy of the flag builder object
159159
"""

ldclient/integrations/test_datav2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def __init__(self, key: str):
182182
def _copy(self) -> FlagBuilderV2:
183183
"""
184184
Creates a deep copy of the flag builder. Subsequent updates to the
185-
original ``FlagBuilderV2`` object will not update the copy and vise versa.
185+
original ``FlagBuilderV2`` object will not update the copy and vice versa.
186186
187187
:return: a copy of the flag builder object
188188
"""

ldclient/interfaces.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ def no_changes() -> "ChangeSet":
13931393
require changes.
13941394
"""
13951395
return ChangeSet(
1396-
intent_code=IntentCode.TRANSFER_NONE, selector=None, changes=[]
1396+
intent_code=IntentCode.TRANSFER_NONE, selector=Selector.no_selector(), changes=[]
13971397
)
13981398

13991399
@staticmethod

ldclient/testing/impl/datasourcev2/test_polling_initializer.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
PollingDataSource,
88
PollingResult,
99
Selector,
10+
fdv1_polling_payload_to_changeset,
1011
polling_payload_to_changeset
1112
)
1213
from ldclient.impl.util import UnsuccessfulResponseException, _Fail, _Success
@@ -138,3 +139,33 @@ def test_handles_transfer_changes():
138139
assert result.value.change_set.intent_code == IntentCode.TRANSFER_CHANGES
139140
assert len(result.value.change_set.changes) == 1
140141
assert result.value.persist is True
142+
143+
144+
def test_handles_fdv1_payload():
145+
"""Test that FDv1 payloads have persist set to False."""
146+
fdv1_data = {
147+
"flags": {
148+
"test-flag": {
149+
"key": "test-flag",
150+
"version": 1,
151+
"on": True,
152+
"variations": [True, False]
153+
}
154+
},
155+
"segments": {}
156+
}
157+
change_set_result = fdv1_polling_payload_to_changeset(fdv1_data)
158+
assert isinstance(change_set_result, _Success)
159+
160+
mock_requester = MockPollingRequester(_Success(value=(change_set_result.value, {})))
161+
ds = PollingDataSource(poll_interval=1.0, requester=mock_requester)
162+
163+
result = ds.fetch(MockSelectorStore(Selector.no_selector()))
164+
165+
assert isinstance(result, _Success)
166+
assert result.value is not None
167+
168+
assert result.value.change_set.intent_code == IntentCode.TRANSFER_FULL
169+
assert len(result.value.change_set.changes) == 1
170+
# FDv1 payloads should not be persisted because they have no selector
171+
assert result.value.persist is False

ldclient/testing/impl/datasourcev2/test_polling_payload_parsing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def test_transfer_none():
4646
change_set = result.value
4747
assert change_set.intent_code == IntentCode.TRANSFER_NONE
4848
assert len(change_set.changes) == 0
49-
assert change_set.selector is None
49+
assert change_set.selector is not None
50+
assert not change_set.selector.is_defined()
5051

5152

5253
def test_transfer_full_with_empty_payload():

ldclient/testing/impl/datasystem/test_fdv2_persistence.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ def test_persistent_store_delete_operations():
370370
ChangeSet,
371371
ChangeType,
372372
IntentCode,
373-
ObjectKind
373+
ObjectKind,
374+
Selector
374375
)
375376

376377
# Pre-populate with a flag
@@ -410,7 +411,7 @@ def test_persistent_store_delete_operations():
410411
},
411412
)
412413
],
413-
selector=None,
414+
selector=Selector.no_selector(),
414415
)
415416
store.apply(init_changeset, True)
416417

@@ -428,7 +429,7 @@ def test_persistent_store_delete_operations():
428429
object=None,
429430
)
430431
],
431-
selector=None,
432+
selector=Selector.no_selector(),
432433
)
433434

434435
store.apply(delete_changeset, True)
@@ -671,7 +672,8 @@ def test_persistent_store_commit_encodes_data_correctly():
671672
ChangeSet,
672673
ChangeType,
673674
IntentCode,
674-
ObjectKind
675+
ObjectKind,
676+
Selector
675677
)
676678

677679
persistent_store = StubFeatureStore()
@@ -699,7 +701,7 @@ def test_persistent_store_commit_encodes_data_correctly():
699701
object=flag_data,
700702
)
701703
],
702-
selector=None,
704+
selector=Selector.no_selector(),
703705
)
704706
store.apply(changeset, True)
705707

@@ -746,7 +748,8 @@ def test_persistent_store_commit_handles_errors():
746748
ChangeSet,
747749
ChangeType,
748750
IntentCode,
749-
ObjectKind
751+
ObjectKind,
752+
Selector
750753
)
751754

752755
class FailingFeatureStore(StubFeatureStore):
@@ -770,7 +773,7 @@ def init(self, all_data):
770773
object={"key": "test-flag", "version": 1, "on": True},
771774
)
772775
],
773-
selector=None,
776+
selector=Selector.no_selector(),
774777
)
775778
store.apply(changeset, True)
776779

0 commit comments

Comments
 (0)