Skip to content

Commit 6beb94c

Browse files
committed
Message id len: 8 bytes
1 parent 8615a6d commit 6beb94c

File tree

7 files changed

+10
-36
lines changed

7 files changed

+10
-36
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ authors = [
1212
description = "Rembus for python"
1313
readme = "README.md"
1414
keywords = ["rembus", "rpc", "publish", "subscribe", "websocket", "cbor"]
15-
requires-python = ">=3.7"
15+
requires-python = ">=3.12"
1616
classifiers = [
1717
"Programming Language :: Python :: 3",
1818
"Operating System :: OS Independent",

src/rembus/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
QOSLevel,
1212
CBOR,
1313
JSON,
14+
MSGID_LEN,
1415
SIG_ECDSA,
1516
SIG_RSA
1617
)

src/rembus/protocol.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
CBOR = 0
3939
JSON = 1
4040

41+
MSGID_LEN = 8
4142

4243
class QOSLevel(IntEnum):
4344
"""The Pub/Sub message QOS level."""
@@ -97,26 +98,10 @@ class QOSLevel(IntEnum):
9798

9899

99100
def msgid():
100-
"""Return an array of 16 random bytes."""
101-
return int.from_bytes(os.urandom(16))
101+
"""Return an array of MSGID_LEN random bytes."""
102+
return int.from_bytes(os.urandom(MSGID_LEN))
102103

103104

104-
def bytes2id(byte_data: bytearray) -> int:
105-
"""
106-
Converts a 16-byte bytearray to a UInt128 integer.
107-
108-
Args:
109-
byte_data: A bytearray of 16 bytes.
110-
111-
Returns:
112-
A Python integer representing the UInt128 value.
113-
"""
114-
if len(byte_data) != 16:
115-
raise ValueError("bytearray must be exactly 16 bytes long")
116-
117-
# Convert bytes to integer (assuming little-endian, adjust if big-endian)
118-
return int.from_bytes(byte_data, byteorder='little', signed=False)
119-
120105

121106
class RembusException(Exception):
122107
"""Base class for all Rembus exceptions."""
@@ -151,8 +136,8 @@ def __str__(self):
151136

152137

153138
def to_bytes(val: int) -> bytes:
154-
"""Convert an int to 16 bytes"""
155-
return val.to_bytes(16)
139+
"""Convert an int to MSGID_LEN bytes"""
140+
return val.to_bytes(MSGID_LEN)
156141

157142

158143
class RembusMsg(BaseModel):

tests/api/test_errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ def test_response_no_data():
6565
mid = 1234
6666
# This is not an error, data is set by default to None
6767
rb._runner.run(rb._rb._send(cbor2.dumps(
68-
[rp.TYPE_RESPONSE, mid.to_bytes(16), rp.STS_OK])))
68+
[rp.TYPE_RESPONSE, mid.to_bytes(rp.MSGID_LEN), rp.STS_OK])))
6969
rb.close()

tests/unit/test_protocol.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@ def test_no_impl():
1717
msg.to_payload(enc=rp.CBOR)
1818

1919

20-
def test_bytes2id():
21-
"""Test the bytes2id function for converting byte arrays to IDs."""
22-
byte_data = bytearray(range(16)) # 0x00 to 0x0F
23-
result = rp.bytes2id(byte_data)
24-
assert isinstance(result, int)
25-
26-
with pytest.raises(ValueError):
27-
rp.bytes2id(bytearray(range(15)))
28-
29-
with pytest.raises(ValueError):
30-
rp.bytes2id(bytearray(range(17)))
31-
3220

3321
def test_types_str():
3422
"""Test the string representation of protocol types."""

tests/unit/test_publish_qos2_duplicate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async def test_publish(mocker, ws_mock):
5555
await rb.subscribe(mytopic)
5656

5757
# send two pubsub messages with the same msgid
58-
msgid = bytes([i for i in range(16)])
58+
msgid = bytes([i for i in range(rp.MSGID_LEN)])
5959
topic = mytopic.__name__
6060
for i in range(3):
6161
rb.outreq[int.from_bytes(msgid)] = rembus.core.FutureResponse(True)

tests/unit/test_unknown_msgid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async def test_unknown_message_id(mocker, ws_mock):
3636
assert mocked_connect.call_args[0][0] == "ws://127.0.0.1:8000/foo"
3737
assert rb.uid.id == 'foo'
3838
# send a response message with an unknown msgid
39-
msgid = bytes([i for i in range(16)])
39+
msgid = bytes([i for i in range(rp.MSGID_LEN)])
4040
req = rp.encode(
4141
[rp.TYPE_RESPONSE, msgid, rp.STS_OK, 'payload']
4242
)

0 commit comments

Comments
 (0)