source: mainline/uspace/srv/sysman/test/job_closure.c@ 5a88d87

Last change on this file since 5a88d87 was 25a9fec, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

sysman: Refactor job.c into job_queue.c and job_closure.c

  • Property mode set to 100644
File size: 6.3 KB
Line 
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_closure.h"
34
35#include "mock_unit.h"
36
37PCUT_INIT
38
39
40static dyn_array_t exp_closure;
41static dyn_array_t act_closure;
42
43static 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
49static 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
76static 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
88static job_t *dummy_job(unit_t *unit, unit_state_t target_state)
89{
90 job_t *result = job_create(unit, target_state);
91 return result;
92}
93
94static void dummy_add_closure(dyn_array_t *closure)
95{
96 dyn_array_foreach(*closure, job_t *, it) {
97 (*it)->unit->job = *it;
98 }
99}
100
101static 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
108PCUT_TEST_SUITE(job_closure);
109
110PCUT_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
123PCUT_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
133PCUT_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_edge(u0, u1);
143 mock_add_edge(u1, u2);
144 mock_add_edge(u2, u3);
145
146 /* Intentionally omit u0 */
147 job_t *main_job = job_create(u1, STATE_STARTED);
148 assert(main_job);
149
150 int rc = job_create_closure(main_job, &act_closure);
151 PCUT_ASSERT_INT_EQUALS(EOK, rc);
152
153 dyn_array_append(&exp_closure, job_t *, dummy_job(u1, STATE_STARTED));
154 dyn_array_append(&exp_closure, job_t *, dummy_job(u2, STATE_STARTED));
155 dyn_array_append(&exp_closure, job_t *, dummy_job(u3, STATE_STARTED));
156
157 dummy_add_closure(&act_closure);
158
159 PCUT_ASSERT_TRUE(same_jobs(&exp_closure, &act_closure));
160 PCUT_ASSERT_TRUE(job_blocked(u1->job, u2->job));
161 PCUT_ASSERT_TRUE(job_blocked(u2->job, u3->job));
162}
163
164PCUT_TEST(job_closure_fork) {
165 unit_t *u0 = mock_units[UNIT_SERVICE][0];
166 unit_t *u1 = mock_units[UNIT_SERVICE][1];
167 unit_t *u2 = mock_units[UNIT_SERVICE][2];
168 unit_t *u3 = mock_units[UNIT_SERVICE][3];
169
170 /*
171 * u0 -> u1 -> u2
172 * \-> u3
173 */
174 mock_add_edge(u0, u1);
175 mock_add_edge(u1, u2);
176 mock_add_edge(u1, u3);
177
178 job_t *main_job = job_create(u1, STATE_STARTED);
179 assert(main_job);
180
181 int rc = job_create_closure(main_job, &act_closure);
182 PCUT_ASSERT_INT_EQUALS(EOK, rc);
183
184 dyn_array_append(&exp_closure, job_t *, dummy_job(u1, STATE_STARTED));
185 dyn_array_append(&exp_closure, job_t *, dummy_job(u2, STATE_STARTED));
186 dyn_array_append(&exp_closure, job_t *, dummy_job(u3, STATE_STARTED));
187
188 dummy_add_closure(&act_closure);
189
190 PCUT_ASSERT_TRUE(same_jobs(&exp_closure, &act_closure));
191 PCUT_ASSERT_TRUE(job_blocked(u1->job, u2->job));
192 PCUT_ASSERT_TRUE(job_blocked(u1->job, u3->job));
193}
194
195PCUT_TEST(job_closure_triangle) {
196 unit_t *u0 = mock_units[UNIT_SERVICE][0];
197 unit_t *u1 = mock_units[UNIT_SERVICE][1];
198 unit_t *u2 = mock_units[UNIT_SERVICE][2];
199 unit_t *u3 = mock_units[UNIT_SERVICE][3];
200
201 /*
202 * u0 -> u1 -> u2
203 * \ v
204 * \-> u3
205 */
206 mock_add_edge(u0, u1);
207 mock_add_edge(u1, u2);
208 mock_add_edge(u1, u3);
209 mock_add_edge(u2, u3);
210
211 job_t *main_job = job_create(u1, STATE_STARTED);
212 assert(main_job);
213
214 int rc = job_create_closure(main_job, &act_closure);
215 PCUT_ASSERT_INT_EQUALS(EOK, rc);
216
217 dyn_array_append(&exp_closure, job_t *, dummy_job(u1, STATE_STARTED));
218 dyn_array_append(&exp_closure, job_t *, dummy_job(u2, STATE_STARTED));
219 dyn_array_append(&exp_closure, job_t *, dummy_job(u3, STATE_STARTED));
220
221 dummy_add_closure(&act_closure);
222
223 PCUT_ASSERT_TRUE(same_jobs(&exp_closure, &act_closure));
224 PCUT_ASSERT_TRUE(job_blocked(u1->job, u2->job));
225 PCUT_ASSERT_TRUE(job_blocked(u1->job, u3->job));
226 PCUT_ASSERT_TRUE(job_blocked(u2->job, u3->job));
227}
228
229
230PCUT_EXPORT(job_closure);
Note: See TracBrowser for help on using the repository browser.