def test_open_socket_parameter(self): self.assertFalse(os.path.exists("./my_socket_file")) msgq = MsgQ("./my_socket_file") msgq.setup() self.assertTrue(os.path.exists("./my_socket_file")) msgq.shutdown() self.assertFalse(os.path.exists("./my_socket_file"))
def test_open_socket_default(self): env_var = None orig_socket_file = None if "BUNDY_MSGQ_SOCKET_FILE" in os.environ: env_var = os.environ["BUNDY_MSGQ_SOCKET_FILE"] del os.environ["BUNDY_MSGQ_SOCKET_FILE"] # temporarily replace the class "default" not to be disrupted by # any running BUNDY instance. if "BUNDY_TEST_SOCKET_FILE" in os.environ: MsgQ.SOCKET_FILE = os.environ["BUNDY_TEST_SOCKET_FILE"] socket_file = MsgQ.SOCKET_FILE self.assertFalse(os.path.exists(socket_file)) msgq = MsgQ() try: msgq.setup() self.assertTrue(os.path.exists(socket_file)) msgq.shutdown() self.assertFalse(os.path.exists(socket_file)) except socket.error: # ok, the install path doesn't exist at all, # so we can't check any further pass if env_var is not None: os.environ["BUNDY_MSGQ_SOCKET_FILE"] = env_var if orig_socket_file is not None: MsgQ.SOCKET_FILE = orig_socket_file
def test_open_socket_environment_variable(self): self.assertFalse(os.path.exists("my_socket_file")) os.environ["BUNDY_MSGQ_SOCKET_FILE"] = "./my_socket_file" msgq = MsgQ() msgq.setup() self.assertTrue(os.path.exists("./my_socket_file")) msgq.shutdown() self.assertFalse(os.path.exists("./my_socket_file"))
def setUp(self): self.__msgq = MsgQ() self.__abort_wait = False self.__result = None self.__notify_thread = threading.Thread(target=self.__notify) self.__wait_thread = threading.Thread(target=self.__wait) # Make sure the threads are killed if left behind by the test. self.__notify_thread.daemon = True self.__wait_thread.daemon = True
def setUp(self): self.__msgq = MsgQ() self.__msgq.kill_socket = self.mock_kill_socket self.__sock = self.MockSocket() self.__data = b'dummy' self.__msgq.sockets[42] = self.__sock self.__msgq.sendbuffs[42] = (None, b'testdata') self.__sock_error = socket.error() self.__killed_socket = None self.__logger = self.LoggerWrapper(msgq.logger) msgq.logger = self.__logger self.__orig_select = msgq.select.select
def send_many(self, data): """ Tries that sending a command many times and getting an answer works. """ msgq = MsgQ() # msgq.run needs to compare with the listen_socket, so we provide # a replacement msgq.listen_socket = DummySocket (queue, out) = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM) def run(): length = len(data) queue_pid = os.fork() if queue_pid == 0: signal.alarm(120) msgq.setup_signalsock() msgq.register_socket(queue) msgq.run() msgq.cleanup_signalsock() else: try: def killall(signum, frame): os.kill(queue_pid, signal.SIGTERM) os._exit(1) signal.signal(signal.SIGALRM, killall) msg = msgq.preparemsg({"type": "ping"}, data) now = time.clock() while time.clock() - now < 0.2: out.sendall(msg) # Check the answer (routing, received) = msgq.read_packet(out.fileno(), out) self.assertEqual({"type": "pong"}, bundy.cc.message.from_wire(routing)) self.assertEqual(data, received) finally: os.kill(queue_pid, signal.SIGTERM) self.terminate_check(run) # Explicitly close temporary socket pair as the Python # interpreter expects it. It may not be 100% exception safe, # but since this is only for tests we prefer brevity. queue.close() out.close()
def get_msgq_with_sockets(self): ''' Create a message queue and prepare it for use with a socket pair. The write end is put into the message queue, so we can check it. It returns (msgq, read_end, write_end). It is expected the sockets are closed by the caller afterwards. Also check the sockets are registered correctly (eg. internal data structures are there for them). ''' msgq = MsgQ() # We do only partial setup, so we don't create the listening socket (read, write) = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM) msgq.register_socket(write) self.assertEqual(1, len(msgq.lnames)) self.assertEqual(write, msgq.lnames[msgq.fd_to_lname[write.fileno()]]) return (msgq, read, write)
def do_send(self, write, read, control_write, control_read, expect_arrive=True, expect_send_exception=None): """ Makes a msgq object that is talking to itself, run it in a separate thread so we can use and test run(). It is given two sets of connected sockets; write/read, and control_write/control_read. The former may be throwing errors and mangle data to test msgq. The second is mainly used to send msgq the stop command. (Note that the terms 'read' and 'write' are from the msgq point of view, so the test itself writes to 'control_read') Parameters: write: a socket that is used to send the data to read: a socket that is used to read the data from control_write: a second socket for communication with msgq control_read: a second socket for communication with msgq expect_arrive: if True, the read socket is read from, and the data that is read is expected to be the same as the data that has been sent to the write socket. expect_send_exception: if not None, this is the exception that is expected to be raised by msgq """ # Some message and envelope data to send and check env = b'{"env": "foo"}' msg = b'{"msg": "bar"}' msgq = MsgQ() # Don't need a listen_socket msgq.listen_socket = DummySocket msgq.setup_signalsock() msgq.register_socket(write) msgq.register_socket(control_write) # Queue the message for sending msgq.sendmsg(write, env, msg) # Run it in a thread msgq_thread = MsgQThread(msgq) # If we're done, just kill it msgq_thread.start() if expect_arrive: (recv_env, recv_msg) = msgq.read_packet(read.fileno(), read) self.assertEqual(env, recv_env) self.assertEqual(msg, recv_msg) # Tell msgq to stop msg = msgq.preparemsg({"type": "stop"}) control_read.sendall(msg) # Wait for thread to stop if it hasn't already. # Put in a (long) timeout; the thread *should* stop, but if it # does not, we don't want the test to hang forever msgq_thread.join(60) # Fail the test if it didn't stop self.assertFalse(msgq_thread.isAlive(), "Thread did not stop") # Clean up some internals of msgq (usually called as part of # shutdown, but we skip that one here) msgq.cleanup_signalsock() # Check the exception from the thread, if any # First, if we didn't expect it; reraise it (to make test fail and # show the stacktrace for debugging) if expect_send_exception is None: if msgq_thread.caught_exception is not None: raise msgq_thread.caught_exception else: # If we *did* expect it, fail it there was none self.assertIsNotNone(msgq_thread.caught_exception)
def setUp(self): self.__msgq = MsgQ()
def test_open_socket_bad(self): msgq = MsgQ("/does/not/exist") self.assertRaises(socket.error, msgq.setup) # But we can clean up after that. msgq.shutdown()