Esempio n. 1
0
    def testPortAllocatorCleanMatchingIP(self):
        # Setup PortAllocator w/o port expiration.
        self._pa = cns.PortAllocator(cns._DEFAULT_CNS_PORT_RANGE, 0)

        # Ensure Get() succeeds and returns the correct port.
        self.assertEquals(self._pa.Get('ip1', t=1),
                          cns._DEFAULT_CNS_PORT_RANGE[0])
        self.assertEquals(self._pa.Get('ip1', t=2),
                          cns._DEFAULT_CNS_PORT_RANGE[0] + 1)
        self.assertEquals(self._pa.Get('ip1', t=3),
                          cns._DEFAULT_CNS_PORT_RANGE[0] + 2)
        self.assertEquals(self._pa.Get('ip2', t=1),
                          cns._DEFAULT_CNS_PORT_RANGE[0] + 3)

        self._pa.Cleanup(all_ports=False, request_ip='ip1')

        self.assertEquals(self._pa._ports.keys(),
                          [cns._DEFAULT_CNS_PORT_RANGE[0] + 3])
        self.assertEquals(self._pa.Get('ip2'), cns._DEFAULT_CNS_PORT_RANGE[0])
        self.assertEquals(self._pa.Get('ip1'),
                          cns._DEFAULT_CNS_PORT_RANGE[0] + 1)

        self._pa.Cleanup(all_ports=False, request_ip='ip2')
        self.assertEquals(self._pa._ports.keys(),
                          [cns._DEFAULT_CNS_PORT_RANGE[0] + 1])

        self._pa.Cleanup(all_ports=False, request_ip='abc')
        self.assertEquals(self._pa._ports.keys(),
                          [cns._DEFAULT_CNS_PORT_RANGE[0] + 1])

        self._pa.Cleanup(all_ports=False, request_ip='ip1')
        self.assertEquals(self._pa._ports.keys(), [])
Esempio n. 2
0
    def setUp(self):
        # Mock out time.time() to accelerate port expiration testing.
        self._old_time = time.time
        self._current_time = 0
        time.time = lambda: self._current_time

        # TODO(dalecurtis): Mock out actual calls to shadi's port setup.
        self._pa = cns.PortAllocator(cns._DEFAULT_CNS_PORT_RANGE,
                                     self._EXPIRY_TIME)
        self._MockTrafficControl()
Esempio n. 3
0
    def testPortAllocatorNoExpiration(self):
        # Setup PortAllocator w/o port expiration.
        self._pa = cns.PortAllocator(cns._DEFAULT_CNS_PORT_RANGE, 0)

        # Ensure Get() succeeds and returns the correct port.
        self.assertEquals(self._pa.Get('test'), cns._DEFAULT_CNS_PORT_RANGE[0])

        # Update fake time to see if ports expire.
        self._current_time += self._EXPIRY_TIME

        # Send second Get() which would normally cause ports to expire. Ensure that
        # the ports did not expire.
        self.assertEquals(self._pa.Get('test2'),
                          cns._DEFAULT_CNS_PORT_RANGE[0] + 1)
        self.assertEquals(
            set(self._pa._ports.keys()),
            set([
                cns._DEFAULT_CNS_PORT_RANGE[0],
                cns._DEFAULT_CNS_PORT_RANGE[0] + 1
            ]))