Debian Codegraph @

testsuite/test-state-machine.c

raw symbols SCIP
1 /* test-state-machine.c
2 *
3 * Copyright 2026 Christian Hergert
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include "config.h"
22
23 #include "test-util.h"
24
25 typedef enum _TestState
26 {
27 TEST_STATE_INITIAL,
28 TEST_STATE_PREPARE,
29 TEST_STATE_READY,
30 TEST_STATE_FAILED,
31 } TestState;
32
33 typedef struct _TransitionData
34 {
35 GArray *visited;
36 DexFuture *interrupt;
37 } TransitionData;
38
39 typedef struct _SchedulerData
40 {
41 DexScheduler *scheduler;
42 gboolean used_scheduler;
43 } SchedulerData;
44
45 GType test_state_get_type (void);
46
47 G_DEFINE_ENUM_TYPE (TestState, test_state,
48 G_DEFINE_ENUM_VALUE (TEST_STATE_INITIAL, "initial"),
49 G_DEFINE_ENUM_VALUE (TEST_STATE_PREPARE, "prepare"),
50 G_DEFINE_ENUM_VALUE (TEST_STATE_READY, "ready"),
51 G_DEFINE_ENUM_VALUE (TEST_STATE_FAILED, "failed"))
52
53 #define TEST_TYPE_STATE (test_state_get_type())
54
55 static void
56 transition_data_free (TransitionData *data)
57 {
58 dex_clear (&data->interrupt);
59 g_array_unref (data->visited);
60 g_free (data);
61 }
62
63 static TransitionData *
64 transition_data_new (void)
65 {
66 TransitionData *data;
67
68 data = g_new0 (TransitionData, 1);
69 data->visited = g_array_new (FALSE, FALSE, sizeof (guint));
70
71 return data;
72 }
73
74 static gboolean
75 transition_basic (DexStateTransitionContext *context,
76 gpointer user_data,
77 GError **error)
78 {
79 TransitionData *data = user_data;
80 guint from;
81 guint to;
82
83 g_assert_nonnull (context);
84
85 from = dex_state_transition_context_get_from (context);
86 to = dex_state_transition_context_get_to (context);
87
88 g_assert_cmpuint (dex_state_transition_context_get_state (context), ==, from);
89
90 g_array_append_val (data->visited, from);
91 g_array_append_val (data->visited, to);
92
93 return TRUE;
94 }
95
96 static gboolean
97 transition_prepare (DexStateTransitionContext *context,
98 gpointer user_data,
99 GError **error)
100 {
101 TransitionData *data = user_data;
102 guint from;
103 guint to;
104
105 from = dex_state_transition_context_get_from (context);
106 to = dex_state_transition_context_get_to (context);
107
108 g_assert_cmpuint (from, ==, TEST_STATE_INITIAL);
109 g_assert_cmpuint (to, ==, TEST_STATE_PREPARE);
110 g_assert_cmpuint (dex_state_transition_context_get_state (context), ==, TEST_STATE_INITIAL);
111
112 g_array_append_val (data->visited, from);
113 g_array_append_val (data->visited, to);
114
115 dex_state_transition_context_set_state (context, TEST_STATE_PREPARE);
116
117 g_assert_cmpuint (dex_state_transition_context_get_state (context), ==, TEST_STATE_PREPARE);
118
119 dex_await (dex_timeout_new_msec (1), NULL);
120
121 dex_state_transition_context_set_state (context, TEST_STATE_READY);
122
123 return TRUE;
124 }
125
126 static gboolean
127 transition_prepare_back (DexStateTransitionContext *context,
128 gpointer user_data,
129 GError **error)
130 {
131 TransitionData *data = user_data;
132 guint from;
133 guint to;
134
135 from = dex_state_transition_context_get_from (context);
136 to = dex_state_transition_context_get_to (context);
137
138 g_assert_cmpuint (from, ==, TEST_STATE_INITIAL);
139 g_assert_cmpuint (to, ==, TEST_STATE_PREPARE);
140 g_assert_cmpuint (dex_state_transition_context_get_state (context), ==, TEST_STATE_INITIAL);
141
142 g_array_append_val (data->visited, from);
143 g_array_append_val (data->visited, to);
144
145 dex_state_transition_context_set_state (context, TEST_STATE_PREPARE);
146
147 g_assert_cmpuint (dex_state_transition_context_get_state (context), ==, TEST_STATE_PREPARE);
148
149 dex_state_transition_context_set_state (context, TEST_STATE_INITIAL);
150
151 return TRUE;
152 }
153
154 static gboolean
155 transition_continue_to_ready (DexStateTransitionContext *context,
156 gpointer user_data,
157 GError **error)
158 {
159 TransitionData *data = user_data;
160 guint from;
161 guint to;
162
163 from = dex_state_transition_context_get_from (context);
164 to = dex_state_transition_context_get_to (context);
165
166 g_assert_cmpuint (from, ==, TEST_STATE_INITIAL);
167 g_assert_cmpuint (to, ==, TEST_STATE_PREPARE);
168 g_assert_cmpuint (dex_state_transition_context_get_state (context), ==, TEST_STATE_INITIAL);
169
170 g_array_append_val (data->visited, from);
171 g_array_append_val (data->visited, to);
172
173 return dex_state_transition_context_continue_to (context, TEST_STATE_READY, error);
174 }
175
176 static gboolean
177 transition_continue_to_failed (DexStateTransitionContext *context,
178 gpointer user_data,
179 GError **error)
180 {
181 TransitionData *data = user_data;
182 guint from;
183 guint to;
184
185 from = dex_state_transition_context_get_from (context);
186 to = dex_state_transition_context_get_to (context);
187
188 g_assert_cmpuint (from, ==, TEST_STATE_INITIAL);
189 g_assert_cmpuint (to, ==, TEST_STATE_PREPARE);
190 g_assert_cmpuint (dex_state_transition_context_get_state (context), ==, TEST_STATE_INITIAL);
191
192 g_array_append_val (data->visited, from);
193 g_array_append_val (data->visited, to);
194
195 return dex_state_transition_context_continue_to (context, TEST_STATE_FAILED, error);
196 }
197
198 static gboolean
199 transition_fail (DexStateTransitionContext *context,
200 gpointer user_data,
201 GError **error)
202 {
203 g_set_error (error,
204 G_IO_ERROR,
205 G_IO_ERROR_FAILED,
206 "Failed transition");
207
208 return FALSE;
209 }
210
211 static gboolean
212 transition_scheduler (DexStateTransitionContext *context,
213 gpointer user_data,
214 GError **error)
215 {
216 SchedulerData *data = user_data;
217 DexScheduler *thread_default;
218
219 thread_default = dex_scheduler_get_thread_default ();
220
221 g_assert_nonnull (thread_default);
222 g_assert_true (dex_scheduler_get_main_context (thread_default) ==
223 dex_scheduler_get_main_context (data->scheduler));
224
225 data->used_scheduler = TRUE;
226
227 return TRUE;
228 }
229
230 static gboolean
231 transition_interrupt (DexStateTransitionContext *context,
232 gpointer user_data,
233 GError **error)
234 {
235 TransitionData *data = user_data;
236 guint from;
237 guint to;
238
239 from = dex_state_transition_context_get_from (context);
240 to = dex_state_transition_context_get_to (context);
241
242 g_assert_cmpuint (from, ==, TEST_STATE_INITIAL);
243 g_assert_cmpuint (to, ==, TEST_STATE_READY);
244
245 g_array_append_val (data->visited, from);
246 g_array_append_val (data->visited, to);
247
248 dex_state_transition_context_set_state (context, TEST_STATE_PREPARE);
249
250 if (!dex_await (dex_state_transition_context_wait_for_interrupt (context), error))
251 return FALSE;
252
253 dex_state_transition_context_set_state (context, TEST_STATE_READY);
254
255 return TRUE;
256 }
257
258 static gboolean
259 transition_interrupt_late_wait (DexStateTransitionContext *context,
260 gpointer user_data,
261 GError **error)
262 {
263 TransitionData *data = user_data;
264 guint from;
265 guint to;
266
267 from = dex_state_transition_context_get_from (context);
268 to = dex_state_transition_context_get_to (context);
269
270 g_assert_cmpuint (from, ==, TEST_STATE_INITIAL);
271 g_assert_cmpuint (to, ==, TEST_STATE_READY);
272
273 g_array_append_val (data->visited, from);
274 g_array_append_val (data->visited, to);
275
276 dex_state_transition_context_set_state (context, TEST_STATE_PREPARE);
277
278 dex_await (dex_timeout_new_msec (10), NULL);
279
280 if (!dex_await (dex_state_transition_context_wait_for_interrupt (context), error))
281 return FALSE;
282
283 dex_state_transition_context_set_state (context, TEST_STATE_READY);
284
285 return TRUE;
286 }
287
288 static gboolean
289 transition_interrupt_not_taken (DexStateTransitionContext *context,
290 gpointer user_data,
291 GError **error)
292 {
293 TransitionData *data = user_data;
294
295 g_assert_null (data->interrupt);
296
297 data->interrupt = dex_state_transition_context_wait_for_interrupt (context);
298
299 return TRUE;
300 }
301
302 static void
303 test_state_machine_basic (void)
304 {
305 static const DexStateTransition transitions[] = {
306 { TEST_STATE_INITIAL, TEST_STATE_READY, transition_basic },
307 };
308 g_autoptr(DexStateMachine) state_machine = NULL;
309 g_autoptr(GError) error = NULL;
310 TransitionData *data;
311 guint state;
312
313 data = transition_data_new ();
314 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
315 TEST_STATE_INITIAL,
316 transitions,
317 G_N_ELEMENTS (transitions),
318 NULL,
319 0,
320 data,
321 (GDestroyNotify)transition_data_free);
322
323 g_assert_true (DEX_IS_STATE_MACHINE (state_machine));
324 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_INITIAL);
325
326 state = dex_await_enum (dex_state_machine_transition (state_machine, TEST_STATE_READY), &error);
327
328 g_assert_no_error (error);
329 g_assert_cmpuint (state, ==, TEST_STATE_READY);
330 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_READY);
331 g_assert_cmpuint (data->visited->len, ==, 2);
332 g_assert_cmpuint (g_array_index (data->visited, guint, 0), ==, TEST_STATE_INITIAL);
333 g_assert_cmpuint (g_array_index (data->visited, guint, 1), ==, TEST_STATE_READY);
334 }
335
336 static void
337 test_state_machine_landing (void)
338 {
339 static const DexStateTransition transitions[] = {
340 { TEST_STATE_INITIAL, TEST_STATE_PREPARE, transition_prepare },
341 };
342 g_autoptr(DexStateMachine) state_machine = NULL;
343 g_autoptr(GError) error = NULL;
344 TransitionData *data;
345 guint state;
346
347 data = transition_data_new ();
348 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
349 TEST_STATE_INITIAL,
350 transitions,
351 G_N_ELEMENTS (transitions),
352 NULL,
353 0,
354 data,
355 (GDestroyNotify)transition_data_free);
356
357 state = dex_await_enum (dex_state_machine_transition (state_machine, TEST_STATE_PREPARE), &error);
358
359 g_assert_no_error (error);
360 g_assert_cmpuint (state, ==, TEST_STATE_READY);
361 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_READY);
362 g_assert_cmpuint (data->visited->len, ==, 2);
363 g_assert_cmpuint (g_array_index (data->visited, guint, 0), ==, TEST_STATE_INITIAL);
364 g_assert_cmpuint (g_array_index (data->visited, guint, 1), ==, TEST_STATE_PREPARE);
365 }
366
367 static void
368 test_state_machine_reverse (void)
369 {
370 static const DexStateTransition transitions[] = {
371 { TEST_STATE_INITIAL, TEST_STATE_READY, transition_basic },
372 { TEST_STATE_READY, TEST_STATE_INITIAL, transition_basic },
373 };
374 g_autoptr(DexStateMachine) state_machine = NULL;
375 g_autoptr(GError) error = NULL;
376 TransitionData *data;
377 guint state;
378
379 data = transition_data_new ();
380 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
381 TEST_STATE_INITIAL,
382 transitions,
383 G_N_ELEMENTS (transitions),
384 NULL,
385 0,
386 data,
387 (GDestroyNotify)transition_data_free);
388
389 state = dex_await_enum (dex_state_machine_transition (state_machine, TEST_STATE_READY), &error);
390
391 g_assert_no_error (error);
392 g_assert_cmpuint (state, ==, TEST_STATE_READY);
393 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_READY);
394
395 state = dex_await_enum (dex_state_machine_transition (state_machine, TEST_STATE_INITIAL), &error);
396
397 g_assert_no_error (error);
398 g_assert_cmpuint (state, ==, TEST_STATE_INITIAL);
399 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_INITIAL);
400 g_assert_cmpuint (data->visited->len, ==, 4);
401 g_assert_cmpuint (g_array_index (data->visited, guint, 0), ==, TEST_STATE_INITIAL);
402 g_assert_cmpuint (g_array_index (data->visited, guint, 1), ==, TEST_STATE_READY);
403 g_assert_cmpuint (g_array_index (data->visited, guint, 2), ==, TEST_STATE_READY);
404 g_assert_cmpuint (g_array_index (data->visited, guint, 3), ==, TEST_STATE_INITIAL);
405 }
406
407 static void
408 test_state_machine_landing_reverse (void)
409 {
410 static const DexStateTransition transitions[] = {
411 { TEST_STATE_INITIAL, TEST_STATE_PREPARE, transition_prepare_back },
412 };
413 g_autoptr(DexStateMachine) state_machine = NULL;
414 g_autoptr(GError) error = NULL;
415 TransitionData *data;
416 guint state;
417
418 data = transition_data_new ();
419 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
420 TEST_STATE_INITIAL,
421 transitions,
422 G_N_ELEMENTS (transitions),
423 NULL,
424 0,
425 data,
426 (GDestroyNotify)transition_data_free);
427
428 state = dex_await_enum (dex_state_machine_transition (state_machine, TEST_STATE_PREPARE), &error);
429
430 g_assert_no_error (error);
431 g_assert_cmpuint (state, ==, TEST_STATE_INITIAL);
432 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_INITIAL);
433 g_assert_cmpuint (data->visited->len, ==, 2);
434 g_assert_cmpuint (g_array_index (data->visited, guint, 0), ==, TEST_STATE_INITIAL);
435 g_assert_cmpuint (g_array_index (data->visited, guint, 1), ==, TEST_STATE_PREPARE);
436 }
437
438 static void
439 test_state_machine_invalid (void)
440 {
441 static const DexStateTransition transitions[] = {
442 { TEST_STATE_INITIAL, TEST_STATE_READY, transition_basic },
443 };
444 g_autoptr(DexStateMachine) state_machine = NULL;
445 g_autoptr(GError) error = NULL;
446 TransitionData *data;
447 guint state;
448
449 data = transition_data_new ();
450 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
451 TEST_STATE_INITIAL,
452 transitions,
453 G_N_ELEMENTS (transitions),
454 NULL,
455 0,
456 data,
457 (GDestroyNotify)transition_data_free);
458
459 state = dex_await_enum (dex_state_machine_transition (state_machine, TEST_STATE_FAILED), &error);
460
461 g_assert_error (error, DEX_ERROR, DEX_ERROR_INVALID_TRANSITION);
462 g_assert_cmpuint (state, ==, 0);
463 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_INITIAL);
464 g_clear_error (&error);
465 }
466
467 static void
468 test_state_machine_failure (void)
469 {
470 static const DexStateTransition transitions[] = {
471 { TEST_STATE_INITIAL, TEST_STATE_FAILED, transition_fail },
472 };
473 g_autoptr(DexStateMachine) state_machine = NULL;
474 g_autoptr(GError) error = NULL;
475 TransitionData *data;
476 guint state;
477
478 data = transition_data_new ();
479 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
480 TEST_STATE_INITIAL,
481 transitions,
482 G_N_ELEMENTS (transitions),
483 NULL,
484 0,
485 data,
486 (GDestroyNotify)transition_data_free);
487
488 state = dex_await_enum (dex_state_machine_transition (state_machine, TEST_STATE_FAILED), &error);
489
490 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
491 g_assert_cmpuint (state, ==, 0);
492 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_INITIAL);
493 g_clear_error (&error);
494 }
495
496 static void
497 test_state_machine_requested_state (void)
498 {
499 static const DexStateTransition transitions[] = {
500 { TEST_STATE_INITIAL, TEST_STATE_FAILED, transition_fail },
501 };
502 g_autoptr(DexStateMachine) state_machine = NULL;
503 g_autoptr(DexFuture) transition = NULL;
504 g_autoptr(GError) error = NULL;
505 guint state;
506
507 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
508 TEST_STATE_INITIAL,
509 transitions,
510 G_N_ELEMENTS (transitions),
511 NULL,
512 0,
513 NULL,
514 NULL);
515
516 g_assert_cmpuint (dex_state_machine_get_requested_state (state_machine),
517 ==,
518 TEST_STATE_INITIAL);
519
520 transition = dex_state_machine_transition (state_machine, TEST_STATE_PREPARE);
521
522 g_assert_cmpuint (dex_state_machine_get_requested_state (state_machine),
523 ==,
524 TEST_STATE_PREPARE);
525
526 state = dex_await_enum (dex_ref (transition), &error);
527
528 g_assert_error (error, DEX_ERROR, DEX_ERROR_INVALID_TRANSITION);
529 g_assert_cmpuint (state, ==, 0);
530 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_INITIAL);
531 g_assert_cmpuint (dex_state_machine_get_requested_state (state_machine),
532 ==,
533 TEST_STATE_PREPARE);
534 dex_clear (&transition);
535 g_clear_error (&error);
536
537 transition = dex_state_machine_transition (state_machine, TEST_STATE_FAILED);
538
539 g_assert_cmpuint (dex_state_machine_get_requested_state (state_machine),
540 ==,
541 TEST_STATE_FAILED);
542
543 state = dex_await_enum (dex_ref (transition), &error);
544
545 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
546 g_assert_cmpuint (state, ==, 0);
547 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_INITIAL);
548 g_assert_cmpuint (dex_state_machine_get_requested_state (state_machine),
549 ==,
550 TEST_STATE_FAILED);
551 dex_clear (&transition);
552 g_clear_error (&error);
553
554 transition = dex_state_machine_transition (state_machine, G_MAXUINT);
555
556 g_assert_cmpuint (dex_state_machine_get_requested_state (state_machine),
557 ==,
558 TEST_STATE_FAILED);
559
560 state = dex_await_enum (dex_ref (transition), &error);
561
562 g_assert_error (error, DEX_ERROR, DEX_ERROR_INVALID_TRANSITION);
563 g_assert_cmpuint (state, ==, 0);
564 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_INITIAL);
565 g_assert_cmpuint (dex_state_machine_get_requested_state (state_machine),
566 ==,
567 TEST_STATE_FAILED);
568 g_clear_error (&error);
569 }
570
571 static void
572 test_state_machine_wait_for_state_immediate (void)
573 {
574 g_autoptr(DexStateMachine) state_machine = NULL;
575 g_autoptr(GError) error = NULL;
576 guint state;
577
578 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
579 TEST_STATE_INITIAL,
580 NULL,
581 0,
582 NULL,
583 0,
584 NULL,
585 NULL);
586
587 state = dex_await_enum (dex_state_machine_wait_for_state (state_machine,
588 TEST_STATE_INITIAL),
589 &error);
590
591 g_assert_no_error (error);
592 g_assert_cmpuint (state, ==, TEST_STATE_INITIAL);
593 }
594
595 static void
596 test_state_machine_wait_for_state_transition (void)
597 {
598 static const DexStateTransition transitions[] = {
599 { TEST_STATE_INITIAL, TEST_STATE_READY, transition_basic },
600 };
601 g_autoptr(DexStateMachine) state_machine = NULL;
602 g_autoptr(DexFuture) wait = NULL;
603 g_autoptr(DexFuture) transition = NULL;
604 g_autoptr(GError) error = NULL;
605 TransitionData *data;
606 guint state;
607
608 data = transition_data_new ();
609 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
610 TEST_STATE_INITIAL,
611 transitions,
612 G_N_ELEMENTS (transitions),
613 NULL,
614 0,
615 data,
616 (GDestroyNotify)transition_data_free);
617
618 wait = dex_state_machine_wait_for_state (state_machine, TEST_STATE_READY);
619
620 g_assert_true (dex_future_is_pending (wait));
621
622 transition = dex_state_machine_transition (state_machine, TEST_STATE_READY);
623 state = dex_await_enum (dex_ref (transition), &error);
624
625 g_assert_no_error (error);
626 g_assert_cmpuint (state, ==, TEST_STATE_READY);
627
628 state = dex_await_enum (dex_ref (wait), &error);
629
630 g_assert_no_error (error);
631 g_assert_cmpuint (state, ==, TEST_STATE_READY);
632 }
633
634 static void
635 test_state_machine_wait_for_state_intermediate (void)
636 {
637 static const DexStateTransition transitions[] = {
638 { TEST_STATE_INITIAL, TEST_STATE_PREPARE, transition_prepare },
639 };
640 g_autoptr(DexStateMachine) state_machine = NULL;
641 g_autoptr(DexFuture) wait = NULL;
642 g_autoptr(DexFuture) transition = NULL;
643 g_autoptr(GError) error = NULL;
644 TransitionData *data;
645 guint state;
646
647 data = transition_data_new ();
648 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
649 TEST_STATE_INITIAL,
650 transitions,
651 G_N_ELEMENTS (transitions),
652 NULL,
653 0,
654 data,
655 (GDestroyNotify)transition_data_free);
656
657 wait = dex_state_machine_wait_for_state (state_machine, TEST_STATE_PREPARE);
658 transition = dex_state_machine_transition (state_machine, TEST_STATE_PREPARE);
659
660 state = dex_await_enum (dex_ref (wait), &error);
661
662 g_assert_no_error (error);
663 g_assert_cmpuint (state, ==, TEST_STATE_PREPARE);
664
665 state = dex_await_enum (dex_ref (transition), &error);
666
667 g_assert_no_error (error);
668 g_assert_cmpuint (state, ==, TEST_STATE_READY);
669 }
670
671 static void
672 test_state_machine_wait_for_state_invalid (void)
673 {
674 g_autoptr(DexStateMachine) state_machine = NULL;
675 g_autoptr(GError) error = NULL;
676 guint state;
677
678 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
679 TEST_STATE_INITIAL,
680 NULL,
681 0,
682 NULL,
683 0,
684 NULL,
685 NULL);
686
687 state = dex_await_enum (dex_state_machine_wait_for_state (state_machine, G_MAXUINT), &error);
688
689 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVAL);
690 g_assert_cmpuint (state, ==, 0);
691 g_clear_error (&error);
692 }
693
694 static void
695 test_state_machine_wait_for_state_finalized (void)
696 {
697 DexStateMachine *state_machine;
698 g_autoptr(DexFuture) wait = NULL;
699 g_autoptr(GError) error = NULL;
700 guint state;
701
702 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
703 TEST_STATE_INITIAL,
704 NULL,
705 0,
706 NULL,
707 0,
708 NULL,
709 NULL);
710
711 wait = dex_state_machine_wait_for_state (state_machine, TEST_STATE_READY);
712
713 g_assert_true (dex_future_is_pending (wait));
714
715 dex_unref (state_machine);
716
717 state = dex_await_enum (dex_ref (wait), &error);
718
719 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
720 g_assert_cmpuint (state, ==, 0);
721 g_clear_error (&error);
722 }
723
724 static void
725 test_state_machine_interrupt_none (void)
726 {
727 g_autoptr(DexStateMachine) state_machine = NULL;
728
729 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
730 TEST_STATE_INITIAL,
731 NULL,
732 0,
733 NULL,
734 0,
735 NULL,
736 NULL);
737
738 g_assert_false (dex_state_machine_interrupt (state_machine));
739 }
740
741 static void
742 test_state_machine_interrupt (void)
743 {
744 static const DexStateTransition transitions[] = {
745 { TEST_STATE_INITIAL, TEST_STATE_READY, transition_interrupt },
746 };
747 g_autoptr(DexStateMachine) state_machine = NULL;
748 g_autoptr(DexFuture) wait = NULL;
749 g_autoptr(DexFuture) transition = NULL;
750 g_autoptr(GError) error = NULL;
751 TransitionData *data;
752 guint state;
753
754 data = transition_data_new ();
755 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
756 TEST_STATE_INITIAL,
757 transitions,
758 G_N_ELEMENTS (transitions),
759 NULL,
760 0,
761 data,
762 (GDestroyNotify)transition_data_free);
763
764 wait = dex_state_machine_wait_for_state (state_machine, TEST_STATE_PREPARE);
765 transition = dex_state_machine_transition (state_machine, TEST_STATE_READY);
766
767 state = dex_await_enum (dex_ref (wait), &error);
768
769 g_assert_no_error (error);
770 g_assert_cmpuint (state, ==, TEST_STATE_PREPARE);
771 g_assert_true (dex_future_is_pending (transition));
772
773 g_assert_true (dex_state_machine_interrupt (state_machine));
774
775 state = dex_await_enum (dex_ref (transition), &error);
776
777 g_assert_no_error (error);
778 g_assert_cmpuint (state, ==, TEST_STATE_READY);
779 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_READY);
780 }
781
782 static void
783 test_state_machine_interrupt_latched (void)
784 {
785 static const DexStateTransition transitions[] = {
786 { TEST_STATE_INITIAL, TEST_STATE_READY, transition_interrupt_late_wait },
787 };
788 g_autoptr(DexStateMachine) state_machine = NULL;
789 g_autoptr(DexFuture) wait = NULL;
790 g_autoptr(DexFuture) transition = NULL;
791 g_autoptr(GError) error = NULL;
792 TransitionData *data;
793 guint state;
794
795 data = transition_data_new ();
796 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
797 TEST_STATE_INITIAL,
798 transitions,
799 G_N_ELEMENTS (transitions),
800 NULL,
801 0,
802 data,
803 (GDestroyNotify)transition_data_free);
804
805 wait = dex_state_machine_wait_for_state (state_machine, TEST_STATE_PREPARE);
806 transition = dex_state_machine_transition (state_machine, TEST_STATE_READY);
807
808 state = dex_await_enum (dex_ref (wait), &error);
809
810 g_assert_no_error (error);
811 g_assert_cmpuint (state, ==, TEST_STATE_PREPARE);
812
813 g_assert_true (dex_state_machine_interrupt (state_machine));
814
815 state = dex_await_enum (dex_ref (transition), &error);
816
817 g_assert_no_error (error);
818 g_assert_cmpuint (state, ==, TEST_STATE_READY);
819 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_READY);
820 }
821
822 static void
823 test_state_machine_interrupt_context_completed (void)
824 {
825 static const DexStateTransition transitions[] = {
826 { TEST_STATE_INITIAL, TEST_STATE_READY, transition_interrupt_not_taken },
827 };
828 g_autoptr(DexStateMachine) state_machine = NULL;
829 g_autoptr(GError) error = NULL;
830 TransitionData *data;
831 guint state;
832
833 data = transition_data_new ();
834 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
835 TEST_STATE_INITIAL,
836 transitions,
837 G_N_ELEMENTS (transitions),
838 NULL,
839 0,
840 data,
841 (GDestroyNotify)transition_data_free);
842
843 state = dex_await_enum (dex_state_machine_transition (state_machine, TEST_STATE_READY), &error);
844
845 g_assert_no_error (error);
846 g_assert_cmpuint (state, ==, TEST_STATE_READY);
847 g_assert_nonnull (data->interrupt);
848
849 g_assert_false (dex_await (dex_ref (data->interrupt), &error));
850 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
851 g_clear_error (&error);
852 g_assert_false (dex_state_machine_interrupt (state_machine));
853 }
854
855 static void
856 test_state_machine_continue_to (void)
857 {
858 static const DexStateTransition transitions[] = {
859 { TEST_STATE_INITIAL, TEST_STATE_PREPARE, transition_continue_to_ready },
860 { TEST_STATE_PREPARE, TEST_STATE_READY, transition_basic },
861 };
862 g_autoptr(DexStateMachine) state_machine = NULL;
863 g_autoptr(GError) error = NULL;
864 TransitionData *data;
865 guint state;
866
867 data = transition_data_new ();
868 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
869 TEST_STATE_INITIAL,
870 transitions,
871 G_N_ELEMENTS (transitions),
872 NULL,
873 0,
874 data,
875 (GDestroyNotify)transition_data_free);
876
877 state = dex_await_enum (dex_state_machine_transition (state_machine, TEST_STATE_PREPARE), &error);
878
879 g_assert_no_error (error);
880 g_assert_cmpuint (state, ==, TEST_STATE_READY);
881 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_READY);
882 g_assert_cmpuint (data->visited->len, ==, 4);
883 g_assert_cmpuint (g_array_index (data->visited, guint, 0), ==, TEST_STATE_INITIAL);
884 g_assert_cmpuint (g_array_index (data->visited, guint, 1), ==, TEST_STATE_PREPARE);
885 g_assert_cmpuint (g_array_index (data->visited, guint, 2), ==, TEST_STATE_PREPARE);
886 g_assert_cmpuint (g_array_index (data->visited, guint, 3), ==, TEST_STATE_READY);
887 }
888
889 static void
890 test_state_machine_continue_to_before_queued (void)
891 {
892 static const DexStateTransition transitions[] = {
893 { TEST_STATE_INITIAL, TEST_STATE_PREPARE, transition_continue_to_ready },
894 { TEST_STATE_PREPARE, TEST_STATE_READY, transition_basic },
895 };
896 g_autoptr(DexStateMachine) state_machine = NULL;
897 g_autoptr(DexFuture) first = NULL;
898 g_autoptr(DexFuture) second = NULL;
899 g_autoptr(GError) error = NULL;
900 TransitionData *data;
901 guint state;
902
903 data = transition_data_new ();
904 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
905 TEST_STATE_INITIAL,
906 transitions,
907 G_N_ELEMENTS (transitions),
908 NULL,
909 0,
910 data,
911 (GDestroyNotify)transition_data_free);
912
913 first = dex_state_machine_transition (state_machine, TEST_STATE_PREPARE);
914 second = dex_state_machine_transition (state_machine, TEST_STATE_READY);
915
916 state = dex_await_enum (dex_ref (first), &error);
917
918 g_assert_no_error (error);
919 g_assert_cmpuint (state, ==, TEST_STATE_READY);
920 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_READY);
921
922 state = dex_await_enum (dex_ref (second), &error);
923
924 g_assert_error (error, DEX_ERROR, DEX_ERROR_INVALID_TRANSITION);
925 g_assert_cmpuint (state, ==, 0);
926 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_READY);
927 g_assert_cmpuint (data->visited->len, ==, 4);
928 g_clear_error (&error);
929 }
930
931 static void
932 test_state_machine_continue_to_invalid (void)
933 {
934 static const DexStateTransition transitions[] = {
935 { TEST_STATE_INITIAL, TEST_STATE_PREPARE, transition_continue_to_failed },
936 };
937 g_autoptr(DexStateMachine) state_machine = NULL;
938 g_autoptr(GError) error = NULL;
939 TransitionData *data;
940 guint state;
941
942 data = transition_data_new ();
943 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
944 TEST_STATE_INITIAL,
945 transitions,
946 G_N_ELEMENTS (transitions),
947 NULL,
948 0,
949 data,
950 (GDestroyNotify)transition_data_free);
951
952 state = dex_await_enum (dex_state_machine_transition (state_machine, TEST_STATE_PREPARE), &error);
953
954 g_assert_error (error, DEX_ERROR, DEX_ERROR_INVALID_TRANSITION);
955 g_assert_cmpuint (state, ==, 0);
956 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_INITIAL);
957 g_assert_cmpuint (data->visited->len, ==, 2);
958 g_assert_cmpuint (g_array_index (data->visited, guint, 0), ==, TEST_STATE_INITIAL);
959 g_assert_cmpuint (g_array_index (data->visited, guint, 1), ==, TEST_STATE_PREPARE);
960 g_clear_error (&error);
961 }
962
963 static void
964 test_state_machine_continue_to_failure (void)
965 {
966 static const DexStateTransition transitions[] = {
967 { TEST_STATE_INITIAL, TEST_STATE_PREPARE, transition_continue_to_failed },
968 { TEST_STATE_PREPARE, TEST_STATE_FAILED, transition_fail },
969 };
970 g_autoptr(DexStateMachine) state_machine = NULL;
971 g_autoptr(GError) error = NULL;
972 TransitionData *data;
973 guint state;
974
975 data = transition_data_new ();
976 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
977 TEST_STATE_INITIAL,
978 transitions,
979 G_N_ELEMENTS (transitions),
980 NULL,
981 0,
982 data,
983 (GDestroyNotify)transition_data_free);
984
985 state = dex_await_enum (dex_state_machine_transition (state_machine, TEST_STATE_PREPARE), &error);
986
987 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
988 g_assert_cmpuint (state, ==, 0);
989 g_assert_cmpuint (dex_state_machine_get_state (state_machine), ==, TEST_STATE_PREPARE);
990 g_assert_cmpuint (data->visited->len, ==, 2);
991 g_assert_cmpuint (g_array_index (data->visited, guint, 0), ==, TEST_STATE_INITIAL);
992 g_assert_cmpuint (g_array_index (data->visited, guint, 1), ==, TEST_STATE_PREPARE);
993 g_clear_error (&error);
994 }
995
996 static void
997 test_state_machine_scheduler (void)
998 {
999 static const DexStateTransition transitions[] = {
1000 { TEST_STATE_INITIAL, TEST_STATE_READY, transition_scheduler },
1001 };
1002 g_autoptr(DexScheduler) scheduler = NULL;
1003 g_autoptr(DexStateMachine) state_machine = NULL;
1004 g_autoptr(GError) error = NULL;
1005 SchedulerData data = {0};
1006 guint state;
1007
1008 scheduler = dex_thread_pool_scheduler_new ();
1009 data.scheduler = scheduler;
1010 state_machine = dex_state_machine_new (TEST_TYPE_STATE,
1011 TEST_STATE_INITIAL,
1012 transitions,
1013 G_N_ELEMENTS (transitions),
1014 scheduler,
1015 dex_get_min_stack_size (),
1016 &data,
1017 NULL);
1018
1019 state = dex_await_enum (dex_state_machine_transition (state_machine, TEST_STATE_READY), &error);
1020
1021 g_assert_no_error (error);
1022 g_assert_cmpuint (state, ==, TEST_STATE_READY);
1023 g_assert_true (data.used_scheduler);
1024 }
1025
1026 static void
1027 test_state_machine_duplicate (void)
1028 {
1029 static const DexStateTransition transitions[] = {
1030 { TEST_STATE_INITIAL, TEST_STATE_READY, transition_basic },
1031 { TEST_STATE_INITIAL, TEST_STATE_READY, transition_basic },
1032 };
1033
1034 g_test_expect_message ("Dex",
1035 G_LOG_LEVEL_CRITICAL,
1036 "*dex_state_machine_validate_transitions*");
1037 g_assert_null (dex_state_machine_new (TEST_TYPE_STATE,
1038 TEST_STATE_INITIAL,
1039 transitions,
1040 G_N_ELEMENTS (transitions),
1041 NULL,
1042 0,
1043 NULL,
1044 NULL));
1045 g_test_assert_expected_messages ();
1046 }
1047
1048 int
1049 main (int argc,
1050 char *argv[])
1051 {
1052 dex_init ();
1053 g_test_init (&argc, &argv, NULL);
1054
1055 _g_test_add_func ("/Dex/StateMachine/basic", test_state_machine_basic);
1056 _g_test_add_func ("/Dex/StateMachine/landing", test_state_machine_landing);
1057 _g_test_add_func ("/Dex/StateMachine/reverse", test_state_machine_reverse);
1058 _g_test_add_func ("/Dex/StateMachine/landing-reverse", test_state_machine_landing_reverse);
1059 _g_test_add_func ("/Dex/StateMachine/invalid", test_state_machine_invalid);
1060 _g_test_add_func ("/Dex/StateMachine/failure", test_state_machine_failure);
1061 _g_test_add_func ("/Dex/StateMachine/requested-state", test_state_machine_requested_state);
1062 _g_test_add_func ("/Dex/StateMachine/wait-for-state-immediate",
1063 test_state_machine_wait_for_state_immediate);
1064 _g_test_add_func ("/Dex/StateMachine/wait-for-state-transition",
1065 test_state_machine_wait_for_state_transition);
1066 _g_test_add_func ("/Dex/StateMachine/wait-for-state-intermediate",
1067 test_state_machine_wait_for_state_intermediate);
1068 _g_test_add_func ("/Dex/StateMachine/wait-for-state-invalid",
1069 test_state_machine_wait_for_state_invalid);
1070 _g_test_add_func ("/Dex/StateMachine/wait-for-state-finalized",
1071 test_state_machine_wait_for_state_finalized);
1072 _g_test_add_func ("/Dex/StateMachine/interrupt-none", test_state_machine_interrupt_none);
1073 _g_test_add_func ("/Dex/StateMachine/interrupt", test_state_machine_interrupt);
1074 _g_test_add_func ("/Dex/StateMachine/interrupt-latched",
1075 test_state_machine_interrupt_latched);
1076 _g_test_add_func ("/Dex/StateMachine/interrupt-context-completed",
1077 test_state_machine_interrupt_context_completed);
1078 _g_test_add_func ("/Dex/StateMachine/continue-to", test_state_machine_continue_to);
1079 _g_test_add_func ("/Dex/StateMachine/continue-to-before-queued",
1080 test_state_machine_continue_to_before_queued);
1081 _g_test_add_func ("/Dex/StateMachine/continue-to-invalid",
1082 test_state_machine_continue_to_invalid);
1083 _g_test_add_func ("/Dex/StateMachine/continue-to-failure",
1084 test_state_machine_continue_to_failure);
1085 _g_test_add_func ("/Dex/StateMachine/scheduler", test_state_machine_scheduler);
1086 _g_test_add_func ("/Dex/StateMachine/duplicate", test_state_machine_duplicate);
1087
1088 return g_test_run ();
1089 }