test_bitstamp_nonce.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from __future__ import annotations
  2. from exec_mcp import bitstamp
  3. def test_next_nonce_advances_within_same_millisecond(monkeypatch):
  4. bitstamp._LAST_NONCE_BY_SCOPE.clear()
  5. monkeypatch.setattr(bitstamp, "_nonce_now_ms", lambda: 1_700_000_000_000)
  6. first = bitstamp._next_nonce("acct-a:key-a")
  7. second = bitstamp._next_nonce("acct-a:key-a")
  8. third = bitstamp._next_nonce("acct-a:key-a")
  9. assert first == 1_700_000_000_000
  10. assert second == 1_700_000_000_001
  11. assert third == 1_700_000_000_002
  12. def test_next_nonce_is_scoped_per_credentials(monkeypatch):
  13. bitstamp._LAST_NONCE_BY_SCOPE.clear()
  14. monkeypatch.setattr(bitstamp, "_nonce_now_ms", lambda: 1_700_000_000_000)
  15. first_a = bitstamp._next_nonce("acct-a:key-a")
  16. first_b = bitstamp._next_nonce("acct-b:key-b")
  17. second_a = bitstamp._next_nonce("acct-a:key-a")
  18. assert first_a == 1_700_000_000_000
  19. assert first_b == 1_700_000_000_000
  20. assert second_a == 1_700_000_000_001
  21. def test_lg_trading_instances_share_nonce_sequence(monkeypatch):
  22. bitstamp._LAST_NONCE_BY_SCOPE.clear()
  23. now_ms = [1_700_000_000_000]
  24. monkeypatch.setattr(bitstamp, "_nonce_now_ms", lambda: now_ms[0])
  25. first = bitstamp.LG_Trading.__new__(bitstamp.LG_Trading)
  26. first._nonce_scope = "acct-a:key-a"
  27. second = bitstamp.LG_Trading.__new__(bitstamp.LG_Trading)
  28. second._nonce_scope = "acct-a:key-a"
  29. nonce_1 = first.get_nonce()
  30. nonce_2 = second.get_nonce()
  31. now_ms[0] += 5
  32. nonce_3 = first.get_nonce()
  33. assert nonce_1 == 1_700_000_000_000
  34. assert nonce_2 == 1_700_000_000_001
  35. assert nonce_3 == 1_700_000_000_005