Browse Source

[2225] changed according the changes of the counter class

 - implemented a dummy counter class inside of the notifyout test and made it
   use it.
Naoki Kambe 12 years ago
parent
commit
365e3472c5

+ 7 - 15
src/lib/python/isc/notify/notify_out.py

@@ -1,4 +1,4 @@
-# Copyright (C) 2010  Internet Systems Consortium.
+# Copyright (C) 2010-2012  Internet Systems Consortium.
 #
 # Permission to use, copy, modify, and distribute this software for any
 # purpose with or without fee is hereby granted, provided that the above
@@ -25,6 +25,7 @@ from isc.datasrc import DataSourceClient
 from isc.net import addr
 import isc
 from isc.log_messages.notify_out_messages import *
+from isc.statistics import counter
 
 logger = isc.log.Logger("notify_out")
 
@@ -127,8 +128,7 @@ class NotifyOut:
     notify message to its slaves). notify service can be started by
     calling  dispatcher(), and it can be stopped by calling shutdown()
     in another thread. '''
-    def __init__(self, datasrc_file, counter_handler=None, verbose=True,
-                 counter_notifyoutv4=None, counter_notifyoutv6=None):
+    def __init__(self, datasrc_file, counter_handler=None, verbose=True):
         self._notify_infos = {} # key is (zone_name, zone_class)
         self._waiting_zones = []
         self._notifying_zones = []
@@ -143,10 +143,6 @@ class NotifyOut:
         # Use nonblock event to eliminate busy loop
         # If there are no notifying zones, clear the event bit and wait.
         self._nonblock_event = threading.Event()
-        # Set counter handlers for counting notifies. An argument is
-        # required for zone name.
-        self._counter_notifyoutv4 = counter_notifyoutv4
-        self._counter_notifyoutv6 = counter_notifyoutv6
 
     def _init_notify_out(self, datasrc_file):
         '''Get all the zones name and its notify target's address.
@@ -484,14 +480,10 @@ class NotifyOut:
             sock = zone_notify_info.create_socket(addrinfo[0])
             sock.sendto(render.get_data(), 0, addrinfo)
             # count notifying by IPv4 or IPv6 for statistics
-            if zone_notify_info.get_socket().family \
-                    == socket.AF_INET \
-                    and self._counter_notifyoutv4 is not None:
-                self._counter_notifyoutv4(zone_notify_info.zone_name)
-            elif zone_notify_info.get_socket().family \
-                    == socket.AF_INET6 \
-                    and self._counter_notifyoutv6 is not None:
-                self._counter_notifyoutv6(zone_notify_info.zone_name)
+            if zone_notify_info.get_socket().family == socket.AF_INET:
+                counter.inc_notifyoutv4(zone_notify_info.zone_name)
+            elif zone_notify_info.get_socket().family == socket.AF_INET6:
+                counter.inc_notifyoutv6(zone_notify_info.zone_name)
             logger.info(NOTIFY_OUT_SENDING_NOTIFY, addrinfo[0],
                         addrinfo[1])
         except (socket.error, addr.InvalidAddress) as err:

+ 40 - 56
src/lib/python/isc/notify/tests/notify_out_test.py

@@ -1,4 +1,4 @@
-# Copyright (C) 2010  Internet Systems Consortium.
+# Copyright (C) 2010-2012  Internet Systems Consortium.
 #
 # Permission to use, copy, modify, and distribute this software for any
 # purpose with or without fee is hereby granted, provided that the above
@@ -92,17 +92,28 @@ class TestZoneNotifyInfo(unittest.TestCase):
         temp_info.prepare_notify_out()
         self.assertIsNone(temp_info.get_current_notify_target())
 
+class DummyNotifyOutCounter:
+    _notifiesv4 = []
+    _notifiesv6 = []
+    def inc_notifyoutv4(self, arg):
+        self._notifiesv4.append(arg)
+    def inc_notifyoutv6(self, arg):
+        self._notifiesv6.append(arg)
+    def get_notifyoutv4(self, arg):
+        return self._notifiesv4.count(arg)
+    def get_notifyoutv6(self, arg):
+        return self._notifiesv6.count(arg)
+    def clear_counters(self):
+        self._notifiesv4 = []
+        self._notifiesv6 = []
+
+counter = DummyNotifyOutCounter()
+notify_out.counter = counter
 
 class TestNotifyOut(unittest.TestCase):
     def setUp(self):
         self._db_file = TESTDATA_SRCDIR + '/test.sqlite3'
-        self._notifiedv4_zone_name = None
-        def _dummy_counter_notifyoutv4(z): self._notifiedv4_zone_name = z
-        self._notifiedv6_zone_name = None
-        def _dummy_counter_notifyoutv6(z): self._notifiedv6_zone_name = z
-        self._notify = notify_out.NotifyOut(self._db_file,
-                                            counter_notifyoutv4=_dummy_counter_notifyoutv4,
-                                            counter_notifyoutv6=_dummy_counter_notifyoutv6)
+        self._notify = notify_out.NotifyOut(self._db_file)
         self._notify._notify_infos[('example.com.', 'IN')] = MockZoneNotifyInfo('example.com.', 'IN')
         self._notify._notify_infos[('example.com.', 'CH')] = MockZoneNotifyInfo('example.com.', 'CH')
         self._notify._notify_infos[('example.net.', 'IN')] = MockZoneNotifyInfo('example.net.', 'IN')
@@ -117,6 +128,9 @@ class TestNotifyOut(unittest.TestCase):
         com_ch_info = self._notify._notify_infos[('example.com.', 'CH')]
         com_ch_info.notify_slaves.append(('1.1.1.1', 5353))
 
