~vorlon/ubuntu/raring/upstart/lp.1199778

« back to all changes in this revision

Viewing changes to init/tests/test_control.c

  • Committer: Scott James Remnant
  • Date: 2007-03-08 14:22:45 UTC
  • Revision ID: scott@netsplit.com-20070308142245-8brvrno3jf5nspmh
* init/control.c (control_watch_jobs): Rename to control_subscribe_jobs
and update to handle new event name.
(control_unwatch_jobs): Rename to control_unsubscribe_jobs and update
to handle the new event name.
(control_watch_events): Rename to control_subscribe_events and update
to handle the new event name.
(control_unwatch_events): Rename to control_unsubscribe_events and
update to handle the new event name.
* init/tests/test_control.c (test_watch_jobs): Rename to
test_subscribe_jobs and update to new event name.
(test_unwatch_jobs): Rename to test_unsubscribe_jobs and update to 
new event name.
(test_watch_events): Rename to test_subscribe_events and update to
new event name.
(test_unwatch_events): Rename to test_unsubscribe events and update
to new event name.
* upstart/message.h: Rename the watch commands to subscribe/unsubscribe
and regroup with new message numbers.
* upstart/message.c (upstart_message_newv) 
(upstart_message_handle): Marshal the updated subscription messages.
* upstart/tests/test_message.c (test_new, my_handler) 
(test_handle): Update tests to new names and numbers.
* TODO: Update.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1020
1020
 
1021
1021
 
1022
1022
void
1023
 
test_watch_jobs (void)
1024
 
{
1025
 
        NihIo              *io;
1026
 
        pid_t               pid;
1027
 
        int                 wait_fd, status;
1028
 
        Job                *job;
1029
 
        NotifySubscription *sub;
1030
 
 
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.
1034
 
         */
1035
 
        TEST_FUNCTION ("control_watch_jobs");
1036
 
        io = control_open ();
1037
 
        upstart_disable_safeties = TRUE;
1038
 
 
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);
1046
 
 
1047
 
        fflush (stdout);
1048
 
        TEST_CHILD_WAIT (pid, wait_fd) {
1049
 
                NihIoMessage *message;
1050
 
                int           sock;
1051
 
                size_t        len;
1052
 
 
1053
 
                sock = upstart_open ();
1054
 
 
1055
 
                message = upstart_message_new (NULL, getppid (),
1056
 
                                               UPSTART_WATCH_JOBS);
1057
 
                assert (nih_io_message_send (message, sock) > 0);
1058
 
                nih_free (message);
1059
 
 
1060
 
                /* Allow the parent to continue so it can receive it */
1061
 
                TEST_CHILD_RELEASE (wait_fd);
1062
 
 
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,
1068
 
                                                       NULL));
1069
 
                nih_free (message);
1070
 
 
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,
1076
 
                                                       NULL));
1077
 
                nih_free (message);
1078
 
 
1079
 
                exit (0);
1080
 
        }
1081
 
 
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);
1085
 
 
1086
 
        sub = notify_subscription_find (pid, NOTIFY_JOB, NULL);
1087
 
        TEST_NE_P (sub, NULL);
1088
 
 
1089
 
        notify_job (job);
1090
 
 
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);
1094
 
 
1095
 
        waitpid (pid, &status, 0);
1096
 
        if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
1097
 
                exit (1);
1098
 
 
1099
 
        nih_list_free (&job->entry);
1100
 
        nih_list_free (&sub->entry);
1101
 
 
1102
 
 
1103
 
        control_close ();
1104
 
        upstart_disable_safeties = FALSE;
1105
 
}
1106
 
 
1107
 
void
1108
 
test_unwatch_jobs (void)
1109
 
{
1110
 
        NihIo              *io;
1111
 
        pid_t               pid;
1112
 
        int                 wait_fd, status;
1113
 
        Job                *job;
1114
 
        NotifySubscription *sub;
1115
 
 
1116
 
        /* Check that we can handle a message from a child process asking us
1117
 
         * to unsubscribe them from job status notifications.
1118
 
         */
1119
 
        TEST_FUNCTION ("control_unwatch_jobs");
1120
 
        io = control_open ();
1121
 
        upstart_disable_safeties = TRUE;
1122
 
 
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);
1131
 
 
1132
 
        fflush (stdout);
