Ejemplo n.º 1
0
 def test_detects_tcp_port_in_use(self):
     test_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     test_s.bind(('localhost', 0))
     port = test_s.getsockname()[1]
     try:
         self.assertFalse(host_utils.is_port_available(port))
     finally:
         test_s.close()
Ejemplo n.º 2
0
 def test_is_port_available_positive(self):
     # Unfortunately, we cannot do this test reliably for SOCK_STREAM
     # because the kernel allow this socket to linger about for some
     # small amount of time.  We're not using SO_REUSEADDR because we
     # are working around behavior on darwin where binding to localhost
     # on some port and then binding again to the wildcard address
     # with SO_REUSEADDR seems to be allowed.
     test_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     test_s.bind(('localhost', 0))
     port = test_s.getsockname()[1]
     test_s.close()
     self.assertTrue(host_utils.is_port_available(port))
Ejemplo n.º 3
0
    def get_droid(self, handle_event=True):
        """Create an sl4n connection to the device.

        Return the connection handler 'droid'. By default, another connection
        on the same session is made for EventDispatcher, and the dispatcher is
        returned to the caller as well.
        If sl4n server is not started on the device, try to start it.

        Args:
            handle_event: True if this droid session will need to handle
                events.

        Returns:
            droid: Android object useds to communicate with sl4n on the android
                device.
            ed: An optional EventDispatcher to organize events for this droid.

        Examples:
            Don't need event handling:
            >>> ad = NativeAndroidDevice()
            >>> droid = ad.get_droid(False)

            Need event handling:
            >>> ad = NativeAndroidDevice()
            >>> droid, ed = ad.get_droid()
        """
        if not self.h_port or not host_utils.is_port_available(self.h_port):
            self.h_port = host_utils.get_available_host_port()
        self.adb.tcp_forward(self.h_port, self.d_port)
        pid = self.adb.shell("pidof -s sl4n", ignore_status=True)
        while (pid):
            self.adb.shell("kill {}".format(pid))
            pid = self.adb.shell("pidof -s sl4n", ignore_status=True)
        call(
            ["adb -s " + self.serial + " shell sh -c \"/system/bin/sl4n\" &"],
            shell=True)
        try:
            time.sleep(3)
            droid = self.start_new_session()
        except:
            droid = self.start_new_session()
        return droid
    def get_droid(self, handle_event=True):
        """Create an sl4a connection to the device.

        Return the connection handler 'droid'. By default, another connection
        on the same session is made for EventDispatcher, and the dispatcher is
        returned to the caller as well.
        If sl4a server is not started on the device, try to start it.

        Args:
            handle_event: True if this droid session will need to handle
                events.

        Returns:
            droid: Android object used to communicate with sl4a on the android
                device.
            ed: An optional EventDispatcher to organize events for this droid.

        Examples:
            Don't need event handling:
            >>> ad = AndroidDevice()
            >>> droid = ad.get_droid(False)

            Need event handling:
            >>> ad = AndroidDevice()
            >>> droid, ed = ad.get_droid()
        """
        forward_success = False
        last_error = None
        for _ in range(PORT_RETRY_COUNT):
            if not self.h_port or not host_utils.is_port_available(
                    self.h_port):
                self.h_port = host_utils.get_available_host_port()
            try:
                self.adb.tcp_forward(self.h_port, self.d_port)
                forward_success = True
                break
            except adb.AdbError as e:
                last_error = e
                pass
        if not forward_success:
            self.log.error(last_error)
            raise last_error

        for i in range(PORT_RETRY_COUNT):
            try:
                if self.is_sl4a_running():
                    self.log.info("Stop sl4a apk")
                    self.stop_sl4a()
                    time.sleep(15)
                self.log.info("Start sl4a apk")
                self.start_sl4a()
                time.sleep(5)
                droid = self.start_new_session()
                if handle_event:
                    ed = self.get_dispatcher(droid)
                    return droid, ed
                return droid
            except Exception as e:
                self.log.warning("get_droid with exception: %s", e)
                if i == PORT_RETRY_COUNT - 1:
                    raise