From 0e73ba2d3d2c48db9370b2728ffef91241b6fa68 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 15 Dec 2023 10:01:51 -1000 Subject: [PATCH 1/2] Revert "chore: add get_percentage_remaining_ttl helper to DNSRecord (#1343)" This reverts commit 7a24b88ee2a7f9d65a4fa6a636d79fdc757b6ce5. --- src/zeroconf/_dns.pxd | 2 -- src/zeroconf/_dns.py | 5 ----- tests/test_dns.py | 20 ++++---------------- 3 files changed, 4 insertions(+), 23 deletions(-) diff --git a/src/zeroconf/_dns.pxd b/src/zeroconf/_dns.pxd index 720805177..d4116a66a 100644 --- a/src/zeroconf/_dns.pxd +++ b/src/zeroconf/_dns.pxd @@ -54,8 +54,6 @@ cdef class DNSRecord(DNSEntry): cpdef get_remaining_ttl(self, double now) - cpdef unsigned int get_percentage_remaining_ttl(self, double now) - cpdef double get_expiration_time(self, cython.uint percent) cpdef bint is_expired(self, double now) diff --git a/src/zeroconf/_dns.py b/src/zeroconf/_dns.py index 262dbb5f4..66fb5b86d 100644 --- a/src/zeroconf/_dns.py +++ b/src/zeroconf/_dns.py @@ -193,11 +193,6 @@ def get_expiration_time(self, percent: _int) -> float: by a certain percentage.""" return self.created + (percent * self.ttl * 10) - def get_percentage_remaining_ttl(self, now: _float) -> _int: - """Returns the percentage remaining of the ttl between 0-100.""" - remain = (self.created + (_EXPIRE_FULL_TIME_MS * self.ttl) - now) / self.ttl / 10 - return 0 if remain <= 0 else round(remain) - # TODO: Switch to just int here def get_remaining_ttl(self, now: _float) -> Union[int, float]: """Returns the remaining TTL in seconds.""" diff --git a/tests/test_dns.py b/tests/test_dns.py index b7e5a8790..0eac568dd 100644 --- a/tests/test_dns.py +++ b/tests/test_dns.py @@ -6,6 +6,7 @@ import logging import os import socket +import time import unittest import unittest.mock @@ -85,32 +86,19 @@ def test_dns_record_abc(self): record.write(None) # type: ignore[arg-type] def test_dns_record_reset_ttl(self): - start = r.current_time_millis() - record = r.DNSRecord( - 'irrelevant', const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL, created=start - ) - later = start + 1000 - record2 = r.DNSRecord( - 'irrelevant', const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL, created=later - ) + record = r.DNSRecord('irrelevant', const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL) + time.sleep(1) + record2 = r.DNSRecord('irrelevant', const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL) now = r.current_time_millis() assert record.created != record2.created assert record.get_remaining_ttl(now) != record2.get_remaining_ttl(now) - assert record.get_percentage_remaining_ttl(now) != record2.get_percentage_remaining_ttl(now) - assert record2.get_percentage_remaining_ttl(later) == 100 - assert record2.get_percentage_remaining_ttl(later + (const._DNS_HOST_TTL * 1000 / 2)) == 50 record.reset_ttl(record2) assert record.ttl == record2.ttl assert record.created == record2.created assert record.get_remaining_ttl(now) == record2.get_remaining_ttl(now) - assert record.get_percentage_remaining_ttl(now) == record2.get_percentage_remaining_ttl(now) - assert record.get_percentage_remaining_ttl(later) == 100 - assert record2.get_percentage_remaining_ttl(later) == 100 - assert record.get_percentage_remaining_ttl(later + (const._DNS_HOST_TTL * 1000 / 2)) == 50 - assert record2.get_percentage_remaining_ttl(later + (const._DNS_HOST_TTL * 1000 / 2)) == 50 def test_service_info_dunder(self): type_ = "_test-srvc-type._tcp.local." From 13eab22c3095889f62eda1c7cb4b42a741691420 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 15 Dec 2023 10:04:47 -1000 Subject: [PATCH 2/2] chore: keep the faster test --- tests/test_dns.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/test_dns.py b/tests/test_dns.py index 0eac568dd..055621356 100644 --- a/tests/test_dns.py +++ b/tests/test_dns.py @@ -6,7 +6,6 @@ import logging import os import socket -import time import unittest import unittest.mock @@ -86,9 +85,14 @@ def test_dns_record_abc(self): record.write(None) # type: ignore[arg-type] def test_dns_record_reset_ttl(self): - record = r.DNSRecord('irrelevant', const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL) - time.sleep(1) - record2 = r.DNSRecord('irrelevant', const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL) + start = r.current_time_millis() + record = r.DNSRecord( + 'irrelevant', const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL, created=start + ) + later = start + 1000 + record2 = r.DNSRecord( + 'irrelevant', const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL, created=later + ) now = r.current_time_millis() assert record.created != record2.created