diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2013-03-31 20:48:06 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2013-03-31 20:48:06 +0000 |
commit | 36291b73fab94a984ae4f7314f983e18492238be (patch) | |
tree | c5a2d753c04ebedcf236b0b31018789afbaf7e85 /src/testing | |
parent | 1517375e5b33fb42d6cd50fcbbc8d990ddd5b81a (diff) | |
download | gnunet-36291b73fab94a984ae4f7314f983e18492238be.tar.gz gnunet-36291b73fab94a984ae4f7314f983e18492238be.zip |
support for asynchronous peer start/stop and service start/stop using ARM API
Diffstat (limited to 'src/testing')
-rw-r--r-- | src/testing/test_testing_peerstartup2.c | 68 | ||||
-rw-r--r-- | src/testing/testing.c | 196 |
2 files changed, 161 insertions, 103 deletions
diff --git a/src/testing/test_testing_peerstartup2.c b/src/testing/test_testing_peerstartup2.c index 45d2280c7..25b9ba2ea 100644 --- a/src/testing/test_testing_peerstartup2.c +++ b/src/testing/test_testing_peerstartup2.c | |||
@@ -33,6 +33,15 @@ | |||
33 | #define LOG(kind,...) \ | 33 | #define LOG(kind,...) \ |
34 | GNUNET_log (kind, __VA_ARGS__) | 34 | GNUNET_log (kind, __VA_ARGS__) |
35 | 35 | ||
36 | |||
37 | #define FAIL_TEST(cond) \ | ||
38 | do { \ | ||
39 | if ((!(cond)) && (GNUNET_OK == status)) { \ | ||
40 | status = GNUNET_SYSERR; \ | ||
41 | } \ | ||
42 | } while (0) \ | ||
43 | |||
44 | |||
36 | /** | 45 | /** |
37 | * The status of the test | 46 | * The status of the test |
38 | */ | 47 | */ |
@@ -57,6 +66,17 @@ struct TestingContext | |||
57 | * The running configuration of the peer | 66 | * The running configuration of the peer |
58 | */ | 67 | */ |
59 | struct GNUNET_CONFIGURATION_Handle *cfg; | 68 | struct GNUNET_CONFIGURATION_Handle *cfg; |
69 | |||
70 | /** | ||
71 | * State | ||
72 | */ | ||
73 | enum { | ||
74 | PEER_INIT, | ||
75 | |||
76 | PEER_STARTED, | ||
77 | |||
78 | PEER_STOPPED | ||
79 | } state; | ||
60 | }; | 80 | }; |
61 | 81 | ||
62 | 82 | ||
@@ -75,14 +95,6 @@ do_shutdown2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
75 | 95 | ||
76 | } | 96 | } |
77 | 97 | ||
78 | static void | ||
79 | peer_stop_cb (void *cls, struct GNUNET_TESTING_Peer *peer, int success) | ||
80 | { | ||
81 | GNUNET_break (GNUNET_NO == success); | ||
82 | status = GNUNET_OK; | ||
83 | GNUNET_SCHEDULER_add_now (&do_shutdown2, cls); | ||
84 | } | ||
85 | |||
86 | 98 | ||
87 | /** | 99 | /** |
88 | * Task for shutdown | 100 | * Task for shutdown |
@@ -98,22 +110,35 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
98 | GNUNET_assert (NULL != test_ctx); | 110 | GNUNET_assert (NULL != test_ctx); |
99 | if (NULL != test_ctx->peer) | 111 | if (NULL != test_ctx->peer) |
100 | { | 112 | { |
101 | GNUNET_break (GNUNET_OK == GNUNET_TESTING_peer_stop2 (test_ctx->peer, | 113 | FAIL_TEST (GNUNET_OK == GNUNET_TESTING_peer_stop2 (test_ctx->peer, |
102 | &peer_stop_cb, | 114 | GNUNET_TIME_UNIT_MINUTES)); |
103 | test_ctx)); | 115 | |
104 | } | 116 | } |
105 | else | 117 | else |
106 | do_shutdown (test_ctx, tc); | 118 | do_shutdown (test_ctx, tc); |
107 | } | 119 | } |
108 | 120 | ||
121 | |||
109 | static void | 122 | static void |
110 | ps_cb (void *cls, struct GNUNET_TESTING_Peer *peer, int success) | 123 | peer_status_cb (void *cls, struct GNUNET_TESTING_Peer *peer, int success) |
111 | { | 124 | { |
112 | struct TestingContext *test_ctx = cls; | 125 | struct TestingContext *test_ctx = cls; |
113 | 126 | ||
114 | GNUNET_break (GNUNET_YES == success); | 127 | switch (test_ctx->state) |
115 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | 128 | { |
116 | &do_shutdown, test_ctx); | 129 | case PEER_INIT: |
130 | FAIL_TEST (GNUNET_YES == success); | ||
131 | test_ctx->state = PEER_STARTED; | ||
132 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
133 | &do_shutdown, test_ctx); | ||
134 | break; | ||
135 | case PEER_STARTED: | ||
136 | FAIL_TEST (GNUNET_NO == success); | ||
137 | GNUNET_SCHEDULER_add_now (&do_shutdown2, cls); | ||
138 | break; | ||
139 | case PEER_STOPPED: | ||
140 | FAIL_TEST (0); | ||
141 | } | ||
117 | } | 142 | } |
118 | 143 | ||
119 | 144 | ||
@@ -137,9 +162,11 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
137 | goto end; | 162 | goto end; |
138 | test_ctx->cfg = GNUNET_CONFIGURATION_dup (cfg); | 163 | test_ctx->cfg = GNUNET_CONFIGURATION_dup (cfg); |
139 | test_ctx->peer = | 164 | test_ctx->peer = |
140 | GNUNET_TESTING_peer_configure (test_ctx->system, | 165 | GNUNET_TESTING_peer_configure2 (test_ctx->system, |
141 | test_ctx->cfg, | 166 | test_ctx->cfg, |
142 | 0, &id, &emsg); | 167 | 0, &id, &emsg, |
168 | &peer_status_cb, | ||
169 | test_ctx); | ||
143 | if (NULL == test_ctx->peer) | 170 | if (NULL == test_ctx->peer) |
144 | { | 171 | { |
145 | if (NULL != emsg) | 172 | if (NULL != emsg) |
@@ -147,11 +174,12 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
147 | goto end; | 174 | goto end; |
148 | } | 175 | } |
149 | if (GNUNET_OK != GNUNET_TESTING_peer_start2 (test_ctx->peer, | 176 | if (GNUNET_OK != GNUNET_TESTING_peer_start2 (test_ctx->peer, |
150 | &ps_cb, test_ctx)) | 177 | GNUNET_TIME_UNIT_MINUTES)) |
151 | goto end; | 178 | goto end; |
152 | return; | 179 | return; |
153 | 180 | ||
154 | end: | 181 | end: |
182 | FAIL_TEST (0); | ||
155 | GNUNET_SCHEDULER_add_now (&do_shutdown, test_ctx); | 183 | GNUNET_SCHEDULER_add_now (&do_shutdown, test_ctx); |
156 | GNUNET_free_non_null (emsg); | 184 | GNUNET_free_non_null (emsg); |
157 | } | 185 | } |
@@ -163,7 +191,7 @@ int main (int argc, char *argv[]) | |||
163 | GNUNET_GETOPT_OPTION_END | 191 | GNUNET_GETOPT_OPTION_END |
164 | }; | 192 | }; |
165 | 193 | ||
166 | status = GNUNET_SYSERR; | 194 | status = GNUNET_OK; |
167 | if (GNUNET_OK != | 195 | if (GNUNET_OK != |
168 | GNUNET_PROGRAM_run (argc, argv, | 196 | GNUNET_PROGRAM_run (argc, argv, |
169 | "test_testing_new_peerstartup", | 197 | "test_testing_new_peerstartup", |
diff --git a/src/testing/testing.c b/src/testing/testing.c index 216dddfea..a96df9674 100644 --- a/src/testing/testing.c +++ b/src/testing/testing.c | |||
@@ -190,7 +190,7 @@ struct GNUNET_TESTING_Peer | |||
190 | */ | 190 | */ |
191 | struct GNUNET_CONFIGURATION_Handle *cfg; | 191 | struct GNUNET_CONFIGURATION_Handle *cfg; |
192 | 192 | ||
193 | GNUNET_TESTING_PeerStartCallback cb; | 193 | GNUNET_TESTING_PeerStatusCallback cb; |
194 | 194 | ||
195 | void *cb_cls; | 195 | void *cb_cls; |
196 | 196 | ||
@@ -997,6 +997,45 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system, | |||
997 | 997 | ||
998 | 998 | ||
999 | /** | 999 | /** |
1000 | * Wrapper over GNUNET_TESTING_peer_configure() to set the | ||
1001 | * GNUNET_TESTING_PeerStatusCallback() for using functions | ||
1002 | * GNUNET_TESTING_peer_start2() and GNUNET_TESTING_peer_stop2() | ||
1003 | * | ||
1004 | * @param system system to use to coordinate resource usage | ||
1005 | * @param cfg configuration to use; will be UPDATED (to reflect needed | ||
1006 | * changes in port numbers and paths) | ||
1007 | * @param key_number number of the hostkey to use for the peer | ||
1008 | * @param id identifier for the daemon, will be set, can be NULL | ||
1009 | * @param emsg set to freshly allocated error message (set to NULL on success), | ||
1010 | * can be NULL | ||
1011 | * @param status_cb the status callback to call upon peer start and stop | ||
1012 | * @return handle to the peer, NULL on error | ||
1013 | */ | ||
1014 | struct GNUNET_TESTING_Peer * | ||
1015 | GNUNET_TESTING_peer_configure2 (struct GNUNET_TESTING_System *system, | ||
1016 | struct GNUNET_CONFIGURATION_Handle *cfg, | ||
1017 | uint32_t key_number, | ||
1018 | struct GNUNET_PeerIdentity *id, | ||
1019 | char **emsg, | ||
1020 | GNUNET_TESTING_PeerStatusCallback status_cb, | ||
1021 | void *cls) | ||
1022 | { | ||
1023 | struct GNUNET_TESTING_Peer *peer; | ||
1024 | |||
1025 | peer = GNUNET_TESTING_peer_configure (system, | ||
1026 | cfg, | ||
1027 | key_number, | ||
1028 | id, | ||
1029 | emsg); | ||
1030 | if (NULL == peer) | ||
1031 | return NULL; | ||
1032 | peer->cb = status_cb; | ||
1033 | peer->cb_cls = cls; | ||
1034 | return peer; | ||
1035 | } | ||
1036 | |||
1037 | |||
1038 | /** | ||
1000 | * Obtain the peer identity from a peer handle. | 1039 | * Obtain the peer identity from a peer handle. |
1001 | * | 1040 | * |
1002 | * @param peer peer handle for which we want the peer's identity | 1041 | * @param peer peer handle for which we want the peer's identity |
@@ -1055,51 +1094,64 @@ GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer) | |||
1055 | } | 1094 | } |
1056 | 1095 | ||
1057 | 1096 | ||
1058 | void | 1097 | /** |
1098 | * Start a service at a peer using its ARM service | ||
1099 | * | ||
1100 | * @param peer the peer whose service has to be started | ||
1101 | * @param service_name name of the service to start | ||
1102 | * @param timeout how long should the ARM API try to send the request to start | ||
1103 | * the service | ||
1104 | * @param cont the callback to call with result and status from ARM API | ||
1105 | * @param cont_cls the closure for the above callback | ||
1106 | * @return GNUNET_OK upon successfully queuing the service start request; | ||
1107 | * GNUNET_SYSERR upon error | ||
1108 | */ | ||
1109 | int | ||
1059 | GNUNET_TESTING_peer_service_start (struct GNUNET_TESTING_Peer *peer, | 1110 | GNUNET_TESTING_peer_service_start (struct GNUNET_TESTING_Peer *peer, |
1060 | const char *service_name, | 1111 | const char *service_name, |
1112 | struct GNUNET_TIME_Relative timeout, | ||
1061 | GNUNET_ARM_ResultCallback cont, | 1113 | GNUNET_ARM_ResultCallback cont, |
1062 | void *cont_cls) | 1114 | void *cont_cls) |
1063 | { | 1115 | { |
1116 | if (NULL == peer->ah) | ||
1117 | return GNUNET_SYSERR; | ||
1064 | GNUNET_ARM_request_service_start (peer->ah, | 1118 | GNUNET_ARM_request_service_start (peer->ah, |
1065 | service_name, | 1119 | service_name, |
1066 | GNUNET_OS_INHERIT_STD_ALL, | 1120 | GNUNET_OS_INHERIT_STD_ALL, |
1067 | GNUNET_TIME_UNIT_MINUTES, | 1121 | timeout, |
1068 | cont, | 1122 | cont, cont_cls); |
1069 | cont_cls); | 1123 | return GNUNET_OK; |
1070 | |||
1071 | } | 1124 | } |
1072 | 1125 | ||
1073 | 1126 | ||
1074 | void GNUNET_TESTING_peer_service_stop (struct GNUNET_TESTING_Peer *peer, | 1127 | /** |
1075 | const char *service_name, | 1128 | * Stop a service at a peer using its ARM service |
1076 | GNUNET_ARM_ResultCallback cont, | 1129 | * |
1077 | void *cont_cls) | 1130 | * @param peer the peer whose service has to be stopped |
1131 | * @param service_name name of the service to stop | ||
1132 | * @param timeout how long should the ARM API try to send the request to stop | ||
1133 | * the service | ||
1134 | * @param cont the callback to call with result and status from ARM API | ||
1135 | * @param cont_cls the closure for the above callback | ||
1136 | * @return GNUNET_OK upon successfully queuing the service stop request; | ||
1137 | * GNUNET_SYSERR upon error | ||
1138 | */ | ||
1139 | int | ||
1140 | GNUNET_TESTING_peer_service_stop (struct GNUNET_TESTING_Peer *peer, | ||
1141 | const char *service_name, | ||
1142 | struct GNUNET_TIME_Relative timeout, | ||
1143 | GNUNET_ARM_ResultCallback cont, | ||
1144 | void *cont_cls) | ||
1078 | { | 1145 | { |
1079 | GNUNET_ARM_request_service_stop (peer->ah, service_name, | 1146 | if (NULL == peer->ah) |
1080 | GNUNET_TIME_UNIT_MINUTES, | 1147 | return GNUNET_SYSERR; |
1148 | GNUNET_ARM_request_service_stop (peer->ah, | ||
1149 | service_name, | ||
1150 | timeout, | ||
1081 | cont, cont_cls); | 1151 | cont, cont_cls); |
1152 | return GNUNET_OK; | ||
1082 | } | 1153 | } |
1083 | 1154 | ||
1084 | static void | ||
1085 | arm_start_result_cb (void *cls, | ||
1086 | struct GNUNET_ARM_Handle *arm, | ||
1087 | enum GNUNET_ARM_RequestStatus rs, | ||
1088 | const char *service, | ||
1089 | enum GNUNET_ARM_Result result) | ||
1090 | { | ||
1091 | struct GNUNET_TESTING_Peer *peer = cls; | ||
1092 | |||
1093 | if ((GNUNET_ARM_REQUEST_SENT_OK != rs) | ||
1094 | || ! ((GNUNET_ARM_RESULT_STARTING == result) | ||
1095 | || (GNUNET_ARM_RESULT_IS_STARTING_ALREADY == result) | ||
1096 | || (GNUNET_ARM_RESULT_IS_STARTED_ALREADY == result))) | ||
1097 | { | ||
1098 | peer->cb (peer->cb_cls, peer, GNUNET_NO); | ||
1099 | return; | ||
1100 | } | ||
1101 | peer->cb (peer->cb_cls, peer, GNUNET_OK); | ||
1102 | } | ||
1103 | 1155 | ||
1104 | /** | 1156 | /** |
1105 | * Function called whenever we connect to or disconnect from ARM. | 1157 | * Function called whenever we connect to or disconnect from ARM. |
@@ -1119,23 +1171,33 @@ conn_status (void *cls, struct GNUNET_ARM_Handle *arm, | |||
1119 | } | 1171 | } |
1120 | 1172 | ||
1121 | 1173 | ||
1174 | /** | ||
1175 | * Start a peer asynchronously using ARM API. Peer's startup is signaled | ||
1176 | * through the GNUNET_TESTING_PeerStatusCallback() given to | ||
1177 | * GNUNET_TESTING_peer_configure2(). To use this function the peer must be | ||
1178 | * configured earlier using GNUNET_TESTING_peer_configure2(); | ||
1179 | * | ||
1180 | * @param peer the peer to start | ||
1181 | * @param timeout how long to wait before giving up to start the peer | ||
1182 | * @return GNUNET_OK upon successfully giving the request to the ARM API (this | ||
1183 | * does not mean that the peer is successfully started); GNUNET_SYSERR | ||
1184 | * upon any error. | ||
1185 | */ | ||
1122 | int | 1186 | int |
1123 | GNUNET_TESTING_peer_start2 (struct GNUNET_TESTING_Peer *peer, | 1187 | GNUNET_TESTING_peer_start2 (struct GNUNET_TESTING_Peer *peer, |
1124 | GNUNET_TESTING_PeerStartCallback cb, | 1188 | struct GNUNET_TIME_Relative timeout) |
1125 | void *cb_cls) | ||
1126 | { | 1189 | { |
1127 | if (NULL != peer->ah) | 1190 | if (NULL != peer->ah) |
1128 | { | 1191 | { |
1129 | GNUNET_break (0); | 1192 | GNUNET_break (0); |
1130 | return GNUNET_SYSERR; | 1193 | return GNUNET_SYSERR; |
1131 | } | 1194 | } |
1132 | GNUNET_assert (NULL != (peer->cb = cb)); | 1195 | GNUNET_assert (NULL != peer->cb); |
1133 | peer->cb_cls = cb_cls; | ||
1134 | peer->ah = GNUNET_ARM_connect (peer->cfg, &conn_status, peer); | 1196 | peer->ah = GNUNET_ARM_connect (peer->cfg, &conn_status, peer); |
1135 | if (NULL == peer->ah) | 1197 | if (NULL == peer->ah) |
1136 | return GNUNET_SYSERR; | 1198 | return GNUNET_SYSERR; |
1137 | //GNUNET_TESTING_peer_service_start (peer, "arm", &arm_start_result_cb, peer); | 1199 | //GNUNET_TESTING_peer_service_start (peer, "arm", &arm_start_result_cb, peer); |
1138 | GNUNET_TESTING_peer_service_start (peer, "arm", NULL, NULL); | 1200 | GNUNET_TESTING_peer_service_start (peer, "arm", timeout, NULL, NULL); |
1139 | return GNUNET_OK; | 1201 | return GNUNET_OK; |
1140 | } | 1202 | } |
1141 | 1203 | ||
@@ -1200,58 +1262,28 @@ GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer) | |||
1200 | return GNUNET_SYSERR; | 1262 | return GNUNET_SYSERR; |
1201 | return GNUNET_OK; | 1263 | return GNUNET_OK; |
1202 | } | 1264 | } |
1203 | |||
1204 | static void | ||
1205 | arm_stop_result_cb (void *cls, | ||
1206 | struct GNUNET_ARM_Handle *arm, | ||
1207 | enum GNUNET_ARM_RequestStatus rs, | ||
1208 | const char *service, | ||
1209 | enum GNUNET_ARM_Result result) | ||
1210 | { | ||
1211 | struct GNUNET_TESTING_Peer *peer = cls; | ||
1212 | |||
1213 | if ((GNUNET_ARM_REQUEST_SENT_OK != rs) | ||
1214 | || ! ((GNUNET_ARM_RESULT_STOPPED == result) | ||
1215 | || (GNUNET_ARM_RESULT_STOPPING == result) | ||
1216 | || (GNUNET_ARM_RESULT_IS_STOPPING_ALREADY == result) | ||
1217 | || (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY == result))) | ||
1218 | { | ||
1219 | peer->cb (peer->cb_cls, peer, GNUNET_NO); | ||
1220 | return; | ||
1221 | } | ||
1222 | peer->cb (peer->cb_cls, peer, GNUNET_OK); | ||
1223 | } | ||
1224 | |||
1225 | |||
1226 | static void | ||
1227 | arm_service_monitor (void *cls, | ||
1228 | struct GNUNET_ARM_MonitorHandle *arm, | ||
1229 | const char *service, | ||
1230 | enum GNUNET_ARM_ServiceStatus status) | ||
1231 | { | ||
1232 | struct GNUNET_TESTING_Peer *peer = cls; | ||
1233 | |||
1234 | peer->mh = arm; | ||
1235 | if (GNUNET_ARM_SERVICE_STOPPED != status) | ||
1236 | return; | ||
1237 | if (0 != strcasecmp (service, "arm")) | ||
1238 | return; | ||
1239 | peer->cb (peer->cb_cls, peer, GNUNET_OK); | ||
1240 | } | ||
1241 | 1265 | ||
1242 | 1266 | ||
1267 | /** | ||
1268 | * Stop a peer asynchronously using ARM API. Peer's shutdown is signaled | ||
1269 | * through the GNUNET_TESTING_PeerStatusCallback() given to | ||
1270 | * GNUNET_TESTING_peer_configure2(). To use this function the peer must be | ||
1271 | * configured earlier using GNUNET_TESTING_peer_configure2(); | ||
1272 | * | ||
1273 | * @param peer the peer to stop | ||
1274 | * @param timeout how long to wait before giving up to stop the peer | ||
1275 | * @return GNUNET_OK upon successfully giving the request to the ARM API (this | ||
1276 | * does not mean that the peer is successfully stopped); GNUNET_SYSERR | ||
1277 | * upon any error. | ||
1278 | */ | ||
1243 | int | 1279 | int |
1244 | GNUNET_TESTING_peer_stop2 (struct GNUNET_TESTING_Peer *peer, | 1280 | GNUNET_TESTING_peer_stop2 (struct GNUNET_TESTING_Peer *peer, |
1245 | GNUNET_TESTING_PeerStartCallback cb, | 1281 | struct GNUNET_TIME_Relative timeout) |
1246 | void *cb_cls) | ||
1247 | { | 1282 | { |
1248 | if (NULL == peer->ah) | 1283 | if (NULL == peer->ah) |
1249 | return GNUNET_SYSERR; | 1284 | return GNUNET_SYSERR; |
1250 | GNUNET_assert (NULL != (peer->cb = cb)); | 1285 | GNUNET_assert (NULL != peer->cb); |
1251 | peer->cb_cls = cb_cls; | 1286 | GNUNET_TESTING_peer_service_stop (peer, "arm", timeout, NULL, NULL); |
1252 | /* if (NULL == peer->mh) */ | ||
1253 | /* peer->mh = GNUNET_ARM_monitor (peer->cfg, &arm_service_monitor, peer); */ | ||
1254 | GNUNET_TESTING_peer_service_stop (peer, "arm", NULL, NULL); | ||
1255 | return GNUNET_OK; | 1287 | return GNUNET_OK; |
1256 | } | 1288 | } |
1257 | 1289 | ||
@@ -1268,8 +1300,6 @@ GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer) | |||
1268 | { | 1300 | { |
1269 | if (NULL != peer->main_process) | 1301 | if (NULL != peer->main_process) |
1270 | GNUNET_TESTING_peer_stop (peer); | 1302 | GNUNET_TESTING_peer_stop (peer); |
1271 | if (NULL != peer->mh) | ||
1272 | GNUNET_ARM_monitor_disconnect_and_free (peer->mh); | ||
1273 | if (NULL != peer->ah) | 1303 | if (NULL != peer->ah) |
1274 | GNUNET_ARM_disconnect_and_free (peer->ah); | 1304 | GNUNET_ARM_disconnect_and_free (peer->ah); |
1275 | GNUNET_free (peer->cfgfile); | 1305 | GNUNET_free (peer->cfgfile); |