60 lines
2.7 KiB
Python
60 lines
2.7 KiB
Python
"""本地:python3 -m unittest test_dividend_jobs;线程、子进程和回调使用替身。"""
|
||
import unittest
|
||
from unittest.mock import patch
|
||
|
||
import test_api_support
|
||
import dividend_jobs as jobs
|
||
import xxl
|
||
|
||
|
||
class JobsTest(unittest.TestCase):
|
||
def setUp(self):
|
||
jobs._jobs.clear(); jobs._running = None
|
||
self.key = patch.object(xxl.config, 'XXL_TRIGGER_KEY', 'test-key'); self.key.start()
|
||
def tearDown(self):
|
||
self.key.stop(); jobs._jobs.clear(); jobs._running = None
|
||
|
||
def test_auth_required(self):
|
||
with self.assertRaises(jobs.HTTPException) as e:
|
||
jobs.trigger(callbackUrl=None, key='bad', x_job_key=None)
|
||
self.assertEqual(e.exception.status_code, 401)
|
||
|
||
def test_duplicate_rejected(self):
|
||
with patch.object(jobs.threading, 'Thread'):
|
||
accepted = jobs.trigger(callbackUrl=None, key='test-key', x_job_key=None)
|
||
with self.assertRaises(jobs.HTTPException) as e:
|
||
jobs.trigger(callbackUrl=None, key='test-key', x_job_key=None)
|
||
self.assertEqual(e.exception.status_code, 409)
|
||
self.assertEqual(jobs._running, accepted['task_id'])
|
||
|
||
def test_plan_running_does_not_block_research(self):
|
||
with patch.object(xxl, '_running_id', 'plan'), patch.object(jobs.threading, 'Thread'):
|
||
result = jobs.trigger(callbackUrl=None, key='test-key', x_job_key=None)
|
||
self.assertEqual(result['status'], 'accepted')
|
||
self.assertEqual(xxl._running_id, 'plan')
|
||
|
||
def run_result(self, outcome):
|
||
with patch.object(jobs.threading, 'Thread'):
|
||
result = jobs.trigger(callbackUrl='http://callback.invalid', key='test-key', x_job_key=None)
|
||
with patch.object(jobs, '_run_step', return_value=outcome) as run, patch.object(jobs, '_notify') as notify, patch.object(xxl, '_running_id', 'plan'):
|
||
jobs._run(result['task_id'], 'http://callback.invalid')
|
||
self.assertEqual(xxl._running_id, 'plan')
|
||
self.assertEqual(run.call_args.args[0], ['dividend_watch.py', 'scan'])
|
||
self.assertEqual(notify.call_args.args[2], outcome[0] == 0)
|
||
self.assertIsNone(jobs._running)
|
||
return jobs._jobs[result['task_id']]
|
||
|
||
def test_success_after_completion(self):
|
||
self.assertEqual(self.run_result((0,'done'))['state'], 'success')
|
||
|
||
def test_failure_callback_not_success(self):
|
||
self.assertEqual(self.run_result((1,'source unavailable'))['state'], 'failed')
|
||
|
||
def test_status_after_restart_unknown(self):
|
||
with self.assertRaises(jobs.HTTPException) as e:
|
||
jobs.status('lost', key='test-key', x_job_key=None)
|
||
self.assertEqual(e.exception.status_code,404)
|
||
|
||
|
||
if __name__ == '__main__': unittest.main()
|