1133
 
        TEST_CHILD_WAIT (pid, wait_fd) {
1134
 
                NihIoMessage *message;
1135
 
                int           sock;
1136
 
                size_t        len;
1137
 
 
1138
 
                sock = upstart_open ();
1139
 
 
1140
 
                message = upstart_message_new (NULL, getppid (),
1141
 
                                               UPSTART_WATCH_JOBS);
1142
 
                assert (nih_io_message_send (message, sock) > 0);
1143
 
                nih_free (message);
1144
 
 
1145
 
                /* Allow the parent to continue so it can receive it */
1146
 
                TEST_CHILD_RELEASE (wait_fd);
1147
 
 
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,
1153
 
                                                       NULL));
1154
 
                nih_free (message);
1155
 
 
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)
1160
 
                                                       check_job_process,
1161
 
                                                       NULL));
1162
 
                nih_free (message);
1163
 
 
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,
1169
 
                                                       NULL));
1170
 
                nih_free (message);
1171
 
 
1172
 
                /* Having received a status update, we know the parent has
1173
 
                 * found our subscription, so now we unsubscribe.
1174
 
                 */
1175
 
                message = upstart_message_new (NULL, getppid (),
1176
 
                                               UPSTART_UNWATCH_JOBS);
1177
 
                assert (nih_io_message_send (message, sock) > 0);
1178
 
                nih_free (message);
1179
 
 
1180
 
                exit (0);
1181
 
        }
1182
 
 
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);
1186
 
 
1187
 
        sub = notify_subscription_find (pid, NOTIFY_JOB, NULL);
1188
 
        TEST_NE_P (sub, NULL);
1189
 
 
1190
 
        destructor_called = 0;
1191
 
        nih_alloc_set_destructor (sub, my_destructor);
1192
 
 
1193
 
        notify_job (job);
1194
 
 
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);
1198
 
 
1199
 
        waitpid (pid, &status, 0);
1200
 
        if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
1201
 
                exit (1);
1202
 
 
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);
1206
 
 
1207
 
        TEST_TRUE (destructor_called);
1208
 
 
1209
 
        nih_list_free (&job->entry);
1210
 
 
1211
 
 
1212
 
        control_close ();
1213
 
        upstart_disable_safeties = FALSE;
1214
 
}
1215
 
 
1216
 
void
1217
 
test_watch_events (void)
1218
 
{
1219
 
        NihIo              *io;
1220
 
        pid_t               pid;
1221
 
        int                 wait_fd, status;
1222
 
        EventEmission      *emission;
1223
 
        NotifySubscription *sub;
1224
 
 
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.
1228
 
         */
1229
 
        TEST_FUNCTION ("control_watch_events");
1230
 
        io = control_open ();
1231
 
        upstart_disable_safeties = TRUE;
1232
 
 
1233
 
        fflush (stdout);
1234
 
        TEST_CHILD_WAIT (pid, wait_fd) {
1235
 
                NihIoMessage *message;
1236
 
                int           sock;
1237
 
                size_t        len;
1238
 
 
1239
 
                sock = upstart_open ();
1240
 
 
1241
 
                message = upstart_message_new (NULL, getppid (),
1242
 
                                               UPSTART_WATCH_EVENTS);
1243
 
                assert (nih_io_message_send (message, sock) > 0);
1244
 
                nih_free (message);
1245
 
 
1246
 
                /* Allow the parent to continue so it can receive it */
1247
 
                TEST_CHILD_RELEASE (wait_fd);
1248
 
 
1249
 
                /* Should receive UPSTART_EVENT */
1250
 
                message = nih_io_message_recv (NULL, sock, &len);
1251
 
                assert0 (upstart_message_handle_using (message, message,
1252
 
                                                       (UpstartMessageHandler)
1253
 
                                                       check_event,
1254
 
                                                       NULL));
1255
 
                nih_free (message);
1256
 
 
1257
 
                exit (0);
1258
 
        }
1259
 
 
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);
1263
 
 
1264
 
        sub = notify_subscription_find (pid, NOTIFY_EVENT, NULL);
