quantplay 2.1.144__tar.gz → 2.1.145__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. {quantplay-2.1.144 → quantplay-2.1.145}/PKG-INFO +1 -1
  2. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/broker_factory.py +43 -29
  3. quantplay-2.1.145/quantplay/utils/caching.py +55 -0
  4. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay.egg-info/PKG-INFO +1 -1
  5. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay.egg-info/SOURCES.txt +1 -0
  6. {quantplay-2.1.144 → quantplay-2.1.145}/setup.py +1 -1
  7. quantplay-2.1.145/tests/test_instrument_cache.py +146 -0
  8. quantplay-2.1.144/quantplay/utils/caching.py +0 -35
  9. {quantplay-2.1.144 → quantplay-2.1.145}/README.md +0 -0
  10. {quantplay-2.1.144 → quantplay-2.1.145}/pyproject.toml +0 -0
  11. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/__init__.py +0 -0
  12. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/__init__.py +0 -0
  13. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/aliceblue.py +0 -0
  14. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/aliceblue_utils/__init__.py +0 -0
  15. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/aliceblue_utils/aliceblue_oauth.py +0 -0
  16. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/aliceblue_v2.py +0 -0
  17. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/angelone.py +0 -0
  18. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/angelone_utils/__init__.py +0 -0
  19. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/angelone_utils/angeloneWS.py +0 -0
  20. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/auto_login/__init__.py +0 -0
  21. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/breeze/__init__.py +0 -0
  22. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/breeze/breeze_utils.py +0 -0
  23. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/dhan.py +0 -0
  24. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/finvasia_utils/__init__.py +0 -0
  25. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/finvasia_utils/fa_noren.py +0 -0
  26. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/finvasia_utils/finvasia_oauth.py +0 -0
  27. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/five_paisa.py +0 -0
  28. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/flattrade.py +0 -0
  29. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/ft_utils/__init__.py +0 -0
  30. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/ft_utils/flattrade_utils.py +0 -0
  31. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/ft_utils/ft_noren.py +0 -0
  32. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/generics/__init__.py +0 -0
  33. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/generics/broker.py +0 -0
  34. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/icici_direct.py +0 -0
  35. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/iifl_xts.py +0 -0
  36. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/jainam_xts.py +0 -0
  37. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/kite_utils.py +0 -0
  38. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/kotak.py +0 -0
  39. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/kotak_utils/__init__.py +0 -0
  40. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/kotak_utils/kotak_ws.py +0 -0
  41. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/kotak_utils/kotak_ws_lib.py +0 -0
  42. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/motilal.py +0 -0
  43. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/noren.py +0 -0
  44. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/shoonya.py +0 -0
  45. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/uplink/__init__.py +0 -0
  46. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/uplink/uplink_utils.py +0 -0
  47. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/upstox.py +0 -0
  48. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/xts.py +0 -0
  49. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/xts_utils/Connect.py +0 -0
  50. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/xts_utils/Exception.py +0 -0
  51. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/xts_utils/InteractiveSocketClient.py +0 -0
  52. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/xts_utils/__init__.py +0 -0
  53. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/xts_utils_v2/ConnectV2.py +0 -0
  54. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/xts_utils_v2/InteractiveSocketClientV2.py +0 -0
  55. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/xts_utils_v2/__init__.py +0 -0
  56. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/broker/zerodha.py +0 -0
  57. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/core/__init__.py +0 -0
  58. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/core/strategy.py +0 -0
  59. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/exception/__init__.py +0 -0
  60. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/exception/exceptions.py +0 -0
  61. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/indicator/__init__.py +0 -0
  62. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/indicator/iv.py +0 -0
  63. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/model/__init__.py +0 -0
  64. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/model/broker.py +0 -0
  65. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/model/broker_response.py +0 -0
  66. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/model/generics.py +0 -0
  67. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/model/instrument_data.py +0 -0
  68. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/model/order_event.py +0 -0
  69. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/py.typed +0 -0
  70. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/strategy/__init__.py +0 -0
  71. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/strategy/iv_spike.py +0 -0
  72. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/strategy/obuy.py +0 -0
  73. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/utils/__init__.py +0 -0
  74. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/utils/constant.py +0 -0
  75. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/utils/exchange.py +0 -0
  76. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/utils/number_utils.py +0 -0
  77. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/utils/pickle_utils.py +0 -0
  78. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/utils/redaction.py +0 -0
  79. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/utils/selenium_utils.py +0 -0
  80. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/wrapper/__init__.py +0 -0
  81. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/wrapper/aws/__init__.py +0 -0
  82. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/wrapper/aws/s3.py +0 -0
  83. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay/wrapper/redis.py +0 -0
  84. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay.egg-info/dependency_links.txt +0 -0
  85. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay.egg-info/requires.txt +0 -0
  86. {quantplay-2.1.144 → quantplay-2.1.145}/quantplay.egg-info/top_level.txt +0 -0
  87. {quantplay-2.1.144 → quantplay-2.1.145}/setup.cfg +0 -0
  88. {quantplay-2.1.144 → quantplay-2.1.145}/tests/__init__.py +0 -0
  89. {quantplay-2.1.144 → quantplay-2.1.145}/tests/conftest.py +0 -0
  90. {quantplay-2.1.144 → quantplay-2.1.145}/tests/test_aliceblue_unified.py +0 -0
  91. {quantplay-2.1.144 → quantplay-2.1.145}/tests/test_deltax_positions.py +0 -0
  92. {quantplay-2.1.144 → quantplay-2.1.145}/tests/test_deltax_root_url.py +0 -0
  93. {quantplay-2.1.144 → quantplay-2.1.145}/tests/test_jainam_xts.py +0 -0
  94. {quantplay-2.1.144 → quantplay-2.1.145}/tests/test_kotak_order_feed_url.py +0 -0
  95. {quantplay-2.1.144 → quantplay-2.1.145}/tests/test_kotak_timeouts.py +0 -0
  96. {quantplay-2.1.144 → quantplay-2.1.145}/tests/test_kotak_ws_per_account.py +0 -0
  97. {quantplay-2.1.144 → quantplay-2.1.145}/tests/test_log_redaction.py +0 -0
  98. {quantplay-2.1.144 → quantplay-2.1.145}/tests/test_motilal_login.py +0 -0
  99. {quantplay-2.1.144 → quantplay-2.1.145}/tests/test_sub_client_all_types.py +0 -0
  100. {quantplay-2.1.144 → quantplay-2.1.145}/tests/test_xts_v2_modify_order.py +0 -0
  101. {quantplay-2.1.144 → quantplay-2.1.145}/tests/test_zerodha_positions_add_ltp.py +0 -0
  102. {quantplay-2.1.144 → quantplay-2.1.145}/tests/wrapper/__init__.py +0 -0
  103. {quantplay-2.1.144 → quantplay-2.1.145}/tests/wrapper/aws/__init__.py +0 -0
  104. {quantplay-2.1.144 → quantplay-2.1.145}/tests/wrapper/aws/s3_test.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: quantplay
