Branch data Line data Source code
1 : : #undef G_DISABLE_ASSERT
2 : :
3 : : #include <shumate/shumate.h>
4 : :
5 : : #define TEST_ETAG "0123456789ABCDEFG"
6 : : #define TEST_DATA "The quick brown fox \0 jumps over the lazy dog"
7 : :
8 : :
9 : : static void
10 : 2 : on_tile_stored (GObject *object, GAsyncResult *res, gpointer user_data)
11 : : {
12 : 4 : g_autoptr(GError) error = NULL;
13 : 2 : GMainLoop *loop = user_data;
14 : :
15 : 2 : shumate_file_cache_store_tile_finish ((ShumateFileCache *) object, res, &error);
16 [ - + ]: 2 : g_assert_no_error (error);
17 : :
18 [ - + ]: 2 : g_main_loop_quit (loop);
19 : 2 : }
20 : :
21 : : static void
22 : 2 : on_tile_retrieved (GObject *object, GAsyncResult *res, gpointer user_data)
23 : : {
24 : 2 : GMainLoop *loop = user_data;
25 : 4 : g_autoptr(GError) error = NULL;
26 [ - + ]: 2 : g_autoptr(GBytes) bytes = NULL;
27 [ + - ]: 2 : g_autofree char *etag = NULL;
28 : 4 : g_autoptr(GBytes) expected_bytes = g_bytes_new_static (TEST_DATA, sizeof TEST_DATA);
29 [ + - ]: 2 : g_autoptr(GDateTime) modtime = NULL;
30 [ + - ]: 4 : g_autoptr(GDateTime) now = g_date_time_new_now_utc ();
31 : :
32 : 2 : bytes = shumate_file_cache_get_tile_finish ((ShumateFileCache *) object, &etag, &modtime, res, &error);
33 [ - + ]: 2 : g_assert_no_error (error);
34 : :
35 [ - + ]: 2 : g_assert_true (g_bytes_equal (bytes, expected_bytes));
36 [ - + ]: 2 : g_assert_cmpstr (etag, ==, TEST_ETAG);
37 : : /* the modification time should be very, very recent */
38 [ - + ]: 2 : g_assert_true (g_date_time_difference (now, modtime) < G_TIME_SPAN_SECOND * 10);
39 : :
40 [ + - ]: 2 : g_main_loop_quit (loop);
41 : 2 : }
42 : :
43 : : /* Test that storing and retrieving a file from the cache works */
44 : : static void
45 : 2 : test_file_cache_store_retrieve ()
46 : : {
47 : 2 : g_autoptr(ShumateFileCache) cache = shumate_file_cache_new_full (100000000, "test", NULL);
48 [ + - ]: 4 : g_autoptr(GBytes) bytes = g_bytes_new_static (TEST_DATA, sizeof TEST_DATA);
49 [ + - + - ]: 4 : g_autoptr(GMainLoop) loop = NULL;
50 : :
51 : : /* Store the tile */
52 : 2 : loop = g_main_loop_new (NULL, TRUE);
53 : 2 : shumate_file_cache_store_tile_async (cache, 0, 0, 256, bytes, TEST_ETAG, NULL, on_tile_stored, loop);
54 : 2 : g_main_loop_run (loop);
55 : :
56 : : /* Now retrieve it */
57 : 2 : g_main_loop_unref (loop);
58 : 2 : loop = g_main_loop_new (NULL, TRUE);
59 : 2 : shumate_file_cache_get_tile_async (cache, 0, 0, 256, NULL, on_tile_retrieved, loop);
60 [ + - ]: 2 : g_main_loop_run (loop);
61 : 2 : }
62 : :
63 : :
64 : : static void
65 : 2 : on_no_tile_retrieved (GObject *object, GAsyncResult *res, gpointer user_data)
66 : : {
67 : 2 : GMainLoop *loop = user_data;
68 : 4 : g_autoptr(GError) error = NULL;
69 [ - + ]: 2 : g_autoptr(GBytes) bytes = NULL;
70 [ - + ]: 2 : g_autofree char *etag = NULL;
71 : 2 : g_autoptr(GDateTime) modtime = NULL;
72 : :
73 : : /* Make sure retrieving the tile returns NULL */
74 : 2 : bytes = shumate_file_cache_get_tile_finish ((ShumateFileCache *) object, &etag, &modtime, res, &error);
75 [ - + ]: 2 : g_assert_no_error (error);
76 [ - + ]: 2 : g_assert_null (bytes);
77 [ - + ]: 2 : g_assert_null (etag);
78 [ - + ]: 2 : g_assert_null (modtime);
79 : :
80 [ - + ]: 2 : g_main_loop_quit (loop);
81 : 2 : }
82 : :
83 : : /* Test that cache misses work properly */
84 : : static void
85 : 2 : test_file_cache_miss ()
86 : : {
87 : 2 : g_autoptr(ShumateFileCache) cache = shumate_file_cache_new_full (100000000, "test", NULL);
88 [ + - ]: 2 : g_autoptr(GMainLoop) loop = NULL;
89 : :
90 : 2 : loop = g_main_loop_new (NULL, TRUE);
91 : 2 : shumate_file_cache_get_tile_async (cache, 0, 0, 256, NULL, on_no_tile_retrieved, loop);
92 [ + - ]: 2 : g_main_loop_run (loop);
93 : 2 : }
94 : :
95 : :
96 : : int
97 : 2 : main (int argc, char *argv[])
98 : : {
99 : 2 : g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
100 : :
101 : 2 : g_test_add_func ("/file-cache/store-retrieve", test_file_cache_store_retrieve);
102 : 2 : g_test_add_func ("/file-cache/miss", test_file_cache_miss);
103 : :
104 : 2 : return g_test_run ();
105 : : }
|