def test_disallowedPortRepeatedly(self):
        """
        If port numbers that cannot be bound are repeatedly selected,
        L{resolver._connectedProtocol} will give up eventually.
        """
        ports = []

        class FakeReactor(object):
            def listenUDP(self, port, *args, **kwargs):
                ports.append(port)
                raise CannotListenError(None, port, None)

        resolver = client.Resolver(servers=[('example.com', 53)])
        resolver._reactor = FakeReactor()

        self.assertRaises(CannotListenError, resolver._connectedProtocol)
        # 1000 is a good round number. I like it.
        self.assertEqual(len(ports), 1000)

        If port numbers that cannot be bound are repeatedly selected,
        L{resolver._connectedProtocol} will give up eventually.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/names/test/test_client.py