'Test the jobs manager'

def test_jobs_manager(self):

    from calibre.srv.jobs import JobsManager
    O = namedtuple(u'O', u'max_jobs    max_job_time')
    class FakeLog(list, ):
        def error(self, *args):
            self.append(u'    '.join(args))
    s = (u'waiting', u'running')
    jm = JobsManager(O(1, 5), FakeLog())
    def job_status(jid):
        return jm.job_status(jid)[0]
    job_id1 = jm.start_job(u'simple    test', u'calibre.srv.jobs', u'sleep_test', args=(1.0,))
    job_id2 = jm.start_job(u't2', u'calibre.srv.jobs', u'sleep_test', args=(3,))
    job_id3 = jm.start_job(u'err    test', u'calibre.srv.jobs', u'error_test')
    job_id = job_id1
    status = jm.job_status(job_id)[0]
    self.assertIn(status, s)
    for jid in (job_id2, job_id3):
        self.assertEqual(job_status(jid), u'waiting')
    while (job_status(job_id) in s):
        time.sleep(0.01)
    (status, result, tb, was_aborted) = jm.job_status(job_id)
    self.assertEqual(status, u'finished')
    self.assertFalse(was_aborted)
    self.assertFalse(tb)
    self.assertEqual(result, 1.0)
    job_id = job_id2
    while (job_status(job_id) == u'waiting'):
        time.sleep(0.01)
    self.assertEqual(u'running', job_status(job_id))
    jm.abort_job(job_id)
    self.assertIn(jm.wait_for_running_job(job_id), (True, None))
    (status, result, tb, was_aborted) = jm.job_status(job_id)
    self.assertEqual(u'finished', status)
    self.assertTrue(was_aborted)
    job_id = job_id3
    while (job_status(job_id) == u'waiting'):
        time.sleep(0.01)
    self.assertIn(jm.wait_for_running_job(job_id), (True, None))
    (status, result, tb, was_aborted) = jm.job_status(job_id)
    self.assertEqual(status, u'finished')
    self.assertFalse(was_aborted)
    self.assertTrue(tb)
    self.assertIn(u'a    testing    error', tb)
    jm.start_job(u'simple    test', u'calibre.srv.jobs', u'sleep_test', args=(1.0,))
    (jm.shutdown(), jm.wait_for_shutdown((monotonic() + 1)))
