tests.MulticastUDPClient

Module Contents

Classes

MCastClient

For use as a test fixture.

MyUDPHandler

Subclasses socketserver.BaseRequestHandler to handle echo functionality.

Functions

main

The main test operations.

Data

__module__

This is a testing related stand-alone utilities module.

__name__

API

tests.MulticastUDPClient.__module__[source]

‘tests’

This is a testing related stand-alone utilities module.

tests.MulticastUDPClient.__name__[source]

‘tests.MulticastUDPClient’

class tests.MulticastUDPClient.MCastClient(*args, **kwargs)[source]

Bases: object

For use as a test fixture.

A trivial implementation of a socket-based object with a function named say. The say function of this class performs a send and recv on a given socket and then prints out simple diognostics about the content sent and any response received.

Testing:

    First some test fixtures:

    >>> import socket as socket
    >>> import random as random
    >>>

Testcase 0: test the class MCastClient is. A: Test that the MulticastUDPClient component is importable. B: Test that the MCastClient class is importable.

    >>> import tests.MulticastUDPClient
    >>> from MulticastUDPClient import MCastClient as MCastClient
    >>> MCastClient is not None
    True
    >>>

Testcase 1: Test the class MCastClient has a say function. A: Test that the MulticastUDPClient component is importable. B: Test that the MCastClient class is importable. C: Test that the MCastClient class has the function named say.

    >>> import tests.MulticastUDPClient
    >>> from MulticastUDPClient import MCastClient as MCastClient
    >>> MCastClient is not None
    True
    >>> MCastClient.say is not None
    True
    >>> type(MCastClient.say)
    <class 'function'>
    >>>

Initialization

Initialize a MCastClient object with optional group address and source port.

The client can be initialized with or without specifying a group address and source port. If no source port is provided, a random port between 50000 and 59999 is generated.

Args: *args: Variable length argument list (Unused). **kwargs: Arbitrary keyword arguments. - grp_addr (str): The multicast group address. - src_port (int): The source port for the client.

Meta Testing:

    First set up test fixtures by importing test context.

            >>> import tests.MulticastUDPClient as MulticastUDPClient
            >>> from MulticastUDPClient import MCastClient as MCastClient
            >>>

    Testcase 1: Initialization without any arguments.

            >>> client = MCastClient()
            >>> 50000 <= client._source_port <= 59999
            True
            >>> client._group_addr is None
            True
            >>>

    Testcase 2: Initialization with only group address.

            >>> tst_args = {}
            >>> client = MCastClient(grp_addr="224.0.0.1")
            >>> client._group_addr
            '224.0.0.1'
            >>> 50000 <= client._source_port <= 59999
            True
            >>>

    Testcase 3: Initialization with only source port.

            >>> client = MCastClient(src_port=55555)
            >>> client._source_port
            55555
            >>> client._group_addr is None
            True
            >>>

    Testcase 4: Initialization with both group address and source port.

            >>> client = MCastClient(grp_addr="224.0.0.2", src_port=55556)
            >>> client._group_addr
            '224.0.0.2'
            >>> client._source_port
            55556
            >>>
__module__[source]

‘tests.MulticastUDPClient.MCastClient’

_group_addr[source]

None

The multicast group address.

_source_port[source]

None

The source port for the client.

static say(address, port, sock, msg)[source][source]

Send a message to a specified multicast address and port, then receive and print it.

This function sends a UTF-8 encoded message to the specified multicast address and port using the provided connection. It then waits for a response, decodes it, and prints both the sent and received messages.

Args: address (str): The multicast group address to send the message to. port (int): The port number to send the message to. sock (socket.socket): The socket connection to use for sending and receiving. msg (str): The message to be sent.

Returns: None

Prints: The sent message and the received response.

Meta Testing:

    First, set up test fixtures:

            >>> import unittest.mock
            >>> from MulticastUDPClient import MCastClient
            >>>

    Testcase 1: Test sending and receiving a message.

            >>> mock_socket = unittest.mock.Mock()
            >>> mock_socket.recv.return_value = b"Response received"
            >>> client = MCastClient()
            >>> client.say("224.0.0.1", 59991, mock_socket, "Test message")
            Sent:     Test message
            Received: Response received
            >>>

    Testcase 2: Test sending a 'STOP' message.

            >>> mock_socket.recv.return_value = b"Stopped"
            >>> client.say("224.0.0.1", 59991, mock_socket, "STOP")
            Sent:     STOP
            Received: Stopped
            >>>

Note: This function assumes that the connection is already properly configured for multicast communication.

class tests.MulticastUDPClient.MyUDPHandler(request, client_address, server)[source]

Bases: socketserver.BaseRequestHandler

Subclasses socketserver.BaseRequestHandler to handle echo functionality.

Simplifies testing by echoing back the received string data in uppercase, after printing the sender’s IP address.

Meta Testing:

    First set up test fixtures by importing test context.

            >>> import tests.MulticastUDPClient as MulticastUDPClient
            >>> from MulticastUDPClient import MyUDPHandler as MyUDPHandler
            >>>

    Testcase 1: MyUDPHandler should be automatically imported.

            >>> MyUDPHandler.__name__ is not None
            True
            >>>

Initialization

__module__[source]

‘tests.MulticastUDPClient.MyUDPHandler’

handle()[source][source]
            Handle incoming UDP requests.

            This method overrides the `handle` method from `socketserver.BaseRequestHandler`
            to process incoming UDP messages. It receives a message from a client, echoes it
            back in uppercase, and prints diagnostic information.

            Meta Testing:

                    First set up test fixtures by importing test context.

                            >>> import socket
                            >>> import threading
                            >>> from tests.MulticastUDPClient import MyUDPHandler
                            >>>

                    Testcase 1: Test handling a simple message.

                            >>> import socketserver
                            >>> server = socketserver.UDPServer(('localhost', 0), MyUDPHandler)
                            >>> threading.Thread(target=server.serve_forever, daemon=True).start()
                            >>> client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                            >>> client_socket.sendto("hello world

“, server.server_address) >>> data, _ = client_socket.recvfrom(1024) >>> data b’HELLO WORLD ‘ >>> server.shutdown() >>>

                    Testcase 2: Test handling an empty message.

                            >>> import socketserver
                            >>> server = socketserver.UDPServer(('localhost', 0), MyUDPHandler)
                            >>> threading.Thread(target=server.serve_forever, daemon=True).start()
                            >>> client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                            >>> client_socket.sendto("

“, server.server_address) >>> data, _ = client_socket.recvfrom(1024) >>> data b’ ‘ >>> server.shutdown() >>>

            Note:
                    This method assumes that the incoming request tuple contains a string and a socket,
                    as per `socketserver.BaseRequestHandler` for datagram services.
tests.MulticastUDPClient.main()[source][source]

The main test operations.

Testing:

    First some test fixtures:

    >>> import socket as socket
    >>> import random as random
    >>>

Testcase 0: test the function main is. A: Test that the MulticastUDPClient component is importable. B: Test that the MulticastUDPClient has a main function.

    >>> import tests.MulticastUDPClient
    >>> tests.MulticastUDPClient.main is not None
    True
    >>> type(tests.MulticastUDPClient.main)
    <class 'function'>
    >>>