diff --git a/tests/test_cache.py b/tests/test_cache.py new file mode 100644 index 000000000..8580b3669 --- /dev/null +++ b/tests/test_cache.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +""" Unit tests for zeroconf._cache. """ + +import logging +import unittest +import unittest.mock + +import zeroconf as r +from zeroconf import const + +log = logging.getLogger('zeroconf') +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +class TestDNSCache(unittest.TestCase): + def test_order(self): + record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a') + record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b') + cache = r.DNSCache() + cache.add(record1) + cache.add(record2) + entry = r.DNSEntry('a', const._TYPE_SOA, const._CLASS_IN) + cached_record = cache.get(entry) + assert cached_record == record2 + + def test_cache_empty_does_not_leak_memory_by_leaving_empty_list(self): + record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a') + record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b') + cache = r.DNSCache() + cache.add(record1) + cache.add(record2) + assert 'a' in cache.cache + cache.remove(record1) + cache.remove(record2) + assert 'a' not in cache.cache + + def test_cache_empty_multiple_calls_does_not_throw(self): + record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a') + record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b') + cache = r.DNSCache() + cache.add(record1) + cache.add(record2) + assert 'a' in cache.cache + cache.remove(record1) + cache.remove(record2) + # Ensure multiple removes does not throw + cache.remove(record1) + cache.remove(record2) + assert 'a' not in cache.cache diff --git a/tests/test_dns.py b/tests/test_dns.py index 557802e18..197357067 100644 --- a/tests/test_dns.py +++ b/tests/test_dns.py @@ -159,43 +159,6 @@ def test_dns_record_is_recent(self): assert record.is_recent(now + (8 * 1000)) is False -class TestDNSCache(unittest.TestCase): - def test_order(self): - record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a') - record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b') - cache = r.DNSCache() - cache.add(record1) - cache.add(record2) - entry = r.DNSEntry('a', const._TYPE_SOA, const._CLASS_IN) - cached_record = cache.get(entry) - assert cached_record == record2 - - def test_cache_empty_does_not_leak_memory_by_leaving_empty_list(self): - record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a') - record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b') - cache = r.DNSCache() - cache.add(record1) - cache.add(record2) - assert 'a' in cache.cache - cache.remove(record1) - cache.remove(record2) - assert 'a' not in cache.cache - - def test_cache_empty_multiple_calls_does_not_throw(self): - record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a') - record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b') - cache = r.DNSCache() - cache.add(record1) - cache.add(record2) - assert 'a' in cache.cache - cache.remove(record1) - cache.remove(record2) - # Ensure multiple removes does not throw - cache.remove(record1) - cache.remove(record2) - assert 'a' not in cache.cache - - def test_dns_record_hashablity_does_not_consider_ttl(): """Test DNSRecord are hashable."""