File: test-timeout.c

package info (click to toggle)
gmobile 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 632 kB
  • sloc: ansic: 2,079; sh: 36; xml: 31; makefile: 10; javascript: 6
file content (76 lines) | stat: -rw-r--r-- 1,478 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2022 Purism SPC
 *
 * SPDX-License-Identifier: GPL-3-or-later
 *
 * Author: Guido Günther <agx@sigxcpu.org>
 */

#define GMOBILE_USE_UNSTABLE_API
#include "gmobile.h"


static void
on_timeout (gpointer data)
{
  g_main_loop_quit (data);
}


static void
test_gm_timeout_simple (void)
{
  g_autoptr (GMainLoop) loop = NULL;
  int seconds = 1;

  loop = g_main_loop_new (NULL, FALSE);
  gm_timeout_add_seconds_once (seconds, on_timeout, loop);
  g_main_loop_run (loop);
}


static void
on_timeout2 (gpointer data)
{
  g_assert_not_reached ();
}


static gboolean
remove_timeout (gpointer data)
{
  g_source_remove (GPOINTER_TO_UINT (data));

  return G_SOURCE_REMOVE;
}


static void
test_gm_timeout_remove (void)
{
  g_autoptr (GMainLoop) loop = NULL;
  guint id;

  loop = g_main_loop_new (NULL, FALSE);
  id = gm_timeout_add_seconds_once (1, on_timeout2, NULL);
  g_assert_nonnull (g_main_context_find_source_by_id (NULL, id));
  g_idle_add (remove_timeout, GUINT_TO_POINTER (id));
  /* End the main loop, id must not have fired yet */
  g_timeout_add_seconds (2, (GSourceFunc)on_timeout, loop);
  g_main_loop_run (loop);
  /* source got removed */
  g_assert_null (g_main_context_find_source_by_id (NULL, id));
}


gint
main (gint argc,
      gchar *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/Gm/timeout/simple", test_gm_timeout_simple);
  g_test_add_func ("/Gm/timeout/remove", test_gm_timeout_remove);

  return g_test_run ();
}