Skip to content

Commit 895a0e0

Browse files
author
BiffoBear
committed
Refactored self._lease_time to self._lease.
1 parent 29727c3 commit 895a0e0

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ def __init__(
160160
self.subnet_mask = _UNASSIGNED_IP_ADDR
161161
self.dns_server_ip = _UNASSIGNED_IP_ADDR
162162

163-
# Lease configuration
164-
self._lease_time = 0
163+
# Lease expiry times
165164
self._t1 = 0
166165
self._t2 = 0
166+
self._lease = 0
167167

168168
# Host name
169169
mac_string = "".join("{:02X}".format(o) for o in mac_address)
@@ -340,9 +340,9 @@ def _process_messaging_states(self, *, message_type: int):
340340
self._dhcp_state = _STATE_INIT
341341
elif message_type == _DHCP_ACK:
342342
debug_msg("Message is ACK, setting FSM state to BOUND.", self._debug)
343-
self._t1 = self._start_time + self._lease_time // 2
344-
self._t2 = self._start_time + self._lease_time - self._lease_time // 8
345-
self._lease_time += self._start_time
343+
self._t1 = self._start_time + self._lease // 2
344+
self._t2 = self._start_time + self._lease - self._lease // 8
345+
self._lease += self._start_time
346346
self._increment_transaction_id()
347347
if not self._renew:
348348
self._eth.ifconfig = (
@@ -424,7 +424,7 @@ def _dhcp_state_machine(self, *, blocking: bool = False) -> None:
424424
debug_msg("No timers have expired. Exiting FSM.", self._debug)
425425
self._socket_release()
426426
return
427-
if now > self._lease_time:
427+
if now > self._lease:
428428
debug_msg(
429429
"Lease has expired, switching state to INIT.", self._debug
430430
)
@@ -646,7 +646,7 @@ def option_reader(pointer: int) -> Tuple[int, int, bytes]:
646646
elif data_type == _DHCP_SERVER_ID:
647647
self.dhcp_server_ip = data
648648
elif data_type == _LEASE_TIME:
649-
self._lease_time = int.from_bytes(data, "big")
649+
self._lease = int.from_bytes(data, "big")
650650
elif data_type == _ROUTERS_ON_SUBNET:
651651
self.gateway_ip = data[:4]
652652
elif data_type == _DNS_SERVERS:
@@ -669,7 +669,7 @@ def option_reader(pointer: int) -> Tuple[int, int, bytes]:
669669
self.local_ip,
670670
self._t1,
671671
self._t2,
672-
self._lease_time,
672+
self._lease,
673673
),
674674
self._debug,
675675
)

tests/test_dhcp_helper_functions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_dhcp_setup_default(self, mocker, mock_wiznet5k, mac_address):
5959
assert dhcp_client.gateway_ip == wiz_dhcp._UNASSIGNED_IP_ADDR
6060
assert dhcp_client.subnet_mask == wiz_dhcp._UNASSIGNED_IP_ADDR
6161
assert dhcp_client.dns_server_ip == wiz_dhcp._UNASSIGNED_IP_ADDR
62-
assert dhcp_client._lease_time == 0
62+
assert dhcp_client._lease == 0
6363
assert dhcp_client._t1 == 0
6464
assert dhcp_client._t2 == 0
6565
mac_string = "".join("{:02X}".format(o) for o in mac_address)
@@ -293,7 +293,7 @@ def test_parse_good_data(
293293
assert dhcp_client.dhcp_server_ip == dhcp_ip
294294
assert dhcp_client.gateway_ip == gate_ip
295295
assert dhcp_client.dns_server_ip == dns_ip
296-
assert dhcp_client._lease_time == lease
296+
assert dhcp_client._lease == lease
297297
assert dhcp_client._t1 == t1
298298
assert dhcp_client._t2 == t2
299299

@@ -775,7 +775,7 @@ def test_called_from_requesting_with_ack(self, mock_dhcp, lease_time):
775775
# Setup with the required state.
776776
mock_dhcp._dhcp_state = wiz_dhcp._STATE_REQUESTING
777777
# Set the lease_time (zero forces a default to be used).
778-
mock_dhcp._lease_time = lease_time
778+
mock_dhcp._lease = lease_time
779779
# Set renew to "renew" to confirm that an ACK sets it to None.
780780
mock_dhcp._renew = "renew"
781781
# Set a start time.
@@ -785,7 +785,7 @@ def test_called_from_requesting_with_ack(self, mock_dhcp, lease_time):
785785
# Confirm timers are correctly set.
786786
assert mock_dhcp._t1 == time.monotonic() + lease_time // 2
787787
assert mock_dhcp._t2 == time.monotonic() + lease_time - lease_time // 8
788-
assert mock_dhcp._lease_time == time.monotonic() + lease_time
788+
assert mock_dhcp._lease == time.monotonic() + lease_time
789789
# Check that renew is forced to None
790790
assert mock_dhcp._renew is None
791791
# FSM state should be bound.

0 commit comments

Comments
 (0)