def test_scheduler_multi_instance(self, method):
        method.side_effect = self.target_method

        second_scheduler = legacy_scheduler.LegacyScheduler(CONF.scheduler)
        second_scheduler.start()

        self.addCleanup(second_scheduler.stop, True)

        job = sched_base.SchedulerJob(
            run_after=DELAY,
            func_name=TARGET_METHOD_PATH,
            func_args={
                'name': 'task',
                'id': '321'
            },
        )

        second_scheduler.schedule(job)

        calls = db_api.get_delayed_calls_to_start(get_time_delay())
        self._assert_single_item(calls, target_method_name=TARGET_METHOD_PATH)

        self.queue.get()
        method.assert_called_once_with(name='task', id='321')

        calls = db_api.get_delayed_calls_to_start(get_time_delay())
        self.assertEqual(0, len(calls))
    def setUp(self):
        super(LegacySchedulerTest, self).setUp()

        self.timeout = timeout.Timeout(seconds=10)
        self.queue = queue.Queue()

        self.override_config('fixed_delay', 1, 'scheduler')
        self.override_config('random_delay', 0, 'scheduler')
        self.override_config('batch_size', 100, 'scheduler')

        self.scheduler = legacy_scheduler.LegacyScheduler(CONF.scheduler)
        self.scheduler.start()

        self.addCleanup(self.scheduler.stop, True)
        self.addCleanup(self.timeout.cancel)
    def test_scheduler_with_custom_batch_size(self):
        self.scheduler.stop()

        number_delayed_calls = 5
        processed_calls_at_time = []
        real_delete_calls_method = \
            legacy_scheduler.LegacyScheduler.delete_calls

        @staticmethod
        def delete_calls_counter(delayed_calls):
            real_delete_calls_method(delayed_calls)

            for _ in range(len(delayed_calls)):
                self.queue.put("item")

            processed_calls_at_time.append(len(delayed_calls))

        legacy_scheduler.LegacyScheduler.delete_calls = delete_calls_counter

        # Create 5 delayed calls.
        for i in range(number_delayed_calls):
            job = sched_base.SchedulerJob(
                run_after=DELAY,
                func_name=TARGET_METHOD_PATH,
                func_args={
                    'name': 'task',
                    'id': i
                },
            )

            self.scheduler.schedule(job)

        # Start scheduler which process 2 calls at a time.
        self.override_config('batch_size', 2, 'scheduler')

        self.scheduler = legacy_scheduler.LegacyScheduler(CONF.scheduler)
        self.scheduler.start()

        # Wait when all of calls will be processed
        for _ in range(number_delayed_calls):
            self.queue.get()

        self.assertListEqual([1, 2, 2], sorted(processed_calls_at_time))