fix: ensure cache does not return stale created and ttl values (#1469) · python-zeroconf/python-zeroconf@e05055c
@@ -279,3 +279,82 @@ def test_name(self):
279279cache = r.DNSCache()
280280cache.async_add_records([record1, record2])
281281assert cache.names() == ["irrelevant"]
282+283+284+def test_async_entries_with_name_returns_newest_record():
285+cache = r.DNSCache()
286+record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=1.0)
287+record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=2.0)
288+cache.async_add_records([record1])
289+cache.async_add_records([record2])
290+assert next(iter(cache.async_entries_with_name("a"))) is record2
291+292+293+def test_async_entries_with_server_returns_newest_record():
294+cache = r.DNSCache()
295+record1 = r.DNSService("a", const._TYPE_SRV, const._CLASS_IN, 1, 1, 1, 1, "a", created=1.0)
296+record2 = r.DNSService("a", const._TYPE_SRV, const._CLASS_IN, 1, 1, 1, 1, "a", created=2.0)
297+cache.async_add_records([record1])
298+cache.async_add_records([record2])
299+assert next(iter(cache.async_entries_with_server("a"))) is record2
300+301+302+def test_async_get_returns_newest_record():
303+cache = r.DNSCache()
304+record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=1.0)
305+record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=2.0)
306+cache.async_add_records([record1])
307+cache.async_add_records([record2])
308+assert cache.get(record2) is record2
309+310+311+def test_async_get_returns_newest_nsec_record():
312+cache = r.DNSCache()
313+record1 = r.DNSNsec("a", const._TYPE_NSEC, const._CLASS_IN, 1, "a", [], created=1.0)
314+record2 = r.DNSNsec("a", const._TYPE_NSEC, const._CLASS_IN, 1, "a", [], created=2.0)
315+cache.async_add_records([record1])
316+cache.async_add_records([record2])
317+assert cache.get(record2) is record2
318+319+320+def test_get_by_details_returns_newest_record():
321+cache = r.DNSCache()
322+record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=1.0)
323+record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=2.0)
324+cache.async_add_records([record1])
325+cache.async_add_records([record2])
326+assert cache.get_by_details("a", const._TYPE_A, const._CLASS_IN) is record2
327+328+329+def test_get_all_by_details_returns_newest_record():
330+cache = r.DNSCache()
331+record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=1.0)
332+record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=2.0)
333+cache.async_add_records([record1])
334+cache.async_add_records([record2])
335+records = cache.get_all_by_details("a", const._TYPE_A, const._CLASS_IN)
336+assert len(records) == 1
337+assert records[0] is record2
338+339+340+def test_async_get_all_by_details_returns_newest_record():
341+cache = r.DNSCache()
342+record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=1.0)
343+record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=2.0)
344+cache.async_add_records([record1])
345+cache.async_add_records([record2])
346+records = cache.async_all_by_details("a", const._TYPE_A, const._CLASS_IN)
347+assert len(records) == 1
348+assert records[0] is record2
349+350+351+def test_async_get_unique_returns_newest_record():
352+cache = r.DNSCache()
353+record1 = r.DNSPointer("a", const._TYPE_PTR, const._CLASS_IN, 1, "a", created=1.0)
354+record2 = r.DNSPointer("a", const._TYPE_PTR, const._CLASS_IN, 1, "a", created=2.0)
355+cache.async_add_records([record1])
356+cache.async_add_records([record2])
357+record = cache.async_get_unique(record1)
358+assert record is record2
359+record = cache.async_get_unique(record2)
360+assert record is record2