1 | /*
|
---|
2 | * Copyright (c) 2015 Michal Koutny
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <assert.h>
|
---|
30 | #include <pcut/pcut.h>
|
---|
31 | #include <stdio.h>
|
---|
32 |
|
---|
33 | #include "../job.h"
|
---|
34 |
|
---|
35 | #include "mock_unit.h"
|
---|
36 |
|
---|
37 | PCUT_INIT
|
---|
38 |
|
---|
39 |
|
---|
40 | static dyn_array_t exp_closure;
|
---|
41 | static dyn_array_t act_closure;
|
---|
42 |
|
---|
43 | static bool same_job(job_t *expected, job_t *actual)
|
---|
44 | {
|
---|
45 | return (expected->unit == actual->unit) &&
|
---|
46 | (expected->target_state == actual->target_state);
|
---|
47 | }
|
---|
48 |
|
---|
49 | static bool same_jobs(dyn_array_t *expected, dyn_array_t *actual)
|
---|
50 | {
|
---|
51 | if (expected->size != actual->size) {
|
---|
52 | printf("%s: |expected| - |actual| = %u\n",
|
---|
53 | __func__, expected->size - actual->size);
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /* Verify expected \subseteq actual (we've compared sizes) */
|
---|
58 | dyn_array_foreach(*expected, job_t *, it_exp) {
|
---|
59 | bool found = false;
|
---|
60 | dyn_array_foreach(*actual, job_t *, it_act) {
|
---|
61 | if (same_job(*it_exp, *it_act)) {
|
---|
62 | found = true;
|
---|
63 | break;
|
---|
64 | }
|
---|
65 | }
|
---|
66 | if (!found) {
|
---|
67 | printf("%s: expected job for %s\n",
|
---|
68 | __func__, unit_name((*it_exp)->unit));
|
---|
69 | return false;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 |
|
---|
76 | static bool job_blocked(job_t *blocked_job, job_t *blocking_job)
|
---|
77 | {
|
---|
78 | bool found = false;
|
---|
79 | dyn_array_foreach(blocking_job->blocked_jobs, job_t *, it) {
|
---|
80 | if (*it == blocked_job) {
|
---|
81 | found = true;
|
---|
82 | break;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | return found;
|
---|
86 | }
|
---|
87 |
|
---|
88 | static job_t *dummy_job(unit_t *unit, unit_state_t target_state)
|
---|
89 | {
|
---|
90 | job_t *old_job = unit->job;
|
---|
91 | unit->job = NULL;
|
---|
92 |
|
---|
93 | job_t *result = job_create(unit, target_state);
|
---|
94 |
|
---|
95 | job_del_ref(&result);
|
---|
96 | unit->job = old_job;
|
---|
97 |
|
---|
98 | return result;
|
---|
99 | }
|
---|
100 |
|
---|
101 | static void destroy_job_closure(dyn_array_t *closure)
|
---|
102 | {
|
---|
103 | dyn_array_foreach(*closure, job_t *, it) {
|
---|
104 | job_del_ref(&(*it));
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | PCUT_TEST_SUITE(job_closure);
|
---|
109 |
|
---|
110 | PCUT_TEST_BEFORE {
|
---|
111 | mock_create_units();
|
---|
112 | mock_set_units_state(STATE_STOPPED);
|
---|
113 |
|
---|
114 | dyn_array_initialize(&exp_closure, job_t *);
|
---|
115 | int rc = dyn_array_reserve(&exp_closure, MAX_TYPES * MAX_UNITS);
|
---|
116 | assert(rc == EOK);
|
---|
117 |
|
---|
118 | dyn_array_initialize(&act_closure, job_t *);
|
---|
119 | rc = dyn_array_reserve(&act_closure, MAX_TYPES * MAX_UNITS);
|
---|
120 | assert(rc == EOK);
|
---|
121 | }
|
---|
122 |
|
---|
123 | PCUT_TEST_AFTER {
|
---|
124 | destroy_job_closure(&act_closure);
|
---|
125 | dyn_array_destroy(&act_closure);
|
---|
126 |
|
---|
127 | destroy_job_closure(&exp_closure);
|
---|
128 | dyn_array_destroy(&exp_closure);
|
---|
129 |
|
---|
130 | mock_destroy_units();
|
---|
131 | }
|
---|
132 |
|
---|
133 | PCUT_TEST(job_closure_linear) {
|
---|
134 | unit_t *u0 = mock_units[UNIT_SERVICE][0];
|
---|
135 | unit_t *u1 = mock_units[UNIT_SERVICE][1];
|
---|
136 | unit_t *u2 = mock_units[UNIT_SERVICE][2];
|
---|
137 | unit_t *u3 = mock_units[UNIT_SERVICE][3];
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * u0 -> u1 -> u2 -> u3
|
---|
141 | */
|
---|
142 | mock_add_dependency(u0, u1);
|
---|
143 | mock_add_dependency(u1, u2);
|
---|
144 | mock_add_dependency(u2, u3);
|
---|
145 |
|
---|
146 | job_t *main_job = job_create(u1, STATE_STARTED);
|
---|
147 | assert(main_job);
|
---|
148 |
|
---|
149 | int rc = job_create_closure(main_job, &act_closure);
|
---|
150 | PCUT_ASSERT_INT_EQUALS(EOK, rc);
|
---|
151 |
|
---|
152 | dyn_array_append(&exp_closure, job_t *, dummy_job(u1, STATE_STARTED));
|
---|
153 | dyn_array_append(&exp_closure, job_t *, dummy_job(u2, STATE_STARTED));
|
---|
154 | dyn_array_append(&exp_closure, job_t *, dummy_job(u3, STATE_STARTED));
|
---|
155 |
|
---|
156 | PCUT_ASSERT_TRUE(same_jobs(&exp_closure, &act_closure));
|
---|
157 | PCUT_ASSERT_TRUE(job_blocked(u1->job, u2->job));
|
---|
158 | PCUT_ASSERT_TRUE(job_blocked(u2->job, u3->job));
|
---|
159 | }
|
---|
160 |
|
---|
161 | PCUT_TEST(job_closure_fork) {
|
---|
162 | unit_t *u0 = mock_units[UNIT_SERVICE][0];
|
---|
163 | unit_t *u1 = mock_units[UNIT_SERVICE][1];
|
---|
164 | unit_t *u2 = mock_units[UNIT_SERVICE][2];
|
---|
165 | unit_t *u3 = mock_units[UNIT_SERVICE][3];
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * u0 -> u1 -> u2
|
---|
169 | * \-> u3
|
---|
170 | */
|
---|
171 | mock_add_dependency(u0, u1);
|
---|
172 | mock_add_dependency(u1, u2);
|
---|
173 | mock_add_dependency(u1, u3);
|
---|
174 |
|
---|
175 | job_t *main_job = job_create(u1, STATE_STARTED);
|
---|
176 | assert(main_job);
|
---|
177 |
|
---|
178 | int rc = job_create_closure(main_job, &act_closure);
|
---|
179 | PCUT_ASSERT_INT_EQUALS(EOK, rc);
|
---|
180 |
|
---|
181 | dyn_array_append(&exp_closure, job_t *, dummy_job(u1, STATE_STARTED));
|
---|
182 | dyn_array_append(&exp_closure, job_t *, dummy_job(u2, STATE_STARTED));
|
---|
183 | dyn_array_append(&exp_closure, job_t *, dummy_job(u3, STATE_STARTED));
|
---|
184 |
|
---|
185 | PCUT_ASSERT_TRUE(same_jobs(&exp_closure, &act_closure));
|
---|
186 | PCUT_ASSERT_TRUE(job_blocked(u1->job, u2->job));
|
---|
187 | PCUT_ASSERT_TRUE(job_blocked(u1->job, u3->job));
|
---|
188 | }
|
---|
189 |
|
---|
190 | PCUT_TEST(job_closure_triangle) {
|
---|
191 | unit_t *u0 = mock_units[UNIT_SERVICE][0];
|
---|
192 | unit_t *u1 = mock_units[UNIT_SERVICE][1];
|
---|
193 | unit_t *u2 = mock_units[UNIT_SERVICE][2];
|
---|
194 | unit_t *u3 = mock_units[UNIT_SERVICE][3];
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * u0 -> u1 -> u2
|
---|
198 | * \ v
|
---|
199 | * \-> u3
|
---|
200 | */
|
---|
201 | mock_add_dependency(u0, u1);
|
---|
202 | mock_add_dependency(u1, u2);
|
---|
203 | mock_add_dependency(u1, u3);
|
---|
204 | mock_add_dependency(u2, u3);
|
---|
205 |
|
---|
206 | job_t *main_job = job_create(u1, STATE_STARTED);
|
---|
207 | assert(main_job);
|
---|
208 |
|
---|
209 | int rc = job_create_closure(main_job, &act_closure);
|
---|
210 | PCUT_ASSERT_INT_EQUALS(EOK, rc);
|
---|
211 |
|
---|
212 | dyn_array_append(&exp_closure, job_t *, dummy_job(u1, STATE_STARTED));
|
---|
213 | dyn_array_append(&exp_closure, job_t *, dummy_job(u2, STATE_STARTED));
|
---|
214 | dyn_array_append(&exp_closure, job_t *, dummy_job(u3, STATE_STARTED));
|
---|
215 |
|
---|
216 | PCUT_ASSERT_TRUE(same_jobs(&exp_closure, &act_closure));
|
---|
217 | PCUT_ASSERT_TRUE(job_blocked(u1->job, u2->job));
|
---|
218 | PCUT_ASSERT_TRUE(job_blocked(u1->job, u3->job));
|
---|
219 | PCUT_ASSERT_TRUE(job_blocked(u2->job, u3->job));
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | PCUT_EXPORT(job_closure);
|
---|