-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathtest_file_data_sourcev2.py
More file actions
475 lines (380 loc) · 14.1 KB
/
test_file_data_sourcev2.py
File metadata and controls
475 lines (380 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import json
import os
import tempfile
import threading
import time
import pytest
from ldclient.config import Config
from ldclient.impl.datasystem.protocolv2 import (
IntentCode,
ObjectKind,
Selector
)
from ldclient.impl.util import _Fail, _Success
from ldclient.integrations import Files
from ldclient.interfaces import DataSourceState
from ldclient.testing.mock_components import MockSelectorStore
# Skip all tests in this module in CI due to flakiness
pytestmark = pytest.mark.skipif(
os.getenv('LD_SKIP_FLAKY_TESTS', '').lower() in ('true', '1', 'yes'),
reason="Skipping flaky test"
)
have_yaml = False
try:
import yaml
have_yaml = True
except ImportError:
pass
all_properties_json = '''
{
"flags": {
"flag1": {
"key": "flag1",
"on": true,
"fallthrough": {
"variation": 2
},
"variations": [ "fall", "off", "on" ]
}
},
"flagValues": {
"flag2": "value2"
},
"segments": {
"seg1": {
"key": "seg1",
"include": ["user1"]
}
}
}
'''
all_properties_yaml = '''
---
flags:
flag1:
key: flag1
"on": true
flagValues:
flag2: value2
segments:
seg1:
key: seg1
include: ["user1"]
'''
flag_only_json = '''
{
"flags": {
"flag1": {
"key": "flag1",
"on": true,
"fallthrough": {
"variation": 2
},
"variations": [ "fall", "off", "on" ]
}
}
}
'''
segment_only_json = '''
{
"segments": {
"seg1": {
"key": "seg1",
"include": ["user1"]
}
}
}
'''
flag_values_only_json = '''
{
"flagValues": {
"flag2": "value2"
}
}
'''
def make_temp_file(content):
"""Create a temporary file with the given content."""
f, path = tempfile.mkstemp()
os.write(f, content.encode("utf-8"))
os.close(f)
return path
def replace_file(path, content):
"""Replace the contents of a file."""
with open(path, 'w') as f:
f.write(content)
def test_creates_valid_initializer():
"""Test that FileDataSourceV2 creates a working initializer."""
path = make_temp_file(all_properties_json)
try:
file_source = Files.new_data_source_v2(paths=[path])
initializer = file_source(Config(sdk_key="dummy"))
result = initializer.fetch(MockSelectorStore(Selector.no_selector()))
assert isinstance(result, _Success)
basis = result.value
assert not basis.persist
assert basis.environment_id is None
assert basis.change_set.intent_code == IntentCode.TRANSFER_FULL
# Should have 2 flags and 1 segment
changes = basis.change_set.changes
assert len(changes) == 3
flag_changes = [c for c in changes if c.kind == ObjectKind.FLAG]
segment_changes = [c for c in changes if c.kind == ObjectKind.SEGMENT]
assert len(flag_changes) == 2
assert len(segment_changes) == 1
# Check selector is no_selector
assert basis.change_set.selector == Selector.no_selector()
finally:
os.remove(path)
def test_initializer_handles_missing_file():
"""Test that initializer returns error for missing file."""
file_source = Files.new_data_source_v2(paths=['no-such-file.json'])
initializer = file_source(Config(sdk_key="dummy"))
result = initializer.fetch(MockSelectorStore(Selector.no_selector()))
assert isinstance(result, _Fail)
assert "no-such-file.json" in result.error
def test_initializer_handles_invalid_json():
"""Test that initializer returns error for invalid JSON."""
path = make_temp_file('{"flagValues":{')
try:
file_source = Files.new_data_source_v2(paths=[path])
initializer = file_source(Config(sdk_key="dummy"))
result = initializer.fetch(MockSelectorStore(Selector.no_selector()))
assert isinstance(result, _Fail)
assert "Unable to load flag data" in result.error
finally:
os.remove(path)
def test_initializer_handles_duplicate_keys():
"""Test that initializer returns error when same key appears in multiple files."""
path1 = make_temp_file(flag_only_json)
path2 = make_temp_file(flag_only_json)
try:
file_source = Files.new_data_source_v2(paths=[path1, path2])
initializer = file_source(Config(sdk_key="dummy"))
result = initializer.fetch(MockSelectorStore(Selector.no_selector()))
assert isinstance(result, _Fail)
assert "was used more than once" in result.error
finally:
os.remove(path1)
os.remove(path2)
def test_initializer_loads_multiple_files():
"""Test that initializer can load from multiple files."""
path1 = make_temp_file(flag_only_json)
path2 = make_temp_file(segment_only_json)
try:
file_source = Files.new_data_source_v2(paths=[path1, path2])
initializer = file_source(Config(sdk_key="dummy"))
result = initializer.fetch(MockSelectorStore(Selector.no_selector()))
assert isinstance(result, _Success)
changes = result.value.change_set.changes
flag_changes = [c for c in changes if c.kind == ObjectKind.FLAG]
segment_changes = [c for c in changes if c.kind == ObjectKind.SEGMENT]
assert len(flag_changes) == 1
assert len(segment_changes) == 1
finally:
os.remove(path1)
os.remove(path2)
def test_initializer_loads_yaml():
"""Test that initializer can parse YAML files."""
if not have_yaml:
pytest.skip("skipping YAML test because pyyaml isn't available")
path = make_temp_file(all_properties_yaml)
try:
file_source = Files.new_data_source_v2(paths=[path])
initializer = file_source(Config(sdk_key="dummy"))
result = initializer.fetch(MockSelectorStore(Selector.no_selector()))
assert isinstance(result, _Success)
changes = result.value.change_set.changes
assert len(changes) == 3 # 2 flags + 1 segment
finally:
os.remove(path)
def test_initializer_handles_flag_values():
"""Test that initializer properly converts flagValues to flags."""
path = make_temp_file(flag_values_only_json)
try:
file_source = Files.new_data_source_v2(paths=[path])
initializer = file_source(Config(sdk_key="dummy"))
result = initializer.fetch(MockSelectorStore(Selector.no_selector()))
assert isinstance(result, _Success)
changes = result.value.change_set.changes
flag_changes = [c for c in changes if c.kind == ObjectKind.FLAG]
assert len(flag_changes) == 1
# Check the flag was created with the expected structure
flag_change = flag_changes[0]
assert flag_change.key == "flag2"
assert flag_change.object['key'] == "flag2"
assert flag_change.object['on'] is True
assert flag_change.object['variations'] == ["value2"]
finally:
os.remove(path)
def test_creates_valid_synchronizer():
"""Test that FileDataSourceV2 creates a working synchronizer."""
path = make_temp_file(all_properties_json)
try:
file_source = Files.new_data_source_v2(paths=[path], force_polling=True, poll_interval=0.1)
synchronizer = file_source(Config(sdk_key="dummy"))
updates = []
update_count = 0
def collect_updates():
nonlocal update_count
for update in synchronizer.sync(MockSelectorStore(Selector.no_selector())):
updates.append(update)
update_count += 1
if update_count == 1:
# Should get initial state
assert update.state == DataSourceState.VALID
assert update.change_set is not None
assert update.change_set.intent_code == IntentCode.TRANSFER_FULL
assert len(update.change_set.changes) == 3
synchronizer.stop()
break
# Start the synchronizer in a thread with timeout to prevent hanging
sync_thread = threading.Thread(target=collect_updates)
sync_thread.start()
# Wait for the thread to complete with timeout
sync_thread.join(timeout=5)
# Ensure thread completed successfully
if sync_thread.is_alive():
synchronizer.stop()
sync_thread.join()
pytest.fail("Synchronizer test timed out after 5 seconds")
assert len(updates) == 1
finally:
synchronizer.stop()
os.remove(path)
def test_synchronizer_detects_file_changes():
"""Test that synchronizer detects and reports file changes."""
path = make_temp_file(flag_only_json)
try:
file_source = Files.new_data_source_v2(paths=[path], force_polling=True, poll_interval=0.1)
synchronizer = file_source(Config(sdk_key="dummy"))
updates = []
update_event = threading.Event()
def collect_updates():
for update in synchronizer.sync(MockSelectorStore(Selector.no_selector())):
updates.append(update)
update_event.set()
if len(updates) >= 2:
break
# Start the synchronizer
sync_thread = threading.Thread(target=collect_updates)
sync_thread.start()
# Wait for initial update
assert update_event.wait(timeout=2), "Did not receive initial update"
assert len(updates) == 1
assert updates[0].state == DataSourceState.VALID
initial_changes = [c for c in updates[0].change_set.changes if c.kind == ObjectKind.FLAG]
assert len(initial_changes) == 1
# Modify the file
update_event.clear()
time.sleep(0.2) # Ensure filesystem timestamp changes
replace_file(path, segment_only_json)
# Wait for the change to be detected
assert update_event.wait(timeout=2), "Did not receive update after file change"
assert len(updates) == 2
assert updates[1].state == DataSourceState.VALID
segment_changes = [c for c in updates[1].change_set.changes if c.kind == ObjectKind.SEGMENT]
assert len(segment_changes) == 1
synchronizer.stop()
sync_thread.join(timeout=2)
finally:
synchronizer.stop()
os.remove(path)
def test_synchronizer_reports_error_on_invalid_file_update():
"""Test that synchronizer reports error when file becomes invalid."""
path = make_temp_file(flag_only_json)
try:
file_source = Files.new_data_source_v2(paths=[path], force_polling=True, poll_interval=0.1)
synchronizer = file_source(Config(sdk_key="dummy"))
updates = []
update_event = threading.Event()
def collect_updates():
for update in synchronizer.sync(MockSelectorStore(Selector.no_selector())):
updates.append(update)
update_event.set()
if len(updates) >= 2:
break
# Start the synchronizer
sync_thread = threading.Thread(target=collect_updates)
sync_thread.start()
# Wait for initial update
assert update_event.wait(timeout=2), "Did not receive initial update"
assert len(updates) == 1
assert updates[0].state == DataSourceState.VALID
# Make the file invalid
update_event.clear()
time.sleep(0.2) # Ensure filesystem timestamp changes
replace_file(path, '{"invalid json')
# Wait for the error to be detected
assert update_event.wait(timeout=2), "Did not receive update after file became invalid"
assert len(updates) == 2
assert updates[1].state == DataSourceState.INTERRUPTED
assert updates[1].error is not None
synchronizer.stop()
sync_thread.join(timeout=2)
finally:
synchronizer.stop()
os.remove(path)
def test_synchronizer_can_be_stopped():
"""Test that synchronizer stops cleanly."""
path = make_temp_file(all_properties_json)
try:
file_source = Files.new_data_source_v2(paths=[path])
synchronizer = file_source(Config(sdk_key="dummy"))
updates = []
def collect_updates():
for update in synchronizer.sync(MockSelectorStore(Selector.no_selector())):
updates.append(update)
# Start the synchronizer
sync_thread = threading.Thread(target=collect_updates)
sync_thread.start()
# Give it a moment to process initial data
time.sleep(0.2)
# Stop it
synchronizer.stop()
# Thread should complete
sync_thread.join(timeout=2)
assert not sync_thread.is_alive()
# Should have received at least the initial update
assert len(updates) >= 1
assert updates[0].state == DataSourceState.VALID
finally:
os.remove(path)
def test_fetch_after_stop_returns_error():
"""Test that fetch returns error after synchronizer is stopped."""
path = make_temp_file(all_properties_json)
try:
file_source = Files.new_data_source_v2(paths=[path])
initializer = file_source(Config(sdk_key="dummy"))
# First fetch should work
result = initializer.fetch(MockSelectorStore(Selector.no_selector()))
assert isinstance(result, _Success)
# Stop the source
initializer.stop()
# Second fetch should fail
result = initializer.fetch(MockSelectorStore(Selector.no_selector()))
assert isinstance(result, _Fail)
assert "closed" in result.error
finally:
os.remove(path)
def test_source_name_property():
"""Test that the data source has the correct name."""
path = make_temp_file(all_properties_json)
try:
file_source = Files.new_data_source_v2(paths=[path])
source = file_source(Config(sdk_key="dummy"))
assert source.name == "FileDataV2"
finally:
source.stop()
os.remove(path)
def test_accepts_single_path_string():
"""Test that paths parameter can be a single string."""
path = make_temp_file(flag_only_json)
try:
# Pass a single string instead of a list
file_source = Files.new_data_source_v2(paths=path)
initializer = file_source(Config(sdk_key="dummy"))
result = initializer.fetch(MockSelectorStore(Selector.no_selector()))
assert isinstance(result, _Success)
assert len(result.value.change_set.changes) == 1
finally:
os.remove(path)