3
- Version: 2.1.144
3
+ Version: 2.1.145
4
4
  Summary: This python package will be stored in AWS CodeArtifact
5
5
  Home-page:
6
6
  Author:
@@ -23,7 +23,7 @@ from quantplay.exception.exceptions import (
23
23
  TokenErrorCode,
24
24
  TokenException,
25
25
  )
26
- from quantplay.utils.caching import InstrumentCache
26
+ from quantplay.utils.caching import InstrumentCache, pickle_version
27
27
  from quantplay.utils.pickle_utils import PickleUtils
28
28
 
29
29
  BrokerType = (
@@ -641,49 +641,63 @@ class BrokerFactory:
641
641
  raise InvalidArgumentException("Invalid broker API configuration")
642
642
 
643
643
  def set_broker_instruments(self, broker_name: str, broker: BrokerType) -> BrokerType:
644
+ file_name = broker_instruments_map[broker_name]
644
645
  symbol_data_key = f"{broker_name}_instruments"
645
646
  quantplay_symbol_key = f"{broker_name}_qplay_symbols"
646
647
  broker_symbol_key = f"{broker_name}_broker_symbols"
647
648
 
648
- symbol_data = instrument_cache.get(symbol_data_key)
649
- quantplay_symbol_map = instrument_cache.get(quantplay_symbol_key)
650
- broker_symbol_map = instrument_cache.get(broker_symbol_key)
649
+ # One load per broker at a time: clients built meanwhile wait, then reuse it.
650
+ with instrument_cache.lock(broker_name):
651
+ instrument_cache.forget_if_changed(
652
+ broker_name,
653
+ file_name,
654
+ (symbol_data_key, quantplay_symbol_key, broker_symbol_key),
655
+ )
651
656
 
652
- if symbol_data is not None:
653
- broker.symbol_data = symbol_data
657
+ symbol_data = instrument_cache.get(symbol_data_key)
658
+ quantplay_symbol_map = instrument_cache.get(quantplay_symbol_key)
659
+ broker_symbol_map = instrument_cache.get(broker_symbol_key)
654
660
 
655
- if broker_name != "Zerodha":
656
- if quantplay_symbol_map is not None and broker_symbol_map is not None:
657
- broker.quantplay_symbol_map = quantplay_symbol_map
658
- broker.broker_symbol_map = broker_symbol_map
661
+ if symbol_data is not None:
662
+ broker.symbol_data = symbol_data
659
663
 
660
- else:
664
+ if broker_name != "Zerodha":
665
+ if quantplay_symbol_map is not None and broker_symbol_map is not None:
666
+ broker.quantplay_symbol_map = quantplay_symbol_map
667
+ broker.broker_symbol_map = broker_symbol_map
668
+
669
+ else:
670
+ broker.initialize_broker_symbol_map()
671
+ instrument_cache.set(
672
+ quantplay_symbol_key, broker.quantplay_symbol_map
673
+ )
674
+ instrument_cache.set(broker_symbol_key, broker.broker_symbol_map)
675
+
676
+ return broker
677
+
678
+ try:
679
+ version = pickle_version(file_name)
680
+ symbol_data = PickleUtils.load_data(file_name)
681
+ broker.symbol_data = symbol_data
682
+
683
+ if broker_name != "Zerodha":
661
684
  broker.initialize_broker_symbol_map()
662
685
  instrument_cache.set(
663
686
  quantplay_symbol_key, broker.quantplay_symbol_map
664
687
  )
665
688
  instrument_cache.set(broker_symbol_key, broker.broker_symbol_map)
666
689
 
667
- return broker
690
+ instrument_cache.set(symbol_data_key, symbol_data)
691
+ instrument_cache.versions[broker_name] = version
668
692
 
669
- try:
670
- symbol_data = PickleUtils.load_data(broker_instruments_map[broker_name])
671
- broker.symbol_data = symbol_data
693
+ except Exception as e:
694
+ if not (isinstance(e, FileNotFoundError)):
695
+ traceback.print_exc()
672
696
 
673
- if broker_name != "Zerodha":
674
- broker.initialize_broker_symbol_map()
675
- instrument_cache.set(quantplay_symbol_key, broker.quantplay_symbol_map)
676
-
677
- instrument_cache.set(symbol_data_key, symbol_data)
678
-
679
- except Exception as e:
680
- if not (isinstance(e, FileNotFoundError)):
681
- traceback.print_exc()
682
-
683
- if broker_name != "Zerodha":
684
- broker.load_instrument(broker_instruments_map[broker_name])
685
- else:
686
- broker.initialize_symbol_data()
697
+ if broker_name != "Zerodha":
698
+ broker.load_instrument(file_name)
699
+ else:
700
+ broker.initialize_symbol_data()
687
701
 
688
702
  return broker
689
703
 
@@ -0,0 +1,55 @@
1
+ import os
2
+ from collections.abc import Hashable
3
+ from threading import Lock
4
+ from typing import Any
5
+
6
+
7
+ def pickle_version(file_name: str) -> int | None:
8
+ try:
9
+ return os.stat(f"/tmp/{file_name}.pickle").st_mtime_ns
10
+ except FileNotFoundError:
11
+ return None
12
+
13
+
14
+ class InstrumentCache:
15
+ """Instrument data shared by every broker client in the process.
16
+
17
+ Entries stay until the pickle they were read from changes on disk. The cache used to
18
+ be wiped every 5 minutes, so the next client of each broker reloaded the instruments
19
+ and rebuilt its symbol maps inside the request, and every request that missed at the
20
+ same moment repeated that work. On Backbone that took longer than its 25 s timeout.
21
+ """
22
+
23
+ def __init__(self) -> None:
24
+ self.cache: dict[Hashable, Any] = {}
25
+ self.versions: dict[str, int | None] = {}
26
+ self._locks: dict[str, Lock] = {}
27
+ self._locks_guard = Lock()
28
+
29
+ def set(self, key: str, data: Any) -> None:
30
+ self.cache[key] = data
31
+
32
+ def get(self, key: str) -> Any | None:
33
+ return self.cache.get(key, None)
34
+
35
+ def delete(self, key: str) -> None:
36
+ del self.cache[key]
37
+
38
+ def clear(self) -> None:
39
+ self.cache = {}
40
+ self.versions = {}
41
+
42
+ def lock(self, group: str) -> Lock:
43
+ with self._locks_guard:
44
+ return self._locks.setdefault(group, Lock())
45
+
46
+ def forget_if_changed(
47
+ self, group: str, file_name: str, keys: tuple[str, ...]
48
+ ) -> None:
49
+ if group not in self.versions:
50
+ return
51
+ if self.versions[group] == pickle_version(file_name):
52
+ return
53
+ for key in keys:
54
+ self.cache.pop(key, None)
55
+ del self.versions[group]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: quantplay
3
- Version: 2.1.144
3
+ Version: 2.1.145
4
4
  Summary: This python package will be stored in AWS CodeArtifact
5
5
  Home-page:
6
6
  Author:
@@ -86,6 +86,7 @@ tests/conftest.py
86
86
  tests/test_aliceblue_unified.py
87
87
  tests/test_deltax_positions.py
88
88
  tests/test_deltax_root_url.py
89
+ tests/test_instrument_cache.py
89
90
  tests/test_jainam_xts.py
90
91
  tests/test_kotak_order_feed_url.py
91
92
  tests/test_kotak_timeouts.py
@@ -21,7 +21,7 @@ requirements = [
21
21
  setup(
22
22
  name="quantplay",
23
23
  long_description=Path("README.md").read_text(),
24
- version="2.1.144",
24
+ version="2.1.145",
25
25
  install_requires=requirements,
26
26
  tests_require=[],
27
27
  packages=find_packages(),
@@ -0,0 +1,146 @@
1
+ """Instrument data is loaded once per process and reused until its pickle changes.
2
+
3
+ The cache used to be wiped every 5 minutes. Each wipe made the next client of every
4
+ broker reload and rebuild its instruments inside the request, and concurrent requests all
5
+ did it at once. On 2026-11-29 that held two Zerodha accounts on Backbone past the 25 s
6
+ client timeout for over a minute.
7
+ """
8
+
9
+ import os
10
+ import threading
11
+ import time
12
+ import uuid
13
+ from typing import Any
14
+
15
+ import pytest
16
+
17
+ from quantplay.broker import broker_factory as factory_module
18
+ from quantplay.broker.broker_factory import (
19
+ BrokerFactory,
20
+ broker_instruments_map,
21
+ instrument_cache,
22
+ )
23
+ from quantplay.utils.caching import InstrumentCache
24
+ from quantplay.utils.pickle_utils import PickleUtils
25
+
26
+ OLD = {"NFO:OLD": {"broker_symbol": "OLD-B", "tradingsymbol": "OLD"}}
27
+ NEW = {"NFO:NEW": {"broker_symbol": "NEW-B", "tradingsymbol": "NEW"}}
28
+
29
+
30
+ class FakeBroker:
31
+ map_builds = 0
32
+
33
+ def initialize_broker_symbol_map(self) -> None:
34
+ FakeBroker.map_builds += 1
35
+ self.broker_symbol_map = {
36
+ v["broker_symbol"]: v["tradingsymbol"] for v in self.symbol_data.values()
37
+ }
38
+ self.quantplay_symbol_map = {
39
+ v["tradingsymbol"]: v["broker_symbol"] for v in self.symbol_data.values()
40
+ }
41
+
42
+ def load_instrument(self, file_name: str) -> None:
43
+ raise AssertionError("pickle exists, the S3 fallback must not run")
44
+
45
+ def initialize_symbol_data(self) -> None:
46
+ raise AssertionError("pickle exists, the Kite download must not run")
47
+
48
+
49
+ @pytest.fixture
50
+ def pickle_file(monkeypatch: pytest.MonkeyPatch):
51
+ name = f"test_instruments_{uuid.uuid4().hex[:10]}"
52
+ for broker in ("Zerodha", "Jainam"):
53
+ monkeypatch.setitem(broker_instruments_map, broker, name)
54
+ instrument_cache.clear()
55
+ FakeBroker.map_builds = 0
56
+ yield name
57
+ instrument_cache.clear()
58
+ path = f"/tmp/{name}.pickle"
59
+ if os.path.exists(path):
60
+ os.remove(path)
61
+
62
+
63
+ @pytest.fixture
64
+ def load_count(monkeypatch: pytest.MonkeyPatch) -> list[int]:
65
+ calls = [0]
66
+ real = PickleUtils.load_data
67
+
68
+ def counting(file_name: str) -> Any:
69
+ calls[0] += 1
70
+ time.sleep(0.05)
71
+ return real(file_name)
72
+
73
+ monkeypatch.setattr(factory_module.PickleUtils, "load_data", staticmethod(counting))
74
+ return calls
75
+
76
+
77
+ def build(broker_name: str) -> FakeBroker:
78
+ broker = FakeBroker()
79
+ BrokerFactory().set_broker_instruments(broker_name, broker) # type: ignore[arg-type]
80
+ return broker
81
+
82
+
83
+ def test_unchanged_pickle_is_read_once(pickle_file: str, load_count: list[int]) -> None:
84
+ PickleUtils.save_data(OLD, pickle_file)
85
+
86
+ for _ in range(5):
87
+ assert build("Zerodha").symbol_data == OLD
88
+
89
+ assert load_count[0] == 1
90
+
91
+
92
+ def test_concurrent_cold_builds_share_one_load(
93
+ pickle_file: str, load_count: list[int]
94
+ ) -> None:
95
+ PickleUtils.save_data(OLD, pickle_file)
96
+ brokers: list[FakeBroker] = []
97
+
98
+ def worker() -> None:
99
+ brokers.append(build("Jainam"))
100
+
101
+ threads = [threading.Thread(target=worker) for _ in range(8)]
102
+ for t in threads:
103
+ t.start()
104
+ for t in threads:
105
+ t.join()
106
+
107
+ assert load_count[0] == 1
108
+ assert FakeBroker.map_builds == 1
109
+ assert len(brokers) == 8 and all(
110
+ b.broker_symbol_map == {"OLD-B": "OLD"} for b in brokers
111
+ )
112
+
113
+
114
+ def test_rewritten_pickle_is_picked_up(pickle_file: str, load_count: list[int]) -> None:
115
+ PickleUtils.save_data(OLD, pickle_file)
116
+ assert build("Jainam").symbol_data == OLD
117
+
118
+ PickleUtils.save_data(NEW, pickle_file)
119
+ stat = os.stat(f"/tmp/{pickle_file}.pickle")
120
+ os.utime(
121
+ f"/tmp/{pickle_file}.pickle",
122
+ ns=(stat.st_atime_ns, stat.st_mtime_ns + 1_000_000_000),
123
+ )
124
+
125
+ rebuilt = build("Jainam")
126
+ assert rebuilt.symbol_data == NEW
127
+ assert rebuilt.broker_symbol_map == {"NEW-B": "NEW"}
128
+ assert load_count[0] == 2
129
+
130
+
131
+ def test_cold_load_caches_both_symbol_maps(
132
+ pickle_file: str, load_count: list[int]
133
+ ) -> None:
134
+ PickleUtils.save_data(OLD, pickle_file)
135
+
136
+ build("Jainam")
137
+ second = build("Jainam")
138
+
139
+ assert FakeBroker.map_builds == 1
140
+ assert second.quantplay_symbol_map == {"OLD": "OLD-B"}
141
+
142
+
143
+ def test_cache_starts_no_background_wipe() -> None:
144
+ before = threading.active_count()
145
+ InstrumentCache()
146
+ assert threading.active_count() == before
@@ -1,35 +0,0 @@
1
- from threading import Event, Thread
2
- from time import sleep
3
- from typing import Any, Hashable
4
-
5
-
6
- class InstrumentCache:
7
- def __init__(self) -> None:
8
- self.cache: dict[Hashable, Any] = {}
9
- self.ttl = 300
10
-
11
- self.stop_event = Event()
12
- self.stop_event.set()
13
-
14
- clear_thread = Thread(target=self.auto_clear, daemon=True)
15
- clear_thread.start()
16
-
17
- def __del__(self) -> None:
18
- self.stop_event.clear()
19
-
20
- def set(self, key: str, data: Any) -> None:
21
- self.cache[key] = data
22
-
23
- def get(self, key: str) -> Any | None:
24
- return self.cache.get(key, None)
25
-
26
- def delete(self, key: str) -> None:
27
- del self.cache[key]
28
-
29
- def clear(self) -> None:
30
- self.cache = {}
31
-
32
- def auto_clear(self) -> None:
33
- while self.stop_event.is_set():
34
- sleep(self.ttl)
35
- self.clear()
File without changes
File without changes
File without changes