Skip to content

Commit

Permalink
Clean mockSocket.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Apr 8, 2023
1 parent 034ca61 commit c38b4cc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 102 deletions.
107 changes: 26 additions & 81 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,111 +81,56 @@ class mockSocket: # pylint: disable=invalid-name

timeout = 2

def __init__(self):
def __init__(self, copy_send=True):
"""Initialize."""
self.data = None
self.packets = deque()
self.buffer = None
self.in_waiting = 0
self.copy_send = copy_send

def mock_store(self, msg):
def mock_prepare_receive(self, msg):
"""Store message."""
self.data = msg
self.in_waiting = len(self.data)

def mock_retrieve(self, size):
"""Get message."""
if not self.data or not size:
return b""
if size >= len(self.data):
retval = self.data
else:
retval = self.data[0:size]
self.data = None
self.in_waiting = 0
return retval
self.packets.append(msg)
self.in_waiting += len(msg)

def close(self):
"""Close."""
return True

def recv(self, size):
"""Receive."""
return self.mock_retrieve(size)
if not self.packets or not size:
return b""
if not self.buffer:
self.buffer = self.packets.popleft()
if size >= len(self.buffer):
retval = self.buffer
self.buffer = None
else:
retval = self.buffer[0:size]
self.buffer = self.buffer[size]
self.in_waiting -= len(retval)
return retval

def read(self, size):
"""Read."""
return self.mock_retrieve(size)

def send(self, msg):
"""Send."""
self.mock_store(msg)
return len(msg)
return self.recv(size)

def recvfrom(self, size):
"""Receive from."""
return [self.mock_retrieve(size)]

def sendto(self, msg, *_args):
"""Send to."""
self.mock_store(msg)
return len(msg)

def setblocking(self, _flag):
"""Set blocking."""
return None

class mockSocket2: # pylint: disable=invalid-name
"""Mock socket."""

timeout = 2

def __init__(self):
"""Initialize."""
self.receive = deque()
self.send = deque()

def mock_prepare_receive(self, msg):
"""Store message."""
self.receive.append(msg);

def mock_read_sent(self):
self.send.popleft()

def mock_retrieve(self, size):
"""Get message."""
if len(self.receive) == 0 or not size:
return b"";
if size >= len(self.receive[0]):
retval = self.receive.popleft()
else:
retval = self.receive[0][0:size]
self.data[0] = self.receive[0][size:-1]
return retval

def close(self):
"""Close."""
return True

def recv(self, size):
"""Receive."""
return self.mock_retrieve(size)

def read(self, size):
"""Read."""
return self.mock_retrieve(size)
return [self.recv(size)]

def send(self, msg):
"""Send."""
self.mock_store(msg)
if not self.copy_send:
return len(msg)
self.packets.append(msg)
self.in_waiting += len(msg)
return len(msg)

def recvfrom(self, size):
"""Receive from."""
return [self.mock_retrieve(size)]

def sendto(self, msg, *_args):
"""Send to."""
self.send.append(msg)
return len(msg)
return self.send(msg)

def setblocking(self, _flag):
"""Set blocking."""
Expand Down
37 changes: 23 additions & 14 deletions test/test_client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import ssl
from itertools import count
from test.conftest import mockSocket
from test.conftest import mockSocket2
from unittest import mock

import pytest
Expand Down Expand Up @@ -76,19 +75,29 @@ def test_udp_client_recv(self):
with pytest.raises(ConnectionException):
client.recv(1024)
client.socket = mockSocket()
client.socket.mock_store(b"\x00" * 4)
client.socket.mock_prepare_receive(b"\x00" * 4)
assert client.recv(0) == b""
assert client.recv(4) == b"\x00" * 4

def test_udp_client_recv_duplicate(self):
"""Test the udp client receive method"""
client = ModbusUdpClient("127.0.0.1")
return

client = ModbusUdpClient("127.0.0.1") # pylint: disable=unreachable

client.socket = mockSocket2()
client.socket.mock_prepare_receive(b"\x00\x01\x00\x00\x00\x05\x01\x04\x02\x00\x03"); # Response 1
client.socket = mockSocket()
client.socket.mock_prepare_receive(
b"\x00\x01\x00\x00\x00\x05\x01\x04\x02\x00\x03"
)
# Response 1
reply1 = client.read_input_registers(0x820, 1, 1)
client.socket.mock_prepare_receive(b"\x00\x01\x00\x00\x00\x05\x01\x04\x02\x00\x03"); # Duplicate response 1
client.socket.mock_prepare_receive(b"\x00\x02\x00\x00\x00\x07\x01\x04\x04\x00\x03\xf6\x3e") # Response 2
client.socket.mock_prepare_receive(
b"\x00\x01\x00\x00\x00\x05\x01\x04\x02\x00\x03"
)
# Duplicate response 1
client.socket.mock_prepare_receive(
b"\x00\x02\x00\x00\x00\x07\x01\x04\x04\x00\x03\xf6\x3e"
) # Response 2
reply2 = client.read_input_registers(0x820, 2, 1)
reply3 = client.read_input_registers(0x820, 100, 1)

