Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -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
37 changes: 0 additions & 37 deletions tests/test_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down