/
worker_task_test.py
90 lines (65 loc) · 2.51 KB
/
worker_task_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
#
# Copyright 2012-2015 Spotify AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import multiprocessing
from helpers import unittest
import mock
import json
import luigi
import luigi.date_interval
import luigi.notifications
from luigi.worker import TaskException, TaskProcess
from luigi.scheduler import DONE, FAILED
luigi.notifications.DEBUG = True
class MyTask(luigi.Task):
# Test overriding the constructor without calling the superconstructor
# This is a simple mistake but caused an error that was very hard to understand
def __init__(self):
pass
class SuccessTask(luigi.Task):
def on_success(self):
return "test success expl"
class FailTask(luigi.Task):
def run(self):
raise BaseException("Uh oh.")
def on_failure(self, exception):
return "test failure expl"
class WorkerTaskTest(unittest.TestCase):
def test_constructor(self):
def f():
luigi.build([MyTask()], local_scheduler=True)
self.assertRaises(TaskException, f)
def test_run_none(self):
def f():
luigi.build([None], local_scheduler=True)
self.assertRaises(TaskException, f)
class TaskProcessTest(unittest.TestCase):
def test_update_result_queue_on_success(self):
task = SuccessTask()
result_queue = multiprocessing.Queue()
task_process = TaskProcess(task, 1, result_queue)
with mock.patch.object(result_queue, 'put') as mock_put:
task_process.run()
mock_put.assert_called_once_with((task.task_id, DONE, json.dumps("test success expl"), [], None))
def test_update_result_queue_on_failure(self):
task = FailTask()
result_queue = multiprocessing.Queue()
task_process = TaskProcess(task, 1, result_queue)
with mock.patch.object(result_queue, 'put') as mock_put:
task_process.run()
mock_put.assert_called_once_with((task.task_id, FAILED, json.dumps("test failure expl"), [], []))
if __name__ == '__main__':
unittest.main()