1265
 
        TEST_NE_P (sub, NULL);
1266
 
 
1267
 
        emission = event_emit ("test", NULL, NULL);
1268
 
        emission->id = 0xdeafbeef;
1269
 
        notify_event (emission);
1270
 
 
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);
1274
 
 
1275
 
        waitpid (pid, &status, 0);
1276
 
        if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
1277
 
                exit (1);
1278
 
 
1279
 
        nih_list_free (&emission->event.entry);
1280
 
        nih_list_free (&sub->entry);
1281
 
 
1282
 
 
1283
 
        control_close ();
1284
 
        upstart_disable_safeties = FALSE;
1285
 
}
1286
 
 
1287
 
void
1288
 
test_unwatch_events (void)
1289
 
{
1290
 
        NihIo              *io;
1291
 
        pid_t               pid;
1292
 
        int                 wait_fd, status;
1293
 
        EventEmission      *emission;
1294
 
        NotifySubscription *sub;
1295
 
 
1296
 
        /* Check that we can handle a message from a child process asking us
1297
 
         * to unsubscribe them from event notifications.
1298
 
         */
1299
 
        TEST_FUNCTION ("control_unwatch_events");
1300
 
        io = control_open ();
1301
 
        upstart_disable_safeties = TRUE;
1302
 
 
1303
 
        fflush (stdout);
1304
 
        TEST_CHILD_WAIT (pid, wait_fd) {
1305
 
                NihIoMessage *message;
1306
 
                int           sock;
1307
 
                size_t        len;
1308
 
 
1309
 
                sock = upstart_open ();
1310
 
 
1311
 
                message = upstart_message_new (NULL, getppid (),
1312
 
                                               UPSTART_WATCH_EVENTS);
1313
 
                assert (nih_io_message_send (message, sock) > 0);
1314
 
                nih_free (message);
1315
 
 
1316
 
                /* Allow the parent to continue so it can receive it */
1317
 
                TEST_CHILD_RELEASE (wait_fd);
1318
 
 
1319
 
                /* Should receive UPSTART_EVENT */
1320
 
                message = nih_io_message_recv (NULL, sock, &len);
1321
 
                assert0 (upstart_message_handle_using (message, message,
1322
 
                                                       (UpstartMessageHandler)
1323
 
                                                       check_event,
1324
 
                                                       NULL));
1325
 
                nih_free (message);
1326
 
 
1327
 
                /* Having received an event, we know the parent has
1328
 
                 * found our subscription, so now we unsubscribe.
1329
 
                 */
1330
 
                message = upstart_message_new (NULL, getppid (),
1331
 
                                               UPSTART_UNWATCH_EVENTS);
1332
 
                assert (nih_io_message_send (message, sock) > 0);
1333
 
                nih_free (message);
1334
 
 
1335
 
                exit (0);
1336
 
        }
1337
 
 
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);
1341
 
 
1342
 
        sub = notify_subscription_find (pid, NOTIFY_EVENT, NULL);
1343
 
        TEST_NE_P (sub, NULL);
1344
 
 
1345
 
        destructor_called = 0;
1346
 
        nih_alloc_set_destructor (sub, my_destructor);
1347
 
 
1348
 
        emission = event_emit ("test", NULL, NULL);
1349
 
        emission->id = 0xdeafbeef;
1350
 
        notify_event (emission);
1351
 
 
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);
1355
 
 
1356
 
        waitpid (pid, &status, 0);
1357
 
        if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
1358
 
                exit (1);
1359
 
 
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);
1363
 
 
1364
 
        TEST_TRUE (destructor_called);
1365
 
 
1366
 
        nih_list_free (&emission->event.entry);
1367
 
 
1368
 
 
1369
 
        control_close ();
1370
 
        upstart_disable_safeties = FALSE;
