quantplay 2.1.142__tar.gz → 2.1.144__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 (102) hide show
  1. {quantplay-2.1.142 → quantplay-2.1.144}/PKG-INFO +1 -1
  2. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/kotak.py +36 -15
  3. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/kotak_utils/kotak_ws.py +9 -2
  4. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/kotak_utils/kotak_ws_lib.py +28 -21
  5. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay.egg-info/PKG-INFO +1 -1
  6. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay.egg-info/SOURCES.txt +2 -0
  7. {quantplay-2.1.142 → quantplay-2.1.144}/setup.py +1 -1
  8. quantplay-2.1.144/tests/test_kotak_timeouts.py +69 -0
  9. quantplay-2.1.144/tests/test_kotak_ws_per_account.py +98 -0
  10. {quantplay-2.1.142 → quantplay-2.1.144}/README.md +0 -0
  11. {quantplay-2.1.142 → quantplay-2.1.144}/pyproject.toml +0 -0
  12. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/__init__.py +0 -0
  13. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/__init__.py +0 -0
  14. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/aliceblue.py +0 -0
  15. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/aliceblue_utils/__init__.py +0 -0
  16. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/aliceblue_utils/aliceblue_oauth.py +0 -0
  17. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/aliceblue_v2.py +0 -0
  18. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/angelone.py +0 -0
  19. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/angelone_utils/__init__.py +0 -0
  20. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/angelone_utils/angeloneWS.py +0 -0
  21. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/auto_login/__init__.py +0 -0
  22. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/breeze/__init__.py +0 -0
  23. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/breeze/breeze_utils.py +0 -0
  24. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/broker_factory.py +0 -0
  25. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/dhan.py +0 -0
  26. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/finvasia_utils/__init__.py +0 -0
  27. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/finvasia_utils/fa_noren.py +0 -0
  28. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/finvasia_utils/finvasia_oauth.py +0 -0
  29. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/five_paisa.py +0 -0
  30. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/flattrade.py +0 -0
  31. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/ft_utils/__init__.py +0 -0
  32. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/ft_utils/flattrade_utils.py +0 -0
  33. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/ft_utils/ft_noren.py +0 -0
  34. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/generics/__init__.py +0 -0
  35. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/generics/broker.py +0 -0
  36. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/icici_direct.py +0 -0
  37. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/iifl_xts.py +0 -0
  38. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/jainam_xts.py +0 -0
  39. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/kite_utils.py +0 -0
  40. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/kotak_utils/__init__.py +0 -0
  41. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/motilal.py +0 -0
  42. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/noren.py +0 -0
  43. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/shoonya.py +0 -0
  44. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/uplink/__init__.py +0 -0
  45. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/uplink/uplink_utils.py +0 -0
  46. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/upstox.py +0 -0
  47. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/xts.py +0 -0
  48. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/xts_utils/Connect.py +0 -0
  49. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/xts_utils/Exception.py +0 -0
  50. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/xts_utils/InteractiveSocketClient.py +0 -0
  51. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/xts_utils/__init__.py +0 -0
  52. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/xts_utils_v2/ConnectV2.py +0 -0
  53. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/xts_utils_v2/InteractiveSocketClientV2.py +0 -0
  54. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/xts_utils_v2/__init__.py +0 -0
  55. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/broker/zerodha.py +0 -0
  56. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/core/__init__.py +0 -0
  57. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/core/strategy.py +0 -0
  58. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/exception/__init__.py +0 -0
  59. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/exception/exceptions.py +0 -0
  60. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/indicator/__init__.py +0 -0
  61. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/indicator/iv.py +0 -0
  62. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/model/__init__.py +0 -0
  63. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/model/broker.py +0 -0
  64. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/model/broker_response.py +0 -0
  65. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/model/generics.py +0 -0
  66. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/model/instrument_data.py +0 -0
  67. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/model/order_event.py +0 -0
  68. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/py.typed +0 -0
  69. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/strategy/__init__.py +0 -0
  70. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/strategy/iv_spike.py +0 -0
  71. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/strategy/obuy.py +0 -0
  72. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/utils/__init__.py +0 -0
  73. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/utils/caching.py +0 -0
  74. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/utils/constant.py +0 -0
  75. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/utils/exchange.py +0 -0
  76. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/utils/number_utils.py +0 -0
  77. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/utils/pickle_utils.py +0 -0
  78. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/utils/redaction.py +0 -0
  79. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/utils/selenium_utils.py +0 -0
  80. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/wrapper/__init__.py +0 -0
  81. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/wrapper/aws/__init__.py +0 -0
  82. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/wrapper/aws/s3.py +0 -0
  83. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay/wrapper/redis.py +0 -0
  84. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay.egg-info/dependency_links.txt +0 -0
  85. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay.egg-info/requires.txt +0 -0
  86. {quantplay-2.1.142 → quantplay-2.1.144}/quantplay.egg-info/top_level.txt +0 -0
  87. {quantplay-2.1.142 → quantplay-2.1.144}/setup.cfg +0 -0
  88. {quantplay-2.1.142 → quantplay-2.1.144}/tests/__init__.py +0 -0
  89. {quantplay-2.1.142 → quantplay-2.1.144}/tests/conftest.py +0 -0
  90. {quantplay-2.1.142 → quantplay-2.1.144}/tests/test_aliceblue_unified.py +0 -0
  91. {quantplay-2.1.142 → quantplay-2.1.144}/tests/test_deltax_positions.py +0 -0
  92. {quantplay-2.1.142 → quantplay-2.1.144}/tests/test_deltax_root_url.py +0 -0
  93. {quantplay-2.1.142 → quantplay-2.1.144}/tests/test_jainam_xts.py +0 -0
  94. {quantplay-2.1.142 → quantplay-2.1.144}/tests/test_kotak_order_feed_url.py +0 -0
  95. {quantplay-2.1.142 → quantplay-2.1.144}/tests/test_log_redaction.py +0 -0
  96. {quantplay-2.1.142 → quantplay-2.1.144}/tests/test_motilal_login.py +0 -0
  97. {quantplay-2.1.142 → quantplay-2.1.144}/tests/test_sub_client_all_types.py +0 -0
  98. {quantplay-2.1.142 → quantplay-2.1.144}/tests/test_xts_v2_modify_order.py +0 -0
  99. {quantplay-2.1.142 → quantplay-2.1.144}/tests/test_zerodha_positions_add_ltp.py +0 -0
  100. {quantplay-2.1.142 → quantplay-2.1.144}/tests/wrapper/__init__.py +0 -0
  101. {quantplay-2.1.142 → quantplay-2.1.144}/tests/wrapper/aws/__init__.py +0 -0
  102. {quantplay-2.1.142 → quantplay-2.1.144}/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.142
