|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +import time |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from UnleashClient import INSTANCES, UnleashClient |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(autouse=True) |
| 12 | +def reset_instances(): |
| 13 | + INSTANCES._reset() |
| 14 | + |
| 15 | + |
| 16 | +class FakeEvent: |
| 17 | + def __init__(self, event: str, data): |
| 18 | + self.event = event |
| 19 | + self.data = data |
| 20 | + |
| 21 | + |
| 22 | +class FiniteSSEClient: |
| 23 | + """SSE client that yields given events then stops.""" |
| 24 | + |
| 25 | + def __init__(self, events): |
| 26 | + self._events = list(events) |
| 27 | + self.closed = False |
| 28 | + |
| 29 | + @property |
| 30 | + def events(self): |
| 31 | + for e in self._events: |
| 32 | + if self.closed: |
| 33 | + break |
| 34 | + yield e |
| 35 | + |
| 36 | + def close(self): |
| 37 | + self.closed = True |
| 38 | + |
| 39 | + def interrupt(self): |
| 40 | + self.close() |
| 41 | + |
| 42 | + |
| 43 | +def _state_with_feature(name: str) -> str: |
| 44 | + payload = { |
| 45 | + "version": 1, |
| 46 | + "features": [ |
| 47 | + {"name": name, "enabled": True, "strategies": [{"name": "default"}]} |
| 48 | + ], |
| 49 | + "segments": [], |
| 50 | + } |
| 51 | + return json.dumps(payload) |
| 52 | + |
| 53 | + |
| 54 | +def test_streaming_processes_unleash_connected_event(): |
| 55 | + captured = {} |
| 56 | + |
| 57 | + def factory(url, headers, timeout): |
| 58 | + captured["url"] = url |
| 59 | + captured["headers"] = headers |
| 60 | + return FiniteSSEClient( |
| 61 | + [FakeEvent("unleash-connected", _state_with_feature("test-feature"))] |
| 62 | + ) |
| 63 | + |
| 64 | + client = UnleashClient( |
| 65 | + url="http://unleash.example", |
| 66 | + app_name="my-test-app", |
| 67 | + instance_id="rspec/test", |
| 68 | + disable_metrics=True, |
| 69 | + disable_registration=True, |
| 70 | + custom_headers={"X-API-KEY": "123"}, |
| 71 | + experimental_mode={"type": "streaming"}, |
| 72 | + sse_client_factory=factory, |
| 73 | + ) |
| 74 | + |
| 75 | + try: |
| 76 | + client.initialize_client() |
| 77 | + time.sleep(0.05) |
| 78 | + |
| 79 | + assert captured["url"].endswith("/client/streaming") |
| 80 | + assert captured["headers"]["X-API-KEY"] == "123" |
| 81 | + |
| 82 | + assert client.is_enabled("test-feature") is True |
| 83 | + finally: |
| 84 | + client.destroy() |
| 85 | + |
| 86 | + |
| 87 | +def test_streaming_processes_unleash_updated_event(): |
| 88 | + captured = {} |
| 89 | + |
| 90 | + def factory(url, headers, timeout): |
| 91 | + captured["url"] = url |
| 92 | + captured["headers"] = headers |
| 93 | + empty_state = json.dumps({"version": 1, "features": [], "segments": []}) |
| 94 | + update_state = _state_with_feature("test-feature") |
| 95 | + return FiniteSSEClient( |
| 96 | + [ |
| 97 | + FakeEvent("unleash-connected", empty_state), |
| 98 | + FakeEvent("unleash-updated", update_state), |
| 99 | + ] |
| 100 | + ) |
| 101 | + |
| 102 | + client = UnleashClient( |
| 103 | + url="http://unleash.example", |
| 104 | + app_name="my-test-app", |
| 105 | + instance_id="rspec/test", |
| 106 | + disable_metrics=True, |
| 107 | + disable_registration=True, |
| 108 | + custom_headers={"X-API-KEY": "123"}, |
| 109 | + experimental_mode={"type": "streaming"}, |
| 110 | + sse_client_factory=factory, |
| 111 | + ) |
| 112 | + |
| 113 | + try: |
| 114 | + client.initialize_client() |
| 115 | + time.sleep(0.05) |
| 116 | + |
| 117 | + assert captured["url"].endswith("/client/streaming") |
| 118 | + assert captured["headers"]["X-API-KEY"] == "123" |
| 119 | + |
| 120 | + assert client.is_enabled("test-feature") is True |
| 121 | + finally: |
| 122 | + client.destroy() |
0 commit comments