|
| 1 | +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | + |
| 3 | +#include "webservice.h" |
| 4 | + |
| 5 | +DUDA_REGISTER("Duda QA", "Unit Test"); |
| 6 | + |
| 7 | +int status_loop_create = -1; |
| 8 | +int status_timeout = -1; |
| 9 | +int status_channel_create = -1; |
| 10 | +int status_channel_rw = -1; |
| 11 | +int status_add = -1; |
| 12 | +int status_delete = -1; |
| 13 | +int status_check_timeout = -1; |
| 14 | +int status_check_ch_w = -1; |
| 15 | +int status_check_ch_r = -1; |
| 16 | +char *status_backend = NULL; |
| 17 | + |
| 18 | +void *cb_worker(void *data) |
| 19 | +{ |
| 20 | + int i; |
| 21 | + int n; |
| 22 | + int pfd[2]; |
| 23 | + int ret; |
| 24 | + int fd_timeout; |
| 25 | + int fd_ch_r = -1; |
| 26 | + int fd_ch_w = -1; |
| 27 | + mk_event_loop_t *evl; |
| 28 | + |
| 29 | + /* create the event loop */ |
| 30 | + evl = event->loop_create(64); |
| 31 | + if (evl) { |
| 32 | + status_loop_create = 0; |
| 33 | + } |
| 34 | + |
| 35 | + fd_timeout = event->loop_timeout_create(evl, 2); |
| 36 | + if (fd_timeout > 0) { |
| 37 | + status_timeout = 0; |
| 38 | + } |
| 39 | + |
| 40 | + ret = event->loop_channel_create(evl, &fd_ch_r, &fd_ch_w); |
| 41 | + if (ret == 0) { |
| 42 | + status_channel_create = 0; |
| 43 | + } |
| 44 | + if (fd_ch_r > 0 && fd_ch_w > 0) { |
| 45 | + status_channel_rw = 0; |
| 46 | + } |
| 47 | + |
| 48 | + pipe(pfd); |
| 49 | + ret = event->loop_add(evl, pfd[0], DUDA_EVENT_READ, NULL); |
| 50 | + if (ret == 0) { |
| 51 | + status_add = 0; |
| 52 | + } |
| 53 | + |
| 54 | + ret = event->loop_delete(evl, pfd[0]); |
| 55 | + if (ret == 0) { |
| 56 | + status_delete = 0; |
| 57 | + } |
| 58 | + |
| 59 | + status_backend = event->loop_backend(); |
| 60 | + |
| 61 | + while (1) { |
| 62 | + n = event->loop_wait(evl); |
| 63 | + for (i = 0; i < n; i ++) { |
| 64 | + int fd = evl->events[i].fd; |
| 65 | + uint64_t val; |
| 66 | + |
| 67 | + if (fd == fd_timeout) { |
| 68 | + ret = read(fd_timeout, &val, sizeof(val)); |
| 69 | + if (ret > 0) { |
| 70 | + status_check_timeout = 0; |
| 71 | + } |
| 72 | + |
| 73 | + val = 0xdeadbeef; |
| 74 | + ret = write(fd_ch_w, &val, sizeof(val)); |
| 75 | + if (ret > 0) { |
| 76 | + status_check_ch_w = 0; |
| 77 | + } |
| 78 | + } |
| 79 | + else if (fd == fd_ch_r) { |
| 80 | + ret = read(fd_ch_r, &val, sizeof(val)); |
| 81 | + if (ret > 0 && val == 0xdeadbeef) { |
| 82 | + status_check_ch_r = 0; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return NULL; |
| 89 | +} |
| 90 | + |
| 91 | +void cb_test(duda_request_t *dr) |
| 92 | +{ |
| 93 | + /* silly delay to let the worker timeout do its job */ |
| 94 | + sleep(3); |
| 95 | + |
| 96 | + response->http_status(dr, 200); |
| 97 | + response->printf(dr, "status_loop_create=%i\n", status_loop_create); |
| 98 | + response->printf(dr, "status_timeout=%i\n", status_timeout); |
| 99 | + response->printf(dr, "status_channel_create=%i\n", status_channel_create); |
| 100 | + response->printf(dr, "status_channel_rw=%i\n", status_channel_rw); |
| 101 | + response->printf(dr, "status_add=%i\n", status_add); |
| 102 | + response->printf(dr, "status_delete=%i\n", status_delete); |
| 103 | + response->printf(dr, "status_check_timeout=%i\n", status_check_timeout); |
| 104 | + response->printf(dr, "status_check_ch_w=%i\n", status_check_ch_w); |
| 105 | + response->printf(dr, "status_check_ch_r=%i\n", status_check_ch_r); |
| 106 | + response->printf(dr, "status_backend=%s\n", status_backend); |
| 107 | + |
| 108 | + response->end(dr, NULL); |
| 109 | +} |
| 110 | + |
| 111 | +int duda_main() |
| 112 | +{ |
| 113 | + worker->spawn(cb_worker, NULL); |
| 114 | + router->map("/test", cb_test); |
| 115 | + return 0; |
| 116 | +} |
0 commit comments