Commit 1b0b4025 authored by Jerry's avatar Jerry
Browse files

add run_timer unittest



git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac289@2640 e5f2f494-b856-4b98-b285-d166d9295462
parent a18b4e4f
......@@ -342,8 +342,27 @@ class TestZoneMgrRefreshInfo(unittest.TestCase):
self.assertTrue(time2 + MAX_TRANSFER_TIMEOUT >= refresh_timeout)
self.assertFalse("notify_master" in self.zoneinfo._zonemgr_refresh_info[ZONE_NAME_CLASS1].keys())
def test_shutdown(self):
pass
def test_run_timer(self):
time1 = time.time()
self.zoneinfo._zonemgr_refresh_info = {
("sd.cn.", "IN"):{
'last_refresh_time': time1 - 7200,
'next_refresh_time': time1 - 1,
'zone_soa_rdata': 'a.dns.cn. root.cnnic.cn. 2009073105 7200 3600 2419200 21600',
'zone_state': ZONE_OK}
}
master_socket, slave_socket = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM)
self.zoneinfo._socket = slave_socket
# Run timer in daemon thread
listener = threading.Thread(target = self.zoneinfo.run_timer, args = ())
listener.setDaemon(True)
listener.start()
slave_socket.close()
zone_state = self.zoneinfo._zonemgr_refresh_info[ZONE_NAME_CLASS1]["zone_state"]
self.assertTrue("refresh_timeout" in self.zoneinfo._zonemgr_refresh_info[ZONE_NAME_CLASS1].keys())
self.assertTrue(zone_state == ZONE_REFRESHING)
self.assertRaises(ZonemgrException, self.zoneinfo.run_timer)
def tearDown(self):
sys.stdout = self.stdout_backup
......@@ -390,7 +409,7 @@ class TestZonemgr(unittest.TestCase):
def test_parse_cmd_params(self):
params1 = {"zone_name" : "org.cn", "zone_class" : "IN", "master" : "127.0.0.1"}
answer1 = ("org.cn", "IN", "127.0.0.1")
answer1 = (("org.cn", "IN"), "127.0.0.1")
self.assertEqual(answer1, self.zonemgr._parse_cmd_params(params1, ZONE_NOTIFY_COMMAND))
params2 = {"zone_name" : "org.cn", "zone_class" : "IN"}
answer2 = ("org.cn", "IN")
......
......@@ -67,9 +67,9 @@ ZONE_REFRESHING = 1
ZONE_EXPIRED = 2
# smallest refresh timeout
LOWERBOUND_REFRESH = 1
LOWERBOUND_REFRESH = 10
# smallest retry timeout
LOWERBOUND_RETRY = 1
LOWERBOUND_RETRY = 5
# max zone transfer timeout
MAX_TRANSFER_TIMEOUT = 14400
......@@ -297,19 +297,20 @@ class ZoneMgrRefreshInfo:
while True:
# Zonemgr has no zone.
if self._zone_mgr_is_empty():
time.sleep(1) # A better time?
time.sleep(LOWERBOUND_RETRY) # A better time?
continue
zone_need_refresh = self._find_need_do_refresh_zone()
# If don't get zone with minimum next refresh time, set timer timeout = LOWERBOUND_REFRESH
if not zone_need_refresh:
timeout = LOWERBOUND_REFRESH
timeout = LOWERBOUND_RETRY
else:
timeout = self._get_zone_next_refresh_time(zone_need_refresh) - self._get_current_time()
if (timeout < 0):
self._do_refresh(zone_need_refresh)
continue
""" Wait for the socket notification for a maximum time of timeout
in seconds (as float)."""
try:
......@@ -317,29 +318,23 @@ class ZoneMgrRefreshInfo:
if rlist:
self._socket.recv(32)
except ValueError as e:
sys.stderr.write("[b10-zonemgr] Socket has been closed\n")
raise ZonemgrException("[b10-zonemgr] Socket has been closed\n")
break
except select.error as e:
if e.args[0] == errno.EINTR:
(rlist, wlist, xlist) = ([], [], [])
else:
sys.stderr.write("[b10-zonemgr] Error with select(): %s\n" % err)
raise ZonemgrException("[b10-zonemgr] Error with select(): %s\n" % err)
break
def start_timer(zone_refresh_info):
"""Keep track of zone timers"""
zone_refresh_info.run_timer()
class Zonemgr:
def __init__(self, verbose = False):
self._setup_session()
self._db_file = self.get_db_file()
# Create socket pair for communicating between main thread and zonemgr timer thread
self._master_socket, self._slave_scoket = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM)
self._zone_refresh_info = ZoneMgrRefreshInfo(self._cc, self._db_file, self._slave_scoket)
self._master_socket, self._slave_socket = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM)
self._zone_refresh_info = ZoneMgrRefreshInfo(self._cc, self._db_file, self._slave_socket)
self._start_zone_refresh_timer()
self._lock = threading.Lock()
......@@ -347,8 +342,8 @@ class Zonemgr:
self._verbose = verbose
def _start_zone_refresh_timer(self):
"""Start a new thread to run zonemgr refresh timer"""
listener = threading.Thread(target = start_timer, args = (self._zone_refresh_info,))
"""Start a new thread to keep track of zone timers"""
listener = threading.Thread(target = self._zone_refresh_info.run_timer, args = ())
listener.setDaemon(True)
listener.start()
......@@ -372,7 +367,7 @@ class Zonemgr:
def shutdown(self):
self._slave_scoket.close()
self._slave_socket.close()
self._master_socket.close()
self._shutdown_event.set()
......
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