Holger Hans Peter Freyther | c7f52c4 | 2016-11-12 21:25:21 +0100 | [diff] [blame] | 1 | #include <osmocom/core/logging.h> |
| 2 | #include <osmocom/core/utils.h> |
| 3 | #include <osmocom/core/write_queue.h> |
| 4 | |
| 5 | static const struct log_info_cat default_categories[] = { |
| 6 | }; |
| 7 | |
| 8 | static const struct log_info log_info = { |
| 9 | .cat = default_categories, |
| 10 | .num_cat = ARRAY_SIZE(default_categories), |
| 11 | }; |
| 12 | |
| 13 | static void test_wqueue_limit(void) |
| 14 | { |
| 15 | struct msgb *msg; |
| 16 | struct osmo_wqueue wqueue; |
| 17 | int rc; |
| 18 | |
| 19 | osmo_wqueue_init(&wqueue, 0); |
| 20 | OSMO_ASSERT(wqueue.max_length == 0); |
| 21 | OSMO_ASSERT(wqueue.current_length == 0); |
| 22 | OSMO_ASSERT(wqueue.read_cb == NULL); |
| 23 | OSMO_ASSERT(wqueue.write_cb == NULL); |
| 24 | OSMO_ASSERT(wqueue.except_cb == NULL); |
| 25 | |
| 26 | /* try to add and fail */ |
| 27 | msg = msgb_alloc(4096, "msg1"); |
| 28 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 29 | OSMO_ASSERT(rc < 0); |
| 30 | |
| 31 | /* add one and fail on the second */ |
| 32 | wqueue.max_length = 1; |
| 33 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 34 | OSMO_ASSERT(rc == 0); |
| 35 | OSMO_ASSERT(wqueue.current_length == 1); |
| 36 | msg = msgb_alloc(4096, "msg2"); |
| 37 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 38 | OSMO_ASSERT(rc < 0); |
| 39 | |
| 40 | /* add one more */ |
| 41 | wqueue.max_length = 2; |
| 42 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 43 | OSMO_ASSERT(rc == 0); |
| 44 | OSMO_ASSERT(wqueue.current_length == 2); |
| 45 | |
| 46 | /* release everything */ |
| 47 | osmo_wqueue_clear(&wqueue); |
| 48 | OSMO_ASSERT(wqueue.current_length == 0); |
| 49 | OSMO_ASSERT(wqueue.max_length == 2); |
| 50 | |
| 51 | /* Add two, fail on the third, free it and the queue */ |
| 52 | msg = msgb_alloc(4096, "msg3"); |
| 53 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 54 | OSMO_ASSERT(rc == 0); |
| 55 | OSMO_ASSERT(wqueue.current_length == 1); |
| 56 | msg = msgb_alloc(4096, "msg4"); |
| 57 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 58 | OSMO_ASSERT(rc == 0); |
| 59 | OSMO_ASSERT(wqueue.current_length == 2); |
| 60 | msg = msgb_alloc(4096, "msg5"); |
| 61 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 62 | OSMO_ASSERT(rc < 0); |
| 63 | OSMO_ASSERT(wqueue.current_length == 2); |
| 64 | msgb_free(msg); |
| 65 | osmo_wqueue_clear(&wqueue); |
| 66 | } |
| 67 | |
| 68 | int main(int argc, char **argv) |
| 69 | { |
| 70 | struct log_target *stderr_target; |
| 71 | |
| 72 | log_init(&log_info, NULL); |
| 73 | stderr_target = log_target_create_stderr(); |
| 74 | log_add_target(stderr_target); |
| 75 | log_set_print_filename(stderr_target, 0); |
| 76 | |
| 77 | test_wqueue_limit(); |
| 78 | |
| 79 | printf("Done\n"); |
| 80 | return 0; |
| 81 | } |