Skip to content

Commit 4765e03

Browse files
[PATCH] Applied changed found in review (- WIP PR #348 -)
Changes in file multicast/__init__.py: * cleaned up docstrings a bit. Changes in file multicast/env.py: * Minor improvements to docstrings including doctests.
1 parent 593bf91 commit 4765e03

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

multicast/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@
223223

224224
_MCAST_DEFAULT_BUFFER_SIZE = 1316
225225
"""
226-
Arbitrary bugger size to use by default, though any value below 65507 should work.
226+
Arbitrary buffer size to use by default, though any value below 65507 should work.
227227
228228
Minimal Testing:
229229
@@ -232,7 +232,7 @@
232232
>>> import multicast
233233
>>>
234234
235-
Testcase 0: Multicast should have a default port.
235+
Testcase 0: Multicast should have a default buffer size.
236236
A: Test that the _MCAST_DEFAULT_BUFFER_SIZE attribute is initialized.
237237
B: Test that the _MCAST_DEFAULT_BUFFER_SIZE attribute is an int.
238238
@@ -245,19 +245,24 @@
245245
True
246246
>>>
247247
248-
Testcase 1: Multicast should have a default port.
248+
Testcase 1: Multicast should have a default and valid buffer size.
249249
A: Test that the _MCAST_DEFAULT_BUFFER_SIZE attribute is initialized.
250250
B: Test that the _MCAST_DEFAULT_BUFFER_SIZE attribute is an int.
251-
C: Test that the _MCAST_DEFAULT_BUFFER_SIZE attribute is RFC-??? compliant.
251+
C: Test that the _MCAST_DEFAULT_BUFFER_SIZE attribute is RFC-791 & RFC-768 compliant.
252+
D: Test that the _MCAST_DEFAULT_BUFFER_SIZE attribute is a smaller than fragment thresholds
253+
for typical ethernet MTUs by default.
252254
253-
>>> multicast._MCAST_DEFAULT_PORT is not None
255+
>>> multicast._MCAST_DEFAULT_BUFFER_SIZE is not None
254256
True
255257
>>> type(multicast._MCAST_DEFAULT_BUFFER_SIZE) is type(1)
256258
True
257259
>>>
258260
>>> multicast._MCAST_DEFAULT_BUFFER_SIZE >= int(56)
259261
True
260262
>>>
263+
>>> multicast._MCAST_DEFAULT_BUFFER_SIZE <= int(65527)
264+
True
265+
>>>
261266
>>> multicast._MCAST_DEFAULT_BUFFER_SIZE <= int(1500)
262267
True
263268
>>>

multicast/env.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -246,24 +246,24 @@ def load_buffer_size() -> int:
246246
"""
247247
Load and validate the multicast buffer size from environment variable.
248248
249-
This function attempts to load the buffer size from the MULTICAST_PORT
250-
environment variable. If the value is valid, it updates the global
251-
_MCAST_DEFAULT_PORT. Invalid values trigger warnings and fall back to
252-
the default.
253-
254-
Returns:
255-
int: The validated port number to use for multicast operations.
256-
Returns the default port if the environment value is invalid.
257-
258-
Raises:
259-
ImportError: If the multicast module cannot be imported.
260-
261-
Load buffer size from environment variables.
262-
263249
This function attempts to load the buffer size from the MULTICAST_BUFFER_SIZE
264250
environment variable. If the value is valid, it returns the buffer size.
265251
Invalid values trigger warnings and fall back to the default.
266252
253+
MTU Considerations for Buffer Size:
254+
When setting a buffer size, consider the MTU of the underlying network:
255+
- Ethernet: 1500 bytes MTU → 1472 bytes max payload (1500 - 28 bytes overhead)
256+
- PPP: 296 bytes MTU → 268 bytes max payload
257+
- Wi-Fi (802.11): 2304 bytes MTU → 2276 bytes max payload
258+
- Frame Relay: 128 bytes MTU → 100 bytes max payload
259+
260+
The overhead consists of:
261+
- UDP header: 8 bytes
262+
- IP header: 20 bytes (without options)
263+
264+
Setting buffer sizes larger than the network's max payload may cause IP
265+
fragmentation, which can lead to performance issues and increased complexity.
266+
267267
Returns:
268268
int: The validated buffer size, or the default value if not set/invalid.
269269
@@ -274,19 +274,23 @@ def load_buffer_size() -> int:
274274
ImportError: If the multicast module cannot be imported.
275275
276276
Minimum Acceptance Testing:
277+
278+
Testcase 0: Setup test fixtures.
277279
>>> import os
278280
>>> from multicast import _MCAST_DEFAULT_BUFFER_SIZE
279281
>>> original_buffer = _MCAST_DEFAULT_BUFFER_SIZE
280282
281-
>>> # Test with valid environment variable
283+
Testcase 1: Test with valid environment variable
282284
>>> os.environ["MULTICAST_BUFFER_SIZE"] = "2048"
283285
>>> buffer_size = load_buffer_size()
284286
>>> buffer_size
285287
2048
286-
>>> _MCAST_DEFAULT_BUFFER_SIZE != 2048 # Global should not be updated by this function
288+
>>> # The function updates the global in the module's namespace, but this doesn't affect
289+
>>> # the imported value in the test namespace
290+
>>> _MCAST_DEFAULT_BUFFER_SIZE != 2048 # Global in test namespace is not updated
287291
True
288292
289-
>>> # Test with invalid (negative) environment variable
293+
Testcase 2: Test with invalid (negative) environment variable
290294
>>> os.environ["MULTICAST_BUFFER_SIZE"] = "-100"
291295
>>> import warnings
292296
>>> with warnings.catch_warnings(record=True) as w:
@@ -297,7 +301,7 @@ def load_buffer_size() -> int:
297301
>>> buffer_size == 1316 # Falls back to default
298302
True
299303
300-
>>> # Test with invalid (zero) environment variable
304+
Testcase 3: Test with invalid (zero) environment variable
301305
>>> os.environ["MULTICAST_BUFFER_SIZE"] = "0"
302306
>>> with warnings.catch_warnings(record=True) as w:
303307
... warnings.simplefilter("always")
@@ -307,7 +311,7 @@ def load_buffer_size() -> int:
307311
>>> buffer_size == 1316 # Falls back to default
308312
True
309313
310-
>>> # Test with invalid (non-integer) environment variable
314+
Testcase 4: Test with invalid (non-integer) environment variable
311315
>>> os.environ["MULTICAST_BUFFER_SIZE"] = 'not_an_integer'
312316
>>> with warnings.catch_warnings(record=True) as w:
313317
... warnings.simplefilter("always")
@@ -317,7 +321,7 @@ def load_buffer_size() -> int:
317321
>>> buffer_size == 1316 # Falls back to default
318322
True
319323
320-
>>> # Test with no environment variable
324+
Testcase 5: Test with no environment variable
321325
>>> if "MULTICAST_BUFFER_SIZE" in os.environ: os.environ.pop("MULTICAST_BUFFER_SIZE")
322326
'not_an_integer'
323327
>>> buffer_size = load_buffer_size()
@@ -580,13 +584,15 @@ def load_TTL() -> int:
580584
ImportError: If the multicast module cannot be imported.
581585
582586
Minimum Acceptance Testing:
587+
588+
Testcase 0: Setup
583589
>>> import os
584590
>>> import socket
585591
>>> from multicast import _MCAST_DEFAULT_TTL
586592
>>> original_ttl = _MCAST_DEFAULT_TTL
587593
>>> original_timeout = socket.getdefaulttimeout()
588594
589-
# Test with valid TTL
595+
Testcase 1: Test with valid TTL
590596
>>> os.environ['MULTICAST_TTL'] = '2'
591597
>>> ttl = load_TTL()
592598
>>> ttl
@@ -596,7 +602,7 @@ def load_TTL() -> int:
596602
>>> socket.getdefaulttimeout() == 2 # Socket timeout was updated
597603
True
598604
599-
# Test with invalid numeric TTL
605+
Testcase 2: Test with invalid numeric TTL
600606
>>> os.environ['MULTICAST_TTL'] = '127'
601607
>>> import warnings
602608
>>> with warnings.catch_warnings(record=True) as w:
@@ -607,7 +613,7 @@ def load_TTL() -> int:
607613
>>> ttl == original_ttl # Falls back to original default
608614
True
609615
610-
# Test with non-numeric TTL
616+
Testcase 3: Test with non-numeric TTL
611617
>>> os.environ['MULTICAST_TTL'] = 'invalid'
612618
>>> with warnings.catch_warnings(record=True) as w:
613619
... warnings.simplefilter("always")
@@ -620,7 +626,7 @@ def load_TTL() -> int:
620626
'invalid'
621627
>>>
622628
623-
# Test with unset environment variable
629+
Testcase 4: Test with unset environment variable
624630
>>> os.environ.pop('MULTICAST_TTL', None)
625631
>>> ttl = load_TTL()
626632
>>> ttl == original_ttl # Uses default
@@ -840,8 +846,9 @@ def load_config() -> dict:
840846
... config = load_config()
841847
... except ValueError:
842848
... print('ValueError raised')
843-
>>> config is None
844-
False
849+
>>> # Verify config is not None (load_config should handle the error and use default)
850+
>>> config is not None
851+
True
845852
846853
# Cleanup
847854
>>> os.environ.pop('MULTICAST_BUFFER_SIZE', None)

0 commit comments

Comments
 (0)