diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-02-26 01:04:30 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-02-26 01:04:30 +0100 |
commit | 9f12a515db584f4b706841c21ce186ec608540c5 (patch) | |
tree | fa40fd8a5e7769708a488979526571334ba39a68 /src/dht | |
parent | dd72b19bd6f8b85a7509751e8f1ff47007e3c8b1 (diff) | |
download | gnunet-9f12a515db584f4b706841c21ce186ec608540c5.tar.gz gnunet-9f12a515db584f4b706841c21ce186ec608540c5.zip |
fix ugly parts of test
Diffstat (limited to 'src/dht')
-rw-r--r-- | src/dht/test_dht_api.c | 180 |
1 files changed, 73 insertions, 107 deletions
diff --git a/src/dht/test_dht_api.c b/src/dht/test_dht_api.c index 99f17699c..8f4e0ed31 100644 --- a/src/dht/test_dht_api.c +++ b/src/dht/test_dht_api.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2009, 2015 GNUnet e.V. | 3 | Copyright (C) 2009, 2015, 2017 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published | 6 | it under the terms of the GNU General Public License as published |
@@ -20,6 +20,7 @@ | |||
20 | /** | 20 | /** |
21 | * @file dht/test_dht_api.c | 21 | * @file dht/test_dht_api.c |
22 | * @brief base test case for dht api | 22 | * @brief base test case for dht api |
23 | * @author Christian Grothoff | ||
23 | * | 24 | * |
24 | * This test case tests DHT api to DUMMY DHT service communication. | 25 | * This test case tests DHT api to DUMMY DHT service communication. |
25 | */ | 26 | */ |
@@ -33,116 +34,70 @@ | |||
33 | /** | 34 | /** |
34 | * How long until we really give up on a particular testcase portion? | 35 | * How long until we really give up on a particular testcase portion? |
35 | */ | 36 | */ |
36 | #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600) | 37 | #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) |
37 | |||
38 | /** | ||
39 | * How long until we give up on any particular operation (and retry)? | ||
40 | */ | ||
41 | #define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3) | ||
42 | |||
43 | #define MTYPE 12345 | ||
44 | |||
45 | |||
46 | struct RetryContext | ||
47 | { | ||
48 | /** | ||
49 | * When to really abort the operation. | ||
50 | */ | ||
51 | struct GNUNET_TIME_Absolute real_timeout; | ||
52 | |||
53 | /** | ||
54 | * What timeout to set for the current attempt (increases) | ||
55 | */ | ||
56 | struct GNUNET_TIME_Relative next_timeout; | ||
57 | |||
58 | /** | ||
59 | * The task identifier of the retry task, so it can be cancelled. | ||
60 | */ | ||
61 | struct GNUNET_SCHEDULER_Task * retry_task; | ||
62 | |||
63 | }; | ||
64 | |||
65 | 38 | ||
66 | static struct GNUNET_DHT_Handle *dht_handle; | 39 | static struct GNUNET_DHT_Handle *dht_handle; |
67 | 40 | ||
68 | static struct GNUNET_DHT_GetHandle *get_handle; | 41 | static struct GNUNET_DHT_GetHandle *get_handle; |
69 | 42 | ||
70 | struct RetryContext retry_context; | 43 | static struct GNUNET_DHT_PutHandle *put_handle; |
71 | 44 | ||
72 | static int ok = 1; | 45 | static int ok = 1; |
73 | 46 | ||
74 | static struct GNUNET_SCHEDULER_Task * die_task; | 47 | static struct GNUNET_SCHEDULER_Task *die_task; |
75 | |||
76 | |||
77 | #if VERBOSE | ||
78 | #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0) | ||
79 | #else | ||
80 | #define OKPP do { ok++; } while (0) | ||
81 | #endif | ||
82 | 48 | ||
83 | 49 | ||
84 | static void | 50 | static void |
85 | end (void *cls) | 51 | do_shutdown (void *cls) |
86 | { | 52 | { |
87 | GNUNET_SCHEDULER_cancel (die_task); | 53 | if (NULL != die_task) |
88 | die_task = NULL; | 54 | { |
89 | GNUNET_DHT_disconnect (dht_handle); | 55 | GNUNET_SCHEDULER_cancel (die_task); |
90 | dht_handle = NULL; | 56 | die_task = NULL; |
91 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 57 | } |
92 | "DHT disconnected, returning success!\n"); | 58 | if (NULL != put_handle) |
93 | ok = 0; | 59 | { |
94 | } | 60 | GNUNET_DHT_put_cancel (put_handle); |
95 | 61 | put_handle = NULL; | |
96 | 62 | } | |
97 | static void | 63 | if (NULL != get_handle) |
98 | end_badly () | ||
99 | { | ||
100 | /* do work here */ | ||
101 | FPRINTF (stderr, "%s", "Ending on an unhappy note.\n"); | ||
102 | if (get_handle != NULL) | ||
103 | { | 64 | { |
104 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping get request!\n"); | ||
105 | GNUNET_DHT_get_stop (get_handle); | 65 | GNUNET_DHT_get_stop (get_handle); |
66 | get_handle = NULL; | ||
106 | } | 67 | } |
107 | if (retry_context.retry_task != NULL) | ||
108 | GNUNET_SCHEDULER_cancel (retry_context.retry_task); | ||
109 | GNUNET_DHT_disconnect (dht_handle); | 68 | GNUNET_DHT_disconnect (dht_handle); |
110 | dht_handle = NULL; | 69 | dht_handle = NULL; |
111 | ok = 1; | ||
112 | } | 70 | } |
113 | 71 | ||
114 | 72 | ||
115 | /** | ||
116 | * Signature of the main function of a task. | ||
117 | * | ||
118 | * @param cls closure | ||
119 | */ | ||
120 | static void | 73 | static void |
121 | test_get_stop (void *cls) | 74 | end_badly (void *cls) |
122 | { | 75 | { |
123 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 76 | die_task = NULL; |
124 | "Called test_get_stop!\n"); | 77 | FPRINTF (stderr, |
125 | GNUNET_assert (NULL != dht_handle); | 78 | "%s", |
126 | GNUNET_DHT_get_stop (get_handle); | 79 | "Ending on an unhappy note.\n"); |
127 | get_handle = NULL; | 80 | GNUNET_SCHEDULER_shutdown (); |
128 | GNUNET_SCHEDULER_add_now (&end, NULL); | 81 | ok = 1; |
129 | } | 82 | } |
130 | 83 | ||
131 | 84 | ||
132 | static void | 85 | static void |
133 | test_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp, | 86 | test_get_iterator (void *cls, |
134 | const struct GNUNET_HashCode * key, | 87 | struct GNUNET_TIME_Absolute exp, |
88 | const struct GNUNET_HashCode *key, | ||
135 | const struct GNUNET_PeerIdentity *get_path, | 89 | const struct GNUNET_PeerIdentity *get_path, |
136 | unsigned int get_path_length, | 90 | unsigned int get_path_length, |
137 | const struct GNUNET_PeerIdentity *put_path, | 91 | const struct GNUNET_PeerIdentity *put_path, |
138 | unsigned int put_path_length, | 92 | unsigned int put_path_length, |
139 | enum GNUNET_BLOCK_Type type, | 93 | enum GNUNET_BLOCK_Type type, |
140 | size_t size, const void *data) | 94 | size_t size, |
95 | const void *data) | ||
141 | { | 96 | { |
142 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 97 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
143 | "test_get_iterator called (we got a result), stopping get request!\n"); | 98 | "test_get_iterator called (we got a result), stopping get request!\n"); |
144 | GNUNET_SCHEDULER_add_now (&test_get_stop, | 99 | GNUNET_SCHEDULER_shutdown (); |
145 | NULL); | 100 | ok = 0; |
146 | } | 101 | } |
147 | 102 | ||
148 | 103 | ||
@@ -153,31 +108,33 @@ test_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp, | |||
153 | * @param success result of PUT | 108 | * @param success result of PUT |
154 | */ | 109 | */ |
155 | static void | 110 | static void |
156 | test_get (void *cls, int success) | 111 | test_get (void *cls, |
112 | int success) | ||
157 | { | 113 | { |
158 | struct GNUNET_HashCode hash; | 114 | struct GNUNET_HashCode hash; |
159 | 115 | ||
116 | put_handle = NULL; | ||
160 | memset (&hash, | 117 | memset (&hash, |
161 | 42, | 118 | 42, |
162 | sizeof (struct GNUNET_HashCode)); | 119 | sizeof (struct GNUNET_HashCode)); |
163 | |||
164 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 120 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
165 | "Called test_get!\n"); | 121 | "Called test_get!\n"); |
166 | GNUNET_assert (dht_handle != NULL); | 122 | GNUNET_assert (dht_handle != NULL); |
167 | retry_context.real_timeout = GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT); | 123 | get_handle = GNUNET_DHT_get_start (dht_handle, |
168 | retry_context.next_timeout = BASE_TIMEOUT; | 124 | GNUNET_BLOCK_TYPE_TEST, |
169 | 125 | &hash, | |
170 | get_handle = | 126 | 1, |
171 | GNUNET_DHT_get_start (dht_handle, | 127 | GNUNET_DHT_RO_NONE, |
172 | GNUNET_BLOCK_TYPE_TEST, &hash, 1, | 128 | NULL, |
173 | GNUNET_DHT_RO_NONE, NULL, 0, &test_get_iterator, | 129 | 0, |
174 | NULL); | 130 | &test_get_iterator, |
175 | 131 | NULL); | |
176 | if (get_handle == NULL) | 132 | |
133 | if (NULL == get_handle) | ||
177 | { | 134 | { |
178 | GNUNET_break (0); | 135 | GNUNET_break (0); |
179 | GNUNET_SCHEDULER_cancel (die_task); | 136 | ok = 1; |
180 | die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); | 137 | GNUNET_SCHEDULER_shutdown (); |
181 | return; | 138 | return; |
182 | } | 139 | } |
183 | } | 140 | } |
@@ -193,29 +150,38 @@ run (void *cls, | |||
193 | size_t data_size = 42; | 150 | size_t data_size = 42; |
194 | 151 | ||
195 | GNUNET_assert (ok == 1); | 152 | GNUNET_assert (ok == 1); |
196 | OKPP; | 153 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, |
197 | die_task = | 154 | NULL); |
198 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | 155 | die_task = GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT, |
199 | (GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, | 156 | &end_badly, |
200 | NULL); | 157 | NULL); |
201 | 158 | memset (&hash, | |
202 | 159 | 42, | |
203 | memset (&hash, 42, sizeof (struct GNUNET_HashCode)); | 160 | sizeof (struct GNUNET_HashCode)); |
204 | data = GNUNET_malloc (data_size); | 161 | data = GNUNET_malloc (data_size); |
205 | memset (data, 43, data_size); | 162 | memset (data, 43, data_size); |
206 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n"); | 163 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
207 | dht_handle = GNUNET_DHT_connect (cfg, 100); | 164 | "Called test_put!\n"); |
208 | GNUNET_assert (dht_handle != NULL); | 165 | dht_handle = GNUNET_DHT_connect (cfg, |
209 | GNUNET_DHT_put (dht_handle, &hash, 1, GNUNET_DHT_RO_NONE, | 166 | 100); |
210 | GNUNET_BLOCK_TYPE_TEST, data_size, data, | 167 | GNUNET_assert (NULL != dht_handle); |
211 | GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT), | 168 | put_handle = GNUNET_DHT_put (dht_handle, |
212 | &test_get, NULL); | 169 | &hash, |
170 | 1, | ||
171 | GNUNET_DHT_RO_NONE, | ||
172 | GNUNET_BLOCK_TYPE_TEST, | ||
173 | data_size, | ||
174 | data, | ||
175 | GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT), | ||
176 | &test_get, | ||
177 | NULL); | ||
213 | GNUNET_free (data); | 178 | GNUNET_free (data); |
214 | } | 179 | } |
215 | 180 | ||
216 | 181 | ||
217 | int | 182 | int |
218 | main (int argc, char *argv[]) | 183 | main (int argc, |
184 | char *argv[]) | ||
219 | { | 185 | { |
220 | if (0 != GNUNET_TESTING_peer_run ("test-dht-api", | 186 | if (0 != GNUNET_TESTING_peer_run ("test-dht-api", |
221 | "test_dht_api_data.conf", | 187 | "test_dht_api_data.conf", |