askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
test_log_replication.py · 8.2 KB · 267 lines
| 1 | """ |
| 2 | Tests for log replication across cluster stations. |
| 3 | |
| 4 | Verifies that: |
| 5 | 1. Logs are appended, not overwritten |
| 6 | 2. Duplicate entries are detected |
| 7 | 3. All log entries are preserved across replication |
| 8 | """ |
| 9 | import pytest |
| 10 | import tempfile |
| 11 | import shutil |
| 12 | from pathlib import Path |
| 13 | from dbbasic_object_core.core.self_logger import SelfLogger |
| 14 | |
| 15 | |
| 16 | class TestLogReplicationBasics: |
| 17 | """Test basic log replication functionality""" |
| 18 | |
| 19 | def setup_method(self): |
| 20 | """Create temp directory for test logs""" |
| 21 | self.temp_dir = Path(tempfile.mkdtemp()) |
| 22 | |
| 23 | def teardown_method(self): |
| 24 | """Clean up temp directory""" |
| 25 | shutil.rmtree(self.temp_dir, ignore_errors=True) |
| 26 | |
| 27 | def test_log_entry_has_entry_id(self): |
| 28 | """Log entries should have unique entry_id""" |
| 29 | logger = SelfLogger('test_object', self.temp_dir, enable_replication=False) |
| 30 | |
| 31 | logger.info('First message') |
| 32 | logger.info('Second message') |
| 33 | |
| 34 | logs = logger.get_logs() |
| 35 | |
| 36 | assert len(logs) == 2 |
| 37 | assert 'entry_id' in logs[0] |
| 38 | assert 'entry_id' in logs[1] |
| 39 | assert logs[0]['entry_id'] != logs[1]['entry_id'] |
| 40 | |
| 41 | def test_entry_ids_are_unique_and_deterministic(self): |
| 42 | """Entry IDs should be unique but deterministic for same input""" |
| 43 | logger = SelfLogger('test_object', self.temp_dir, enable_replication=False) |
| 44 | |
| 45 | # Log same message twice |
| 46 | logger.info('Same message') |
| 47 | logger.info('Same message') |
| 48 | |
| 49 | logs = logger.get_logs() |
| 50 | |
| 51 | # IDs should be different (different timestamps) |
| 52 | assert logs[0]['entry_id'] != logs[1]['entry_id'] |
| 53 | |
| 54 | def test_multiple_log_entries_all_preserved(self): |
| 55 | """All log entries should be preserved in order""" |
| 56 | logger = SelfLogger('test_object', self.temp_dir, enable_replication=False) |
| 57 | |
| 58 | # Log multiple entries |
| 59 | for i in range(10): |
| 60 | logger.info(f'Message {i}') |
| 61 | |
| 62 | logs = logger.get_logs() |
| 63 | |
| 64 | assert len(logs) == 10 |
| 65 | for i in range(10): |
| 66 | assert logs[i]['message'] == f'Message {i}' |
| 67 | |
| 68 | |
| 69 | class TestLogAppendEndpoint: |
| 70 | """Test /cluster/append_log endpoint""" |
| 71 | |
| 72 | def setup_method(self): |
| 73 | """Setup test environment""" |
| 74 | self.temp_dir = Path(tempfile.mkdtemp()) |
| 75 | |
| 76 | # Clean up any existing test data from previous runs |
| 77 | test_log_dir = Path('data/logs/test_object') |
| 78 | if test_log_dir.exists(): |
| 79 | shutil.rmtree(test_log_dir) |
| 80 | |
| 81 | # Create mock request |
| 82 | class MockRequest: |
| 83 | def __init__(self): |
| 84 | self.body = b'' |
| 85 | |
| 86 | self.request = MockRequest() |
| 87 | |
| 88 | def teardown_method(self): |
| 89 | """Clean up""" |
| 90 | shutil.rmtree(self.temp_dir, ignore_errors=True) |
| 91 | # Clean up test data |
| 92 | test_log_dir = Path('data/logs/test_object') |
| 93 | if test_log_dir.exists(): |
| 94 | shutil.rmtree(test_log_dir) |
| 95 | |
| 96 | def test_append_log_endpoint_accepts_entry(self): |
| 97 | """Endpoint should accept and append log entry""" |
| 98 | from api.cluster.append_log import POST |
| 99 | import json |
| 100 | |
| 101 | log_entry = { |
| 102 | 'timestamp': '2025-11-16T12:00:00', |
| 103 | 'level': 'INFO', |
| 104 | 'message': 'Test message' |
| 105 | } |
| 106 | |
| 107 | payload = { |
| 108 | 'object_id': 'test_object', |
| 109 | 'entry_id': 'abc123', |
| 110 | 'log_entry': log_entry, |
| 111 | 'source_station': 'station2' |
| 112 | } |
| 113 | |
| 114 | self.request.body = json.dumps(payload).encode('utf-8') |
| 115 | |
| 116 | response = POST(self.request) |
| 117 | |
| 118 | # Response is a tuple (status, headers, [body]) |
| 119 | response_body = response[2][0] if isinstance(response, tuple) else response |
| 120 | response_data = json.loads(response_body) |
| 121 | assert response_data['status'] == 'ok' |
| 122 | |
| 123 | # Verify log file created |
| 124 | log_file = Path('data/logs/test_object/log.tsv') |
| 125 | assert log_file.exists() |
| 126 | |
| 127 | def test_duplicate_entries_rejected(self): |
| 128 | """Duplicate entry_id should be rejected""" |
| 129 | from api.cluster.append_log import POST |
| 130 | import json |
| 131 | |
| 132 | log_entry = { |
| 133 | 'timestamp': '2025-11-16T12:00:00', |
| 134 | 'level': 'INFO', |
| 135 | 'message': 'Test message' |
| 136 | } |
| 137 | |
| 138 | payload = { |
| 139 | 'object_id': 'test_object', |
| 140 | 'entry_id': 'duplicate123', |
| 141 | 'log_entry': log_entry, |
| 142 | 'source_station': 'station2' |
| 143 | } |
| 144 | |
| 145 | self.request.body = json.dumps(payload).encode('utf-8') |
| 146 | |
| 147 | # First POST should succeed |
| 148 | response1 = POST(self.request) |
| 149 | body1 = response1[2][0] if isinstance(response1, tuple) else response1 |
| 150 | data1 = json.loads(body1) |
| 151 | assert data1['status'] == 'ok' |
| 152 | |
| 153 | # Second POST with same entry_id should be marked as duplicate |
| 154 | response2 = POST(self.request) |
| 155 | body2 = response2[2][0] if isinstance(response2, tuple) else response2 |
| 156 | data2 = json.loads(body2) |
| 157 | assert data2['status'] == 'duplicate' |
| 158 | |
| 159 | def test_multiple_entries_all_appended(self): |
| 160 | """Multiple log entries should all be appended""" |
| 161 | from api.cluster.append_log import POST |
| 162 | import json |
| 163 | |
| 164 | # Send 5 different log entries |
| 165 | for i in range(5): |
| 166 | log_entry = { |
| 167 | 'timestamp': f'2025-11-16T12:00:0{i}', |
| 168 | 'level': 'INFO', |
| 169 | 'message': f'Message {i}' |
| 170 | } |
| 171 | |
| 172 | payload = { |
| 173 | 'object_id': 'test_object', |
| 174 | 'entry_id': f'entry_{i}', |
| 175 | 'log_entry': log_entry, |
| 176 | 'source_station': 'station2' |
| 177 | } |
| 178 | |
| 179 | self.request.body = json.dumps(payload).encode('utf-8') |
| 180 | response = POST(self.request) |
| 181 | |
| 182 | body = response[2][0] if isinstance(response, tuple) else response |
| 183 | data = json.loads(body) |
| 184 | assert data['status'] == 'ok' |
| 185 | |
| 186 | # Verify all 5 entries exist in log file |
| 187 | logger = SelfLogger('test_object', Path('data'), enable_replication=False) |
| 188 | logs = logger.get_logs() |
| 189 | |
| 190 | assert len(logs) == 5 |
| 191 | for i in range(5): |
| 192 | assert logs[i]['message'] == f'Message {i}' |
| 193 | |
| 194 | |
| 195 | class TestLogReplicationIntegration: |
| 196 | """Integration tests for end-to-end log replication""" |
| 197 | |
| 198 | def setup_method(self): |
| 199 | """Setup test environment""" |
| 200 | self.temp_dir = Path(tempfile.mkdtemp()) |
| 201 | |
| 202 | def teardown_method(self): |
| 203 | """Clean up""" |
| 204 | shutil.rmtree(self.temp_dir, ignore_errors=True) |
| 205 | |
| 206 | def test_logger_creates_entries_with_unique_ids(self): |
| 207 | """Logger should create unique IDs for each entry""" |
| 208 | logger = SelfLogger('test_object', self.temp_dir, enable_replication=False) |
| 209 | |
| 210 | logger.info('Entry 1') |
| 211 | logger.info('Entry 2') |
| 212 | logger.info('Entry 3') |
| 213 | |
| 214 | logs = logger.get_logs() |
| 215 | |
| 216 | # All entries should have unique IDs |
| 217 | entry_ids = [log['entry_id'] for log in logs] |
| 218 | assert len(entry_ids) == len(set(entry_ids)) # No duplicates |
| 219 | |
| 220 | def test_backward_compatibility_with_old_logs(self): |
| 221 | """Should handle old logs without entry_id field""" |
| 222 | # Create a fresh logger (this will create new log file with entry_id) |
| 223 | logger = SelfLogger('test_object', self.temp_dir, enable_replication=False) |
| 224 | |
| 225 | # Log message - will have entry_id |
| 226 | logger.info('New message') |
| 227 | |
| 228 | logs = logger.get_logs() |
| 229 | |
| 230 | # Log should be readable and have entry_id |
| 231 | assert len(logs) == 1 |
| 232 | assert 'entry_id' in logs[0] |
| 233 | assert logs[0]['entry_id'] != '' |
| 234 | assert logs[0]['message'] == 'New message' |
| 235 | |
| 236 | |
| 237 | class TestLogReplicationPerformance: |
| 238 | """Test performance aspects of log replication""" |
| 239 | |
| 240 | def setup_method(self): |
| 241 | """Setup test environment""" |
| 242 | self.temp_dir = Path(tempfile.mkdtemp()) |
| 243 | |
| 244 | def teardown_method(self): |
| 245 | """Clean up""" |
| 246 | shutil.rmtree(self.temp_dir, ignore_errors=True) |
| 247 | |
| 248 | def test_logging_many_entries_is_fast(self): |
| 249 | """Logging many entries should complete quickly""" |
| 250 | import time |
| 251 | |
| 252 | logger = SelfLogger('test_object', self.temp_dir, enable_replication=False) |
| 253 | |
| 254 | start = time.time() |
| 255 | |
| 256 | # Log 100 entries |
| 257 | for i in range(100): |
| 258 | logger.info(f'Message {i}') |
| 259 | |
| 260 | elapsed = time.time() - start |
| 261 | |
| 262 | # Should complete in under 1 second |
| 263 | assert elapsed < 1.0 |
| 264 | |
| 265 | # Verify all logs exist |
| 266 | logs = logger.get_logs() |
| 267 | assert len(logs) == 100 |