Browse Source

[1429] The socket_request_handler

Michal 'vorner' Vaner 13 years ago
parent
commit
567260cde6
2 changed files with 61 additions and 1 deletions
  1. 9 1
      src/bin/bind10/bind10_src.py.in
  2. 52 0
      src/bin/bind10/tests/bind10_test.py.in

+ 9 - 1
src/bin/bind10/bind10_src.py.in

@@ -73,6 +73,7 @@ from isc.log_messages.bind10_messages import *
 import isc.bind10.component
 import isc.bind10.special_component
 import isc.bind10.socket_cache
+import libutil_io_python
 
 isc.log.init("b10-boss")
 logger = isc.log.Logger("boss")
@@ -820,7 +821,13 @@ class BoB:
         The function looks into the _socket_cache and sends the socket
         identified by the tocken back over the unix_socket.
         """
-        pass
+        try:
+            fd = self._socket_cache.get_socket(token, unix_socket.fileno())
+            unix_socket.sendall("1\n")
+            libutil_io_python.send_fd(unix_socket.fileno(), fd)
+        except Exception as e:
+            # TODO Log
+            unix_socket.sendall("0\n")
 
     def socket_consumer_dead(self, unix_socket):
         """
@@ -828,6 +835,7 @@ class BoB:
         sockets sent to it are to be considered closed. This function signals
         so to the _socket_cache.
         """
+        # TODO Log
         self._socket_cache.drop_application(unix_socket.fileno())
 
     def insert_creator(self, creator):

+ 52 - 0
src/bin/bind10/tests/bind10_test.py.in

@@ -14,6 +14,7 @@
 # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
 from bind10_src import ProcessInfo, BoB, parse_args, dump_pid, unlink_pid_file, _BASETIME
+import bind10_src
 
 # XXX: environment tests are currently disabled, due to the preprocessor
 #      setup that we have now complicating the environment
@@ -112,20 +113,38 @@ class TestCacheCommands(unittest.TestCase):
         # Fake the cache here so we can pretend it is us and hijack the
         # calls to its methods.
         self.__boss._socket_cache = self
+        self.__raise_exception = None
         # What was and wasn't called.
         self.__drop_app_called = None
+        self.__get_socket_called = None
+        self.__send_fd_called = None
+        bind10_src.libutil_io_python.send_fd = self.__send_fd
+
+    def __send_fd(self, to, socket):
+        """
+        A function to hook the send_fd in the bind10_src.
+        """
+        self.__send_fd_called = (to, socket)
 
     class FalseSocket:
         """
         A socket where we can fake methods we need instead of having a real
         socket.
         """
+        def __init__(self):
+            self.send = ""
         def fileno(self):
             """
             The file number. Used for identifying the remote application.
             """
             return 42
 
+        def sendall(self, data):
+            """
+            Adds data to the self.send.
+            """
+            self.send += data
+
     def drop_application(self, application):
         """
         Part of pretending to be the cache. Logs the parameter to
@@ -140,6 +159,39 @@ class TestCacheCommands(unittest.TestCase):
         self.__boss.socket_consumer_dead(self.FalseSocket())
         self.assertEqual(42, self.__drop_app_called)
 
+    def get_socket(self, token, application):
+        """
+        Part of pretending to be the cache. If there's anything in
+        __raise_exception, it is raised. Otherwise, the call is logged
+        into __get_socket_called and a number is returned.
+        """
+        if self.__raise_exception is not None:
+            raise self.__raise_exception
+        self.__get_socket_called = (token, application)
+        return 13
+
+    def test_request_handler(self):
+        """
+        Test that a request for socket is forwarded and the socket is sent
+        back, if it returns a socket.
+        """
+        socket = self.FalseSocket()
+        # An exception from the cache
+        self.__raise_exception = ValueError("Test value error")
+        self.__boss.socket_request_handler("token", socket)
+        # It was called, but it threw, so it is not noted here
+        self.assertIsNone(self.__get_socket_called)
+        self.assertEqual("0\n", socket.send)
+        # It should not have sent any socket.
+        self.assertIsNone(self.__send_fd_called)
+        # Now prepare a valid scenario
+        self.__raise_exception = None
+        socket.send = ""
+        self.__boss.socket_request_handler("token", socket)
+        self.assertEqual("1\n", socket.send)
+        self.assertEqual((42, 13), self.__send_fd_called)
+        self.assertEqual(("token", 42), self.__get_socket_called)
+
 class TestBoB(unittest.TestCase):
     def test_init(self):
         bob = BoB()