Statistics
| Revision:

root / trunk / tests / test_download_queue_one_server.c @ 1847

History | View | Annotate | Download (7.4 KB)

1
/* 
2
    Copyright (C) 2005-2011  Erik van Pienbroek
3

                
4
    This program is free software; you can redistribute it and/or modify
5
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation; either version 2 of the License, or
7
    (at your option) any later version.
8

                
9
    This program 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
12
    GNU General Public License for more details.
13

                
14
    You should have received a copy of the GNU General Public License
15
    along with this program; if not, write to the Free Software
16
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
*/
18

                
19
#include 
20
#include 
21
#include 
22

                
23
#include "common.h"
24
#include "test_download_queue.h"
25

                
26
#include "nntpgrab_utils.h"
27
#include "download_queue.h"
28

                
29
void
30
test_download_queue_setup_one_server_before(void *data, gconstpointer user_data)
31
{
32
    char *errmsg = NULL;
33
    NGConfigServer server;
34

                
35
    g_assert(configuration_get_avail_servers(config) == NULL);
36

                
37
    memset(&server, 0, sizeof(server));
38
    strcpy(server.servername, "server1");
39
    strcpy(server.hostname, "host1");
40
    server.port = 119;
41
    server.enabled = TRUE;
42
    server.max_threads = 3;
43
    server.priority = SERVER_PRIORITY_HIGH;
44

                
45
    if (!configuration_add_server(config, server, &errmsg)) {
46
        FAIL_TEST("Unable to add server: %s\n", errmsg);
47
        g_free(errmsg);
48
        return;
49
    }
50

                
51
    fill_queue_with_dummy_data();
52
}
53

                
54
void
55
test_download_queue_setup_one_server_after(void *data, gconstpointer user_data)
56
{
57
    char *errmsg = NULL;
58
    NGConfigServer server;
59

                
60
    g_assert(configuration_get_avail_servers(config) == NULL);
61

                
62
    memset(&server, 0, sizeof(server));
63
    strcpy(server.servername, "server1");
64
    strcpy(server.hostname, "host1");
65
    server.port = 119;
66
    server.enabled = TRUE;
67
    server.max_threads = 1;
68
    server.priority = SERVER_PRIORITY_HIGH;
69

                
70
    fill_queue_with_dummy_data();
71

                
72
    if (!configuration_add_server(config, server, &errmsg)) {
73
        FAIL_TEST("Unable to add server: %s\n", errmsg);
74
        g_free(errmsg);
75
        return;
76
    }
77
}
78

                
79
void
80
test_download_queue_teardown_one_server(void *data, gconstpointer user_data)
81
{
82
    char *errmsg = NULL;
83

                
84
    g_assert(configuration_get_avail_servers(config) != NULL);
85

                
86
    drop_dummy_data_from_queue();
87

                
88
    if (!configuration_del_server(config, "server1", &errmsg)) {
89
        FAIL_TEST("Unable to remove server: %s\n", errmsg);
90
        g_free(errmsg);
91
        return;
92
    }
93
}
94
static void
95
test_download_queue_one_server_generic(gboolean all_available)
96
{
97
    /* There should be 10 files with 4 parts each in the download queue now */
98
    int i, j;
99

                
100
    NNTPCollection *prev_collection = NULL;
101
    NNTPFile *prev_file = NULL;
102

                
103
    NNTPCollection *collection = NULL;
104
    NNTPFile *file = NULL;
105
    NNTPPart *part = NULL;
106
    gboolean nothing_to_download_or_active = FALSE;
107

                
108
    GList *list, *files = NULL;
109

                
110
    for (i = 1; i <= 10; i++) {
111
        prev_file = NULL;
112

                
113
        for (j = 1; j <= 4; j++) {
114
            /* There is 1 server configured at the moment, so a request for the next part for server_id 0 
115
             * should return TRUE and nothing_do_download should_or_active be FALSE */
116
            if (!download_queue_get_next_part_to_download(&collection, &file, &part, 0, ¬hing_to_download_or_active)) {
117
                g_test_message("nothing_to_download = %i", nothing_to_download_or_active);
118
                FAIL_TEST("download_queue_get_next_part_to_download returned FALSE while it isn't expected");
119
                return;
120
            } else {
121
                char *expected_subject = g_strdup_printf("file %i", i);
122

                
123
                g_assert_cmpint(nothing_to_download_or_active, ==, FALSE);
124
                g_assert(collection != NULL);
125
                g_assert(file != NULL);
126
                g_assert(part != NULL);
127

                
128
                g_assert_cmpint(part->part_num, ==, j);
129
                g_assert_cmpstr(file->subject, ==, expected_subject);
130

                
131
                g_free(expected_subject);
132

                
133
                if (prev_collection == NULL) {
134
                    prev_collection = collection;
135
                } else {
136
                    g_assert(prev_collection == collection);
137
                }
138

                
139
                if (prev_file == NULL) {
140
                    prev_file = file;
141
                    files = g_list_append(files, file);
142
                } else {
143
                    g_assert(prev_file == file);
144
                }
145
            }
146

                
147
            /* Update the status of the part */
148
            if (!all_available && j % 2 == 0) {
149
                /* Indicate the every second part isn't available on the server and it shouldn't be retried again */
150
                download_queue_update_part_status("server1", 0, collection, file, part, 0, FALSE, FALSE, TRUE);
151
            } else {
152
                /* Indicate that the file was downloaded succesfully */
153
                download_queue_update_part_status("server1", 0, collection, file, part, 0, TRUE, FALSE, FALSE);
154
            }
155

                
156
            collection = NULL;
157
            file = NULL;
158
            part = NULL;
159
        }
160
    }
161

                
162
    /* We processed all files now. If we try to request another part it should indicate that there's nothing to download anymore */
163
    if (!download_queue_get_next_part_to_download(&collection, &file, &part, 0, ¬hing_to_download_or_active)) {
164
        /* Nothing is returned which is good */
165
        /* The flag nothing_to_download_or_active should be FALSE as all files are downloaded, but not decoded yet */
166
        g_assert_cmpint(nothing_to_download_or_active, ==, FALSE);
167
        g_assert(collection == NULL);
168
        g_assert(file == NULL);
169
        g_assert(part == NULL);
170
    } else {
171
        FAIL_TEST("download_queue_get_next_part_to_download returned TRUE while it isn't expected");
172
        return;
173
    }
174

                
175
    /* Mark all files as finished (downloaded+decoded) */
176
    list = files;
177
    while (list) {
178
        file = (NNTPFile*) list->data;
179

                
180
        if (!all_available && g_list_index(files, file) % 2 == 0) {
181
            /* Indicate that not all parts were available */
182
            download_queue_mark_file_as_decoded_and_unref(prev_collection, file, "somefilename", TASK_STATE_FINISHED_INCOMPLETE);
183
        } else {
184
            /* Indicate that all parts were decoded succesfully */
185
            download_queue_mark_file_as_decoded_and_unref(prev_collection, file, "somefilename", TASK_STATE_FINISHED_COMPLETE);
186
        }
187

                
188
        list = g_list_next(list);
189
    }
190
    g_list_free(files);
191

                
192
    /* Verify that nothing_to_download_or_active is TRUE now */
193
    if (!download_queue_get_next_part_to_download(&collection, &file, &part, 0, ¬hing_to_download_or_active)) {
194
        /* Nothing is returned which is good */
195
        g_assert_cmpint(nothing_to_download_or_active, ==, TRUE);
196
        g_assert(collection == NULL);
197
        g_assert(file == NULL);
198
        g_assert(part == NULL);
199
    } else {
200
        FAIL_TEST("download_queue_get_next_part_to_download returned TRUE while it isn't expected");
201
        return;
202
    }
203
}
204

                
205
void
206
test_download_queue_one_server_all_available(void *data, gconstpointer user_data)
207
{
208
    /* Assume for this testcase that all parts are available on the server */
209
    test_download_queue_one_server_generic(TRUE);
210
}
211

                
212
void
213
test_download_queue_one_server_some_available(void *data, gconstpointer user_data)
214
{
215
    /* Assume for this testcase that some parts are available on the server */
216
    test_download_queue_one_server_generic(FALSE);
217
}