1371
 
}
1372
 
 
1373
 
 
1374
 
void
1375
1023
test_job_find (void)
1376
1024
{
1377
1025
        NihIo *io;
3263
2911
}
3264
2912
 
3265
2913
 
 
2914
void
 
2915
test_subscribe_jobs (void)
 
2916
{
 
2917
        NihIo              *io;
 
2918
        pid_t               pid;
 
2919
        int                 wait_fd, status;
 
2920
        Job                *job;
 
2921
        NotifySubscription *sub;
 
2922
 
 
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.
 
2926
         */
 
2927
        TEST_FUNCTION ("control_subscribe_jobs");
 
2928
        io = control_open ();
 
2929
        upstart_disable_safeties = TRUE;
 
2930
 
 
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);
 
2938
 
 
2939
        fflush (stdout);
 
2940
        TEST_CHILD_WAIT (pid, wait_fd) {
 
2941
                NihIoMessage *message;
 
2942
                int           sock;
 
2943
                size_t        len;
 
2944
 
 
2945
                sock = upstart_open ();
 
2946
 
 
2947
                message = upstart_message_new (NULL, getppid (),
 
2948
                                               UPSTART_SUBSCRIBE_JOBS);
 
2949
                assert (nih_io_message_send (message, sock) > 0);
 
2950
                nih_free (message);
 
2951
 
 
2952
                /* Allow the parent to continue so it can receive it */
 
2953
                TEST_CHILD_RELEASE (wait_fd);
 
2954
 
 
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,
 
2960
                                                       NULL));
 
2961
                nih_free (message);
 
2962
 
 
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,
 
2968
                                                       NULL));
 
2969
                nih_free (message);
 
2970
 
 
2971
                exit (0);
 
2972
        }
 
2973
 
 
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);
 
2977
 
 
2978
        sub = notify_subscription_find (pid, NOTIFY_JOB, NULL);
 
2979
        TEST_NE_P (sub, NULL);
 
2980
 
 
2981
        notify_job (job);
 
2982
 
 
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);
 
2986
 
 
2987
        waitpid (pid, &status, 0);
 
2988
        if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
 
2989
                exit (1);
 
2990
 
 
2991
        nih_list_free (&job->entry);
 
2992
        nih_list_free (&sub->entry);
 
2993
 
 
2994
 
 
2995
        control_close ();
 
2996
        upstart_disable_safeties = FALSE;
 
2997
}
 
2998
 
 
2999
void
 
3000
test_unsubscribe_jobs (void)
 
3001
{
 
3002
        NihIo              *io;
 
3003
        pid_t               pid;
 
3004
        int                 wait_fd, status;
 
3005
        Job                *job;
 
3006
        NotifySubscription *sub;
 
3007
 
 
3008
        /* Check that we can handle a message from a child process asking us
 
3009
         * to unsubscribe them from job status notifications.
 
3010
         */
 
3011
        TEST_FUNCTION ("control_unsubscribe_jobs");
 
3012
        io = control_open ();
 
3013
        upstart_disable_safeties = TRUE;
 
3014
 
 
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);
 
3023
 
 
3024
        fflush (stdout);
 
3025
        TEST_CHILD_WAIT (pid, wait_fd) {
 
3026
                NihIoMessage *message;
 
3027
                int           sock;
 
3028
                size_t        len;
 
3029
 
 
3030
                sock = upstart_open ();
 
3031
 
 
3032
                message = upstart_message_new (NULL, getppid (),
 
3033
                                               UPSTART_SUBSCRIBE_JOBS);
 
3034
                assert (nih_io_message_send (message, sock) > 0);
 
3035
                nih_free (message);
 
3036
 
 
3037
                /* Allow the parent to continue so it can receive it */
 
3038
                TEST_CHILD_RELEASE (wait_fd);
 
3039
 
 
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,
 
3045
                                                       NULL));
 
3046
                nih_free (message);
 
3047
 
 
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)
 
3052
                                                       check_job_process,
 
3053
                                                       NULL));
 
