From 8ff021ac9f7371901d05a98ec56ce7a59fa0c4bf Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 15 Dec 2023 08:54:28 -1000 Subject: [PATCH 1/8] chore: add get_percentage_remaining_ttl helper to DNSRecord This is an internal helper --- src/zeroconf/_dns.pxd | 2 ++ src/zeroconf/_dns.py | 5 +++++ tests/test_dns.py | 18 ++++++++++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/zeroconf/_dns.pxd b/src/zeroconf/_dns.pxd index d4116a66..86af4c80 100644 --- a/src/zeroconf/_dns.pxd +++ b/src/zeroconf/_dns.pxd @@ -54,6 +54,8 @@ cdef class DNSRecord(DNSEntry): cpdef get_remaining_ttl(self, double now) + cpdef double 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 66fb5b86..7ad5cea2 100644 --- a/src/zeroconf/_dns.py +++ b/src/zeroconf/_dns.py @@ -193,6 +193,11 @@ 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) -> float: + """Returns the percentage remaining off the ttl.""" + remain = (self.created + (_EXPIRE_FULL_TIME_MS * self.ttl) - now) / 1000.0 + return 0 if remain < 0 else (remain / self.ttl) * 100 + # 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 0eac568d..cdd677ae 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,19 +85,30 @@ 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 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(now) < 100 + assert record2.get_percentage_remaining_ttl(later) == 100 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 def test_service_info_dunder(self): type_ = "_test-srvc-type._tcp.local." From 7c68ccf7fd2aaf8a48666997d94d4c7e7833c890 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 15 Dec 2023 08:55:41 -1000 Subject: [PATCH 2/8] chore: docstring --- src/zeroconf/_dns.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zeroconf/_dns.py b/src/zeroconf/_dns.py index 7ad5cea2..580dc7c3 100644 --- a/src/zeroconf/_dns.py +++ b/src/zeroconf/_dns.py @@ -194,7 +194,7 @@ def get_expiration_time(self, percent: _int) -> float: return self.created + (percent * self.ttl * 10) def get_percentage_remaining_ttl(self, now: _float) -> float: - """Returns the percentage remaining off the ttl.""" + """Returns the percentage remaining of the ttl between 0-100.""" remain = (self.created + (_EXPIRE_FULL_TIME_MS * self.ttl) - now) / 1000.0 return 0 if remain < 0 else (remain / self.ttl) * 100 From 2b0dc785f76c356bcad8dccb2679d1a44517cf83 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 15 Dec 2023 09:01:54 -1000 Subject: [PATCH 3/8] fix: reduce precision loss --- src/zeroconf/_dns.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/zeroconf/_dns.py b/src/zeroconf/_dns.py index 580dc7c3..937c6d8e 100644 --- a/src/zeroconf/_dns.py +++ b/src/zeroconf/_dns.py @@ -195,8 +195,8 @@ def get_expiration_time(self, percent: _int) -> float: def get_percentage_remaining_ttl(self, now: _float) -> float: """Returns the percentage remaining of the ttl between 0-100.""" - remain = (self.created + (_EXPIRE_FULL_TIME_MS * self.ttl) - now) / 1000.0 - return 0 if remain < 0 else (remain / self.ttl) * 100 + remain = (self.created + (_EXPIRE_FULL_TIME_MS * self.ttl) - now) / self.ttl / 10 + return 0 if remain < 0 else remain # TODO: Switch to just int here def get_remaining_ttl(self, now: _float) -> Union[int, float]: From 309d894e0958560d4d18656c76702db2e987a492 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 15 Dec 2023 09:02:53 -1000 Subject: [PATCH 4/8] chore: more tests --- tests/test_dns.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_dns.py b/tests/test_dns.py index cdd677ae..227be8bc 100644 --- a/tests/test_dns.py +++ b/tests/test_dns.py @@ -100,6 +100,7 @@ def test_dns_record_reset_ttl(self): assert record.get_percentage_remaining_ttl(now) != record2.get_percentage_remaining_ttl(now) assert record.get_percentage_remaining_ttl(now) < 100 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) From 46c102c5a1a2ad3be20499896eb94bd490b0df22 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 15 Dec 2023 09:07:49 -1000 Subject: [PATCH 5/8] fix: drop float precision --- src/zeroconf/_dns.pxd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zeroconf/_dns.pxd b/src/zeroconf/_dns.pxd index 86af4c80..72080517 100644 --- a/src/zeroconf/_dns.pxd +++ b/src/zeroconf/_dns.pxd @@ -54,7 +54,7 @@ cdef class DNSRecord(DNSEntry): cpdef get_remaining_ttl(self, double now) - cpdef double get_percentage_remaining_ttl(self, double now) + cpdef unsigned int get_percentage_remaining_ttl(self, double now) cpdef double get_expiration_time(self, cython.uint percent) From be9db707ee0792cf11b5213ed0b9b83d1b35b261 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 15 Dec 2023 09:08:35 -1000 Subject: [PATCH 6/8] fix: drop float precision --- src/zeroconf/_dns.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/zeroconf/_dns.py b/src/zeroconf/_dns.py index 937c6d8e..ae677681 100644 --- a/src/zeroconf/_dns.py +++ b/src/zeroconf/_dns.py @@ -193,10 +193,10 @@ 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) -> float: + 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 remain + return 0 if remain <= 0 else int(remain) # TODO: Switch to just int here def get_remaining_ttl(self, now: _float) -> Union[int, float]: From 9df2d17d1f8abd0032258b8b4c7de3ad24cc3258 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 15 Dec 2023 09:13:47 -1000 Subject: [PATCH 7/8] fix: drop float precision --- src/zeroconf/_dns.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zeroconf/_dns.py b/src/zeroconf/_dns.py index ae677681..262dbb5f 100644 --- a/src/zeroconf/_dns.py +++ b/src/zeroconf/_dns.py @@ -196,7 +196,7 @@ def get_expiration_time(self, percent: _int) -> float: 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 int(remain) + return 0 if remain <= 0 else round(remain) # TODO: Switch to just int here def get_remaining_ttl(self, now: _float) -> Union[int, float]: From 878df42dc3a35cc8eaca64a473dceb7e4a62ea7a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 15 Dec 2023 09:19:12 -1000 Subject: [PATCH 8/8] fix: drop float precision --- tests/test_dns.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_dns.py b/tests/test_dns.py index 227be8bc..b7e5a879 100644 --- a/tests/test_dns.py +++ b/tests/test_dns.py @@ -98,7 +98,6 @@ def test_dns_record_reset_ttl(self): 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(now) < 100 assert record2.get_percentage_remaining_ttl(later) == 100 assert record2.get_percentage_remaining_ttl(later + (const._DNS_HOST_TTL * 1000 / 2)) == 50 @@ -110,6 +109,8 @@ def test_dns_record_reset_ttl(self): 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."