def test_delayqueue_limits(self):
        dsp = mq.DelayQueue(
            burst_limit=self.burst_limit, time_limit_ms=self.time_limit_ms, autostart=True
        )
        assert dsp.is_alive() is True

        for _ in range(self.N):
            dsp(self.call)

        starttime = perf_counter()
        # wait up to 20 sec more than needed
        app_endtime = (self.N * self.burst_limit / (1000 * self.time_limit_ms)) + starttime + 20
        while not dsp._queue.empty() and perf_counter() < app_endtime:
            sleep(1)
        assert dsp._queue.empty() is True  # check loop exit condition

        dsp.stop()
        assert dsp.is_alive() is False

        assert self.testtimes or self.N == 0
        passes, fails = [], []
        delta = (self.time_limit_ms - self.margin_ms) / 1000
        for start, stop in enumerate(range(self.burst_limit + 1, len(self.testtimes))):
            part = self.testtimes[start:stop]
            if (part[-1] - part[0]) >= delta:
                passes.append(part)
            else:
                fails.append(part)
        assert fails == []
    def test_delayqueue_limits(self):
        '''Test that DelayQueue dispatched calls don't hit time-window limit'''
        print = self._print
        dsp = mq.DelayQueue(burst_limit=self._msglimit,
                            time_limit_ms=self._timelimit,
                            autostart=True)
        print('Started dispatcher {}\nStatus: {}'
              ''.format(dsp, ['inactive', 'active'][dsp.is_alive()]))
        self.assertTrue(dsp.is_alive())

        print('Dispatching {} calls @ {}'.format(self._N, time.asctime()))

        for i in range(self._N):
            dsp(self.testcall)

        print('Queue filled, waiting 4 dispatch finish @ ' +
              str(time.asctime()))

        starttime = mq.curtime()
        app_endtime = ((self._N * self._msglimit / (1000 * self._timelimit)) +
                       starttime + 20)  # wait up to 20 sec more than needed
        while not dsp._queue.empty() and mq.curtime() < app_endtime:
            time.sleep(1)
        self.assertTrue(dsp._queue.empty())  # check loop exit condition

        dsp.stop()
        print('Dispatcher ' + ['stopped', '!NOT STOPPED!'][dsp.is_alive()] +
              ' @ ' + str(time.asctime()))
        self.assertFalse(dsp.is_alive())

        self.assertTrue(self.testtimes or self._N == 0)
        print('Calculating call time windows')
        passes, fails = [], []
        delta = (self._timelimit - self._margin) / 1000
        it = enumerate(range(self._msglimit + 1, len(self.testtimes)))
        for start, stop in it:
            part = self.testtimes[start:stop]
            if (part[-1] - part[0]) >= delta:
                passes.append(part)
            else:
                fails.append(part)

        print('Tested: {}, Passed: {}, Failed: {}'
              ''.format(len(passes + fails), len(passes), len(fails)))
        if fails:
            print('(!) Got mismatches: ' + ';\n'.join(map(str, fails)))
        self.assertFalse(fails)