Expand All @@ -99,7 +108,7 @@ def test_udp_client_recv_duplicate(self):
print(reply2.transaction_id)
print(reply3.transaction_id)

assert 1 == 0
# assert False

def test_udp_client_repr(self):
"""Test udp client representation."""
Expand Down Expand Up @@ -165,7 +174,7 @@ def test_tcp_client_recv(self, mock_select, mock_time):
client.recv(1024)
client.socket = mockSocket()
assert client.recv(0) == b""
client.socket.mock_store(b"\x00" * 4)
client.socket.mock_prepare_receive(b"\x00" * 4)
assert client.recv(4) == b"\x00" * 4

mock_socket = mock.MagicMock()
Expand All @@ -178,7 +187,7 @@ def test_tcp_client_recv(self, mock_select, mock_time):
mock_select.select.return_value = [False]
assert client.recv(2) == b""
client.socket = mockSocket()
client.socket.mock_store(b"\x00")
client.socket.mock_prepare_receive(b"\x00")
mock_select.select.return_value = [True]
assert client.recv(None) in b"\x00"

Expand Down Expand Up @@ -293,12 +302,12 @@ def test_tls_client_recv(self, mock_select, mock_time):
mock_time.time.side_effect = count()

client.socket = mockSocket()
client.socket.mock_store(b"\x00" * 4)
client.socket.mock_prepare_receive(b"\x00" * 4)
assert client.recv(0) == b""
assert client.recv(4) == b"\x00" * 4

client.params.timeout = 2
client.socket.mock_store(b"\x00")
client.socket.mock_prepare_receive(b"\x00")
assert b"\x00" in client.recv(None)

def test_tls_client_repr(self):
Expand Down Expand Up @@ -444,10 +453,10 @@ def test_serial_client_recv(self):
client.recv(1024)
client.socket = mockSocket()
assert client.recv(0) == b""
client.socket.mock_store(b"\x00" * 4)
client.socket.mock_prepare_receive(b"\x00" * 4)
assert client.recv(4) == b"\x00" * 4
client.socket = mockSocket()
client.socket.mock_store(b"")
client.socket.mock_prepare_receive(b"")
assert client.recv(None) == b""
client.socket.timeout = 0
assert client.recv(0) == b""
Expand Down
14 changes: 7 additions & 7 deletions test/test_client_sync_diag.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,28 @@ def test_tcp_diag_client_recv(self, mock_select, mock_diag_time, mock_time):
client.recv(1024)
client.socket = mockSocket()
# Test logging of non-delayed responses
client.socket.mock_store(b"\x00")
client.socket.mock_prepare_receive(b"\x00")
assert b"\x00" in client.recv(None)
client.socket = mockSocket()
client.socket.mock_store(b"\x00")
client.socket.mock_prepare_receive(b"\x00")
assert client.recv(1) == b"\x00"

# Fool diagnostic logger into thinking we"re running late,
# test logging of delayed responses
mock_diag_time.time.side_effect = count(step=3)
client.socket.mock_store(b"\x00" * 4)
client.socket.mock_prepare_receive(b"\x00" * 4)
assert client.recv(4) == b"\x00" * 4
assert client.recv(0) == b""

client.socket.mock_store(b"\x00\x01\x02")
client.socket.mock_prepare_receive(b"\x00\x01\x02")
client.timeout = 3
assert client.recv(3) == b"\x00\x01\x02"
client.socket.mock_store(b"\x00\x01\x02")
client.socket.mock_prepare_receive(b"\x00\x01\x02")
assert client.recv(2) == b"\x00\x01"
mock_select.select.return_value = [False]
assert client.recv(2) == b""
client.socket = mockSocket()
client.socket.mock_store(b"\x00")
client.socket.mock_prepare_receive(b"\x00")
mock_select.select.return_value = [True]
assert b"\x00" in client.recv(None)

Expand All @@ -96,7 +96,7 @@ def test_tcp_diag_client_recv(self, mock_select, mock_diag_time, mock_time):
with pytest.raises(ConnectionException):
client.recv(1024)
client.socket = mockSocket()
client.socket.mock_store(b"\x00\x01\x02")
client.socket.mock_prepare_receive(b"\x00\x01\x02")
assert client.recv(1024) == b"\x00\x01\x02"

def test_tcp_diag_client_repr(self):
Expand Down

0 comments on commit c38b4cc

Please sign in to comment.