Exemplo n.º 1
0
    def test_listen_on_connect_returns(self):
        """On a successful connect, the function returns None."""
        mock_socket = self.mocker.patch(socket.socket)
        mock_socket.bind(("127.0.0.1", 800))
        self.mocker.result(True)
        mock_socket.close()

        self.mocker.replay()
        watcher = PortWatcher("127.0.0.1", 800, 30, True)
        self.assertEqual(watcher.sync_wait(), True)
Exemplo n.º 2
0
    def test_listen_on_connect_returns(self):
        """On a successful connect, the function returns None."""
        mock_socket = self.mocker.patch(socket.socket)
        mock_socket.bind(("127.0.0.1", 800))
        self.mocker.result(True)
        mock_socket.close()

        self.mocker.replay()
        watcher = PortWatcher("127.0.0.1", 800, 30, True)
        self.assertEqual(watcher.sync_wait(), True)
Exemplo n.º 3
0
    def test_wait_stops_when_watcher_stopped(self):
        """
        If the watcher is stopped, no more attempts are made to attempt
        connect to the socket.
        """
        watcher = PortWatcher("127.0.0.1", 800, 30)

        mock_socket = self.mocker.patch(socket.socket)
        sleep = self.mocker.replace("time.sleep")

        mock_socket.connect(("127.0.0.1", 800))
        self.mocker.throw(socket.error(errno.ECONNREFUSED))
        sleep(0.5)
        self.mocker.call(watcher.stop)
        self.mocker.replay()
        self.assertEquals(watcher.sync_wait(), None)
Exemplo n.º 4
0
 def test_existing_connect(self):
     """Test watch fires asap if a real server is then available."""
     server_deferred, port_deferred = self.bind_port(0.2)
     port = yield port_deferred
     yield self.sleep(0.1)
     yield PortWatcher("127.0.0.1", port, 1).async_wait()
     yield server_deferred
Exemplo n.º 5
0
    def test_wait_stops_when_watcher_stopped(self):
        """
        If the watcher is stopped, no more attempts are made to attempt
        connect to the socket.
        """
        watcher = PortWatcher("127.0.0.1", 800, 30)

        mock_socket = self.mocker.patch(socket.socket)
        sleep = self.mocker.replace("time.sleep")

        mock_socket.connect(("127.0.0.1", 800))
        self.mocker.throw(socket.error(errno.ECONNREFUSED))
        sleep(0.5)
        self.mocker.call(watcher.stop)
        self.mocker.replay()
        self.assertEquals(watcher.sync_wait(), None)
Exemplo n.º 6
0
 def test_unknown_error_raises(self):
     """Unknown errors are thrown to the caller."""
     mock_socket = self.mocker.patch(socket.socket)
     mock_socket.connect(("127.0.0.1", 465))
     self.mocker.throw(SyntaxError())
     self.mocker.replay()
     watcher = PortWatcher("127.0.0.1", 465, 5)
     self.assertRaises(SyntaxError, watcher.sync_wait)
Exemplo n.º 7
0
 def test_listen_unknown_socket_error_raises(self):
     """Unknown socket errors are thrown to the caller."""
     mock_socket = self.mocker.patch(socket.socket)
     mock_socket.bind(("127.0.0.1", 465))
     self.mocker.throw(socket.error(2000))
     self.mocker.replay()
     watcher = PortWatcher("127.0.0.1", 465, 5, True)
     self.assertRaises(socket.error, watcher.sync_wait)
Exemplo n.º 8
0
    def test_connect(self):
        """Test watch fires soon after a real server becomes available.

        0.5 second for the approximation is based on the polling
        interval of PortWatcher."""
        now = time.time()
        reactor.callLater(0.7, self.bind_port, 1, 22181)
        yield PortWatcher("127.0.0.1", 22181, 1.5).async_wait()
        self.failUnlessApproximates(time.time() - now, 0.7, 0.5)
Exemplo n.º 9
0
    def test_listen_nothing_there(self):
        """Test with a real socket server watching for port availability.

        This should result in the watch returning almost immediately,
        since nothing is (or should be - dangers of real testing)
        holding this port."""
        now = time.time()
        yield PortWatcher("127.0.0.1", 22181, 10, True).async_wait()
        self.failUnlessApproximates(time.time() - now, 0, 0.1)
Exemplo n.º 10
0
 def test_listen(self):
     """Test with a real socket server watching for port availability."""
     bind_port_time = 0.7
     now = time.time()
     server_deferred, port_deferred = self.bind_port(bind_port_time)
     port = yield port_deferred
     yield PortWatcher("127.0.0.1", port, 10, True).async_wait()
     self.failUnlessApproximates(time.time() - now, bind_port_time, 0.5)
     yield server_deferred
Exemplo n.º 11
0
 def test_wait_until_timeout_raises_timeout(self):
     """If the timeout is exceeded, a socket.timeout error is raised."""
     self.assertRaises(socket.timeout,
                       PortWatcher("localhost", 800, 0).sync_wait)