forked from iron-io/iron_worker_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
142 lines (102 loc) · 3.88 KB
/
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from iron_worker import *
import unittest
from datetime import datetime
from datetime import timedelta
class TestIronWorker(unittest.TestCase):
def setUp(self):
self.code_name = "test_code"
self.worker = IronWorker()
self.package = CodePackage()
self.package.merge("testDir", True)
self.package.executable = "hello.py"
self.worker.upload(self.package, name=self.code_name)
def test_getCodeDetails(self):
codes = self.worker.codes()
code = self.worker.code(codes[0].id)
self.assertEqual(codes[0].id, code.id)
def test_postTask(self):
payload = {
"dict": {"a": 1, "b": 2},
"var": "alpha",
"list": ['apples', 'oranges', 'bananas']
}
resp = self.worker.queue(code_name=self.code_name, payload=payload)
task_id = resp.id
tasks = self.worker.tasks()
task_ids = []
for task in tasks:
task_ids.append(task.id)
self.assertIn(task_id, task_ids)
def test_getTaskDetails(self):
payload = {
"dict": {"a": 1, "b": 2},
"var": "alpha",
"list": ['apples', 'oranges', 'bananas']
}
resp = self.worker.queue(code_name=self.code_name, payload=payload)
task_id = resp.id
tasks = self.worker.tasks()
task_ids = []
for task in tasks:
task_ids.append(task.id)
self.assertIn(task_id, task_ids)
task = self.worker.task(resp)
self.assertEqual(task_id, task.id)
def test_zcancelTask(self):
tasks = self.worker.tasks()
for task in tasks:
self.worker.cancel(task)
new_tasks = self.worker.tasks()
real_tasks = []
for task in new_tasks:
if task.status not in ['cancelled', 'error']:
real_tasks.append(task)
self.assertEqual(len(real_tasks), 0)
def test_postSchedule(self):
resp = self.worker.queue(code_name=self.code_name, delay=120)
schedules = self.worker.tasks()
schedule_ids = []
for schedule in schedules:
schedule_ids.append(schedule.id)
time.sleep(2)
self.assertIn(resp.id, schedule_ids)
def test_postScheduleAndPayload(self):
resp = self.worker.queue(code_name=self.code_name, delay=120,
payload={"foo": "bar"})
schedules = self.worker.tasks()
schedule_ids = []
for schedule in schedules:
schedule_ids.append(schedule.id)
time.sleep(2)
self.assertIn(resp.id, schedule_ids)
def test_postAdvancedSchedule(self):
delta = timedelta(hours=1)
start_at = datetime.now() + delta # one hour from now
resp = self.worker.queue(
code_name=self.code_name,
payload={"schedule": "AWESOME SCHEDULE!"},
start_at=start_at, run_every=3600, run_times=8)
schedules = self.worker.tasks(scheduled=True)
schedule_ids = []
for schedule in schedules:
schedule_ids.append(schedule.id)
time.sleep(2)
self.assertIn(resp.id, schedule_ids)
def test_zcancelSchedule(self):
schedules = self.worker.tasks(scheduled=True)
for schedule in schedules:
self.worker.cancel(schedule)
new_schedules = self.worker.tasks(scheduled=True)
real_schedules = []
for schedule in new_schedules:
if schedule.status not in ['cancelled', 'error']:
real_schedules.append(schedule)
self.assertEqual(len(real_schedules), 0)
def test_zdeleteCode(self):
codes = self.worker.codes()
for code in codes:
self.worker.deleteCode(code)
new_codes = self.worker.codes()
self.assertEqual(len(new_codes), 0)
if __name__ == '__main__':
unittest.main()