-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from vladak/subscribe_vs_remaining_len
encode/decode remaining length properly for {,UN}SUBSCRIBE/SUBACK
- Loading branch information
Showing
4 changed files
with
499 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# SPDX-FileCopyrightText: 2023 Vladimír Kotal | ||
# | ||
# SPDX-License-Identifier: Unlicense | ||
|
||
"""fake socket class for protocol level testing""" | ||
|
||
from unittest import mock | ||
|
||
|
||
class Mocket: | ||
""" | ||
Mock Socket tailored for MiniMQTT testing. Records sent data, | ||
hands out pre-recorded reply. | ||
Inspired by the Mocket class from Adafruit_CircuitPython_Requests | ||
""" | ||
|
||
def __init__(self, to_send): | ||
self._to_send = to_send | ||
|
||
self.sent = bytearray() | ||
|
||
self.timeout = mock.Mock() | ||
self.connect = mock.Mock() | ||
self.close = mock.Mock() | ||
|
||
def send(self, bytes_to_send): | ||
"""merely record the bytes. return the length of this bytearray.""" | ||
self.sent.extend(bytes_to_send) | ||
return len(bytes_to_send) | ||
|
||
# MiniMQTT checks for the presence of "recv_into" and switches behavior based on that. | ||
def recv_into(self, retbuf, bufsize): | ||
"""return data from internal buffer""" | ||
size = min(bufsize, len(self._to_send)) | ||
if size == 0: | ||
return size | ||
chop = self._to_send[0:size] | ||
retbuf[0:] = chop | ||
self._to_send = self._to_send[size:] | ||
return size |
Oops, something went wrong.