+    def tearDown(self):
+        counter.clear_counters()
+
     def test_send_notify(self):
         notify_out._MAX_NOTIFY_NUM = 2
 
@@ -268,77 +282,47 @@ class TestNotifyOut(unittest.TestCase):
 
     def test_send_notify_message_udp_ipv4(self):
         example_com_info = self._notify._notify_infos[('example.net.', 'IN')]
+
+        self.assertEqual(counter.get_notifyoutv4('example.net.'), 0)
+        self.assertEqual(counter.get_notifyoutv6('example.net.'), 0)
+
         example_com_info.prepare_notify_out()
-        self.assertIsNone(self._notifiedv4_zone_name)
-        self.assertIsNone(self._notifiedv6_zone_name)
         ret = self._notify._send_notify_message_udp(example_com_info,
                                                     ('192.0.2.1', 53))
         self.assertTrue(ret)
         self.assertEqual(socket.AF_INET, example_com_info.sock_family)
-        self.assertEqual(self._notifiedv4_zone_name, 'example.net.')
-        self.assertIsNone(self._notifiedv6_zone_name)
+        self.assertGreater(counter.get_notifyoutv4('example.net.'), 0)
+        self.assertEqual(counter.get_notifyoutv6('example.net.'), 0)
 
     def test_send_notify_message_udp_ipv6(self):
         example_com_info = self._notify._notify_infos[('example.net.', 'IN')]
-        self.assertIsNone(self._notifiedv4_zone_name)
-        self.assertIsNone(self._notifiedv6_zone_name)
+
+        self.assertEqual(counter.get_notifyoutv4('example.net.'), 0)
+        self.assertEqual(counter.get_notifyoutv6('example.net.'), 0)
+
         ret = self._notify._send_notify_message_udp(example_com_info,
                                                     ('2001:db8::53', 53))
         self.assertTrue(ret)
         self.assertEqual(socket.AF_INET6, example_com_info.sock_family)
-        self.assertIsNone(self._notifiedv4_zone_name)
-        self.assertEqual(self._notifiedv6_zone_name, 'example.net.')
-
-    def test_send_notify_message_udp_ipv4_with_nonetype_notifyoutv4(self):
-        example_com_info = self._notify._notify_infos[('example.net.', 'IN')]
-        example_com_info.prepare_notify_out()
-        self.assertIsNone(self._notifiedv4_zone_name)
-        self.assertIsNone(self._notifiedv6_zone_name)
-        self._notify._counter_notifyoutv4 = None
-        self._notify._send_notify_message_udp(example_com_info,
-                                              ('192.0.2.1', 53))
-        self.assertIsNone(self._notifiedv4_zone_name)
-        self.assertIsNone(self._notifiedv6_zone_name)
-
-    def test_send_notify_message_udp_ipv4_with_notcallable_notifyoutv4(self):
-        example_com_info = self._notify._notify_infos[('example.net.', 'IN')]
-        example_com_info.prepare_notify_out()
-        self._notify._counter_notifyoutv4 = 'NOT CALLABLE'
-        self.assertRaises(TypeError,
-                          self._notify._send_notify_message_udp,
-                          example_com_info, ('192.0.2.1', 53))
-
-    def test_send_notify_message_udp_ipv6_with_nonetype_notifyoutv6(self):
-        example_com_info = self._notify._notify_infos[('example.net.', 'IN')]
-        self.assertIsNone(self._notifiedv4_zone_name)
-        self.assertIsNone(self._notifiedv6_zone_name)
-        self._notify._counter_notifyoutv6 = None
-        self._notify._send_notify_message_udp(example_com_info,
-                                              ('2001:db8::53', 53))
-        self.assertIsNone(self._notifiedv4_zone_name)
-        self.assertIsNone(self._notifiedv6_zone_name)
-
-    def test_send_notify_message_udp_ipv6_with_notcallable_notifyoutv6(self):
-        example_com_info = self._notify._notify_infos[('example.net.', 'IN')]
-        self._notify._counter_notifyoutv6 = 'NOT CALLABLE'
-        self.assertRaises(TypeError,
-                          self._notify._send_notify_message_udp,
-                          example_com_info, ('2001:db8::53', 53))
+        self.assertEqual(counter.get_notifyoutv4('example.net.'), 0)
+        self.assertGreater(counter.get_notifyoutv6('example.net.'), 0)
 
     def test_send_notify_message_with_bogus_address(self):
         example_com_info = self._notify._notify_infos[('example.net.', 'IN')]
 
+        self.assertEqual(counter.get_notifyoutv4('example.net.'), 0)
+        self.assertEqual(counter.get_notifyoutv6('example.net.'), 0)
+
         # As long as the underlying data source validates RDATA this shouldn't
         # happen, but right now it's not actually the case.  Even if the
         # data source does its job, it's prudent to confirm the behavior for
         # an unexpected case.
-        self.assertIsNone(self._notifiedv4_zone_name)
-        self.assertIsNone(self._notifiedv6_zone_name)
         ret = self._notify._send_notify_message_udp(example_com_info,
                                                     ('invalid', 53))
         self.assertFalse(ret)
-        self.assertIsNone(self._notifiedv4_zone_name)
-        self.assertIsNone(self._notifiedv6_zone_name)
+
+        self.assertEqual(counter.get_notifyoutv4('example.net.'), 0)
+        self.assertEqual(counter.get_notifyoutv6('example.net.'), 0)
 
     def test_zone_notify_handler(self):
         old_send_msg = self._notify._send_notify_message_udp