3054
                nih_free (message);
 
3055
 
 
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,
 
3061
                                                       NULL));
 
3062
                nih_free (message);
 
3063
 
 
3064
                /* Having received a status update, we know the parent has
 
3065
                 * found our subscription, so now we unsubscribe.
 
3066
                 */
 
3067
                message = upstart_message_new (NULL, getppid (),
 
3068
                                               UPSTART_UNSUBSCRIBE_JOBS);
 
3069
                assert (nih_io_message_send (message, sock) > 0);
 
3070
                nih_free (message);
 
3071
 
 
3072
                exit (0);
 
3073
        }
 
3074
 
 
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);
 
3078
 
 
3079
        sub = notify_subscription_find (pid, NOTIFY_JOB, NULL);
 
3080
        TEST_NE_P (sub, NULL);
 
3081
 
 
3082
        destructor_called = 0;
 
3083
        nih_alloc_set_destructor (sub, my_destructor);
 
3084
 
 
3085
        notify_job (job);
 
3086
 
 
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);
 
3090
 
 
3091
        waitpid (pid, &status, 0);
 
3092
        if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
 
3093
                exit (1);
 
3094
 
 
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);
 
3098
 
 
3099
        TEST_TRUE (destructor_called);
 
3100
 
 
3101
        nih_list_free (&job->entry);
 
3102
 
 
3103
 
 
3104
        control_close ();
 
3105
        upstart_disable_safeties = FALSE;
 
3106
}
 
3107
 
 
3108
void
 
3109
test_subscribe_events (void)
 
3110
{
 
3111
        NihIo              *io;
 
3112
        pid_t               pid;
 
3113
        int                 wait_fd, status;
 
3114
        EventEmission      *emission;
 
3115
        NotifySubscription *sub;
 
3116
 
 
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.
 
3120
         */
 
3121
        TEST_FUNCTION ("control_subscribe_events");
 
3122
        io = control_open ();
 
3123
        upstart_disable_safeties = TRUE;
 
3124
 
 
3125
        fflush (stdout);
 
3126
        TEST_CHILD_WAIT (pid, wait_fd) {
 
3127
                NihIoMessage *message;
 
3128
                int           sock;
 
3129
                size_t        len;
 
3130
 
 
3131
                sock = upstart_open ();
 
3132
 
 
3133
                message = upstart_message_new (NULL, getppid (),
 
3134
                                               UPSTART_SUBSCRIBE_EVENTS);
 
3135
                assert (nih_io_message_send (message, sock) > 0);
 
3136
                nih_free (message);
 
3137
 
 
3138
                /* Allow the parent to continue so it can receive it */
 
3139
                TEST_CHILD_RELEASE (wait_fd);
 
3140
 
 
3141
                /* Should receive UPSTART_EVENT */
 
3142
                message = nih_io_message_recv (NULL, sock, &len);
 
3143
                assert0 (upstart_message_handle_using (message, message,
 
3144
                                                       (UpstartMessageHandler)
 
3145
                                                       check_event,
 
3146
                                                       NULL));
 
3147
                nih_free (message);
 
3148
 
 
3149
                exit (0);
 
3150
        }
 
3151
 
 
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);
 
3155
 
 
3156
        sub = notify_subscription_find (pid, NOTIFY_EVENT, NULL);
 
3157
        TEST_NE_P (sub, NULL);
 
3158
 
 
3159
        emission = event_emit ("test", NULL, NULL);
 
3160
        emission->id = 0xdeafbeef;
 
3161
        notify_event (emission);
 
3162
 
 
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);
 
3166
 
 
3167
        waitpid (pid, &status, 0);
 
3168
        if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
 
3169
                exit (1);
 
3170
 
 
3171
        nih_list_free (&emission->event.entry);
 
3172
        nih_list_free (&sub->entry);
 
3173
 
 
3174
 
 
3175
        control_close ();
 
3176
        upstart_disable_safeties = FALSE;
 
3177
}
 
3178
 
 
3179
void
 
3180
test_unsubscribe_events (void)
 
