Esempio 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(utils.is_port_available(port))
     finally:
         test_s.close()
Esempio n. 2
0
 def test_is_port_available_negative(self, mock_list_occupied_adb_ports):
     test_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     test_s.bind(('localhost', 0))
     port = test_s.getsockname()[1]
     try:
         self.assertFalse(utils.is_port_available(port))
     finally:
         test_s.close()
Esempio n. 3
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(utils.is_port_available(port))
Esempio n. 4
0
    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()
        """
        if not self.h_port or not utils.is_port_available(self.h_port):
            self.h_port = utils.get_available_host_port()
        self.adb.tcp_forward(self.h_port, self.d_port)

        try:
            droid = self.start_new_session()
        except:
            sl4a_client.start_sl4a(self.adb)
            droid = self.start_new_session()

        if handle_event:
            ed = self.get_dispatcher(droid)
            return droid, ed
        return droid