File: progress.c

package info (click to toggle)
rauc 1.15-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,336 kB
  • sloc: ansic: 36,989; python: 3,354; sh: 1,391; xml: 53; makefile: 41
file content (227 lines) | stat: -rw-r--r-- 7,021 bytes parent folder | download
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <stdio.h>
#include <locale.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <locale.h>
#include <context.h>

gint callback_counter;
gint last_percentage;

static void test_progress_callback(gint percentage,
		const gchar *message,
		gint nesting_depth)
{
	/* percentage sanity checks */
	g_assert_cmpint(percentage, >=, 0);
	g_assert_cmpint(percentage, <=, 100);

	g_assert_nonnull(message);
	g_assert_cmpint(percentage, >=, last_percentage);

	callback_counter++;
	last_percentage = percentage;
}

static void progress_test_nesting(void)
{
	RaucProgressStep *step;

	/* reset global state */
	callback_counter = 0;
	last_percentage = 0;

	g_assert_nonnull(r_context()->progress_callback);

	/* test several nested steps */
	r_context_begin_step("test_1", "testing step 1", 1);
	r_context_begin_step("test_1.1", "testing step 1.1", 1);
	r_context_begin_step("test_1.1.1", "testing step 1.1.1", 1);
	r_context_begin_step("test_1.1.1.1", "testing step 1.1.1.1", 2);
	r_context_begin_step("test_1.1.1.1.1", "testing step 1.1.1.1.1", 0);
	g_assert_cmpint(g_list_length(r_context()->progress), ==, 5);
	r_context_end_step("test_1.1.1.1.1", TRUE);

	r_context_begin_step("test_1.1.1.1.2", "testing step 1.1.1.1.2", 0);
	g_assert_cmpint(g_list_length(r_context()->progress), ==, 5);

	/* test RaucProgressStep items in list */
	for (guint i = 0; i < g_list_length(r_context()->progress); i++) {
		step = g_list_nth_data(r_context()->progress, i);

		g_assert_nonnull(step->description);
		g_assert_nonnull(step->name);

		g_assert_cmpint(step->substeps_total, >=, 0);
		g_assert_cmpint(step->substeps_done, >=, 0);
		g_assert_cmpint(step->substeps_done, <=, step->substeps_done);
	}

	r_context_end_step("test_1.1.1.1.2", TRUE);
	r_context_end_step("test_1.1.1.1", TRUE);
	r_context_end_step("test_1.1.1", TRUE);
	r_context_end_step("test_1.1", TRUE);
	r_context_end_step("test_1", TRUE);

	g_assert_cmpint(g_list_length(r_context()->progress), ==, 0);
	g_assert_cmpint(last_percentage, ==, 100);

	/* callback should have been called twice per step */
	g_assert_cmpint(callback_counter, ==, 12);
}

static void progress_test_unsuccessful_substep(void)
{
	/* reset global state */
	callback_counter = 0;
	last_percentage = 0;

	/* test unsuccessful substep */
	r_context_begin_step("test_1", "testing step 1", 1);
	r_context_begin_step("test_1.1", "testing step 1.1", 0);
	r_context_end_step("test_1.1", FALSE);
	r_context_end_step("test_1", TRUE);

	g_assert_cmpint(last_percentage, ==, 100);

	/* callback should have been called twice per step */
	g_assert_cmpint(callback_counter, ==, 4);
}

static void progress_test_explicit_percentage(void)
{
	/* reset global state */
	callback_counter = 0;
	last_percentage = 0;

	/* test setting explicit percentage */
	r_context_begin_step("test_1", "testing step 1", 2);
	r_context_begin_step("test_1.1", "testing step 1.1", 0);
	r_context_end_step("test_1.1", TRUE);
	g_assert_cmpint(last_percentage, ==, 50);

	r_context_begin_step("test_1.2", "testing step 1.2", 0);

	r_context_set_step_percentage("test_1.2", 25);
	g_assert_cmpint(last_percentage, ==, 62);

	r_context_set_step_percentage("test_1.2", 50);
	g_assert_cmpint(last_percentage, ==, 75);

	r_context_set_step_percentage("test_1.2", 75);
	g_assert_cmpint(last_percentage, ==, 87);

	r_context_end_step("test_1.2", TRUE);
	r_context_end_step("test_1", TRUE);
	g_assert_cmpint(last_percentage, ==, 100);

	/* callback gets called twice per step and once per explicit percentage
	 * set
	 */
	g_assert_cmpint(callback_counter, ==, 9);
}

