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)
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)
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
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)
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)
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)
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)
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
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)