def testBindPortNumberGiven(self): port, close = attemptBinding(bindPort=0); close() PortNumberGenerator.bind(port=port) self.assertNotBound(bindV4(ip='127.0.0.1', port=port, protocol='tcp', reuse=True)) if has_dual_stack(): self.assertNotBound(bindV6(ip='::1', port=port, protocol='tcp', reuse=True)) PortNumberGenerator.unbind(port=port) self.assertBoundAndUnbind(bindV4(ip='127.0.0.1', port=port, protocol='tcp', reuse=True)) if has_dual_stack(): self.assertBoundAndUnbind(bindV6(ip='::1', port=port, protocol='tcp', reuse=True))
def testBindPortNumberGiven(self): port, close = attemptBinding(bindPort=0) close() PortNumberGenerator.bind(port=port) self.assertNotBound( bindV4(ip='127.0.0.1', port=port, protocol='tcp', reuse=True)) if has_dual_stack(): self.assertNotBound( bindV6(ip='::1', port=port, protocol='tcp', reuse=True)) PortNumberGenerator.unbind(port=port) self.assertBoundAndUnbind( bindV4(ip='127.0.0.1', port=port, protocol='tcp', reuse=True)) if has_dual_stack(): self.assertBoundAndUnbind( bindV6(ip='::1', port=port, protocol='tcp', reuse=True))
def testBindPortNumberGivenKeepsOldUsedPortsOnFailure(self): port1 = PortNumberGenerator.next() port2 = PortNumberGenerator.next(blockSize=2) port3 = port2 + 1 PortNumberGenerator.bind(port3) # Overlap with already bind port: port3. self.assertEqual(set([port1, port2, port3]), PortNumberGenerator._usedPorts) self.assertEqual(set([port3]), set(PortNumberGenerator._bound.keys())) try: PortNumberGenerator.bind(port2, blockSize=2) except RuntimeError as e: self.assertEqual('Port(s) already bound', str(e)) else: self.fail() self.assertEqual(set([port1, port2, port3]), PortNumberGenerator._usedPorts) self.assertEqual(set([port3]), set(PortNumberGenerator._bound.keys())) # Overlap with bound port: port3. PortNumberGenerator.unbind(port=port3) _port, close = attemptBinding(bindPort=port3) self.assertTrue(_port) self.assertEqual(set(), set(PortNumberGenerator._bound.keys())) try: PortNumberGenerator.bind(port2, blockSize=2) except RuntimeError as e: self.assertEqual('Port(s) are not free!', str(e)) else: self.fail() self.assertEqual(set([port1, port2, port3]), PortNumberGenerator._usedPorts) self.assertEqual(set(), set(PortNumberGenerator._bound.keys())) close() # Cleanup