|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | + |
| 5 | +""" Unit tests for zeroconf._cache. """ |
| 6 | + |
| 7 | +import logging |
| 8 | +import unittest |
| 9 | +import unittest.mock |
| 10 | + |
| 11 | +import zeroconf as r |
| 12 | +from zeroconf import const |
| 13 | + |
| 14 | +log = logging.getLogger('zeroconf') |
| 15 | +original_logging_level = logging.NOTSET |
| 16 | + |
| 17 | + |
| 18 | +def setup_module(): |
| 19 | + global original_logging_level |
| 20 | + original_logging_level = log.level |
| 21 | + log.setLevel(logging.DEBUG) |
| 22 | + |
| 23 | + |
| 24 | +def teardown_module(): |
| 25 | + if original_logging_level != logging.NOTSET: |
| 26 | + log.setLevel(original_logging_level) |
| 27 | + |
| 28 | + |
| 29 | +class TestDNSCache(unittest.TestCase): |
| 30 | + def test_order(self): |
| 31 | + record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a') |
| 32 | + record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b') |
| 33 | + cache = r.DNSCache() |
| 34 | + cache.add(record1) |
| 35 | + cache.add(record2) |
| 36 | + entry = r.DNSEntry('a', const._TYPE_SOA, const._CLASS_IN) |
| 37 | + cached_record = cache.get(entry) |
| 38 | + assert cached_record == record2 |
| 39 | + |
| 40 | + def test_cache_empty_does_not_leak_memory_by_leaving_empty_list(self): |
| 41 | + record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a') |
| 42 | + record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b') |
| 43 | + cache = r.DNSCache() |
| 44 | + cache.add(record1) |
| 45 | + cache.add(record2) |
| 46 | + assert 'a' in cache.cache |
| 47 | + cache.remove(record1) |
| 48 | + cache.remove(record2) |
| 49 | + assert 'a' not in cache.cache |
| 50 | + |
| 51 | + def test_cache_empty_multiple_calls_does_not_throw(self): |
| 52 | + record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a') |
| 53 | + record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b') |
| 54 | + cache = r.DNSCache() |
| 55 | + cache.add(record1) |
| 56 | + cache.add(record2) |
| 57 | + assert 'a' in cache.cache |
| 58 | + cache.remove(record1) |
| 59 | + cache.remove(record2) |
| 60 | + # Ensure multiple removes does not throw |
| 61 | + cache.remove(record1) |
| 62 | + cache.remove(record2) |
| 63 | + assert 'a' not in cache.cache |
0 commit comments