diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-07-06 14:22:51 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-07-06 14:22:51 +0000 |
commit | 0f9e6bcd1e511abae16ecc4c86056b0c26d73936 (patch) | |
tree | 4ba3af76391ee6c67563316de29b6ad8830cd7f2 /src/peerstore/test_peerstore_api_sync.c | |
parent | f1e619572751f7652db025f66f119d6a0308114b (diff) | |
download | gnunet-0f9e6bcd1e511abae16ecc4c86056b0c26d73936.tar.gz gnunet-0f9e6bcd1e511abae16ecc4c86056b0c26d73936.zip |
-fix non-deterministic peerstore sync failure
Diffstat (limited to 'src/peerstore/test_peerstore_api_sync.c')
-rw-r--r-- | src/peerstore/test_peerstore_api_sync.c | 131 |
1 files changed, 112 insertions, 19 deletions
diff --git a/src/peerstore/test_peerstore_api_sync.c b/src/peerstore/test_peerstore_api_sync.c index 2b2d34374..f53aad544 100644 --- a/src/peerstore/test_peerstore_api_sync.c +++ b/src/peerstore/test_peerstore_api_sync.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) | 3 | Copyright (C) 2015 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 |
@@ -19,26 +19,69 @@ | |||
19 | */ | 19 | */ |
20 | /** | 20 | /** |
21 | * @file peerstore/test_peerstore_api_sync.c | 21 | * @file peerstore/test_peerstore_api_sync.c |
22 | * @brief testcase for peerstore sync before disconnect feature | 22 | * @brief testcase for peerstore sync-on-disconnect feature. Stores |
23 | * a value just before disconnecting, and then checks that | ||
24 | * this value is actually stored. | ||
25 | * @author Omar Tarabai | ||
26 | * @author Christian Grothoff (minor fix, comments) | ||
23 | */ | 27 | */ |
24 | #include "platform.h" | 28 | #include "platform.h" |
25 | #include "gnunet_util_lib.h" | 29 | #include "gnunet_util_lib.h" |
26 | #include "gnunet_testing_lib.h" | 30 | #include "gnunet_testing_lib.h" |
27 | #include "gnunet_peerstore_service.h" | 31 | #include "gnunet_peerstore_service.h" |
28 | 32 | ||
29 | static int ok = 1; | 33 | /** |
34 | * Overall result, 0 for success. | ||
35 | */ | ||
36 | static int ok = 404; | ||
30 | 37 | ||
38 | /** | ||
39 | * Configuration we use. | ||
40 | */ | ||
31 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | 41 | static const struct GNUNET_CONFIGURATION_Handle *cfg; |
32 | 42 | ||
43 | /** | ||
44 | * handle to talk to the peerstore. | ||
45 | */ | ||
33 | static struct GNUNET_PEERSTORE_Handle *h; | 46 | static struct GNUNET_PEERSTORE_Handle *h; |
34 | 47 | ||
35 | static char *subsystem = "test_peerstore_api_sync"; | 48 | /** |
49 | * Subsystem we store the value for. | ||
50 | */ | ||
51 | static const char *subsystem = "test_peerstore_api_sync"; | ||
52 | |||
53 | /** | ||
54 | * Fake PID under which we store the value. | ||
55 | */ | ||
36 | static struct GNUNET_PeerIdentity pid; | 56 | static struct GNUNET_PeerIdentity pid; |
37 | static char *key = "test_peerstore_api_store_key"; | ||
38 | static char *val = "test_peerstore_api_store_val"; | ||
39 | 57 | ||
58 | /** | ||
59 | * Test key we're storing the test value under. | ||
60 | */ | ||
61 | static const char *key = "test_peerstore_api_store_key"; | ||
62 | |||
63 | /** | ||
64 | * Test value we are storing. | ||
65 | */ | ||
66 | static const char *val = "test_peerstore_api_store_val"; | ||
67 | |||
68 | |||
69 | /** | ||
70 | * Function that should be called with the result of the | ||
71 | * lookup, and finally once with NULL to signal the end | ||
72 | * of the iteration. | ||
73 | * | ||
74 | * Upon the first call, we set "ok" to success. On the | ||
75 | * second call (end of iteration) we terminate the test. | ||
76 | * | ||
77 | * @param cls NULL | ||
78 | * @param record the information stored in the peerstore | ||
79 | * @param emsg any error message | ||
80 | * @return #GNUNET_YES (all good, continue) | ||
81 | */ | ||
40 | static int | 82 | static int |
41 | iterate_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, | 83 | iterate_cb (void *cls, |
84 | const struct GNUNET_PEERSTORE_Record *record, | ||
42 | const char *emsg) | 85 | const char *emsg) |
43 | { | 86 | { |
44 | const char *rec_val; | 87 | const char *rec_val; |
@@ -46,7 +89,8 @@ iterate_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, | |||
46 | GNUNET_break (NULL == emsg); | 89 | GNUNET_break (NULL == emsg); |
47 | if (NULL == record) | 90 | if (NULL == record) |
48 | { | 91 | { |
49 | GNUNET_PEERSTORE_disconnect (h, GNUNET_NO); | 92 | GNUNET_PEERSTORE_disconnect (h, |
93 | GNUNET_NO); | ||
50 | GNUNET_SCHEDULER_shutdown (); | 94 | GNUNET_SCHEDULER_shutdown (); |
51 | return GNUNET_YES; | 95 | return GNUNET_YES; |
52 | } | 96 | } |
@@ -57,25 +101,68 @@ iterate_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, | |||
57 | } | 101 | } |
58 | 102 | ||
59 | 103 | ||
104 | /** | ||
105 | * Run the 2nd stage of the test where we fetch the | ||
106 | * data that should have been stored. | ||
107 | * | ||
108 | * @param cls NULL | ||
109 | * @param tc unused | ||
110 | */ | ||
111 | static void | ||
112 | test_cont (void *cls, | ||
113 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
114 | { | ||
115 | h = GNUNET_PEERSTORE_connect (cfg); | ||
116 | GNUNET_PEERSTORE_iterate (h, | ||
117 | subsystem, | ||
118 | &pid, key, | ||
119 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
120 | &iterate_cb, NULL); | ||
121 | } | ||
122 | |||
123 | |||
124 | /** | ||
125 | * Actually run the test. | ||
126 | */ | ||
60 | static void | 127 | static void |
61 | test1 () | 128 | test1 () |
62 | { | 129 | { |
63 | GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val, strlen (val) + 1, | ||
64 | GNUNET_TIME_UNIT_FOREVER_ABS, | ||
65 | GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL); | ||
66 | GNUNET_PEERSTORE_disconnect (h, GNUNET_YES); | ||
67 | h = GNUNET_PEERSTORE_connect (cfg); | 130 | h = GNUNET_PEERSTORE_connect (cfg); |
68 | GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key, | 131 | GNUNET_PEERSTORE_store (h, |
69 | GNUNET_TIME_UNIT_FOREVER_REL, &iterate_cb, NULL); | 132 | subsystem, |
133 | &pid, | ||
134 | key, | ||
135 | val, strlen (val) + 1, | ||
136 | GNUNET_TIME_UNIT_FOREVER_ABS, | ||
137 | GNUNET_PEERSTORE_STOREOPTION_REPLACE, | ||
138 | NULL, NULL); | ||
139 | GNUNET_PEERSTORE_disconnect (h, | ||
140 | GNUNET_YES); | ||
141 | h = NULL; | ||
142 | /* We need to wait a little bit to give the disconnect | ||
143 | a chance to actually finish the operation; otherwise, | ||
144 | the test may fail non-deterministically if the new | ||
145 | connection is faster than the cleanup routine of the | ||
146 | old one. */ | ||
147 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
148 | &test_cont, | ||
149 | NULL); | ||
70 | } | 150 | } |
71 | 151 | ||
72 | 152 | ||
153 | /** | ||
154 | * Initialize globals and launch the test. | ||
155 | * | ||
156 | * @param cls NULL | ||
157 | * @param c configuration to use | ||
158 | * @param peer handle to our peer (unused) | ||
159 | */ | ||
73 | static void | 160 | static void |
74 | run (void *cls, const struct GNUNET_CONFIGURATION_Handle *c, | 161 | run (void *cls, |
162 | const struct GNUNET_CONFIGURATION_Handle *c, | ||
75 | struct GNUNET_TESTING_Peer *peer) | 163 | struct GNUNET_TESTING_Peer *peer) |
76 | { | 164 | { |
77 | cfg = c; | 165 | cfg = c; |
78 | h = GNUNET_PEERSTORE_connect (cfg); | ||
79 | GNUNET_assert (NULL != h); | 166 | GNUNET_assert (NULL != h); |
80 | memset (&pid, 1, sizeof (pid)); | 167 | memset (&pid, 1, sizeof (pid)); |
81 | test1 (); | 168 | test1 (); |
@@ -86,10 +173,16 @@ int | |||
86 | main (int argc, char *argv[]) | 173 | main (int argc, char *argv[]) |
87 | { | 174 | { |
88 | if (0 != | 175 | if (0 != |
89 | GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore", | 176 | GNUNET_TESTING_service_run ("test-gnunet-peerstore-sync", |
90 | "test_peerstore_api_data.conf", &run, NULL)) | 177 | "peerstore", |
178 | "test_peerstore_api_data.conf", | ||
179 | &run, NULL)) | ||
91 | return 1; | 180 | return 1; |
181 | if (0 != ok) | ||
182 | fprintf (stderr, | ||
183 | "Test failed: %d\n", | ||
184 | ok). | ||
92 | return ok; | 185 | return ok; |
93 | } | 186 | } |
94 | 187 | ||
95 | /* end of test_peerstore_api_store.c */ | 188 | /* end of test_peerstore_api_sync.c */ |