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 <fibril.h>
|
---|
31 | #include <pcut/pcut.h>
|
---|
32 | #include <stdbool.h>
|
---|
33 |
|
---|
34 | #include "mock_unit.h"
|
---|
35 | #include "../job_queue.h"
|
---|
36 | #include "../sysman.h"
|
---|
37 |
|
---|
38 | PCUT_INIT
|
---|
39 |
|
---|
40 | PCUT_TEST_SUITE(job_queue);
|
---|
41 |
|
---|
42 | static bool initialized = false;
|
---|
43 |
|
---|
44 | static fid_t fibril_event_loop;
|
---|
45 |
|
---|
46 | static void job_finished_cb(void *object, void *arg)
|
---|
47 | {
|
---|
48 | job_t *job = object;
|
---|
49 | job_t **job_ptr = arg;
|
---|
50 |
|
---|
51 | /* Pass job reference to the caller */
|
---|
52 | *job_ptr = job;
|
---|
53 | }
|
---|
54 |
|
---|
55 | PCUT_TEST_BEFORE
|
---|
56 | {
|
---|
57 | mock_create_units();
|
---|
58 | mock_set_units_state(STATE_STOPPED);
|
---|
59 |
|
---|
60 | if (!initialized) {
|
---|
61 | sysman_events_init();
|
---|
62 | job_queue_init();
|
---|
63 |
|
---|
64 | #if 0
|
---|
65 | fibril_condvar_initialize(&async_finished_cv);
|
---|
66 | fibril_mutex_initialize(&async_finished_mtx);
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | fibril_event_loop = fibril_create(sysman_events_loop, NULL);
|
---|
70 | fibril_add_ready(fibril_event_loop);
|
---|
71 |
|
---|
72 | initialized = true;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | PCUT_TEST_AFTER
|
---|
77 | {
|
---|
78 | mock_destroy_units();
|
---|
79 | }
|
---|
80 |
|
---|
81 | PCUT_TEST(single_start_sync)
|
---|
82 | {
|
---|
83 | unit_type_vmts[UNIT_TARGET]->start = &mock_unit_vmt_start_sync;
|
---|
84 |
|
---|
85 | unit_t *u = mock_units[UNIT_TARGET][0];
|
---|
86 | job_t *job = NULL;
|
---|
87 |
|
---|
88 | errno_t rc = sysman_run_job(u, STATE_STARTED, 0, &job_finished_cb,
|
---|
89 | &job);
|
---|
90 | PCUT_ASSERT_INT_EQUALS(EOK, rc);
|
---|
91 |
|
---|
92 | sysman_process_queue();
|
---|
93 | PCUT_ASSERT_NOT_NULL(job);
|
---|
94 | PCUT_ASSERT_EQUALS(STATE_STARTED, u->state);
|
---|
95 |
|
---|
96 | job_del_ref(&job);
|
---|
97 | }
|
---|
98 |
|
---|
99 | PCUT_TEST(single_start_async)
|
---|
100 | {
|
---|
101 | unit_type_vmts[UNIT_TARGET]->start = &mock_unit_vmt_start_async;
|
---|
102 | unit_type_vmts[UNIT_TARGET]->exposee_created =
|
---|
103 | &mock_unit_vmt_exposee_created;
|
---|
104 |
|
---|
105 | unit_t *u = mock_units[UNIT_TARGET][0];
|
---|
106 | job_t *job = NULL;
|
---|
107 |
|
---|
108 | errno_t rc = sysman_run_job(u, STATE_STARTED, 0, &job_finished_cb, &job);
|
---|
109 | PCUT_ASSERT_INT_EQUALS(EOK, rc);
|
---|
110 |
|
---|
111 | sysman_process_queue();
|
---|
112 | PCUT_ASSERT_EQUALS(STATE_STARTING, u->state);
|
---|
113 |
|
---|
114 | sysman_raise_event(&sysman_event_unit_exposee_created, u);
|
---|
115 | sysman_process_queue();
|
---|
116 |
|
---|
117 | PCUT_ASSERT_NOT_NULL(job);
|
---|
118 | PCUT_ASSERT_EQUALS(STATE_STARTED, u->state);
|
---|
119 |
|
---|
120 | job_del_ref(&job);
|
---|
121 | }
|
---|
122 |
|
---|
123 | PCUT_TEST(multipath_to_started_unit)
|
---|
124 | {
|
---|
125 | /* Setup mock behavior */
|
---|
126 | unit_type_vmts[UNIT_SERVICE]->start = &mock_unit_vmt_start_sync;
|
---|
127 |
|
---|
128 | unit_type_vmts[UNIT_MOUNT]->start = &mock_unit_vmt_start_async;
|
---|
129 | unit_type_vmts[UNIT_MOUNT]->exposee_created =
|
---|
130 | &mock_unit_vmt_exposee_created;
|
---|
131 |
|
---|
132 | /* Define mock units */
|
---|
133 | unit_t *s0 = mock_units[UNIT_SERVICE][0];
|
---|
134 | unit_t *s1 = mock_units[UNIT_SERVICE][1];
|
---|
135 | unit_t *m0 = mock_units[UNIT_MOUNT][0];
|
---|
136 |
|
---|
137 | /* All services require root fs */
|
---|
138 | mock_add_edge(s0, m0);
|
---|
139 | mock_add_edge(s1, m0);
|
---|
140 |
|
---|
141 | /* S1 requires another mount and S0 */
|
---|
142 | mock_add_edge(s1, s0);
|
---|
143 |
|
---|
144 | /* Enforce initial state */
|
---|
145 | m0->state = STATE_STARTED;
|
---|
146 |
|
---|
147 | /* Run test */
|
---|
148 | job_t *job = NULL;
|
---|
149 | errno_t rc = sysman_run_job(s1, STATE_STARTED, 0, &job_finished_cb, &job);
|
---|
150 | PCUT_ASSERT_INT_EQUALS(EOK, rc);
|
---|
151 |
|
---|
152 | sysman_process_queue();
|
---|
153 |
|
---|
154 | PCUT_ASSERT_NOT_NULL(job);
|
---|
155 | PCUT_ASSERT_EQUALS(STATE_STARTED, s0->state);
|
---|
156 | PCUT_ASSERT_EQUALS(STATE_STARTED, s1->state);
|
---|
157 | }
|
---|
158 |
|
---|
159 | PCUT_TEST(merge_jobs_with_callback)
|
---|
160 | {
|
---|
161 | /* Setup mock behavior */
|
---|
162 | unit_type_vmts[UNIT_SERVICE]->start = &mock_unit_vmt_start_async;
|
---|
163 | unit_type_vmts[UNIT_SERVICE]->exposee_created =
|
---|
164 | &mock_unit_vmt_exposee_created;
|
---|
165 |
|
---|
166 | /* Define mock units */
|
---|
167 | unit_t *s0 = mock_units[UNIT_SERVICE][0];
|
---|
168 |
|
---|
169 | /* Create and start first job */
|
---|
170 | job_t *j0 = NULL;
|
---|
171 | errno_t rc = sysman_run_job(s0, STATE_STARTED, 0, &job_finished_cb, &j0);
|
---|
172 | PCUT_ASSERT_INT_EQUALS(EOK, rc);
|
---|
173 |
|
---|
174 | sysman_process_queue();
|
---|
175 | /* Job not finished */
|
---|
176 | PCUT_ASSERT_NULL(j0);
|
---|
177 |
|
---|
178 | /*
|
---|
179 | * While s0 is starting in j0, create second job that should be merged
|
---|
180 | * into the existing one.
|
---|
181 | */
|
---|
182 | job_t *j1 = NULL;
|
---|
183 | rc = sysman_run_job(s0, STATE_STARTED, 0, &job_finished_cb, &j1);
|
---|
184 | PCUT_ASSERT_INT_EQUALS(EOK, rc);
|
---|
185 |
|
---|
186 | sysman_process_queue();
|
---|
187 | /* Job not finished */
|
---|
188 | PCUT_ASSERT_NULL(j1);
|
---|
189 |
|
---|
190 | sysman_raise_event(&sysman_event_unit_exposee_created, s0);
|
---|
191 | sysman_process_queue();
|
---|
192 |
|
---|
193 | PCUT_ASSERT_NOT_NULL(j0);
|
---|
194 | PCUT_ASSERT_NOT_NULL(j1);
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * Jobs were, merged so both callbacks should have been called with the
|
---|
198 | * same job
|
---|
199 | */
|
---|
200 | PCUT_ASSERT_EQUALS(j0, j1);
|
---|
201 |
|
---|
202 | /* And there should be exactly two references (per each callback) */
|
---|
203 | job_del_ref(&j0);
|
---|
204 | job_del_ref(&j0);
|
---|
205 |
|
---|
206 | PCUT_ASSERT_NULL(j0);
|
---|
207 | }
|
---|
208 |
|
---|
209 | PCUT_EXPORT(job_queue);
|
---|