def test_await(self):
        latch = CountUpDownLatch()
        latch._lock = MagicMock()
        # Simulate a count down when the wait is called.
        latch._lock.wait = MagicMock(side_effect=latch.count_down)

        latch. await ()  # This should be a no-op as count is 0.
        latch.count_up()

        latch. await ()  # This should call the wait method
        latch._lock.wait.assert_called_once_with()
    def test_count(self):
        """ Test that count is updated and decremented correctly """
        latch = CountUpDownLatch()
        latch._lock = MagicMock()
        self.assertEqual(latch.count, 0)

        self.assertRaises(ValueError, latch.count_down)

        latch.count_up()
        self.assertEqual(latch.count, 1)

        latch.count_down()
        self.assertEqual(latch.count, 0)
        latch._lock.notifyAll.assert_called_once_with()
    def test_functional(self):
        """ Test teh count up and down functionality by synchronizing two
        threads.
        """
        latch = CountUpDownLatch()
        # Start the waiter thread.
        self._helper = ThreadedHelper(latch)
        self._helper.start()
        self._helper.set_wait()

        max_i = 3
        i = 0
        while self._helper.wait and i < max_i:
            sleep(1)
            i += 1

        # Either we are out of the waiting block or it has been too long.
        self.assertEqual(self._helper.unblocked_count, 1)
        self.assertEqual(self._helper.wait_count, 1)

        latch.count_up()
        latch.count_up()
        self._helper.set_wait()

        # Wait for the helpers await call or upto 3 seconds.
        i = 0
        while self._helper.wait_count != 2 and i < max_i:
            sleep(1)
            i += 1

        # This should wake up the helper thread.
        latch.count_down()
        latch.count_down()
        self.assertTrue(latch.count == 0)
        # Wait till the other thread is done waiting or upto 3 seconds.
        i = 0
        while self._helper.wait and i < max_i:
            sleep(1)
            i += 1
        self.assertEqual(self._helper.unblocked_count, 2)
        self.assertEqual(self._helper.wait_count, 2)
Esempio n. 4
0
    def __init__(self, scheduler_id, ut_ratio, enable_health_checker=True):
        """Create a new leaf scheduler.

        :param scheduler_id: scheduler id
        :type scheduler_id: str
        :type enable_health_checker: enables health checking of children.
        """
        self._logger = logging.getLogger(__name__)
        self._logger.info("Creating leaf scheduler: %s" % scheduler_id)
        self.lock = threading.RLock()
        self._latch = CountUpDownLatch()
        self._place_strategy = RandomSubsetStrategy(PLACE_FAN_OUT_RATIO,
                                                    MIN_PLACE_FAN_OUT,
                                                    MAX_PLACE_FAN_OUT)
        self._scheduler_id = scheduler_id
        self._hosts = []
        self._scorer = DefaultScorer(ut_ratio)
        self._threadpool = None
        self._initialize_services(scheduler_id)
        self._health_checker = None
        self._enable_health_checker = enable_health_checker
        self._configured = ConfigStates.UNINITIALIZED