1
|
|
|
# coding: utf8 |
2
|
|
|
|
3
|
|
|
# Copyright 2013-2017 Vincent Jacques <[email protected]> |
4
|
|
|
|
5
|
|
|
from __future__ import division, absolute_import, print_function |
6
|
|
|
|
7
|
|
|
import unittest |
8
|
|
|
|
9
|
|
|
from ActionTree import * |
10
|
|
|
from . import * |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class TimingTestCase(ActionTreeTestCase): |
14
|
|
View Code Duplication |
def test_success(self): |
|
|
|
|
15
|
|
|
a = self._action("a") |
16
|
|
|
|
17
|
|
|
report = execute(a) |
18
|
|
|
|
19
|
|
|
self.assertIsInstance(report.get_action_status(a).pending_time, datetime.datetime) |
20
|
|
|
self.assertEqual(report.get_action_status(a).ready_time, report.get_action_status(a).pending_time) |
21
|
|
|
self.assertIsNone(report.get_action_status(a).cancel_time) |
22
|
|
|
self.assertEqual(report.get_action_status(a).start_time, report.get_action_status(a).ready_time) |
23
|
|
|
self.assertGreater(report.get_action_status(a).success_time, report.get_action_status(a).start_time) |
24
|
|
|
self.assertIsNone(report.get_action_status(a).failure_time) |
25
|
|
|
|
26
|
|
View Code Duplication |
def test_failure(self): |
|
|
|
|
27
|
|
|
a = self._action("a", exception=Exception()) |
28
|
|
|
|
29
|
|
|
report = execute(a, do_raise=False) |
30
|
|
|
|
31
|
|
|
self.assertIsInstance(report.get_action_status(a).pending_time, datetime.datetime) |
32
|
|
|
self.assertEqual(report.get_action_status(a).ready_time, report.get_action_status(a).pending_time) |
33
|
|
|
self.assertIsNone(report.get_action_status(a).cancel_time) |
34
|
|
|
self.assertEqual(report.get_action_status(a).start_time, report.get_action_status(a).ready_time) |
35
|
|
|
self.assertIsNone(report.get_action_status(a).success_time) |
36
|
|
|
self.assertGreater(report.get_action_status(a).failure_time, report.get_action_status(a).start_time) |
37
|
|
|
|
38
|
|
|
def test_cancelation_before_ready(self): |
39
|
|
|
a = self._action("a") |
40
|
|
|
b = self._action("b", exception=Exception()) |
41
|
|
|
a.add_dependency(b) |
42
|
|
|
|
43
|
|
|
report = execute(a, do_raise=False) |
44
|
|
|
|
45
|
|
|
self.assertIsInstance(report.get_action_status(b).pending_time, datetime.datetime) |
46
|
|
|
self.assertEqual(report.get_action_status(b).ready_time, report.get_action_status(b).pending_time) |
47
|
|
|
self.assertIsNone(report.get_action_status(b).cancel_time) |
48
|
|
|
self.assertEqual(report.get_action_status(b).start_time, report.get_action_status(b).ready_time) |
49
|
|
|
self.assertIsNone(report.get_action_status(b).success_time) |
50
|
|
|
self.assertGreater(report.get_action_status(b).failure_time, report.get_action_status(b).start_time) |
51
|
|
|
|
52
|
|
|
self.assertIsInstance(report.get_action_status(a).pending_time, datetime.datetime) |
53
|
|
|
self.assertIsNone(report.get_action_status(a).ready_time) |
54
|
|
|
self.assertEqual(report.get_action_status(a).cancel_time, report.get_action_status(b).failure_time) |
55
|
|
|
self.assertIsNone(report.get_action_status(a).start_time) |
56
|
|
|
self.assertIsNone(report.get_action_status(a).success_time) |
57
|
|
|
self.assertIsNone(report.get_action_status(a).failure_time) |
58
|
|
|
|
59
|
|
|
def test_cancelation_with_keep_going(self): |
60
|
|
|
a = self._action("a") |
61
|
|
|
b = self._action("b") |
62
|
|
|
a.add_dependency(b) |
63
|
|
|
c = self._action("c", exception=Exception()) |
64
|
|
|
b.add_dependency(c) |
65
|
|
|
|
66
|
|
|
report = execute(a, keep_going=True, do_raise=False) |
67
|
|
|
|
68
|
|
|
self.assertEqual(report.get_action_status(b).cancel_time, report.get_action_status(c).failure_time) |
69
|
|
|
self.assertEqual(report.get_action_status(a).cancel_time, report.get_action_status(b).cancel_time) |
70
|
|
|
|
71
|
|
|
def test_leaves_have_same_ready_time(self): |
72
|
|
|
a = self._action("a") |
73
|
|
|
b = self._action("b") |
74
|
|
|
c = self._action("c") |
75
|
|
|
d = self._action("d") |
76
|
|
|
a.add_dependency(b) |
77
|
|
|
a.add_dependency(c) |
78
|
|
|
a.add_dependency(d) |
79
|
|
|
|
80
|
|
|
report = execute(a) |
81
|
|
|
|
82
|
|
|
self.assertEqual(report.get_action_status(c).ready_time, report.get_action_status(b).ready_time) |
83
|
|
|
self.assertEqual(report.get_action_status(d).ready_time, report.get_action_status(b).ready_time) |
84
|
|
|
|
85
|
|
|
def test_many_dependencies_with_unlimited_cpu_cores(self): |
86
|
|
|
MANY = 20 |
87
|
|
|
a = self._action("a") |
88
|
|
|
deps = [self._action(i) for i in range(MANY)] |
89
|
|
|
for dep in deps: |
90
|
|
|
a.add_dependency(dep) |
91
|
|
|
|
92
|
|
|
report = execute(a, cpu_cores=UNLIMITED) |
93
|
|
|
for dep in deps[1:]: |
94
|
|
|
self.assertEqual(report.get_action_status(dep).start_time, report.get_action_status(deps[0]).start_time) |
95
|
|
|
|
96
|
|
|
def test_many_dependencies_with_one_cpu_cores(self): |
97
|
|
|
MANY = 20 |
98
|
|
|
a = self._action("a") |
99
|
|
|
deps = [self._action(i) for i in range(MANY)] |
100
|
|
|
for dep in deps: |
101
|
|
|
a.add_dependency(dep) |
102
|
|
|
|
103
|
|
|
report = execute(a, cpu_cores=1) |
104
|
|
|
|
105
|
|
|
# No two actions have started at the same time |
106
|
|
|
start_times = set(report.get_action_status(dep).start_time for dep in deps) |
107
|
|
|
self.assertEqual(len(start_times), MANY) |
108
|
|
|
|
109
|
|
|
def test_many_dependencies_with_limited_cpu_cores(self): |
110
|
|
|
MANY = 20 |
111
|
|
|
a = self._action("a") |
112
|
|
|
deps = [self._action(i) for i in range(MANY)] |
113
|
|
|
for dep in deps: |
114
|
|
|
a.add_dependency(dep) |
115
|
|
|
|
116
|
|
|
report = execute(a, cpu_cores=3) |
117
|
|
|
|
118
|
|
|
# Only the first three actions have started at the same time |
119
|
|
|
start_times = set(report.get_action_status(dep).start_time for dep in deps) |
120
|
|
|
self.assertEqual(len(start_times), MANY - 2) |
121
|
|
|
|
122
|
|
|
def test_scarce_resource_with_many_cpu_cores(self): |
123
|
|
|
r = Resource(1) |
124
|
|
|
a = self._action("a") |
125
|
|
|
b = self._action("b") |
126
|
|
|
b.require_resource(r, 1) |
127
|
|
|
c = self._action("c") |
128
|
|
|
c.require_resource(r, 1) |
129
|
|
|
a.add_dependency(b) |
130
|
|
|
a.add_dependency(c) |
131
|
|
|
|
132
|
|
|
report = execute(a, cpu_cores=6) |
133
|
|
|
|
134
|
|
|
# @todo Start next action at the same timestamp |
135
|
|
|
self.assertTrue( |
136
|
|
|
report.get_action_status(b).start_time > report.get_action_status(c).success_time or |
137
|
|
|
report.get_action_status(c).start_time > report.get_action_status(b).success_time |
138
|
|
|
) |
139
|
|
|
|
140
|
|
|
def test_abundant_resource_with_many_cpu_cores(self): |
141
|
|
|
r = Resource(2) |
142
|
|
|
a = self._action("a") |
143
|
|
|
b = self._action("b") |
144
|
|
|
b.require_resource(r, 1) |
145
|
|
|
c = self._action("c") |
146
|
|
|
c.require_resource(r, 1) |
147
|
|
|
a.add_dependency(b) |
148
|
|
|
a.add_dependency(c) |
149
|
|
|
|
150
|
|
|
report = execute(a, cpu_cores=6) |
151
|
|
|
|
152
|
|
|
self.assertEqual(report.get_action_status(c).ready_time, report.get_action_status(b).ready_time) |
153
|
|
|
self.assertEqual(report.get_action_status(c).start_time, report.get_action_status(b).start_time) |
154
|
|
|
|