Skip to content

Commit e2b96fe

Browse files
committed
Relocate cache tests to tests/test_cache.py
1 parent 18ddb8d commit e2b96fe

2 files changed

Lines changed: 63 additions & 37 deletions

File tree

tests/test_cache.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

tests/test_dns.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -159,43 +159,6 @@ def test_dns_record_is_recent(self):
159159
assert record.is_recent(now + (8 * 1000)) is False
160160

161161

162-
class TestDNSCache(unittest.TestCase):
163-
def test_order(self):
164-
record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a')
165-
record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b')
166-
cache = r.DNSCache()
167-
cache.add(record1)
168-
cache.add(record2)
169-
entry = r.DNSEntry('a', const._TYPE_SOA, const._CLASS_IN)
170-
cached_record = cache.get(entry)
171-
assert cached_record == record2
172-
173-
def test_cache_empty_does_not_leak_memory_by_leaving_empty_list(self):
174-
record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a')
175-
record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b')
176-
cache = r.DNSCache()
177-
cache.add(record1)
178-
cache.add(record2)
179-
assert 'a' in cache.cache
180-
cache.remove(record1)
181-
cache.remove(record2)
182-
assert 'a' not in cache.cache
183-
184-
def test_cache_empty_multiple_calls_does_not_throw(self):
185-
record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a')
186-
record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b')
187-
cache = r.DNSCache()
188-
cache.add(record1)
189-
cache.add(record2)
190-
assert 'a' in cache.cache
191-
cache.remove(record1)
192-
cache.remove(record2)
193-
# Ensure multiple removes does not throw
194-
cache.remove(record1)
195-
cache.remove(record2)
196-
assert 'a' not in cache.cache
197-
198-
199162
def test_dns_record_hashablity_does_not_consider_ttl():
200163
"""Test DNSRecord are hashable."""
201164

0 commit comments

Comments
 (0)