No issues found
1 /*
2 * Copyright (C) 2011, Nokia <ivan.frade@nokia.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20 #include <glib.h>
21
22 /* NOTE: We're not including tracker-miner.h here because this is private. */
23 #include <libtracker-miner/tracker-task-pool.h>
24
25 static void
26 test_task_pool_limit_set (void)
27 {
28 TrackerTaskPool *pool;
29
30 pool = tracker_task_pool_new (5);
31 g_assert_cmpint (tracker_task_pool_get_limit(pool), ==, 5);
32
33 tracker_task_pool_set_limit (pool, 3);
34 g_assert_cmpint (tracker_task_pool_get_limit(pool), ==, 3);
35
36 g_assert (tracker_task_pool_limit_reached (pool) == FALSE);
37 g_object_unref (pool);
38 }
39
40 static void
41 add_task (TrackerTaskPool *pool,
42 const gchar *filename,
43 gint expected_size,
44 gboolean hit_limit)
45 {
46 TrackerTask *task;
47
48 task = tracker_task_new (g_file_new_for_path (filename), NULL, NULL);
49 tracker_task_pool_add (pool, task);
50
51 g_assert_cmpint (tracker_task_pool_get_size (pool), ==, expected_size);
52 g_assert (tracker_task_pool_limit_reached (pool) == hit_limit);
53
54 g_object_unref (tracker_task_get_file (task));
55 tracker_task_unref (task);
56 }
57
58 static void
59 remove_task (TrackerTaskPool *pool,
60 const gchar *filename,
61 gint expected_size,
62 gboolean hit_limit)
63 {
64 TrackerTask *task;
65
66 task = tracker_task_new (g_file_new_for_path (filename), NULL, NULL);
67 tracker_task_pool_remove (pool, task);
68
69 g_assert_cmpint (tracker_task_pool_get_size (pool), ==, expected_size);
70 g_assert (hit_limit == tracker_task_pool_limit_reached (pool));
71
72 g_object_unref (tracker_task_get_file (task));
73 tracker_task_unref (task);
74 }
75
76 static void
77 test_task_pool_add_remove (void)
78 {
79 TrackerTaskPool *pool;
80
81 pool = tracker_task_pool_new (3);
82
83 /* Additions ... */
84 add_task (pool, "/dev/null", 1, FALSE);
85 add_task (pool, "/dev/null2", 2, FALSE);
86 add_task (pool, "/dev/null3", 3, TRUE);
87
88 /* We can go over the limit */
89 add_task (pool, "/dev/null4", 4, TRUE);
90
91 /* Remove something that doesn't exist */
92 remove_task (pool, "/dev/null/imNotInThePool", 4, TRUE);
93
94 /* Removals ... (in different order)*/
95 remove_task (pool, "/dev/null4", 3, TRUE);
96 remove_task (pool, "/dev/null2", 2, FALSE);
97 remove_task (pool, "/dev/null3", 1, FALSE);
98 remove_task (pool, "/dev/null", 0, FALSE);
99
100 /* Remove in empty queue */
101 remove_task (pool, "/dev/null/random", 0, FALSE);
102
103 g_object_unref (pool);
104 }
105
106 static void
107 test_task_pool_find (void)
108 {
109 TrackerTaskPool *pool;
110 TrackerTask *task;
111 GFile *goal;
112
113 pool = tracker_task_pool_new (3);
114
115 add_task (pool, "/dev/null", 1, FALSE);
116 add_task (pool, "/dev/null2", 2, FALSE);
117 add_task (pool, "/dev/null3", 3, TRUE);
118
119 /* Search first, last, in the middle... */
120 goal = g_file_new_for_path ("/dev/null2");
121 task = tracker_task_pool_find (pool, goal);
122 g_assert (task);
123 g_object_unref (goal);
124
125 goal = g_file_new_for_path ("/dev/null");
126 task = tracker_task_pool_find (pool, goal);
127 g_assert (task);
128 g_object_unref (goal);
129
130 goal = g_file_new_for_path ("/dev/null3");
131 task = tracker_task_pool_find (pool, goal);
132 g_assert (task);
133 g_object_unref (goal);
134
135 goal = g_file_new_for_path ("/dev/thisDoesntExists");
136 task = tracker_task_pool_find (pool, goal);
137 g_assert (task == NULL);
138 g_object_unref (goal);
139
140 g_object_unref (pool);
141 }
142
143 static void
144 count_elements_cb (gpointer data,
145 gpointer user_data)
146 {
147 gint *counter = (gint*)user_data;
148 (*counter) += 1;
149 }
150
151 static void
152 test_task_pool_foreach (void)
153 {
154 TrackerTaskPool *pool;
155 int counter = 0;
156
157 pool = tracker_task_pool_new (3);
158
159 add_task (pool, "/dev/null", 1, FALSE);
160 add_task (pool, "/dev/null2", 2, FALSE);
161 add_task (pool, "/dev/null3", 3, TRUE);
162
163 tracker_task_pool_foreach (pool, count_elements_cb, &counter);
164
165 g_assert_cmpint (counter, ==, 3);
166 }
167
168 gint
169 main (gint argc, gchar **argv)
170 {
171 g_test_init (&argc, &argv, NULL);
172
173 g_test_add_func ("/libtracker-miner/tracker-task-pool/limit_set",
174 test_task_pool_limit_set);
175
176 g_test_add_func ("/libtracker-miner/tracker-task-pool/add_remove",
177 test_task_pool_add_remove);
178
179 g_test_add_func ("/libtracker-miner/tracker-task-pool/find",
180 test_task_pool_find);
181
182 g_test_add_func ("/libtracker-miner/tracker-task-pool/foreach",
183 test_task_pool_foreach);
184
185 return g_test_run ();
186 }