Exemplo n.º 1
0
    def test_log_listener(self):
        connection = '127.0.0.1', 0
        self.log_server = mozlog.LogMessageServer(
            connection,
            self.logger,
            message_callback=self.message_callback,
            timeout=0.5)

        message_string_one = json.dumps({
            '_message': 'socket message one',
            'action': 'test_message',
            '_level': 'DEBUG'
        })
        message_string_two = json.dumps({
            '_message': 'socket message two',
            'action': 'test_message',
            '_level': 'DEBUG'
        })

        message_string_three = json.dumps({
            '_message': 'socket message three',
            'action': 'test_message',
            '_level': 'DEBUG'
        })

        message_string = message_string_one + '\n' + \
            message_string_two + '\n' + \
            message_string_three + '\n'

        server_thread = threading.Thread(target=self.log_server.handle_request)
        server_thread.start()

        host, port = self.log_server.server_address

        sock = socket.socket()
        sock.connect((host, port))

        # Sleeps prevent listener from receiving entire message in a single call
        # to recv in order to test reconstruction of partial messages.
        sock.sendall(message_string[:8])
        time.sleep(.01)
        sock.sendall(message_string[8:32])
        time.sleep(.01)
        sock.sendall(message_string[32:64])
        time.sleep(.01)
        sock.sendall(message_string[64:128])
        time.sleep(.01)
        sock.sendall(message_string[128:])

        server_thread.join()
Exemplo n.º 2
0
    def test_log_listener(self):
        connection = "127.0.0.1", 0
        self.log_server = mozlog.LogMessageServer(
            connection,
            self.logger,
            message_callback=self.message_callback,
            timeout=0.5)

        message_string_one = json.dumps({
            "_message": "socket message one",
            "action": "test_message",
            "_level": "DEBUG",
        })
        message_string_two = json.dumps({
            "_message": "socket message two",
            "action": "test_message",
            "_level": "DEBUG",
        })

        message_string_three = json.dumps({
            "_message": "socket message three",
            "action": "test_message",
            "_level": "DEBUG",
        })

        message_string = (message_string_one + "\n" + message_string_two +
                          "\n" + message_string_three + "\n")

        server_thread = threading.Thread(target=self.log_server.handle_request)
        server_thread.start()

        host, port = self.log_server.server_address

        sock = socket.socket()
        sock.connect((host, port))

        # Sleeps prevent listener from receiving entire message in a single call
        # to recv in order to test reconstruction of partial messages.
        sock.sendall(message_string[:8].encode())
        time.sleep(0.01)
        sock.sendall(message_string[8:32].encode())
        time.sleep(0.01)
        sock.sendall(message_string[32:64].encode())
        time.sleep(0.01)
        sock.sendall(message_string[64:128].encode())
        time.sleep(0.01)
        sock.sendall(message_string[128:].encode())

        server_thread.join()