1023
test_watch_jobs (void)
1027
int wait_fd, status;
1029
NotifySubscription *sub;
1031
/* Check that we can handle a message from a child process asking us
1032
* to subscribe them to job status notifications. We then tickle
1033
* a job so that the child gets a status notification.
1035
TEST_FUNCTION ("control_watch_jobs");
1036
io = control_open ();
1037
upstart_disable_safeties = TRUE;
1039
job = job_new (NULL, "test");
1040
job->id = 0xdeafbeef;
1041
job->goal = JOB_START;
1042
job->state = JOB_STARTING;
1043
job->process[PROCESS_PRE_START] = job_process_new (job->process);
1044
job->process[PROCESS_MAIN] = job_process_new (job->process);
1045
job->process[PROCESS_POST_STOP] = job_process_new (job->process);
1048
TEST_CHILD_WAIT (pid, wait_fd) {
1049
NihIoMessage *message;
1053
sock = upstart_open ();
1055
message = upstart_message_new (NULL, getppid (),
1056
UPSTART_WATCH_JOBS);
1057
assert (nih_io_message_send (message, sock) > 0);
1060
/* Allow the parent to continue so it can receive it */
1061
TEST_CHILD_RELEASE (wait_fd);
1063
/* Should receive UPSTART_JOB_STATUS */
1064
message = nih_io_message_recv (NULL, sock, &len);
1065
assert0 (upstart_message_handle_using (message, message,
1066
(UpstartMessageHandler)
1067
check_job_status__starting,
1071
/* Should receive UPSTART_JOB_STATUS_END */
1072
message = nih_io_message_recv (NULL, sock, &len);
1073
assert0 (upstart_message_handle_using (message, message,
1074
(UpstartMessageHandler)
1075
check_job_status_end__starting,
1082
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1083
while (! NIH_LIST_EMPTY (io->send_q))
1084
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1086
sub = notify_subscription_find (pid, NOTIFY_JOB, NULL);
1087
TEST_NE_P (sub, NULL);
1091
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1092
while (! NIH_LIST_EMPTY (io->send_q))
1093
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1095
waitpid (pid, &status, 0);
1096
if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
1099
nih_list_free (&job->entry);
1100
nih_list_free (&sub->entry);
1104
upstart_disable_safeties = FALSE;
1108
test_unwatch_jobs (void)
1112
int wait_fd, status;
1114
NotifySubscription *sub;
1116
/* Check that we can handle a message from a child process asking us
1117
* to unsubscribe them from job status notifications.
1119
TEST_FUNCTION ("control_unwatch_jobs");
1120
io = control_open ();
1121
upstart_disable_safeties = TRUE;
1123
job = job_new (NULL, "test");
1124
job->id = 0xdeafbeef;
1125
job->goal = JOB_STOP;
1126
job->state = JOB_STOPPING;
1127
job->process[PROCESS_PRE_START] = job_process_new (job->process);
1128
job->process[PROCESS_MAIN] = job_process_new (job->process);
1129
job->process[PROCESS_MAIN]->pid = 1000;
1130
job->process[PROCESS_POST_STOP] = job_process_new (job->process);
1133
TEST_CHILD_WAIT (pid, wait_fd) {
1134
NihIoMessage *message;
1138
sock = upstart_open ();
1140
message = upstart_message_new (NULL, getppid (),
1141
UPSTART_WATCH_JOBS);
1142
assert (nih_io_message_send (message, sock) > 0);
1145
/* Allow the parent to continue so it can receive it */
1146
TEST_CHILD_RELEASE (wait_fd);
1148
/* Should receive UPSTART_JOB_STATUS */
1149
message = nih_io_message_recv (NULL, sock, &len);
1150
assert0 (upstart_message_handle_using (message, message,
1151
(UpstartMessageHandler)
1152
check_job_status__stopping,
1156
/* Should receive UPSTART_JOB_PROCESS */
1157
message = nih_io_message_recv (NULL, sock, &len);
1158
assert0 (upstart_message_handle_using (message, message,
1159
(UpstartMessageHandler)
1164
/* Should receive UPSTART_JOB_STATUS_END */
1165
message = nih_io_message_recv (NULL, sock, &len);
1166
assert0 (upstart_message_handle_using (message, message,
1167
(UpstartMessageHandler)
1168
check_job_status_end__stopping,
1172
/* Having received a status update, we know the parent has
1173
* found our subscription, so now we unsubscribe.
1175
message = upstart_message_new (NULL, getppid (),
1176
UPSTART_UNWATCH_JOBS);
1177
assert (nih_io_message_send (message, sock) > 0);
1183
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1184
while (! NIH_LIST_EMPTY (io->send_q))
1185
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1187
sub = notify_subscription_find (pid, NOTIFY_JOB, NULL);
1188
TEST_NE_P (sub, NULL);
1190
destructor_called = 0;
1191
nih_alloc_set_destructor (sub, my_destructor);
1195
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1196
while (! NIH_LIST_EMPTY (io->send_q))
1197
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1199
waitpid (pid, &status, 0);
1200
if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
1203
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1204
while (! NIH_LIST_EMPTY (io->send_q))
1205
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1207
TEST_TRUE (destructor_called);
1209
nih_list_free (&job->entry);
1213
upstart_disable_safeties = FALSE;
1217
test_watch_events (void)
1221
int wait_fd, status;
1222
EventEmission *emission;
1223
NotifySubscription *sub;
1225
/* Check that we can handle a message from a child process asking us
1226
* to subscribe them to event notifications. We then emit an event
1227
* so that the child gets a notification.
1229
TEST_FUNCTION ("control_watch_events");
1230
io = control_open ();
1231
upstart_disable_safeties = TRUE;
1234
TEST_CHILD_WAIT (pid, wait_fd) {
1235
NihIoMessage *message;
1239
sock = upstart_open ();
1241
message = upstart_message_new (NULL, getppid (),
1242
UPSTART_WATCH_EVENTS);
1243
assert (nih_io_message_send (message, sock) > 0);
1246
/* Allow the parent to continue so it can receive it */
1247
TEST_CHILD_RELEASE (wait_fd);
1249
/* Should receive UPSTART_EVENT */
1250
message = nih_io_message_recv (NULL, sock, &len);
1251
assert0 (upstart_message_handle_using (message, message,
1252
(UpstartMessageHandler)
1260
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1261
while (! NIH_LIST_EMPTY (io->send_q))
1262
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1264
sub = notify_subscription_find (pid, NOTIFY_EVENT, NULL);
1265
TEST_NE_P (sub, NULL);
1267
emission = event_emit ("test", NULL, NULL);
1268
emission->id = 0xdeafbeef;
1269
notify_event (emission);
1271
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1272
while (! NIH_LIST_EMPTY (io->send_q))
1273
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1275
waitpid (pid, &status, 0);
1276
if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
1279
nih_list_free (&emission->event.entry);
1280
nih_list_free (&sub->entry);
1284
upstart_disable_safeties = FALSE;
1288
test_unwatch_events (void)
1292
int wait_fd, status;
1293
EventEmission *emission;
1294
NotifySubscription *sub;
1296
/* Check that we can handle a message from a child process asking us
1297
* to unsubscribe them from event notifications.
1299
TEST_FUNCTION ("control_unwatch_events");
1300
io = control_open ();
1301
upstart_disable_safeties = TRUE;
1304
TEST_CHILD_WAIT (pid, wait_fd) {
1305
NihIoMessage *message;
1309
sock = upstart_open ();
1311
message = upstart_message_new (NULL, getppid (),
1312
UPSTART_WATCH_EVENTS);
1313
assert (nih_io_message_send (message, sock) > 0);
1316
/* Allow the parent to continue so it can receive it */
1317
TEST_CHILD_RELEASE (wait_fd);
1319
/* Should receive UPSTART_EVENT */
1320
message = nih_io_message_recv (NULL, sock, &len);
1321
assert0 (upstart_message_handle_using (message, message,
1322
(UpstartMessageHandler)
1327
/* Having received an event, we know the parent has
1328
* found our subscription, so now we unsubscribe.
1330
message = upstart_message_new (NULL, getppid (),
1331
UPSTART_UNWATCH_EVENTS);
1332
assert (nih_io_message_send (message, sock) > 0);
1338
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1339
while (! NIH_LIST_EMPTY (io->send_q))
1340
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1342
sub = notify_subscription_find (pid, NOTIFY_EVENT, NULL);
1343
TEST_NE_P (sub, NULL);
1345
destructor_called = 0;
1346
nih_alloc_set_destructor (sub, my_destructor);
1348
emission = event_emit ("test", NULL, NULL);
1349
emission->id = 0xdeafbeef;
1350
notify_event (emission);
1352
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1353
while (! NIH_LIST_EMPTY (io->send_q))
1354
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1356
waitpid (pid, &status, 0);
1357
if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
1360
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1361
while (! NIH_LIST_EMPTY (io->send_q))
1362
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
1364
TEST_TRUE (destructor_called);
1366
nih_list_free (&emission->event.entry);
1370
upstart_disable_safeties = FALSE;
1375
1023
test_job_find (void)
2915
test_subscribe_jobs (void)
2919
int wait_fd, status;
2921
NotifySubscription *sub;
2923
/* Check that we can handle a message from a child process asking us
2924
* to subscribe them to job status notifications. We then tickle
2925
* a job so that the child gets a status notification.
2927
TEST_FUNCTION ("control_subscribe_jobs");
2928
io = control_open ();
2929
upstart_disable_safeties = TRUE;
2931
job = job_new (NULL, "test");
2932
job->id = 0xdeafbeef;
2933
job->goal = JOB_START;
2934
job->state = JOB_STARTING;
2935
job->process[PROCESS_PRE_START] = job_process_new (job->process);
2936
job->process[PROCESS_MAIN] = job_process_new (job->process);
2937
job->process[PROCESS_POST_STOP] = job_process_new (job->process);
2940
TEST_CHILD_WAIT (pid, wait_fd) {
2941
NihIoMessage *message;
2945
sock = upstart_open ();
2947
message = upstart_message_new (NULL, getppid (),
2948
UPSTART_SUBSCRIBE_JOBS);
2949
assert (nih_io_message_send (message, sock) > 0);
2952
/* Allow the parent to continue so it can receive it */
2953
TEST_CHILD_RELEASE (wait_fd);
2955
/* Should receive UPSTART_JOB_STATUS */
2956
message = nih_io_message_recv (NULL, sock, &len);
2957
assert0 (upstart_message_handle_using (message, message,
2958
(UpstartMessageHandler)
2959
check_job_status__starting,
2963
/* Should receive UPSTART_JOB_STATUS_END */
2964
message = nih_io_message_recv (NULL, sock, &len);
2965
assert0 (upstart_message_handle_using (message, message,
2966
(UpstartMessageHandler)
2967
check_job_status_end__starting,
2974
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
2975
while (! NIH_LIST_EMPTY (io->send_q))
2976
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
2978
sub = notify_subscription_find (pid, NOTIFY_JOB, NULL);
2979
TEST_NE_P (sub, NULL);
2983
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
2984
while (! NIH_LIST_EMPTY (io->send_q))
2985
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
2987
waitpid (pid, &status, 0);
2988
if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
2991
nih_list_free (&job->entry);
2992
nih_list_free (&sub->entry);
2996
upstart_disable_safeties = FALSE;
3000
test_unsubscribe_jobs (void)
3004
int wait_fd, status;
3006
NotifySubscription *sub;
3008
/* Check that we can handle a message from a child process asking us
3009
* to unsubscribe them from job status notifications.
3011
TEST_FUNCTION ("control_unsubscribe_jobs");
3012
io = control_open ();
3013
upstart_disable_safeties = TRUE;
3015
job = job_new (NULL, "test");
3016
job->id = 0xdeafbeef;
3017
job->goal = JOB_STOP;
3018
job->state = JOB_STOPPING;
3019
job->process[PROCESS_PRE_START] = job_process_new (job->process);
3020
job->process[PROCESS_MAIN] = job_process_new (job->process);
3021
job->process[PROCESS_MAIN]->pid = 1000;
3022
job->process[PROCESS_POST_STOP] = job_process_new (job->process);
3025
TEST_CHILD_WAIT (pid, wait_fd) {
3026
NihIoMessage *message;
3030
sock = upstart_open ();
3032
message = upstart_message_new (NULL, getppid (),
3033
UPSTART_SUBSCRIBE_JOBS);
3034
assert (nih_io_message_send (message, sock) > 0);
3037
/* Allow the parent to continue so it can receive it */
3038
TEST_CHILD_RELEASE (wait_fd);
3040
/* Should receive UPSTART_JOB_STATUS */
3041
message = nih_io_message_recv (NULL, sock, &len);
3042
assert0 (upstart_message_handle_using (message, message,
3043
(UpstartMessageHandler)
3044
check_job_status__stopping,
3048
/* Should receive UPSTART_JOB_PROCESS */
3049
message = nih_io_message_recv (NULL, sock, &len);
3050
assert0 (upstart_message_handle_using (message, message,
3051
(UpstartMessageHandler)
3056
/* Should receive UPSTART_JOB_STATUS_END */
3057
message = nih_io_message_recv (NULL, sock, &len);
3058
assert0 (upstart_message_handle_using (message, message,
3059
(UpstartMessageHandler)
3060
check_job_status_end__stopping,
3064
/* Having received a status update, we know the parent has
3065
* found our subscription, so now we unsubscribe.
3067
message = upstart_message_new (NULL, getppid (),
3068
UPSTART_UNSUBSCRIBE_JOBS);
3069
assert (nih_io_message_send (message, sock) > 0);
3075
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3076
while (! NIH_LIST_EMPTY (io->send_q))
3077
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3079
sub = notify_subscription_find (pid, NOTIFY_JOB, NULL);
3080
TEST_NE_P (sub, NULL);
3082
destructor_called = 0;
3083
nih_alloc_set_destructor (sub, my_destructor);
3087
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3088
while (! NIH_LIST_EMPTY (io->send_q))
3089
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3091
waitpid (pid, &status, 0);
3092
if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
3095
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3096
while (! NIH_LIST_EMPTY (io->send_q))
3097
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3099
TEST_TRUE (destructor_called);
3101
nih_list_free (&job->entry);
3105
upstart_disable_safeties = FALSE;
3109
test_subscribe_events (void)
3113
int wait_fd, status;
3114
EventEmission *emission;
3115
NotifySubscription *sub;
3117
/* Check that we can handle a message from a child process asking us
3118
* to subscribe them to event notifications. We then emit an event
3119
* so that the child gets a notification.
3121
TEST_FUNCTION ("control_subscribe_events");
3122
io = control_open ();
3123
upstart_disable_safeties = TRUE;
3126
TEST_CHILD_WAIT (pid, wait_fd) {
3127
NihIoMessage *message;
3131
sock = upstart_open ();
3133
message = upstart_message_new (NULL, getppid (),
3134
UPSTART_SUBSCRIBE_EVENTS);
3135
assert (nih_io_message_send (message, sock) > 0);
3138
/* Allow the parent to continue so it can receive it */
3139
TEST_CHILD_RELEASE (wait_fd);
3141
/* Should receive UPSTART_EVENT */
3142
message = nih_io_message_recv (NULL, sock, &len);
3143
assert0 (upstart_message_handle_using (message, message,
3144
(UpstartMessageHandler)
3152
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3153
while (! NIH_LIST_EMPTY (io->send_q))
3154
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3156
sub = notify_subscription_find (pid, NOTIFY_EVENT, NULL);
3157
TEST_NE_P (sub, NULL);
3159
emission = event_emit ("test", NULL, NULL);
3160
emission->id = 0xdeafbeef;
3161
notify_event (emission);
3163
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3164
while (! NIH_LIST_EMPTY (io->send_q))
3165
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3167
waitpid (pid, &status, 0);
3168
if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
3171
nih_list_free (&emission->event.entry);
3172
nih_list_free (&sub->entry);
3176
upstart_disable_safeties = FALSE;
3180
test_unsubscribe_events (void)
3184
int wait_fd, status;
3185
EventEmission *emission;
3186
NotifySubscription *sub;
3188
/* Check that we can handle a message from a child process asking us
3189
* to unsubscribe them from event notifications.
3191
TEST_FUNCTION ("control_unsubscribe_events");
3192
io = control_open ();
3193
upstart_disable_safeties = TRUE;
3196
TEST_CHILD_WAIT (pid, wait_fd) {
3197
NihIoMessage *message;
3201
sock = upstart_open ();
3203
message = upstart_message_new (NULL, getppid (),
3204
UPSTART_SUBSCRIBE_EVENTS);
3205
assert (nih_io_message_send (message, sock) > 0);
3208
/* Allow the parent to continue so it can receive it */
3209
TEST_CHILD_RELEASE (wait_fd);
3211
/* Should receive UPSTART_EVENT */
3212
message = nih_io_message_recv (NULL, sock, &len);
3213
assert0 (upstart_message_handle_using (message, message,
3214
(UpstartMessageHandler)
3219
/* Having received an event, we know the parent has
3220
* found our subscription, so now we unsubscribe.
3222
message = upstart_message_new (NULL, getppid (),
3223
UPSTART_UNSUBSCRIBE_EVENTS);
3224
assert (nih_io_message_send (message, sock) > 0);
3230
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3231
while (! NIH_LIST_EMPTY (io->send_q))
3232
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3234
sub = notify_subscription_find (pid, NOTIFY_EVENT, NULL);
3235
TEST_NE_P (sub, NULL);
3237
destructor_called = 0;
3238
nih_alloc_set_destructor (sub, my_destructor);
3240
emission = event_emit ("test", NULL, NULL);
3241
emission->id = 0xdeafbeef;
3242
notify_event (emission);
3244
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3245
while (! NIH_LIST_EMPTY (io->send_q))
3246
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3248
waitpid (pid, &status, 0);
3249
if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
3252
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3253
while (! NIH_LIST_EMPTY (io->send_q))
3254
io->watch->watcher (io, io->watch, NIH_IO_READ | NIH_IO_WRITE);
3256
TEST_TRUE (destructor_called);
3258
nih_list_free (&emission->event.entry);
3262
upstart_disable_safeties = FALSE;
3267
3267
main (int argc,