/* Tests that going back and forth in step percentage does not break the
 * progress.
 * At the moment, this behavior is required for proper adaptive mode fallback.
 */
static void progress_test_explicit_percentage_backwards(void)
{
	/* reset global state */
	callback_counter = 0;
	last_percentage = 0;

	r_context_begin_step("test_1", "testing step 1", 1);
	r_context_begin_step("test_1.1", "testing step 1.1", 0);
	g_assert_cmpint(last_percentage, ==, 0);

	r_context_set_step_percentage("test_1.1", 50);
	g_assert_cmpint(last_percentage, ==, 50);
	g_assert_cmpint(callback_counter, ==, 3);

	/* When jumping back, callback must not be called */
	r_context_set_step_percentage("test_1.1", 25);
	g_assert_cmpint(last_percentage, ==, 50);
	g_assert_cmpint(callback_counter, ==, 3);

	/* When increasing, but being below the so-far maximum,
	 * callback still must not be called */
	r_context_set_step_percentage("test_1.1", 30);
	g_assert_cmpint(last_percentage, ==, 50);
	g_assert_cmpint(callback_counter, ==, 3);

	/* When exceedeing the so-far maximum, callback be called and progress
	 * must proceed */
	r_context_set_step_percentage("test_1.1", 75);
	g_assert_cmpint(last_percentage, ==, 75);
	g_assert_cmpint(callback_counter, ==, 4);

	r_context_end_step("test_1.1", TRUE);
	r_context_end_step("test_1", TRUE);
	g_assert_cmpint(last_percentage, ==, 100);
	g_assert_cmpint(callback_counter, ==, 6);
}

static void progress_test_weighted_steps(void)
{
	/* reset global state */
	callback_counter = 0;
	last_percentage = 0;

	/* test setting explicit percentage */
	r_context_begin_step("test_1", "testing step 1", 10);
	r_context_begin_step_weighted("test_1.1", "testing step 1.1", 1, 2);
	r_context_begin_step("test_1.1.1", "testing step 1", 0);
	r_context_end_step("test_1.1.1", TRUE);
	g_assert_cmpint(last_percentage, ==, 20);
	r_context_end_step("test_1.1", TRUE);
	g_assert_cmpint(last_percentage, ==, 20);

	r_context_begin_step_weighted("test_1.1", "testing step 1.1", 0, 7);
	r_context_end_step("test_1.1", TRUE);
	g_assert_cmpint(last_percentage, ==, 90);

	r_context_begin_step("test_1.2", "testing step 1.2", 0);

	r_context_set_step_percentage("test_1.2", 25);
	g_assert_cmpint(last_percentage, ==, 92);

	r_context_set_step_percentage("test_1.2", 50);
	g_assert_cmpint(last_percentage, ==, 95);

	r_context_set_step_percentage("test_1.2", 75);
	g_assert_cmpint(last_percentage, ==, 97);

	r_context_end_step("test_1.2", TRUE);
	r_context_end_step("test_1", TRUE);
	g_assert_cmpint(last_percentage, ==, 100);

	/* callback gets called twice per step and once per explicit percentage
	 * set
	 */
	g_assert_cmpint(callback_counter, ==, 13);
}

int main(int argc, char *argv[])
{
	setlocale(LC_ALL, "C");

	g_assert(g_setenv("GIO_USE_VFS", "local", TRUE));

	/* set up config/context */
	r_context_conf()->configpath = g_strdup("test/test.conf");
	r_context();

	r_context_register_progress_callback(test_progress_callback);

	g_test_init(&argc, &argv, NULL);

	g_test_add_func("/progress/test_nesting", progress_test_nesting);
	g_test_add_func("/progress/test_unsuccessful_substep", progress_test_unsuccessful_substep);
	g_test_add_func("/progress/test_explicit_percentage", progress_test_explicit_percentage);
	g_test_add_func("/progress/test_explicit_percentage_backwards", progress_test_explicit_percentage_backwards);
	g_test_add_func("/progress/test_weighted_steps", progress_test_weighted_steps);

	return g_test_run();
}