3
+ Version: 2.1.144
4
4
  Summary: This python package will be stored in AWS CodeArtifact
5
5
  Home-page:
6
6
  Author:
@@ -54,6 +54,12 @@ def _login_error_code(response_text: str) -> TokenErrorCode | None:
54
54
  return None
55
55
 
56
56
 
57
+ # (connect, read) seconds. Reads were unbounded and hung Backbone for 60-120 s on
58
+ # 2026-11-08; order writes keep no read limit (see request()).
59
+ KOTAK_READ_TIMEOUT: tuple[float, float] = (5, 15)
60
+ KOTAK_WRITE_TIMEOUT: tuple[float, None] = (5, None)
61
+
62
+
57
63
  class Kotak(Broker):
58
64
  """
59
65
  Kotak Neo V2 API Implementation
@@ -967,24 +973,39 @@ class Kotak(Broker):
967
973
  "exit_bracket_order",
968
974
  }
969
975
  request_proxies = self.proxies if item in proxy_required else None
976
+ # An order write only bounds the connect: once Kotak has the request, timing out
977
+ # would leave the outcome unknown, and a caller's retry could place it twice.
978
+ timeout = KOTAK_WRITE_TIMEOUT if item in proxy_required else KOTAK_READ_TIMEOUT
970
979
 
971
- if method == "GET":
972
- resp = requests.get(
973
- url=url,
974
- params=request_params,
975
- headers=request_headers,
976
- proxies=request_proxies,
977
- )
980
+ try:
981
+ if method == "GET":
982
+ resp = requests.get(
983
+ url=url,
984
+ params=request_params,
985
+ headers=request_headers,
986
+ proxies=request_proxies,
987
+ timeout=timeout,
988
+ )
978
989
 
979
- elif method == "POST":
980
- url += "?" + urlencode(query_params)
990
+ elif method == "POST":
991
+ url += "?" + urlencode(query_params)
981
992
 
982
- resp = requests.post(
983
- url,
984
- headers=request_headers,
985
- data=request_body, # type:ignore
986
- proxies=request_proxies,
987
- )
993
+ resp = requests.post(
994
+ url,
995
+ headers=request_headers,
996
+ data=request_body, # type:ignore
997
+ proxies=request_proxies,
998
+ timeout=timeout,
999
+ )
1000
+ except requests.exceptions.ConnectTimeout as e:
1001
+ raise BrokerException(
1002
+ f"Kotak did not accept a connection within {timeout[0]} s. "
1003
+ "Nothing was sent. Please try again."
1004
+ ) from e
1005
+ except requests.exceptions.ReadTimeout as e:
1006
+ raise BrokerException(
1007
+ f"Kotak did not respond within {timeout[1]} s. Please try again."
1008
+ ) from e
988
1009
 
989
1010
  if resp and resp.ok:
990
1011
  resp_data = resp.json()
@@ -58,6 +58,7 @@ class NeoWebSocket:
58
58
  self.on_close: Callable[[], None] | None = None
59
59
  self.on_open: Callable[[], None] | None = None
60
60
  self.hsi_thread = None
61
+ self.ping_thread: threading.Thread | None = None
61
62
 
62
63
  def on_hsi_open(self) -> None:
63
64
  server = "WEB"
@@ -84,7 +85,9 @@ class NeoWebSocket:
84
85
  if self.on_close:
85
86
  self.on_close()
86
87
 
87
- self.reconnect()
88
+ # No reconnect() here: run_forever(reconnect=5) already reconnects this socket.
89
+ # Doing both started a second socket per drop, and the bursts got 403s from
90
+ # Kotak's gateway.
88
91
 
89
92
  @retry(
90
93
  wait_exponential_multiplier=3000,
@@ -110,7 +113,11 @@ class NeoWebSocket:
110
113
  req = json.loads(message)
111
114
  if req["type"] == "cn":
112
115
  self.is_hsi_open = 1
113
- threading.Thread(target=self.start_hsi_ping_thread).start()
116
+ if self.ping_thread is None or not self.ping_thread.is_alive():
117
+ self.ping_thread = threading.Thread(
118
+ target=self.start_hsi_ping_thread, daemon=True
119
+ )
120
+ self.ping_thread.start()
114
121
 
115
122
  if self.on_message:
116
123
  self.on_message({"type": "order_feed", "data": message})
@@ -22,25 +22,29 @@ class StartHSIServer:
22
22
  self.onmessage = onmessage
23
23
  self.onerror = onerror
24
24
  self.onclose = onclose
25
- # self.token, self.sid = token, sid
26
- global hsiWs
25
+ # One socket per instance. This used to be a module-level `global hsiWs`
26
+ # (copied from Kotak's single-user client), so with several Kotak accounts in
27
+ # one process every login frame and heartbeat went to whichever account
28
+ # connected last, and closing one account closed another's feed.
29
+ self.ws = websocket.WebSocketApp(
30
+ self.url,
31
+ on_open=self.on_open,
32
+ on_message=self.on_message,
33
+ on_error=self.on_error,
34
+ on_close=self.on_close,
35
+ )
36
+
37
+ def run(self) -> None:
27
38
  try:
28
- # websocket.enableTrace(True)
29
- hsiWs = websocket.WebSocketApp(
30
- self.url,
31
- on_open=self.on_open,
32
- on_message=self.on_message,
33
- on_error=self.on_error,
34
- on_close=self.on_close,
35
- )
36
- hsiWs.run_forever( # type: ignore
39
+ # reconnect=5 is the only reconnect path: it re-runs on_open, which re-sends
40
+ # this account's CONNECTION frame on this account's socket.
41
+ self.ws.run_forever( # type: ignore
37
42
  ping_interval=5, reconnect=5, sslopt={"cert_reqs": ssl.CERT_NONE}
38
43
  )
39
44
  except Exception:
40
45
  print("WebSocket not supported!")
41
46
 
42
47
  def on_message(self, ws: websocket.WebSocket, message: Any) -> None:
43
- # print("Received message:", message)
44
48
  self.onmessage(message)
45
49
 
46
50
  def on_error(self, ws: websocket.WebSocket, error: Any) -> None:
@@ -50,15 +54,11 @@ class StartHSIServer:
50
54
  def on_close(
51
55
  self, ws: websocket.WebSocket, close_status_code: Any, close_msg: Any
52
56
  ) -> None:
53
- # print("Connection closed")
54
57
  self.openState = 0
55
58
  self.readyState = 0
56
- if hsiWs:
57
- hsiWs.close() # type: ignore
58
59
  self.onclose()
59
60
 
60
61
  def on_open(self, ws: websocket.WebSocket) -> None:
61
- # print("Connection established HSWebSocket")
62
62
  self.openState = 1
63
63
  self.readyState = 1
64
64
  self.onopen()
@@ -67,6 +67,7 @@ class StartHSIServer:
67
67
  class HSIWebSocket:
68
68
  def __init__(self) -> None:
69
69
  self.hsiSocket = None
70
+ self.server: StartHSIServer | None = None
70
71
  self.reqData = None
71
72
  self.openState = 0
72
73
  self.readyState = 0
@@ -89,7 +90,10 @@ class HSIWebSocket:
89
90
  self.onmessage = onmessage
90
91
  self.onclose = onclose
91
92
  self.onerror = onerror
92
- StartHSIServer(self.url, self.onopen, self.onmessage, self.onerror, self.onclose)
93
+ self.server = StartHSIServer(
94
+ self.url, self.onopen, self.onmessage, self.onerror, self.onclose
95
+ )
96
+ self.server.run()
93
97
 
94
98
  def send(self, d: str) -> None:
95
99
  reqJson = json.loads(d)
@@ -129,10 +133,13 @@ class HSIWebSocket:
129
133
  else:
130
134
  print("Invalid Request !")
131
135
 
132
- if hsiWs and req:
136
+ if self.server and req:
133
137
  js_obj = json.dumps(req)
134
138
  js_obj = str(js_obj).replace('"', "").replace(" ", "")
135
- hsiWs.send(js_obj)
139
+ try:
140
+ self.server.ws.send(js_obj)
141
+ except websocket.WebSocketException as e:
142
+ print(f"Unable to send request! Reason: {e}")
136
143
  else:
137
144
  print(
138
145
  "Unable to send request! Reason: Connection faulty or request not valid!"
@@ -142,5 +149,5 @@ class HSIWebSocket:
142
149
  self.openState = 0
143
150
  self.readyState = 0
144
151
 
145
- if hsiWs:
146
- hsiWs.close() # type: ignore
152
+ if self.server:
153
+ self.server.ws.close()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: quantplay
3
- Version: 2.1.142
3
+ Version: 2.1.144
4
4
  Summary: This python package will be stored in AWS CodeArtifact
5
5
  Home-page:
6
6
  Author:
@@ -88,6 +88,8 @@ tests/test_deltax_positions.py
88
88
  tests/test_deltax_root_url.py
89
89
  tests/test_jainam_xts.py
90
90
  tests/test_kotak_order_feed_url.py
91
+ tests/test_kotak_timeouts.py
92
+ tests/test_kotak_ws_per_account.py
91
93
  tests/test_log_redaction.py
92
94
  tests/test_motilal_login.py
93
95
  tests/test_sub_client_all_types.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.142",
24
+ version="2.1.144",
25
25
  install_requires=requirements,
26
26
  tests_require=[],
27
27
  packages=find_packages(),
@@ -0,0 +1,69 @@
1
+ """Kotak HTTP calls are time-limited; order writes only bound the connect.
2
+
3
+ Unbounded Kotak reads hung Backbone for 60-120 s on 2026-11-08. A read limit on an
4
+ order write would leave the outcome unknown, and a retry could place it twice.
5
+ """
6
+
7
+ from typing import Any
8
+
9
+ import pytest
10
+ import requests
11
+
12
+ from quantplay.broker import kotak as kotak_module
13
+ from quantplay.broker.kotak import KOTAK_READ_TIMEOUT, KOTAK_WRITE_TIMEOUT, Kotak
14
+ from quantplay.exception.exceptions import BrokerException
15
+
16
+
17
+ def _client() -> Kotak:
18
+ client = Kotak.__new__(Kotak)
19
+ client.base_url = "https://kotak.example"
20
+ client.configuration = {"Sid": "s", "Auth": "a", "access_token": "t"}
21
+ client.proxies = None
22
+ return client
23
+
24
+
25
+ class _Ok:
26
+ ok = True
27
+
28
+ def json(self) -> dict[str, Any]:
29
+ return {"stat": "Ok"}
30
+
31
+
32
+ def _capture(monkeypatch: pytest.MonkeyPatch, raises: Exception | None = None) -> list[Any]:
33
+ seen: list[Any] = []
34
+
35
+ def fake(*args: Any, **kwargs: Any) -> _Ok:
36
+ seen.append(kwargs.get("timeout"))
37
+ if raises is not None:
38
+ raise raises
39
+ return _Ok()
40
+
41
+ monkeypatch.setattr(kotak_module.requests, "get", fake)
42
+ monkeypatch.setattr(kotak_module.requests, "post", fake)
43
+ return seen
44
+
45
+
46
+ @pytest.mark.parametrize("item", ["positions", "order_book", "holdings", "limits"])
47
+ def test_reads_are_time_limited(monkeypatch: pytest.MonkeyPatch, item: str) -> None:
48
+ seen = _capture(monkeypatch)
49
+ _client().request(item) # type: ignore[arg-type]
50
+ assert seen == [KOTAK_READ_TIMEOUT] and KOTAK_READ_TIMEOUT[1] is not None
51
+
52
+
53
+ @pytest.mark.parametrize("item", ["place_order", "modify_order", "cancel_order"])
54
+ def test_order_writes_only_bound_the_connect(monkeypatch: pytest.MonkeyPatch, item: str) -> None:
55
+ seen = _capture(monkeypatch)
56
+ _client().request(item, body={}) # type: ignore[arg-type]
57
+ assert seen == [KOTAK_WRITE_TIMEOUT] and KOTAK_WRITE_TIMEOUT[1] is None
58
+
59
+
60
+ def test_read_timeout_is_a_readable_broker_error(monkeypatch: pytest.MonkeyPatch) -> None:
61
+ _capture(monkeypatch, raises=requests.exceptions.ReadTimeout())
62
+ with pytest.raises(BrokerException, match="Kotak did not respond within 15 s"):
63
+ _client().request("positions")
64
+
65
+
66
+ def test_connect_timeout_says_nothing_was_sent(monkeypatch: pytest.MonkeyPatch) -> None:
67
+ _capture(monkeypatch, raises=requests.exceptions.ConnectTimeout())
68
+ with pytest.raises(BrokerException, match="Nothing was sent"):
69
+ _client().request("place_order", body={})
@@ -0,0 +1,98 @@
1
+ """Each Kotak account owns its order-feed socket.
2
+
3
+ PMS runs many Kotak accounts in one process. The library kept one module-level socket,
4
+ so every account's login frame went to whichever account connected last (16 x 403 within
5
+ a minute of PMS starting on 2026-11-09).
6
+ """
7
+
8
+ import json
9
+ from typing import Any
10
+
11
+ import pytest
12
+ import websocket
13
+
14
+ from quantplay.broker.kotak_utils import kotak_ws, kotak_ws_lib
15
+ from quantplay.broker.kotak_utils.kotak_ws import NeoWebSocket
16
+ from quantplay.broker.kotak_utils.kotak_ws_lib import HSIWebSocket
17
+
18
+
19
+ class _FakeApp:
20
+ def __init__(self, url: str, **handlers: Any) -> None:
21
+ self.url, self.handlers, self.sent, self.closed = url, handlers, [], False
22
+
23
+ def run_forever(self, **kwargs: Any) -> None:
24
+ self.handlers["on_open"](self)
25
+
26
+ def send(self, data: str) -> None:
27
+ if self.closed:
28
+ raise websocket.WebSocketConnectionClosedException("socket is already closed.")
29
+ self.sent.append(data)
30
+
31
+ def close(self) -> None:
32
+ self.closed = True
33
+
34
+
35
+ @pytest.fixture(autouse=True)
36
+ def fake_app(monkeypatch: pytest.MonkeyPatch) -> None:
37
+ monkeypatch.setattr(kotak_ws_lib.websocket, "WebSocketApp", _FakeApp)
38
+
39
+
40
+ def _account(sid: str) -> NeoWebSocket:
41
+ ws = NeoWebSocket(sid=sid, token=f"token-{sid}", server_id=None, data_center=None)
42
+ ws.start_hsi_websocket()
43
+ return ws
44
+
45
+
46
+ def test_each_account_sends_its_login_on_its_own_socket() -> None:
47
+ a, b = _account("A"), _account("B")
48
+ sent_a = a.hsiWebsocket.server.ws.sent # type: ignore[union-attr]
49
+ sent_b = b.hsiWebsocket.server.ws.sent # type: ignore[union-attr]
50
+ assert len(sent_a) == 1 and "Sid:A" in sent_a[0]
51
+ assert len(sent_b) == 1 and "Sid:B" in sent_b[0]
52
+
53
+
54
+ def test_closing_one_account_leaves_the_other_open() -> None:
55
+ a, b = _account("A"), _account("B")
56
+ a.hsiWebsocket.close() # type: ignore[union-attr]
57
+ assert a.hsiWebsocket.server.ws.closed # type: ignore[union-attr]
58
+ assert not b.hsiWebsocket.server.ws.closed # type: ignore[union-attr]
59
+
60
+
61
+ def test_a_drop_does_not_start_a_second_socket(monkeypatch: pytest.MonkeyPatch) -> None:
62
+ a = _account("A")
63
+ started: list[int] = []
64
+ monkeypatch.setattr(a, "start_hsi_websocket_thread", lambda: started.append(1))
65
+ a.on_hsi_close()
66
+ assert started == []
67
+
68
+
69
+ def test_repeated_login_acks_keep_one_heartbeat_thread(monkeypatch: pytest.MonkeyPatch) -> None:
70
+ monkeypatch.setattr(kotak_ws.time, "sleep", lambda s: None)
71
+ a = _account("A")
72
+ threads: list[Any] = []
73
+
74
+ class _Thread:
75
+ def __init__(self, target: Any, daemon: bool = False) -> None:
76
+ threads.append(self)
77
+
78
+ def start(self) -> None:
79
+ pass
80
+
81
+ def is_alive(self) -> bool:
82
+ return True
83
+
84
+ monkeypatch.setattr(kotak_ws.threading, "Thread", _Thread)
85
+ for _ in range(3):
86
+ a.on_hsi_message(json.dumps({"type": "cn"}))
87
+ assert len(threads) == 1
88
+
89
+
90
+ def test_heartbeat_on_a_closed_socket_does_not_raise() -> None:
91
+ a = _account("A")
92
+ a.hsiWebsocket.close() # type: ignore[union-attr]
93
+ a.hsiWebsocket.send(json.dumps({"type": "HB"})) # type: ignore[union-attr]
94
+
95
+
96
+ def test_library_has_no_shared_socket() -> None:
97
+ assert not hasattr(kotak_ws_lib, "hsiWs")
98
+ assert HSIWebSocket().server is None
File without changes
File without changes
File without changes