|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import time |
| 4 | +from threading import Event, Thread |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from intersect_sdk.client import IntersectClient |
| 10 | +from intersect_sdk.client_callback_definitions import IntersectClientCallback |
| 11 | +from intersect_sdk.config.client import IntersectClientConfig |
| 12 | +from intersect_sdk.shared_callback_definitions import IntersectDirectMessageParams |
| 13 | + |
| 14 | + |
| 15 | +def test_timeout_callback_is_called(): |
| 16 | + """Tests that the timeout callback is called when a request times out.""" |
| 17 | + config = IntersectClientConfig( |
| 18 | + system='test', |
| 19 | + facility='test', |
| 20 | + organization='test', |
| 21 | + brokers=[{'host': 'localhost', 'port': 1883, 'protocol': 'mqtt3.1.1', 'username': 'test', 'password': 'test'}], |
| 22 | + initial_message_event_config=IntersectClientCallback(), |
| 23 | + terminate_after_initial_messages=True, |
| 24 | + ) |
| 25 | + |
| 26 | + timeout_called = [] |
| 27 | + |
| 28 | + def on_timeout(operation_id): |
| 29 | + timeout_called.append(operation_id) |
| 30 | + |
| 31 | + def user_callback(source, operation_id, has_error, payload): |
| 32 | + pass |
| 33 | + |
| 34 | + client = IntersectClient(config, user_callback=user_callback) |
| 35 | + |
| 36 | + # Mock the control plane and data plane managers |
| 37 | + client._control_plane_manager = MagicMock() |
| 38 | + client._control_plane_manager.is_connected.return_value = True |
| 39 | + client._control_plane_manager.considered_unrecoverable.return_value = False |
| 40 | + client._data_plane_manager = MagicMock() |
| 41 | + client._data_plane_manager.outgoing_message_data_handler.return_value = b'test' |
| 42 | + |
| 43 | + # Manually start the timeout thread (since startup() would try to connect) |
| 44 | + client._stop_timeout_thread = Event() |
| 45 | + client._timeout_thread = Thread(target=client._check_timeouts, daemon=True) |
| 46 | + client._timeout_thread.start() |
| 47 | + |
| 48 | + message = IntersectDirectMessageParams( |
| 49 | + destination='test.test.test.test.test', |
| 50 | + operation='test_op', |
| 51 | + payload='test', |
| 52 | + timeout=0.1, |
| 53 | + on_timeout=on_timeout, |
| 54 | + ) |
| 55 | + |
| 56 | + client._send_userspace_message(message) |
| 57 | + |
| 58 | + # Wait for timeout to trigger |
| 59 | + time.sleep(0.3) |
| 60 | + |
| 61 | + assert len(timeout_called) == 1 |
| 62 | + assert timeout_called[0] == 'test_op' |
| 63 | + |
| 64 | + # Clean up |
| 65 | + client._stop_timeout_thread.set() |
| 66 | + client._timeout_thread.join() |
| 67 | + |
| 68 | + |
| 69 | +def test_timeout_callback_is_not_called(): |
| 70 | + """Tests that the timeout callback is not called when a request is fulfilled.""" |
| 71 | + config = IntersectClientConfig( |
| 72 | + system='test', |
| 73 | + facility='test', |
| 74 | + organization='test', |
| 75 | + brokers=[{'host': 'localhost', 'port': 1883, 'protocol': 'mqtt3.1.1', 'username': 'test', 'password': 'test'}], |
| 76 | + initial_message_event_config=IntersectClientCallback(), |
| 77 | + terminate_after_initial_messages=True, |
| 78 | + ) |
| 79 | + |
| 80 | + timeout_called = [] |
| 81 | + |
| 82 | + def on_timeout(operation_id): |
| 83 | + timeout_called.append(operation_id) |
| 84 | + |
| 85 | + user_callback_called = [] |
| 86 | + |
| 87 | + def user_callback(source, operation_id, has_error, payload): |
| 88 | + user_callback_called.append(True) |
| 89 | + return None |
| 90 | + |
| 91 | + client = IntersectClient(config, user_callback=user_callback) |
| 92 | + |
| 93 | + # Mock the control plane and data plane managers |
| 94 | + client._control_plane_manager = MagicMock() |
| 95 | + client._control_plane_manager.is_connected.return_value = True |
| 96 | + client._control_plane_manager.considered_unrecoverable.return_value = False |
| 97 | + client._data_plane_manager = MagicMock() |
| 98 | + client._data_plane_manager.outgoing_message_data_handler.return_value = b'test' |
| 99 | + client._data_plane_manager.incoming_message_data_handler.return_value = b'"test"' |
| 100 | + |
| 101 | + # Manually start the timeout thread |
| 102 | + client._stop_timeout_thread = Event() |
| 103 | + client._timeout_thread = Thread(target=client._check_timeouts, daemon=True) |
| 104 | + client._timeout_thread.start() |
| 105 | + |
| 106 | + message = IntersectDirectMessageParams( |
| 107 | + destination='test.test.test.test.test', |
| 108 | + operation='test_op', |
| 109 | + payload='test', |
| 110 | + timeout=0.5, |
| 111 | + on_timeout=on_timeout, |
| 112 | + ) |
| 113 | + |
| 114 | + client._send_userspace_message(message) |
| 115 | + |
| 116 | + # Simulate receiving a response before the timeout |
| 117 | + from datetime import datetime, timezone |
| 118 | + from uuid import uuid4 |
| 119 | + |
| 120 | + response_message = { |
| 121 | + 'messageId': str(uuid4()), |
| 122 | + 'headers': { |
| 123 | + 'source': 'test.test.test.test.test', |
| 124 | + 'destination': client._hierarchy.hierarchy_string('.'), |
| 125 | + 'has_error': False, |
| 126 | + 'sdk_version': '0.8.0', |
| 127 | + 'created_at': datetime.now(timezone.utc), |
| 128 | + 'data_handler': 0, # IntersectDataHandler.MESSAGE |
| 129 | + }, |
| 130 | + 'operationId': 'test_op', |
| 131 | + 'payload': 'test', |
| 132 | + 'contentType': 'application/json', |
| 133 | + } |
| 134 | + |
| 135 | + client._handle_userspace_message(response_message) |
| 136 | + |
| 137 | + # Wait to make sure timeout doesn't fire |
| 138 | + time.sleep(0.7) |
| 139 | + |
| 140 | + assert len(timeout_called) == 0 |
| 141 | + assert len(user_callback_called) == 1 |
| 142 | + |
| 143 | + # Clean up |
| 144 | + client._stop_timeout_thread.set() |
| 145 | + client._timeout_thread.join() |
| 146 | + |
| 147 | + |
| 148 | +def test_response_after_timeout_is_ignored(): |
| 149 | + """Tests that responses arriving after timeout are ignored and user_callback is not called.""" |
| 150 | + config = IntersectClientConfig( |
| 151 | + system='test', |
| 152 | + facility='test', |
| 153 | + organization='test', |
| 154 | + brokers=[{'host': 'localhost', 'port': 1883, 'protocol': 'mqtt3.1.1', 'username': 'test', 'password': 'test'}], |
| 155 | + initial_message_event_config=IntersectClientCallback(), |
| 156 | + terminate_after_initial_messages=True, |
| 157 | + ) |
| 158 | + |
| 159 | + timeout_called = [] |
| 160 | + |
| 161 | + def on_timeout(operation_id): |
| 162 | + timeout_called.append(operation_id) |
| 163 | + |
| 164 | + user_callback_called = [] |
| 165 | + |
| 166 | + def user_callback(source, operation_id, has_error, payload): |
| 167 | + user_callback_called.append(True) |
| 168 | + return None |
| 169 | + |
| 170 | + client = IntersectClient(config, user_callback=user_callback) |
| 171 | + |
| 172 | + # Mock the control plane and data plane managers |
| 173 | + client._control_plane_manager = MagicMock() |
| 174 | + client._control_plane_manager.is_connected.return_value = True |
| 175 | + client._control_plane_manager.considered_unrecoverable.return_value = False |
| 176 | + client._data_plane_manager = MagicMock() |
| 177 | + client._data_plane_manager.outgoing_message_data_handler.return_value = b'test' |
| 178 | + client._data_plane_manager.incoming_message_data_handler.return_value = b'"test"' |
| 179 | + |
| 180 | + # Manually start the timeout thread |
| 181 | + client._stop_timeout_thread = Event() |
| 182 | + client._timeout_thread = Thread(target=client._check_timeouts, daemon=True) |
| 183 | + client._timeout_thread.start() |
| 184 | + |
| 185 | + message = IntersectDirectMessageParams( |
| 186 | + destination='test.test.test.test.test', |
| 187 | + operation='test_op', |
| 188 | + payload='test', |
| 189 | + timeout=0.1, |
| 190 | + on_timeout=on_timeout, |
| 191 | + ) |
| 192 | + |
| 193 | + client._send_userspace_message(message) |
| 194 | + |
| 195 | + # Wait for timeout to trigger |
| 196 | + time.sleep(0.3) |
| 197 | + |
| 198 | + # Timeout should have been called |
| 199 | + assert len(timeout_called) == 1 |
| 200 | + assert timeout_called[0] == 'test_op' |
| 201 | + |
| 202 | + # Now simulate receiving a late response after timeout |
| 203 | + from datetime import datetime, timezone |
| 204 | + from uuid import uuid4 |
| 205 | + |
| 206 | + response_message = { |
| 207 | + 'messageId': str(uuid4()), |
| 208 | + 'headers': { |
| 209 | + 'source': 'test.test.test.test.test', |
| 210 | + 'destination': client._hierarchy.hierarchy_string('.'), |
| 211 | + 'has_error': False, |
| 212 | + 'sdk_version': '0.8.0', |
| 213 | + 'created_at': datetime.now(timezone.utc), |
| 214 | + 'data_handler': 0, # IntersectDataHandler.MESSAGE |
| 215 | + }, |
| 216 | + 'operationId': 'test_op', |
| 217 | + 'payload': 'test', |
| 218 | + 'contentType': 'application/json', |
| 219 | + } |
| 220 | + |
| 221 | + client._handle_userspace_message(response_message) |
| 222 | + |
| 223 | + # User callback should NOT have been called since the request already timed out |
| 224 | + assert len(user_callback_called) == 0 |
| 225 | + |
| 226 | + # Clean up |
| 227 | + client._stop_timeout_thread.set() |
| 228 | + client._timeout_thread.join() |
0 commit comments