Skip to content

Commit 0dbfbc8

Browse files
[DEBUG] Part 1 of 2 (- WIP #264 -)
* This is a Partial implementation
1 parent 77ec941 commit 0dbfbc8

File tree

2 files changed

+128
-5
lines changed

2 files changed

+128
-5
lines changed

multicast/__init__.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,52 @@
215215
>>>
216216
217217
218+
"""
219+
220+
global _MCAST_DEFAULT_BUFFER_SIZE # skipcq: PYL-W0604
221+
222+
_MCAST_DEFAULT_BUFFER_SIZE = 1316
223+
"""
224+
Arbitrary bugger size to use by default, though any value below 1500 would probably work well.
225+
226+
Minimal Testing:
227+
228+
First set up test fixtures by importing multicast.
229+
230+
>>> import multicast
231+
>>>
232+
233+
Testcase 0: Multicast should have a default port.
234+
A: Test that the _MCAST_DEFAULT_BUFFER_SIZE attribute is initialized.
235+
B: Test that the _MCAST_DEFAULT_BUFFER_SIZE attribute is an int.
236+
237+
>>> multicast._MCAST_DEFAULT_BUFFER_SIZE is not None
238+
True
239+
>>> type(multicast._MCAST_DEFAULT_BUFFER_SIZE) is type(1)
240+
True
241+
>>>
242+
>>> multicast._MCAST_DEFAULT_BUFFER_SIZE > int(1)
243+
True
244+
>>>
245+
246+
Testcase 1: Multicast should have a default port.
247+
A: Test that the _MCAST_DEFAULT_BUFFER_SIZE attribute is initialized.
248+
B: Test that the _MCAST_DEFAULT_BUFFER_SIZE attribute is an int.
249+
C: Test that the _MCAST_DEFAULT_BUFFER_SIZE attribute is RFC-??? compliant.
250+
251+
>>> multicast._MCAST_DEFAULT_PORT is not None
252+
True
253+
>>> type(multicast._MCAST_DEFAULT_BUFFER_SIZE) is type(1)
254+
True
255+
>>>
256+
>>> multicast._MCAST_DEFAULT_BUFFER_SIZE >= int(56)
257+
True
258+
>>>
259+
>>> multicast._MCAST_DEFAULT_BUFFER_SIZE <= int(1500)
260+
True
261+
>>>
262+
263+
218264
"""
219265

220266
global _MCAST_DEFAULT_PORT # skipcq: PYL-W0604
@@ -414,8 +460,7 @@
414460
_MCAST_DEFAULT_BIND_IP = _config["bind_addr"]
415461
global _MCAST_DEFAULT_GROUPS # skipcq: PYL-W0604
416462
_MCAST_DEFAULT_GROUPS = _config["groups"]
417-
global _MCAST_DEFAULT_BUFFER # skipcq: PYL-W0604
418-
_MCAST_DEFAULT_BUFFER = _config["buffer_size"]
463+
_MCAST_DEFAULT_BUFFER_SIZE = _config["buffer_size"]
419464

420465
del _config # skipcq - cleanup any bootstrap/setup leaks early
421466

multicast/env.py

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,41 @@
9898
raise baton from err
9999

100100

101+
def validate_buffer_size(size: int) -> bool:
102+
"""
103+
Validate if the buffer size is a positive integer.
104+
105+
Arguments:
106+
size (int) -- The buffer size to validate.
107+
108+
Returns:
109+
bool: True if the buffer size is valid (> 0), False otherwise.
110+
111+
Raises:
112+
ValueError: If the size cannot be converted to an integer.
113+
114+
Minimum Acceptance Testing:
115+
>>> validate_buffer_size(1316)
116+
True
117+
>>> validate_buffer_size(1)
118+
True
119+
>>> validate_buffer_size(0)
120+
False
121+
>>> validate_buffer_size(-1)
122+
False
123+
>>> try:
124+
... validate_buffer_size('invalid')
125+
... except ValueError:
126+
... print('ValueError raised')
127+
ValueError raised
128+
"""
129+
try:
130+
size_num = int(size)
131+
return size_num > 0
132+
except (ValueError, TypeError) as err:
133+
raise ValueError(f"Invalid buffer size value: {size}. Must be a positive integer.") from err
134+
135+
101136
def validate_port(port: int) -> bool:
102137
"""
103138
Validate if the port number is within the dynamic/private port range.
@@ -202,6 +237,49 @@ def validate_ttl(ttl: int) -> bool:
202237
) from err
203238

204239

240+
def load_buffer() -> int:
241+
"""
242+
Load and validate the multicast buffer size from environment variable.
243+
244+
This function attempts to load the buffer size from the MULTICAST_PORT
245+
environment variable. If the value is valid, it updates the global
246+
_MCAST_DEFAULT_PORT. Invalid values trigger warnings and fall back to
247+
the default.
248+
249+
Returns:
250+
int: The validated port number to use for multicast operations.
251+
Returns the default port if the environment value is invalid.
252+
253+
Raises:
254+
ImportError: If the multicast module cannot be imported.
255+
256+
[WIP - DRAFT]
257+
258+
"""
259+
# Import globals that we'll potentially update
260+
from multicast import _MCAST_DEFAULT_BUFFER_SIZE
261+
try:
262+
buffer_size = int(
263+
os.getenv("MULTICAST_BUFFER_SIZE", _MCAST_DEFAULT_BUFFER_SIZE) # skipcq: PYL-W1508
264+
)
265+
except ValueError:
266+
warnings.warn(
267+
"Invalid MULTICAST_BUFFER_SIZE value, using default 1316",
268+
stacklevel=2
269+
)
270+
buffer_size = _MCAST_DEFAULT_BUFFER_SIZE
271+
# Validate and potentially update port
272+
if validate_buffer_size(buffer_size):
273+
globals()["_MCAST_DEFAULT_BUFFER_SIZE"] = buffer_size
274+
else:
275+
warnings.warn(
276+
f"Invalid MULTICAST_BUFFER_SIZE {buffer_size}, using default 1316",
277+
stacklevel=2
278+
)
279+
buffer_size = _MCAST_DEFAULT_BUFFER_SIZE
280+
return buffer_size
281+
282+
205283
def load_port() -> int:
206284
"""
207285
Load and validate the multicast port from environment variable.
@@ -673,8 +751,8 @@ def load_config() -> dict:
673751
... config = load_config()
674752
... len(w) == 0 # expected failure - Warning was NOT issued
675753
True
676-
>>> config['buffer_size'] # expected failure - undefined or Falls back to default
677-
-1024
754+
>>> config['buffer_size'] # Undefined or Falls-back to default
755+
1316
678756
679757
# Cleanup
680758
>>> os.environ.pop('MULTICAST_BUFFER_SIZE', None)
@@ -705,7 +783,7 @@ def load_config() -> dict:
705783
ttl = load_TTL()
706784
groups_str = os.getenv("MULTICAST_GROUPS", "")
707785
bind_addr = os.getenv("MULTICAST_BIND_ADDR", group) # skipcq: PYL-W1508
708-
buffer_size = int(os.getenv("MULTICAST_BUFFER_SIZE", 1316)) # skipcq: PYL-W1508
786+
buffer_size = load_buffer()
709787
# Process and validate groups
710788
groups = set()
711789
if groups_str:

0 commit comments

Comments
 (0)