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
|
#include <dazzle.h>
static GMainLoop *main_loop;
static DzlTaskCache *cache;
static GObject *foo;
static void
populate_callback (DzlTaskCache *self,
gconstpointer key,
GTask *task,
gpointer user_data)
{
foo = g_object_new (G_TYPE_OBJECT, NULL);
g_object_add_weak_pointer (G_OBJECT (foo), (gpointer *)&foo);
g_task_return_pointer (task, foo, g_object_unref);
}
static void
get_foo_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
GError *error = NULL;
GObject *ret;
ret = dzl_task_cache_get_finish (cache, result, &error);
g_assert_no_error (error);
g_assert_nonnull (ret);
g_assert_true (ret == foo);
g_assert_true (dzl_task_cache_evict (cache, "foo"));
g_object_unref (ret);
g_main_loop_quit (main_loop);
}
static void
test_task_cache (void)
{
main_loop = g_main_loop_new (NULL, FALSE);
cache = dzl_task_cache_new (g_str_hash,
g_str_equal,
(GBoxedCopyFunc)g_strdup,
(GBoxedFreeFunc)g_free,
g_object_ref,
g_object_unref,
100 /* msec */,
populate_callback, NULL, NULL);
g_assert_null (dzl_task_cache_peek (cache, "foo"));
g_assert_false (dzl_task_cache_evict (cache, "foo"));
dzl_task_cache_get_async (cache, "foo", TRUE, NULL, get_foo_cb, NULL);
g_main_loop_run (main_loop);
g_main_loop_unref (main_loop);
g_assert_null (foo);
}
static void
populate_callback_raw_value (DzlTaskCache *self,
gconstpointer key,
GTask *task,
gpointer user_data)
{
g_task_return_pointer (task, GINT_TO_POINTER ((gint) TRUE), NULL);
}
static void
get_foo_raw_value_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
GError *error = NULL;
gboolean value;
gpointer ret;
ret = dzl_task_cache_get_finish (cache, result, &error);
g_assert_no_error (error);
g_assert_nonnull (ret);
value = (gboolean) GPOINTER_TO_INT (ret);
g_assert_true (value);
g_assert_true (dzl_task_cache_evict (cache, "foo"));
g_main_loop_quit (main_loop);
}
static void
test_task_cache_raw_value (void)
{
main_loop = g_main_loop_new (NULL, FALSE);
cache = dzl_task_cache_new (g_str_hash,
g_str_equal,
(GBoxedCopyFunc)g_strdup,
(GBoxedFreeFunc)g_free,
NULL,
NULL,
100 /* msec */,
populate_callback_raw_value, NULL, NULL);
g_assert_null (dzl_task_cache_peek (cache, "foo"));
g_assert_false (dzl_task_cache_evict (cache, "foo"));
dzl_task_cache_get_async (cache, "foo", TRUE, NULL, get_foo_raw_value_cb, NULL);
g_main_loop_run (main_loop);
g_main_loop_unref (main_loop);
}
gint
main (gint argc,
gchar *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/Dazzle/TaskCache/basic", test_task_cache);
g_test_add_func ("/Dazzle/TaskCache/raw-value", test_task_cache_raw_value);
return g_test_run ();
}
|