Unit test fixes

This commit is contained in:
Pascal Serrarens 2025-04-28 18:13:22 +02:00
parent 2fe73b39be
commit 4726e443b4
7 changed files with 28 additions and 23 deletions

View File

@ -11,10 +11,14 @@ class ParticipantMsg(IMessage):
## The length of the message
length: int = 2
def __init__(self, data = None):
def __init__(self, arg1):
"""! Create a new message for sending
"""
self.network_id = data
if isinstance(arg1, bytes):
buffer = arg1
self.network_id = buffer[1]
else:
self.network_id = arg1
def Serialize(self, buffer_ref):
"""! Serialize the message into a byte array.

View File

@ -33,12 +33,12 @@ class ParticipantUDP(Participant):
name = "Participant"
isolated_participant = None
def __init__(self, port=7681, ip_address=None, local_port=0):
super().__init__(ip_address = "127.0.0.1", port = port)
def __init__(self, port=7681, ip_address=None, local_port=7681):
super().__init__(ip_address = "127.0.0.1", port = local_port)
if local_port == 0:
local_port = port
self.local_port = local_port
# if local_port == 0:
# local_port = port
# self.local_port = local_port
## True if the participant is running isolated.
# Isolated participants do not communicate with other participants
@ -48,7 +48,7 @@ class ParticipantUDP(Participant):
## The other participants communicating with this participant
self.others = []
if self.port != 0:
if port != 0:
self.is_isolated = False
if ip_address is not None:
self.remote_site = Participant(ip_address, port)
@ -59,7 +59,7 @@ class ParticipantUDP(Participant):
self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.udp_socket.bind(("0.0.0.0", self.local_port))
self.udp_socket.bind(("0.0.0.0", self.port))
self.thread = threading.Thread(target = self.Receiver)
self.thread.daemon = True

View File

@ -23,7 +23,8 @@ class SiteServer(ParticipantUDP):
def ProcessParticipantMsg(self, sender, msg):
print(f'{self.name} received Participant ')
self.Send(sender, NetworkIdMsg())
if msg.network_id != sender.network_id:
self.Send(sender, sender.network_id)
def ProcessNetworkId(self, msg):
pass

View File

@ -39,7 +39,7 @@ class Thing:
owner = parent.owner
self.SetParent(parent)
elif owner == None:
from ParticipantUDP import ParticipantUDP
from RoboidControl.ParticipantUDP import ParticipantUDP
owner = ParticipantUDP.Isolated()
self.children = []

View File

@ -1,5 +1,5 @@
from Thing import Thing
from Messages import LowLevelMessages
from RoboidControl.Thing import Thing
from RoboidControl.Messages import LowLevelMessages
class TemperatureSensor(Thing):
def __init__(self, thing_id):

View File

@ -4,11 +4,11 @@ from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parent.parent))
import time
from Participants.SiteServer import SiteServer
from ParticipantUDP import ParticipantUDP
from Thing import *
from Things.DifferentialDrive import DifferentialDrive
from Things.TouchSensor import TouchSensor
from RoboidControl.Participants.SiteServer import SiteServer
from RoboidControl.ParticipantUDP import ParticipantUDP
from RoboidControl.Thing import *
from RoboidControl.Things.DifferentialDrive import DifferentialDrive
from RoboidControl.Things.TouchSensor import TouchSensor
# Start a site server
site = SiteServer(port=7691)

View File

@ -3,13 +3,13 @@ import sys
from pathlib import Path
# Add the project root to sys.path
sys.path.append(str(Path(__file__).resolve().parent.parent))
sys.path.append(str(Path(__file__).resolve().parent))
import unittest
from Thing import Thing
from ParticipantUDP import ParticipantUDP
from Participants.SiteServer import SiteServer
from RoboidControl.Thing import Thing
from RoboidControl.ParticipantUDP import ParticipantUDP
from RoboidControl.Participants.SiteServer import SiteServer
class ThingTest(unittest.TestCase):
@ -33,7 +33,7 @@ class ThingTest(unittest.TestCase):
def test_site_participant(self):
site = SiteServer(port=7681)
participant = ParticipantUDP(port=7681, ip_address="127.0.0.1", local_port=7682)
participant = ParticipantUDP(ip_address="127.0.0.1", port=7681, local_port=7682)
milliseconds = time.time() * 1000
start_time = milliseconds