|
10 | 10 | import os |
11 | 11 | import unittest |
12 | 12 | from threading import Event |
| 13 | +from unittest.mock import patch |
13 | 14 |
|
14 | 15 | import pytest |
15 | 16 |
|
@@ -95,100 +96,105 @@ def update_service(self, zeroconf, type, name): |
95 | 96 | ) |
96 | 97 | zeroconf_registrar.register_service(info_service) |
97 | 98 |
|
98 | | - try: |
99 | | - service_added.wait(1) |
100 | | - assert service_added.is_set() |
101 | | - |
102 | | - # short pause to allow multicast timers to expire |
103 | | - time.sleep(3) |
104 | | - |
105 | | - # clear the answer cache to force query |
106 | | - _clear_cache(zeroconf_browser) |
107 | | - |
108 | | - cached_info = ServiceInfo(type_, registration_name) |
109 | | - cached_info.load_from_cache(zeroconf_browser) |
110 | | - assert cached_info.properties == {} |
111 | | - |
112 | | - # get service info without answer cache |
113 | | - info = zeroconf_browser.get_service_info(type_, registration_name) |
114 | | - assert info is not None |
115 | | - assert info.properties[b'prop_none'] is None |
116 | | - assert info.properties[b'prop_string'] == properties['prop_string'] |
117 | | - assert info.properties[b'prop_float'] == b'1.0' |
118 | | - assert info.properties[b'prop_blank'] == properties['prop_blank'] |
119 | | - assert info.properties[b'prop_true'] == b'1' |
120 | | - assert info.properties[b'prop_false'] == b'0' |
121 | | - assert info.addresses == addresses[:1] # no V6 by default |
122 | | - assert set(info.addresses_by_version(r.IPVersion.All)) == set(addresses) |
123 | | - |
124 | | - cached_info = ServiceInfo(type_, registration_name) |
125 | | - cached_info.load_from_cache(zeroconf_browser) |
126 | | - assert cached_info.properties is not None |
127 | | - |
128 | | - # Populate the cache |
129 | | - zeroconf_browser.get_service_info(subtype, registration_name) |
130 | | - |
131 | | - # get service info with only the cache |
132 | | - cached_info = ServiceInfo(subtype, registration_name) |
133 | | - cached_info.load_from_cache(zeroconf_browser) |
134 | | - assert cached_info.properties is not None |
135 | | - assert cached_info.properties[b'prop_float'] == b'1.0' |
136 | | - |
137 | | - # get service info with only the cache with the lowercase name |
138 | | - cached_info = ServiceInfo(subtype, registration_name.lower()) |
139 | | - cached_info.load_from_cache(zeroconf_browser) |
140 | | - # Ensure uppercase output is preserved |
141 | | - assert cached_info.name == registration_name |
142 | | - assert cached_info.key == registration_name.lower() |
143 | | - assert cached_info.properties is not None |
144 | | - assert cached_info.properties[b'prop_float'] == b'1.0' |
145 | | - |
146 | | - info = zeroconf_browser.get_service_info(subtype, registration_name) |
147 | | - assert info is not None |
148 | | - assert info.properties is not None |
149 | | - assert info.properties[b'prop_none'] is None |
150 | | - |
151 | | - cached_info = ServiceInfo(subtype, registration_name.lower()) |
152 | | - cached_info.load_from_cache(zeroconf_browser) |
153 | | - assert cached_info.properties is not None |
154 | | - assert cached_info.properties[b'prop_none'] is None |
155 | | - |
156 | | - # test TXT record update |
157 | | - sublistener = MySubListener() |
158 | | - zeroconf_browser.add_service_listener(registration_name, sublistener) |
159 | | - properties['prop_blank'] = b'an updated string' |
160 | | - desc.update(properties) |
161 | | - info_service = ServiceInfo( |
162 | | - subtype, |
163 | | - registration_name, |
164 | | - 80, |
165 | | - 0, |
166 | | - 0, |
167 | | - desc, |
168 | | - "ash-2.local.", |
169 | | - addresses=[socket.inet_aton("10.0.1.2")], |
170 | | - ) |
171 | | - zeroconf_registrar.update_service(info_service) |
172 | | - service_updated.wait(1) |
173 | | - assert service_updated.is_set() |
174 | | - |
175 | | - info = zeroconf_browser.get_service_info(type_, registration_name) |
176 | | - assert info is not None |
177 | | - assert info.properties[b'prop_blank'] == properties['prop_blank'] |
178 | | - |
179 | | - cached_info = ServiceInfo(subtype, registration_name) |
180 | | - cached_info.load_from_cache(zeroconf_browser) |
181 | | - assert cached_info.properties is not None |
182 | | - assert cached_info.properties[b'prop_blank'] == properties['prop_blank'] |
183 | | - |
184 | | - zeroconf_registrar.unregister_service(info_service) |
185 | | - service_removed.wait(1) |
186 | | - assert service_removed.is_set() |
187 | | - |
188 | | - finally: |
189 | | - zeroconf_registrar.close() |
190 | | - zeroconf_browser.remove_service_listener(listener) |
191 | | - zeroconf_browser.close() |
| 99 | + with patch.object( |
| 100 | + zeroconf_registrar.engine.protocols[0], "suppress_duplicate_packet", return_value=False |
| 101 | + ), patch.object( |
| 102 | + zeroconf_registrar.engine.protocols[1], "suppress_duplicate_packet", return_value=False |
| 103 | + ): |
| 104 | + try: |
| 105 | + service_added.wait(1) |
| 106 | + assert service_added.is_set() |
| 107 | + |
| 108 | + # short pause to allow multicast timers to expire |
| 109 | + time.sleep(3) |
| 110 | + |
| 111 | + # clear the answer cache to force query |
| 112 | + _clear_cache(zeroconf_browser) |
| 113 | + |
| 114 | + cached_info = ServiceInfo(type_, registration_name) |
| 115 | + cached_info.load_from_cache(zeroconf_browser) |
| 116 | + assert cached_info.properties == {} |
| 117 | + |
| 118 | + # get service info without answer cache |
| 119 | + info = zeroconf_browser.get_service_info(type_, registration_name) |
| 120 | + assert info is not None |
| 121 | + assert info.properties[b'prop_none'] is None |
| 122 | + assert info.properties[b'prop_string'] == properties['prop_string'] |
| 123 | + assert info.properties[b'prop_float'] == b'1.0' |
| 124 | + assert info.properties[b'prop_blank'] == properties['prop_blank'] |
| 125 | + assert info.properties[b'prop_true'] == b'1' |
| 126 | + assert info.properties[b'prop_false'] == b'0' |
| 127 | + assert info.addresses == addresses[:1] # no V6 by default |
| 128 | + assert set(info.addresses_by_version(r.IPVersion.All)) == set(addresses) |
| 129 | + |
| 130 | + cached_info = ServiceInfo(type_, registration_name) |
| 131 | + cached_info.load_from_cache(zeroconf_browser) |
| 132 | + assert cached_info.properties is not None |
| 133 | + |
| 134 | + # Populate the cache |
| 135 | + zeroconf_browser.get_service_info(subtype, registration_name) |
| 136 | + |
| 137 | + # get service info with only the cache |
| 138 | + cached_info = ServiceInfo(subtype, registration_name) |
| 139 | + cached_info.load_from_cache(zeroconf_browser) |
| 140 | + assert cached_info.properties is not None |
| 141 | + assert cached_info.properties[b'prop_float'] == b'1.0' |
| 142 | + |
| 143 | + # get service info with only the cache with the lowercase name |
| 144 | + cached_info = ServiceInfo(subtype, registration_name.lower()) |
| 145 | + cached_info.load_from_cache(zeroconf_browser) |
| 146 | + # Ensure uppercase output is preserved |
| 147 | + assert cached_info.name == registration_name |
| 148 | + assert cached_info.key == registration_name.lower() |
| 149 | + assert cached_info.properties is not None |
| 150 | + assert cached_info.properties[b'prop_float'] == b'1.0' |
| 151 | + |
| 152 | + info = zeroconf_browser.get_service_info(subtype, registration_name) |
| 153 | + assert info is not None |
| 154 | + assert info.properties is not None |
| 155 | + assert info.properties[b'prop_none'] is None |
| 156 | + |
| 157 | + cached_info = ServiceInfo(subtype, registration_name.lower()) |
| 158 | + cached_info.load_from_cache(zeroconf_browser) |
| 159 | + assert cached_info.properties is not None |
| 160 | + assert cached_info.properties[b'prop_none'] is None |
| 161 | + |
| 162 | + # test TXT record update |
| 163 | + sublistener = MySubListener() |
| 164 | + zeroconf_browser.add_service_listener(registration_name, sublistener) |
| 165 | + properties['prop_blank'] = b'an updated string' |
| 166 | + desc.update(properties) |
| 167 | + info_service = ServiceInfo( |
| 168 | + subtype, |
| 169 | + registration_name, |
| 170 | + 80, |
| 171 | + 0, |
| 172 | + 0, |
| 173 | + desc, |
| 174 | + "ash-2.local.", |
| 175 | + addresses=[socket.inet_aton("10.0.1.2")], |
| 176 | + ) |
| 177 | + zeroconf_registrar.update_service(info_service) |
| 178 | + service_updated.wait(1) |
| 179 | + assert service_updated.is_set() |
| 180 | + |
| 181 | + info = zeroconf_browser.get_service_info(type_, registration_name) |
| 182 | + assert info is not None |
| 183 | + assert info.properties[b'prop_blank'] == properties['prop_blank'] |
| 184 | + |
| 185 | + cached_info = ServiceInfo(subtype, registration_name) |
| 186 | + cached_info.load_from_cache(zeroconf_browser) |
| 187 | + assert cached_info.properties is not None |
| 188 | + assert cached_info.properties[b'prop_blank'] == properties['prop_blank'] |
| 189 | + |
| 190 | + zeroconf_registrar.unregister_service(info_service) |
| 191 | + service_removed.wait(1) |
| 192 | + assert service_removed.is_set() |
| 193 | + |
| 194 | + finally: |
| 195 | + zeroconf_registrar.close() |
| 196 | + zeroconf_browser.remove_service_listener(listener) |
| 197 | + zeroconf_browser.close() |
192 | 198 |
|
193 | 199 |
|
194 | 200 | def test_servicelisteners_raise_not_implemented(): |
|
0 commit comments