Repository navigation
Expand file tree
/
Copy pathtest_sync_realtime.py
More file actions
1223 lines (1110 loc) · 52.1 KB
/
Copy pathtest_sync_realtime.py
File metadata and controls
1223 lines (1110 loc) · 52.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import tempfile
import threading
import time
import unittest
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from unittest.mock import ANY, Mock, patch
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.api import routes
from app.config import Settings
from app.db import get_connection, init_db
from app.sync_realtime import RealtimeService, RouteBusesService
from app.tdx_client import TDXJSONResponse
class _FakeTDXClient:
def __init__(self) -> None:
self.eta_payload_by_route: dict[str, list[dict]] = {}
self.buses_payload_by_route: dict[str, list[dict]] = {}
def fetch_estimated_time_of_arrival_batch(
self,
city: str,
routeids: list[str],
*,
if_modified_since: str | None = None,
) -> TDXJSONResponse:
payload: list[dict] = []
for routeid in routeids:
payload.extend(self.eta_payload_by_route.get(routeid, []))
return TDXJSONResponse(payload=payload, status_code=200, last_modified=None)
def fetch_estimated_time_of_arrival(self, city: str, routeid: str) -> list[dict]:
return list(self.eta_payload_by_route.get(routeid, []))
def fetch_realtime_by_frequency_batch(
self,
city: str,
routeids: list[str],
*,
if_modified_since: str | None = None,
) -> TDXJSONResponse:
payload: list[dict] = []
for routeid in routeids:
payload.extend(self.buses_payload_by_route.get(routeid, []))
return TDXJSONResponse(payload=payload, status_code=200, last_modified=None)
def fetch_realtime_by_frequency(self, city: str, routeid: str) -> list[dict]:
return list(self.buses_payload_by_route.get(routeid, []))
class _FakeNtpcOpenDataClient:
def __init__(self) -> None:
self.eta_payload_by_route: dict[str, list[dict]] = {}
self.requested_routeids: list[list[str]] = []
def fetch_estimated_time_of_arrival_by_subroute(
self,
routeids: list[str],
) -> dict[str, list[dict]]:
self.requested_routeids.append(list(routeids))
return {
routeid: list(self.eta_payload_by_route.get(routeid, []))
for routeid in routeids
}
class RealtimeBackfillTests(unittest.TestCase):
def _gps_time(self, epoch_seconds: float) -> str:
from datetime import datetime, timezone
return datetime.fromtimestamp(epoch_seconds, tz=timezone.utc).isoformat()
def setUp(self) -> None:
self.temp_dir = tempfile.TemporaryDirectory()
self.db_path = Path(self.temp_dir.name) / "bus.db"
self.download_db_path = Path(self.temp_dir.name) / "downloads" / "bus.db"
self.download_db_path.parent.mkdir(parents=True, exist_ok=True)
self.app_db_path = Path(self.temp_dir.name) / "app.db"
init_db(self.db_path)
self.settings = Settings(
project_dir=Path(self.temp_dir.name),
db_path=self.db_path,
download_db_path=self.download_db_path,
tdx_client_id="test",
tdx_client_secret="test",
tdx_base_url="https://example.invalid",
tdx_token_url="https://example.invalid/token",
tdx_cities=("Taichung", "NewTaipei"),
tdx_request_timeout=30,
tdx_token_refresh_skew=300,
tdx_retry_attempts=1,
tdx_retry_backoff=1.0,
tdx_min_request_interval=0.0,
realtime_cache_ttl=5,
realtime_track_ttl=30,
cors_origins=(),
auth_public_base_url="https://bus.example.invalid",
auth_state_ttl_seconds=600,
auth_snowflake_node_id=0,
discord_oauth_client_id=None,
discord_oauth_client_secret=None,
google_oauth_client_id=None,
google_oauth_client_secret=None,
google_native_oauth_client_ids=(),
app_db_path=self.app_db_path,
)
self.client = _FakeTDXClient()
self.ntpc_client = _FakeNtpcOpenDataClient()
self._seed_route("TXG307")
def tearDown(self) -> None:
self.temp_dir.cleanup()
def _seed_route(self, routeid: str) -> None:
with get_connection(self.db_path) as connection:
with connection:
connection.execute(
"INSERT INTO routes (routeid, name, name_en) VALUES (?, ?, ?)",
(routeid, "307", "307"),
)
connection.execute(
"INSERT INTO paths (routeid, pathid, name, name_en) VALUES (?, ?, ?, ?)",
(routeid, 0, "Outbound", "Outbound"),
)
stops = [
(routeid, 0, 1, "STOP1", "Stop 1", "Stop 1", 24.1000, 120.6500),
(routeid, 0, 2, "STOP2", "Stop 2", "Stop 2", 24.1006, 120.6500),
(routeid, 0, 3, "STOP3", "Stop 3", "Stop 3", 24.1012, 120.6500),
]
connection.executemany(
"""
INSERT INTO stops (routeid, pathid, seq, stopid, name, name_en, lat, lon)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
stops,
)
path_points = [
(routeid, 0, 1, 24.1000, 120.6500),
(routeid, 0, 2, 24.1006, 120.6500),
(routeid, 0, 3, 24.1012, 120.6500),
]
connection.executemany(
"INSERT INTO path_points (routeid, pathid, seq, lat, lon) VALUES (?, ?, ?, ?, ?)",
path_points,
)
travel_times = [
(routeid, 0, 1, 2, 60.0, 4, "eta"),
(routeid, 0, 2, 3, 90.0, 4, "eta"),
]
connection.executemany(
"""
INSERT INTO stop_travel_times
(routeid, direction, from_seq, to_seq, avg_seconds, sample_count, source)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
travel_times,
)
def _build_services(self) -> tuple[RealtimeService, RouteBusesService]:
route_buses_service = RouteBusesService(self.settings, self.client)
realtime_service = RealtimeService(
self.settings,
self.client,
route_buses_service=route_buses_service,
ntpc_opendata_client=self.ntpc_client,
)
return realtime_service, route_buses_service
def test_waiting_batch_requests_recheck_the_city_cache(self) -> None:
service, _ = self._build_services()
city_lock = threading.Lock()
ready = threading.Barrier(3)
def wait_for_both_callers(city):
ready.wait(timeout=5)
return city_lock
with patch.object(service, "_get_city_refresh_lock", side_effect=wait_for_both_callers), \
patch.object(self.client, "fetch_estimated_time_of_arrival_batch",
wraps=self.client.fetch_estimated_time_of_arrival_batch) as fetch, \
ThreadPoolExecutor(max_workers=2) as executor:
with city_lock:
first = executor.submit(service.get_batch_snapshots, ["TXG307"])
second = executor.submit(service.get_batch_snapshots, ["TXG307"])
ready.wait(timeout=5)
self.assertIn("TXG307", first.result(timeout=5))
self.assertIn("TXG307", second.result(timeout=5))
self.assertEqual(fetch.call_count, 1)
def test_eta_reference_and_source_time_survive_cache_and_304(self) -> None:
source_time = datetime.fromisoformat("2026-10-10T08:00:00+08:00").timestamp()
self.client.eta_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG307", "Direction": 0, "StopID": "STOP1",
"EstimateTime": 120, "UpdateTime": "2026-10-10T08:00:00+08:00",
}]
service, _ = self._build_services()
with patch("app.sync_realtime.time.time", return_value=source_time + 35):
first = service.get_snapshot("TXG307")
stop = first["paths"][0]["stops"][0]
self.assertEqual(stop["eta"], 85)
self.assertEqual(stop["updated_at"], source_time)
self.assertEqual(stop["eta_reference_at"], source_time + 35)
with patch("app.sync_realtime.time.time", return_value=source_time + 50), \
patch.object(self.client, "fetch_estimated_time_of_arrival_batch",
return_value=TDXJSONResponse(None, 304, None)):
second = service.get_snapshot("TXG307")
self.assertEqual(second["paths"], first["paths"])
self.assertEqual(service._cache["TXG307"].fetched_at, source_time + 35)
def test_eta_refreshes_report_item_counts_to_the_empty_feed_monitor(self) -> None:
monitor = Mock()
service = RealtimeService(self.settings, self.client, empty_feed_monitor=monitor)
service.get_snapshot("TXG307")
monitor.record.assert_called_once_with("Taichung", ["TXG307"], 0)
# Rows that no longer map onto the route leave users just as blank.
monitor.reset_mock()
service._cache.clear()
self.client.eta_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG999", "Direction": 0, "StopID": "STOP1",
"EstimateTime": 120, "UpdateTime": datetime.now().astimezone().isoformat(),
}]
service.get_snapshot("TXG307")
monitor.record.assert_called_once_with("Taichung", ["TXG307"], 0)
monitor.reset_mock()
service._cache.clear()
self.client.eta_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG307", "Direction": 0, "StopID": "STOP1",
"EstimateTime": 120, "UpdateTime": datetime.now().astimezone().isoformat(),
}]
service.get_batch_snapshots(["TXG307"])
monitor.record.assert_called_once_with("Taichung", ["TXG307"], 1)
monitor.reset_mock()
service._cache["TXG307"].expires_at = 0
with patch.object(self.client, "fetch_estimated_time_of_arrival_batch",
return_value=TDXJSONResponse(None, 304, None)):
service.get_snapshot("TXG307")
monitor.record.assert_not_called()
def test_repeated_failures_cannot_extend_stale_snapshot_lifetime(self) -> None:
service, _ = self._build_services()
with patch("app.sync_realtime.time.time", return_value=1000):
service.get_snapshot("TXG307")
with patch.object(self.client, "fetch_estimated_time_of_arrival_batch",
side_effect=RuntimeError("offline")):
for now in (1010, 1040, 1089):
with patch("app.sync_realtime.time.time", return_value=now):
self.assertTrue(service.get_snapshot("TXG307")["stale"])
self.assertIn("TXG307", service.get_batch_snapshots(["TXG307"]))
with patch("app.sync_realtime.time.time", return_value=1091):
with self.assertRaises(RuntimeError):
service.get_snapshot("TXG307")
self.assertEqual(service.get_batch_snapshots(["TXG307"]), {})
def test_prefer_cached_returns_before_slow_refresh_and_coalesces_it(self) -> None:
now = datetime.now().astimezone().isoformat()
self.client.eta_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG307", "Direction": 0, "StopID": "STOP1",
"EstimateTime": 120, "UpdateTime": now,
}]
service, _ = self._build_services()
service.get_snapshot("TXG307")
service._cache["TXG307"].expires_at = 0
self.client.eta_payload_by_route["TXG307"][0]["EstimateTime"] = 240
started = threading.Event()
release = threading.Event()
original = self.client.fetch_estimated_time_of_arrival_batch
def blocked_eta(*args, **kwargs):
started.set()
if not release.wait(3):
raise TimeoutError("test refresh was not released")
return original(*args, **kwargs)
with patch.object(self.client, "fetch_estimated_time_of_arrival_batch", side_effect=blocked_eta) as fetch, \
ThreadPoolExecutor(max_workers=1) as executor:
try:
preview = executor.submit(service.get_snapshot, "TXG307", prefer_cached=True)
self.assertTrue(preview.result(timeout=1)["stale"])
self.assertTrue(started.wait(1))
self.assertTrue(service.get_snapshot("TXG307", prefer_cached=True)["stale"])
self.assertEqual(fetch.call_count, 1)
finally:
release.set()
deadline = time.monotonic() + 3
while service._background_refreshing_cities and time.monotonic() < deadline:
time.sleep(0.01)
self.assertFalse(service._background_refreshing_cities)
self.assertGreater(service.get_snapshot("TXG307")["paths"][0]["stops"][0]["eta"], 220)
def test_prefer_cached_does_not_reuse_old_eta_source(self) -> None:
old_time = datetime.fromtimestamp(time.time() - 95).astimezone().isoformat()
self.client.eta_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG307", "Direction": 0, "StopID": "STOP1",
"EstimateTime": 120, "UpdateTime": old_time,
}]
service, _ = self._build_services()
service.get_snapshot("TXG307")
service._cache["TXG307"].expires_at = 0
self.client.eta_payload_by_route["TXG307"][0].update({
"EstimateTime": 240, "UpdateTime": datetime.now().astimezone().isoformat(),
})
with patch.object(self.client, "fetch_estimated_time_of_arrival_batch",
wraps=self.client.fetch_estimated_time_of_arrival_batch) as fetch:
refreshed = service.get_snapshot("TXG307", prefer_cached=True)
try:
fetch.assert_called_once()
self.assertFalse(refreshed.get("stale", False))
self.assertGreater(refreshed["paths"][0]["stops"][0]["eta"], 220)
finally:
deadline = time.monotonic() + 3
while service._eta_first_loads and time.monotonic() < deadline:
time.sleep(0.01)
self.assertFalse(service._eta_first_loads)
def test_prefer_cached_does_not_extend_snapshot_lifetime(self) -> None:
now = datetime.now().astimezone().isoformat()
self.client.eta_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG307", "Direction": 0, "StopID": "STOP1",
"EstimateTime": 120, "UpdateTime": now,
}]
service, _ = self._build_services()
service.get_snapshot("TXG307")
service._cache["TXG307"].expires_at = 0
service._cache["TXG307"].fetched_at -= 91
with patch.object(self.client, "fetch_estimated_time_of_arrival_batch",
wraps=self.client.fetch_estimated_time_of_arrival_batch) as fetch:
refreshed = service.get_snapshot("TXG307", prefer_cached=True)
try:
fetch.assert_called_once()
self.assertFalse(refreshed.get("stale", False))
finally:
deadline = time.monotonic() + 3
while service._eta_first_loads and time.monotonic() < deadline:
time.sleep(0.01)
self.assertFalse(service._eta_first_loads)
def test_cold_route_returns_eta_before_deferred_travel_time_write(self) -> None:
now = datetime.now().astimezone().isoformat()
self.client.eta_payload_by_route["TXG307"] = [
{
"SubRouteUID": "TXG307", "Direction": 0, "StopID": stopid,
"EstimateTime": eta, "UpdateTime": now, "PlateNumb": "ABC-1234",
}
for stopid, eta in (("STOP1", 120), ("STOP2", 180))
]
service, _ = self._build_services()
scheduled: list = []
snapshot = service.get_snapshot(
"TXG307",
schedule_travel_time_update=scheduled.append,
)
self.assertEqual(snapshot["paths"][0]["stops"][0]["eta"], 120)
self.assertEqual(len(scheduled), 1)
with get_connection(self.db_path) as connection:
before = connection.execute(
"SELECT sample_count FROM stop_travel_times WHERE routeid = ? "
"AND from_seq = 1 AND to_seq = 2 AND source = 'eta'",
("TXG307",),
).fetchone()["sample_count"]
self.assertEqual(before, 4)
scheduled[0]()
with get_connection(self.db_path) as connection:
after = connection.execute(
"SELECT sample_count FROM stop_travel_times WHERE routeid = ? "
"AND from_seq = 1 AND to_seq = 2 AND source = 'eta'",
("TXG307",),
).fetchone()["sample_count"]
self.assertEqual(after, 5)
def test_cold_native_eta_precedes_slow_vehicle_fetch_without_duplicate_eta_calls(self) -> None:
now = datetime.now().astimezone().isoformat()
self.client.eta_payload_by_route["TXG307"] = [
{
"SubRouteUID": "TXG307", "Direction": 0, "StopID": stopid,
"EstimateTime": eta, "UpdateTime": now, "PlateNumb": "ABC-1234",
}
for stopid, eta in (("STOP1", 120), ("STOP2", 180))
]
self.client.buses_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG307", "PlateNumb": "ABC-1234", "Direction": 0,
"BusPosition": {"PositionLat": 24.1, "PositionLon": 120.65},
}]
service, buses_service = self._build_services()
started = threading.Event()
release = threading.Event()
original = self.client.fetch_realtime_by_frequency_batch
def blocked_buses(*args, **kwargs):
started.set()
if not release.wait(3):
raise TimeoutError("test bus source was not released")
return original(*args, **kwargs)
with patch.object(self.client, "fetch_realtime_by_frequency_batch", side_effect=blocked_buses), \
patch.object(self.client, "fetch_estimated_time_of_arrival_batch",
wraps=self.client.fetch_estimated_time_of_arrival_batch) as fetch_eta, \
ThreadPoolExecutor(max_workers=2) as executor:
try:
early = executor.submit(service.get_snapshot, "TXG307", prefer_cached=True)
preview = early.result(timeout=1)
self.assertTrue(preview["partial"])
self.assertEqual(preview["paths"][0]["stops"][0]["eta"], 120)
self.assertTrue(started.wait(1))
self.assertEqual(service.get_snapshot("TXG307", prefer_cached=True), preview)
self.assertEqual(fetch_eta.call_count, 1)
self.assertNotIn("TXG307", buses_service._cache)
self.assertNotIn("TXG307", service._last_native_plate_states)
with get_connection(self.db_path) as connection:
count = connection.execute(
"SELECT sample_count FROM stop_travel_times WHERE routeid = ? "
"AND from_seq = 1 AND to_seq = 2 AND source = 'eta'",
("TXG307",),
).fetchone()["sample_count"]
self.assertEqual(count, 4)
finally:
release.set()
deadline = time.monotonic() + 3
while service._eta_first_loads and time.monotonic() < deadline:
time.sleep(0.01)
self.assertFalse(service._eta_first_loads)
final = service.get_snapshot("TXG307")
self.assertNotIn("partial", final)
self.assertIn("TXG307", buses_service._cache)
self.assertIn("TXG307", service._last_native_plate_states)
with get_connection(self.db_path) as connection:
count = connection.execute(
"SELECT sample_count FROM stop_travel_times WHERE routeid = ? "
"AND from_seq = 1 AND to_seq = 2 AND source = 'eta'",
("TXG307",),
).fetchone()["sample_count"]
self.assertEqual(count, 5)
def test_eta_first_failure_before_eta_is_retryable(self) -> None:
service, _ = self._build_services()
with patch.object(self.client, "fetch_estimated_time_of_arrival_batch",
side_effect=RuntimeError("upstream offline")):
with self.assertRaisesRegex(RuntimeError, "upstream offline"):
service.get_snapshot("TXG307", prefer_cached=True)
self.client.eta_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG307", "Direction": 0, "StopID": "STOP1",
"EstimateTime": 120, "UpdateTime": datetime.now().astimezone().isoformat(),
}]
retried = service.get_snapshot("TXG307", prefer_cached=True)
self.assertEqual(retried["paths"][0]["stops"][0]["eta"], 120)
deadline = time.monotonic() + 3
while service._eta_first_loads and time.monotonic() < deadline:
time.sleep(0.01)
self.assertFalse(service._eta_first_loads)
def test_eta_first_waits_when_source_timestamp_is_missing(self) -> None:
self.client.eta_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG307", "Direction": 0, "StopID": "STOP1",
"EstimateTime": 120,
}]
service, _ = self._build_services()
result = service.get_snapshot("TXG307", prefer_cached=True)
self.assertNotIn("partial", result)
self.assertEqual(result["paths"][0]["stops"][0]["eta"], 120)
def test_failed_background_refresh_keeps_original_source_lifetime(self) -> None:
now = datetime.now().astimezone().isoformat()
self.client.eta_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG307", "Direction": 0, "StopID": "STOP1",
"EstimateTime": 120, "UpdateTime": now,
}]
service, _ = self._build_services()
service.get_snapshot("TXG307")
original_fetched_at = service._cache["TXG307"].fetched_at
service._cache["TXG307"].expires_at = 0
with patch.object(self.client, "fetch_estimated_time_of_arrival_batch",
side_effect=RuntimeError("offline")) as fetch:
self.assertTrue(service.get_snapshot("TXG307", prefer_cached=True)["stale"])
deadline = time.monotonic() + 3
while service._background_refreshing_cities and time.monotonic() < deadline:
time.sleep(0.01)
self.assertFalse(service._background_refreshing_cities)
fetch.assert_called_once()
self.assertEqual(service._cache["TXG307"].fetched_at, original_fetched_at)
service._cache["TXG307"].fetched_at -= 91
with self.assertRaises(RuntimeError):
service.get_snapshot("TXG307", prefer_cached=True)
def test_only_opted_in_route_realtime_requests_prefer_cached_data(self) -> None:
app = FastAPI()
service = Mock()
service.get_snapshot.return_value = {"paths": []}
app.state.realtime_service = service
app.include_router(routes.router)
with TestClient(app) as client:
self.assertEqual(client.get("/api/v1/routes/TXG307/realtime").status_code, 200)
service.get_snapshot.assert_called_with("TXG307")
self.assertEqual(client.get("/api/v1/routes/TXG307/realtime?prefer_cached=true").status_code, 200)
service.get_snapshot.assert_called_with(
"TXG307", prefer_cached=True, schedule_travel_time_update=ANY,
)
def test_old_source_data_is_retained_and_flagged_stale(self) -> None:
source_time = datetime.fromisoformat("2026-10-10T08:00:00+08:00").timestamp()
self.client.eta_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG307", "Direction": 0, "StopID": "STOP1",
"EstimateTime": 0, "UpdateTime": "2026-10-10T08:00:00+08:00",
}]
service, _ = self._build_services()
with patch("app.sync_realtime.time.time", return_value=source_time + 91):
snapshot = service.get_snapshot("TXG307")
self.assertTrue(snapshot["stale"])
self.assertEqual(snapshot["paths"][0]["stops"][0]["eta"], -1)
def test_vehicle_failure_preserves_successful_eta_without_seeding_map_cache(self) -> None:
source_time = datetime.fromisoformat("2026-10-10T08:00:00+08:00").timestamp()
self.client.eta_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG307", "Direction": 0, "StopID": "STOP1",
"EstimateTime": 120, "UpdateTime": "2026-10-10T08:00:00+08:00",
}]
for mode in ("single", "batch"):
with self.subTest(mode=mode):
service, buses = self._build_services()
with patch("app.sync_realtime.time.time", return_value=source_time + 10), \
patch.object(self.client, "fetch_realtime_by_frequency_batch",
side_effect=TimeoutError("GPS feed unavailable")):
snapshot = (service.get_snapshot("TXG307") if mode == "single"
else service.get_batch_snapshots(["TXG307"])["TXG307"])
self.assertEqual(snapshot["paths"][0]["stops"][0]["eta"], 110)
self.assertNotIn("TXG307", buses._cache)
def test_batch_only_refreshes_routes_still_missing_after_lock(self) -> None:
self._seed_route("TXG308")
service, _ = self._build_services()
snapshot = service.get_snapshot("TXG307")
service._cache.clear()
def concurrent_refresh(city):
service._set_cached("TXG307", snapshot)
return threading.Lock()
with patch.object(service, "_get_city_refresh_lock", side_effect=concurrent_refresh), \
patch.object(self.client, "fetch_estimated_time_of_arrival_batch",
wraps=self.client.fetch_estimated_time_of_arrival_batch) as fetch:
result = service.get_batch_snapshots(["TXG307", "TXG308"])
self.assertEqual(set(result), {"TXG307", "TXG308"})
self.assertEqual(fetch.call_args.args[1], ["TXG308"])
def test_realtime_supplies_map_cache_for_single_batch_and_fallback(self) -> None:
for mode in ("single", "batch", "fallback"):
for has_buses in (False, True):
with self.subTest(mode=mode, has_buses=has_buses):
service, buses_service = self._build_services()
self.client.buses_payload_by_route["TXG307"] = [{
"SubRouteUID": "TXG307", "PlateNumb": "AAA-1234",
"Direction": 0, "BusPosition": {"PositionLat": 24.1, "PositionLon": 120.65},
}] if has_buses else []
if mode == "single":
service.get_snapshot("TXG307")
elif mode == "batch":
service.get_batch_snapshots(["TXG307"])
else:
service._get_single_route_snapshot(
"TXG307", service._load_static_route("TXG307"), force_refresh=False,
)
with patch.object(self.client, "fetch_realtime_by_frequency_batch") as fetch:
buses = buses_service.get_buses("TXG307")
fetch.assert_not_called()
self.assertEqual(len(buses), int(has_buses))
def test_eta_304_and_failure_do_not_extend_vehicle_cache(self) -> None:
service, buses_service = self._build_services()
service.get_snapshot("TXG307")
original_expiry = buses_service._cache["TXG307"].expires_at
for response in (TDXJSONResponse([], 304, None), RuntimeError("offline")):
with self.subTest(response=type(response).__name__):
service._cache["TXG307"].expires_at = 0
kwargs = {"side_effect": response} if isinstance(response, Exception) else {"return_value": response}
with patch.object(self.client, "fetch_estimated_time_of_arrival_batch", **kwargs), \
patch.object(self.client, "fetch_realtime_by_frequency_batch") as fetch:
service.get_snapshot("TXG307")
fetch.assert_not_called()
self.assertEqual(buses_service._cache["TXG307"].expires_at, original_expiry)
def test_uses_ntpc_eta_fallback_when_new_taipei_tdx_eta_is_empty(self) -> None:
routeid = "NWT157491"
self._seed_route(routeid)
self.client.eta_payload_by_route[routeid] = []
self.client.buses_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "NTPC-ETA",
"BusPosition": {"PositionLat": 25.0, "PositionLon": 121.0},
"GPSTime": "2026-06-22T10:00:05+08:00",
},
]
self.ntpc_client.eta_payload_by_route[routeid] = [
{
"routeid": "16468",
"stopid": "STOP2",
"estimatetime": "45",
"goback": "0",
},
{
"routeid": "16468",
"stopid": "STOP3",
"estimatetime": "-1",
"goback": "0",
},
]
realtime_service, _ = self._build_services()
snapshot = realtime_service.get_snapshot(routeid, force_refresh=True)
path = next(path for path in snapshot["paths"] if path["pathid"] == 0)
stops = {stop["stopid"]: stop for stop in path["stops"]}
self.assertEqual(stops["STOP2"]["eta"], 45)
self.assertEqual(
stops["STOP2"]["etas"],
[
{
"plate": None,
"eta": 45,
"is_arriving": False,
"source": "ntpc_opendata",
"estimated": False,
}
],
)
self.assertNotIn("STOP3", stops)
def test_eta_first_waits_for_vehicle_dependent_ntpc_fallback(self) -> None:
routeid = "NWT157491"
self._seed_route(routeid)
self.client.buses_payload_by_route[routeid] = [{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "NTPC-ETA",
"BusPosition": {"PositionLat": 25.0, "PositionLon": 121.0},
}]
self.ntpc_client.eta_payload_by_route[routeid] = [{
"routeid": "16468", "stopid": "STOP2", "estimatetime": "45", "goback": "0",
}]
service, _ = self._build_services()
result = service.get_snapshot(routeid, prefer_cached=True)
self.assertNotIn("partial", result)
self.assertEqual(result["paths"][0]["stops"][0]["eta"], 45)
def test_skips_ntpc_eta_fallback_when_tdx_has_no_realtime_buses(self) -> None:
routeid = "NWT157491"
self._seed_route(routeid)
self.client.eta_payload_by_route[routeid] = []
self.client.buses_payload_by_route[routeid] = []
self.ntpc_client.eta_payload_by_route[routeid] = [
{
"routeid": "16468",
"stopid": "STOP2",
"estimatetime": "0",
"goback": "0",
},
]
realtime_service, _ = self._build_services()
snapshot = realtime_service.get_snapshot(routeid, force_refresh=True)
path = next(path for path in snapshot["paths"] if path["pathid"] == 0)
stops = {stop["stopid"]: stop for stop in path["stops"]}
self.assertNotIn("STOP2", stops)
self.assertEqual(self.ntpc_client.requested_routeids, [])
def test_ntpc_eta_fallback_does_not_override_native_tdx_eta(self) -> None:
routeid = "NWT157491"
self._seed_route(routeid)
self.client.eta_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"StopID": "STOP1",
"EstimateTime": 120,
"UpdateTime": "2026-06-22T10:00:00+08:00",
}
]
self.ntpc_client.eta_payload_by_route[routeid] = [
{
"routeid": "16468",
"stopid": "STOP2",
"estimatetime": "45",
"goback": "0",
},
]
realtime_service, _ = self._build_services()
snapshot = realtime_service.get_snapshot(routeid, force_refresh=True)
path = next(path for path in snapshot["paths"] if path["pathid"] == 0)
stops = {stop["stopid"]: stop for stop in path["stops"]}
self.assertIn("STOP1", stops)
self.assertNotIn("STOP2", stops)
self.assertEqual(self.ntpc_client.requested_routeids, [])
def test_does_not_backfill_on_first_seen_bus_only_snapshot(self) -> None:
routeid = "TXG307"
self.client.eta_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"StopID": "STOP1",
"EstimateTime": 120,
"UpdateTime": "2026-06-22T10:00:00+08:00",
"PlateNumb": "AAA-0001",
"VehicleStopStatus": 0,
}
]
self.client.buses_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "AAA-0001",
"BusPosition": {"PositionLat": 24.1000, "PositionLon": 120.6500},
"GPSTime": "2026-06-22T10:00:00+08:00",
},
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "BBB-0002",
"BusPosition": {"PositionLat": 24.10062, "PositionLon": 120.6500},
"GPSTime": "2026-06-22T10:00:05+08:00",
},
]
realtime_service, _ = self._build_services()
snapshot = realtime_service.get_snapshot(routeid, force_refresh=True)
path = snapshot["paths"][0]
stops = {stop["stopid"]: stop for stop in path["stops"]}
self.assertNotIn("STOP2", stops)
self.assertNotIn("STOP3", stops)
def test_backfills_missing_bus_into_buses_and_etas_after_eta_disappears(self) -> None:
routeid = "TXG307"
realtime_service, _ = self._build_services()
base_time = 1_700_000_000.0
with patch("app.sync_realtime.time.time", return_value=base_time):
self.client.eta_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"StopID": "STOP2",
"EstimateTime": 30,
"UpdateTime": "2026-06-22T10:00:00+08:00",
"PlateNumb": "BBB-0002",
"VehicleStopStatus": 1,
}
]
self.client.buses_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "BBB-0002",
"BusPosition": {"PositionLat": 24.10062, "PositionLon": 120.6500},
"GPSTime": self._gps_time(base_time - 5),
},
]
realtime_service.get_snapshot(routeid, force_refresh=True)
with patch("app.sync_realtime.time.time", return_value=base_time + 10):
self.client.eta_payload_by_route[routeid] = []
self.client.buses_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "BBB-0002",
"BusPosition": {"PositionLat": 24.10062, "PositionLon": 120.6500},
"GPSTime": self._gps_time(base_time + 5),
},
]
snapshot = realtime_service.get_snapshot(routeid, force_refresh=True)
path = snapshot["paths"][0]
stops = {stop["stopid"]: stop for stop in path["stops"]}
self.assertIn("STOP2", stops)
self.assertIn("STOP3", stops)
anchor_stop = stops["STOP2"]
downstream_stop = stops["STOP3"]
self.assertEqual(
anchor_stop["buses"],
[{"id": "BBB-0002", "type": "normal", "source": "backfill_buses"}],
)
self.assertIn(
{
"plate": "BBB-0002",
"eta": 0,
"is_arriving": True,
"source": "backfill_buses",
"estimated": True,
},
anchor_stop["etas"],
)
self.assertEqual(anchor_stop["eta"], 0)
self.assertIn(
{
"plate": "BBB-0002",
"eta": 90,
"is_arriving": False,
"source": "backfill_buses",
"estimated": True,
},
downstream_stop["etas"],
)
def test_does_not_backfill_from_gps_without_previous_native_eta_plate(self) -> None:
routeid = "TXG307"
base_time = 1_700_000_000.0
self.client.eta_payload_by_route[routeid] = []
self.client.buses_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "ZZZ-9999",
"BusPosition": {"PositionLat": 24.10062, "PositionLon": 120.6500},
"GPSTime": self._gps_time(base_time - 5),
},
]
realtime_service, _ = self._build_services()
with patch("app.sync_realtime.time.time", return_value=base_time):
snapshot = realtime_service.get_snapshot(routeid, force_refresh=True)
path = snapshot["paths"][0]
self.assertEqual(path["stops"], [])
def test_does_not_backfill_multiple_gps_only_buses_on_same_path(self) -> None:
routeid = "TXG307"
base_time = 1_700_000_000.0
self.client.eta_payload_by_route[routeid] = []
self.client.buses_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "GPS-0001",
"BusPosition": {"PositionLat": 24.1000, "PositionLon": 120.6500},
"GPSTime": self._gps_time(base_time - 5),
},
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "GPS-0002",
"BusPosition": {"PositionLat": 24.10062, "PositionLon": 120.6500},
"GPSTime": self._gps_time(base_time - 5),
},
]
realtime_service, _ = self._build_services()
with patch("app.sync_realtime.time.time", return_value=base_time):
snapshot = realtime_service.get_snapshot(routeid, force_refresh=True)
path = snapshot["paths"][0]
self.assertEqual(path["stops"], [])
def test_backfill_requires_eta_to_disappear_from_previous_snapshot(self) -> None:
routeid = "TXG307"
realtime_service, _ = self._build_services()
base_time = 1_700_000_000.0
with patch("app.sync_realtime.time.time", return_value=base_time):
self.client.eta_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"StopID": "STOP2",
"EstimateTime": 45,
"UpdateTime": "2026-06-22T10:00:00+08:00",
"PlateNumb": "BBB-0002",
"VehicleStopStatus": 0,
}
]
self.client.buses_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "BBB-0002",
"BusPosition": {"PositionLat": 24.10062, "PositionLon": 120.6500},
"GPSTime": self._gps_time(base_time - 5),
},
]
realtime_service.get_snapshot(routeid, force_refresh=True)
with patch("app.sync_realtime.time.time", return_value=base_time + 10):
self.client.eta_payload_by_route[routeid] = []
self.client.buses_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "BBB-0002",
"BusPosition": {"PositionLat": 24.10062, "PositionLon": 120.6500},
"GPSTime": self._gps_time(base_time + 5),
},
]
snapshot = realtime_service.get_snapshot(routeid, force_refresh=True)
path = snapshot["paths"][0]
stops = {stop["stopid"]: stop for stop in path["stops"]}
self.assertIn("STOP2", stops)
stop2 = stops["STOP2"]
self.assertEqual(stop2["buses"], [{"id": "BBB-0002", "type": "normal", "source": "backfill_buses"}])
def test_backfill_expires_after_disappearance_window(self) -> None:
routeid = "TXG307"
realtime_service, _ = self._build_services()
base_time = 1_700_000_000.0
with patch("app.sync_realtime.time.time", return_value=base_time):
self.client.eta_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"StopID": "STOP2",
"EstimateTime": 45,
"UpdateTime": "2026-06-22T10:00:00+08:00",
"PlateNumb": "BBB-0002",
"VehicleStopStatus": 0,
}
]
self.client.buses_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "BBB-0002",
"BusPosition": {"PositionLat": 24.10062, "PositionLon": 120.6500},
"GPSTime": self._gps_time(base_time - 5),
},
]
realtime_service.get_snapshot(routeid, force_refresh=True)
with patch("app.sync_realtime.time.time", return_value=base_time + 200.0):
self.client.eta_payload_by_route[routeid] = []
self.client.buses_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"PlateNumb": "BBB-0002",
"BusPosition": {"PositionLat": 24.10062, "PositionLon": 120.6500},
"GPSTime": self._gps_time(base_time + 195),
},
]
snapshot = realtime_service.get_snapshot(routeid, force_refresh=True)
path = snapshot["paths"][0]
for stop in path["stops"]:
self.assertEqual(stop["buses"], [])
self.assertEqual(stop["etas"], [])
def test_does_not_backfill_vehicle_last_seen_at_terminal_stop(self) -> None:
routeid = "TXG307"
realtime_service, _ = self._build_services()
self.client.eta_payload_by_route[routeid] = [
{
"RouteUID": routeid,
"SubRouteUID": routeid,
"Direction": 0,
"StopID": "STOP3",
"EstimateTime": 10,