Commit 078162ae authored by JINMEI Tatuya's avatar JINMEI Tatuya
Browse files

[trac964] pre-work refactoring: delay the creation of socket until the time

of sending a notify.  adjusting the main code and tests to this behavior,
while keeping externally observable behavior as much as possible.
parent c589e8b9
......@@ -66,7 +66,7 @@ class ZoneNotifyInfo:
self.zone_name = zone_name_
self.zone_class = class_
self.notify_msg_id = 0
self.notify_timeout = 0
self.notify_timeout = None
self.notify_try_num = 0 #Notify times sending to one target.
def set_next_notify_target(self):
......@@ -77,8 +77,7 @@ class ZoneNotifyInfo:
self._notify_current = None
def prepare_notify_out(self):
'''Create the socket and set notify timeout time to now'''
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #TODO support IPv6?
'''Set notify timeout time to now'''
self.notify_timeout = time.time()
self.notify_try_num = 0
self._slave_index = 0
......@@ -89,6 +88,12 @@ class ZoneNotifyInfo:
if self._sock:
self._sock.close()
self._sock = None
self.notify_timeout = None
def create_socket(self, addrinfo):
# XXX we'll fix this
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return self._sock
def get_socket(self):
return self._sock
......@@ -270,8 +275,15 @@ class NotifyOut:
sock = self._notify_infos[info].get_socket()
if sock:
valid_socks.append(sock)
# If a non null timeout is specified notify has been scheduled
# (in which case socket is still None) or sent (with a valid
# socket). In either case we need add the zone to notifying_zones
# so that we can invoke the appropriate event for the zone after
# select.
tmp_timeout = self._notify_infos[info].notify_timeout
if tmp_timeout is not None:
notifying_zones[info] = self._notify_infos[info]
tmp_timeout = self._notify_infos[info].notify_timeout
if min_timeout is not None:
if tmp_timeout < min_timeout:
min_timeout = tmp_timeout
......@@ -380,7 +392,7 @@ class NotifyOut:
render.set_length_limit(512)
msg.to_wire(render)
zone_notify_info.notify_msg_id = qid
sock = zone_notify_info.get_socket()
sock = zone_notify_info.create_socket(addrinfo)
try:
sock.sendto(render.get_data(), 0, addrinfo)
self._log_msg('info', 'sending notify to %s' % addr_to_str(addrinfo))
......
......@@ -49,12 +49,13 @@ class MockSocket():
return self._remote_sock
# We subclass the ZoneNotifyInfo class we're testing here, only
# to override the prepare_notify_out() method.
# to override the create_socket() method.
class MockZoneNotifyInfo(notify_out.ZoneNotifyInfo):
def prepare_notify_out(self):
super().prepare_notify_out();
def create_socket(self, addrinfo):
super().create_socket(addrinfo)
self._sock.close()
self._sock = MockSocket()
return self._sock
class TestZoneNotifyInfo(unittest.TestCase):
def setUp(self):
......@@ -62,11 +63,12 @@ class TestZoneNotifyInfo(unittest.TestCase):
def test_prepare_finish_notify_out(self):
self.info.prepare_notify_out()
self.assertNotEqual(self.info._sock, None)
self.assertNotEqual(self.info.notify_timeout, None)
self.assertIsNone(self.info._notify_current)
self.info.finish_notify_out()
self.assertEqual(self.info._sock, None)
self.assertEqual(self.info.notify_timeout, None)
def test_set_next_notify_target(self):
self.info.notify_slaves.append(('127.0.0.1', 53))
......@@ -153,6 +155,11 @@ class TestNotifyOut(unittest.TestCase):
self.assertEqual(len(replied_zones), 0)
self.assertEqual(len(timeout_zones), 2)
# Trigger timeout events to "send" notifies via a mock socket
for zone in timeout_zones:
self._notify._zone_notify_handler(timeout_zones[zone],
notify_out._EVENT_TIMEOUT)
# Now make one socket be readable
self._notify._notify_infos[('example.net.', 'IN')].notify_timeout = time.time() + 10
self._notify._notify_infos[('example.com.', 'IN')].notify_timeout = time.time() + 10
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment