summaryrefslogtreecommitdiff
path: root/tests/libtracker-miner/tracker-task-pool-test.c
blob: 0ece7c39f3a72c84b942efebaa68a0af210c2074 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (C) 2011, Nokia <ivan.frade@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 */
#include <glib.h>

/* NOTE: We're not including tracker-miner.h here because this is private. */
#include <libtracker-miner/tracker-task-pool.h>

static void
test_task_pool_limit_set (void)
{
        TrackerTaskPool *pool;

        pool = tracker_task_pool_new (5);
        g_assert_cmpint (tracker_task_pool_get_limit(pool), ==, 5);

        tracker_task_pool_set_limit (pool, 3);
        g_assert_cmpint (tracker_task_pool_get_limit(pool), ==, 3);

        g_assert (tracker_task_pool_limit_reached (pool) == FALSE);
        g_object_unref (pool);
}

static void
add_task (TrackerTaskPool *pool,
          const gchar     *filename,
          gint             expected_size,
          gboolean         hit_limit)
{
        TrackerTask *task;

        task = tracker_task_new (g_file_new_for_path (filename), NULL, NULL);
        tracker_task_pool_add (pool, task);

        g_assert_cmpint (tracker_task_pool_get_size (pool), ==, expected_size);
        g_assert (tracker_task_pool_limit_reached (pool) == hit_limit);

        g_object_unref (tracker_task_get_file (task));
        tracker_task_unref (task);
}

static void
remove_task (TrackerTaskPool *pool,
             const gchar     *filename,
             gint             expected_size,
             gboolean         hit_limit)
{
        TrackerTask *task;

        task = tracker_task_new (g_file_new_for_path (filename), NULL, NULL);
        tracker_task_pool_remove (pool, task);

        g_assert_cmpint (tracker_task_pool_get_size (pool), ==, expected_size);
        g_assert (hit_limit == tracker_task_pool_limit_reached (pool));

        g_object_unref (tracker_task_get_file (task));
        tracker_task_unref (task);
}

static void
test_task_pool_add_remove (void)
{
        TrackerTaskPool *pool;

        pool = tracker_task_pool_new (3);

        /* Additions ... */
        add_task (pool, "/dev/null", 1, FALSE);
        add_task (pool, "/dev/null2", 2, FALSE);
        add_task (pool, "/dev/null3", 3, TRUE);

        /* We can go over the limit */
        add_task (pool, "/dev/null4", 4, TRUE);

        /* Remove something that doesn't exist */
        remove_task (pool, "/dev/null/imNotInThePool", 4, TRUE);

        /* Removals ... (in different order)*/
        remove_task (pool, "/dev/null4", 3, TRUE);
        remove_task (pool, "/dev/null2", 2, FALSE);
        remove_task (pool, "/dev/null3", 1, FALSE);
        remove_task (pool, "/dev/null", 0, FALSE);

        /* Remove in empty queue */
        remove_task (pool, "/dev/null/random", 0, FALSE);

        g_object_unref (pool);
}

static void
test_task_pool_find (void)
{
        TrackerTaskPool *pool;
        TrackerTask *task;
        GFile *goal;

        pool = tracker_task_pool_new (3);

        add_task (pool, "/dev/null", 1, FALSE);
        add_task (pool, "/dev/null2", 2, FALSE);
        add_task (pool, "/dev/null3", 3, TRUE);

        /* Search first, last, in the middle... */
        goal = g_file_new_for_path ("/dev/null2");
        task = tracker_task_pool_find (pool, goal);
        g_assert (task);
        g_object_unref (goal);

        goal = g_file_new_for_path ("/dev/null");
        task = tracker_task_pool_find (pool, goal);
        g_assert (task);
        g_object_unref (goal);

        goal = g_file_new_for_path ("/dev/null3");
        task = tracker_task_pool_find (pool, goal);
        g_assert (task);
        g_object_unref (goal);

        goal = g_file_new_for_path ("/dev/thisDoesntExists");
        task = tracker_task_pool_find (pool, goal);
        g_assert (task == NULL);
        g_object_unref (goal);

        g_object_unref (pool);
}

static void
count_elements_cb (gpointer data,
                   gpointer user_data)
{
        gint *counter = (gint*)user_data;
        (*counter) += 1;
}

static void
test_task_pool_foreach (void)
{
        TrackerTaskPool *pool;
        int counter = 0;

        pool = tracker_task_pool_new (3);

        add_task (pool, "/dev/null", 1, FALSE);
        add_task (pool, "/dev/null2", 2, FALSE);
        add_task (pool, "/dev/null3", 3, TRUE);

        tracker_task_pool_foreach (pool, count_elements_cb, &counter);

        g_assert_cmpint (counter, ==, 3);
}

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

        g_test_add_func ("/libtracker-miner/tracker-task-pool/limit_set",
                         test_task_pool_limit_set);

        g_test_add_func ("/libtracker-miner/tracker-task-pool/add_remove",
                         test_task_pool_add_remove);

        g_test_add_func ("/libtracker-miner/tracker-task-pool/find",
                         test_task_pool_find);
 
        g_test_add_func ("/libtracker-miner/tracker-task-pool/foreach",
                         test_task_pool_foreach);

        return g_test_run ();
}