3181
{
 
3182
        NihIo              *io;
 
3183
        pid_t               pid;
 
3184
        int                 wait_fd, status;
 
3185
        EventEmission      *emission;
 
3186
        NotifySubscription *sub;
 
3187
 
 
3188
        /* Check that we can handle a message from a child process asking us
 
3189
         * to unsubscribe them from event notifications.
 
3190
         */
 
3191
        TEST_FUNCTION ("control_unsubscribe_events");
 
3192
        io = control_open ();
 
3193
        upstart_disable_safeties = TRUE;
 
3194
 
 
3195
        fflush (stdout);
 
3196
        TEST_CHILD_WAIT (pid, wait_fd) {
 
3197
                NihIoMessage *message;
 
3198
                int           sock;
 
3199
                size_t        len;
 
3200
 
 
3201
                sock = upstart_open ();
 
3202
 
 
3203
                message = upstart_message_new (NULL, getppid (),
 
3204
                                               UPSTART_SUBSCRIBE_EVENTS);
 
3205
                assert (nih_io_message_send (message, sock) > 0);
 
3206
                nih_free (message);
 
3207
 
 
3208
                /* Allow the parent to continue so it can receive it */
 
3209
                TEST_CHILD_RELEASE (wait_fd);
 
3210
 
 
3211
                /* Should receive UPSTART_EVENT */
 
3212
                message = nih_io_message_recv (NULL, sock, &len);
 
3213
                assert0 (upstart_message_handle_using (message, message,
 
3214
                                                       (UpstartMessageHandler)
 
3215
                                                       check_event,
 
3216
                                                       NULL));
 
3217
                nih_free (message);
 
3218
 
 
3219
                /* Having received an event, we know the parent has
 
3220
                 * found our subscription, so now we unsubscribe.
 
3221
                 */
 
3222
                message = upstart_message_new (NULL, getppid (),
 
3223
                                               UPSTART_UNSUBSCRIBE_EVENTS);
 
3224
                assert (nih_io_message_send (message, sock) > 0);
 
3225
                nih_free (message);
 
3226
 
 
3227
                exit (0);
 
3228
        }
 
3229
 
 
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);
 
3233
 
 
3234
        sub = notify_subscription_find (pid, NOTIFY_EVENT, NULL);
 
3235
        TEST_NE_P (sub, NULL);
 
3236
 
 
3237
        destructor_called = 0;
 
3238
        nih_alloc_set_destructor (sub, my_destructor);
 
3239
 
 
3240
        emission = event_emit ("test", NULL, NULL);
 
3241
        emission->id = 0xdeafbeef;
 
3242
        notify_event (emission);
 
3243
 
 
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);
 
3247
 
 
3248
        waitpid (pid, &status, 0);
 
3249
        if ((! WIFEXITED (status)) || (WEXITSTATUS (status) != 0))
 
3250
                exit (1);
 
3251
 
 
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);
 
3255
 
 
3256
        TEST_TRUE (destructor_called);
 
3257
 
 
3258
        nih_list_free (&emission->event.entry);
 
3259
 
 
3260
 
 
3261
        control_close ();
 
3262
        upstart_disable_safeties = FALSE;
 
3263
}
 
3264
 
 
3265
 
3266
3266
int
3267
3267
main (int   argc,
3268
3268
      char *argv[])
3272
3272
        test_error_handler ();
3273
3273
        test_send_job_status ();
3274
3274
        test_send_instance ();
3275
 
        test_watch_jobs ();
3276
 
        test_unwatch_jobs ();
3277
 
        test_watch_events ();
3278
 
        test_unwatch_events ();
3279
3275
        test_job_find ();
3280
3276
        test_job_query ();
3281
3277
        test_job_start ();
3282
3278
        test_job_stop ();
3283
3279
        test_event_emit ();
 
3280
        test_subscribe_jobs ();
 
3281
        test_unsubscribe_jobs ();
 
3282
        test_subscribe_events ();
 
3283
        test_unsubscribe_events ();
3284
3284
 
3285
3285
        return 0;
3286
3286
}