arehbein | ae07127 | 2023-09-16 21:18:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2023 by sysmocom - s.f.m.c. GmbH. |
| 3 | * Authors: Holger Hans Peter Freyther |
| 4 | * Alexander Rehbein |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
Holger Hans Peter Freyther | c7f52c4 | 2016-11-12 21:25:21 +0100 | [diff] [blame] | 12 | #include <osmocom/core/logging.h> |
| 13 | #include <osmocom/core/utils.h> |
| 14 | #include <osmocom/core/write_queue.h> |
| 15 | |
| 16 | static const struct log_info_cat default_categories[] = { |
| 17 | }; |
| 18 | |
| 19 | static const struct log_info log_info = { |
| 20 | .cat = default_categories, |
| 21 | .num_cat = ARRAY_SIZE(default_categories), |
| 22 | }; |
| 23 | |
| 24 | static void test_wqueue_limit(void) |
| 25 | { |
| 26 | struct msgb *msg; |
| 27 | struct osmo_wqueue wqueue; |
| 28 | int rc; |
arehbein | ae07127 | 2023-09-16 21:18:41 +0200 | [diff] [blame] | 29 | size_t dropped_msgs; |
Holger Hans Peter Freyther | c7f52c4 | 2016-11-12 21:25:21 +0100 | [diff] [blame] | 30 | |
| 31 | osmo_wqueue_init(&wqueue, 0); |
| 32 | OSMO_ASSERT(wqueue.max_length == 0); |
| 33 | OSMO_ASSERT(wqueue.current_length == 0); |
| 34 | OSMO_ASSERT(wqueue.read_cb == NULL); |
| 35 | OSMO_ASSERT(wqueue.write_cb == NULL); |
| 36 | OSMO_ASSERT(wqueue.except_cb == NULL); |
| 37 | |
| 38 | /* try to add and fail */ |
| 39 | msg = msgb_alloc(4096, "msg1"); |
| 40 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 41 | OSMO_ASSERT(rc < 0); |
| 42 | |
| 43 | /* add one and fail on the second */ |
| 44 | wqueue.max_length = 1; |
| 45 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 46 | OSMO_ASSERT(rc == 0); |
| 47 | OSMO_ASSERT(wqueue.current_length == 1); |
| 48 | msg = msgb_alloc(4096, "msg2"); |
| 49 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 50 | OSMO_ASSERT(rc < 0); |
| 51 | |
| 52 | /* add one more */ |
| 53 | wqueue.max_length = 2; |
| 54 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 55 | OSMO_ASSERT(rc == 0); |
| 56 | OSMO_ASSERT(wqueue.current_length == 2); |
| 57 | |
| 58 | /* release everything */ |
| 59 | osmo_wqueue_clear(&wqueue); |
| 60 | OSMO_ASSERT(wqueue.current_length == 0); |
| 61 | OSMO_ASSERT(wqueue.max_length == 2); |
| 62 | |
| 63 | /* Add two, fail on the third, free it and the queue */ |
| 64 | msg = msgb_alloc(4096, "msg3"); |
| 65 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 66 | OSMO_ASSERT(rc == 0); |
| 67 | OSMO_ASSERT(wqueue.current_length == 1); |
| 68 | msg = msgb_alloc(4096, "msg4"); |
| 69 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 70 | OSMO_ASSERT(rc == 0); |
| 71 | OSMO_ASSERT(wqueue.current_length == 2); |
| 72 | msg = msgb_alloc(4096, "msg5"); |
| 73 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 74 | OSMO_ASSERT(rc < 0); |
| 75 | OSMO_ASSERT(wqueue.current_length == 2); |
| 76 | msgb_free(msg); |
| 77 | osmo_wqueue_clear(&wqueue); |
arehbein | ae07127 | 2023-09-16 21:18:41 +0200 | [diff] [blame] | 78 | |
| 79 | /* Update limit */ |
| 80 | OSMO_ASSERT(osmo_wqueue_set_maxlen(&wqueue, 5) == 0); |
| 81 | OSMO_ASSERT(osmo_wqueue_set_maxlen(&wqueue, 1) == 0); |
| 82 | OSMO_ASSERT(osmo_wqueue_set_maxlen(&wqueue, 4) == 0); |
| 83 | |
| 84 | /* Add three, update limit to 1 */ |
| 85 | OSMO_ASSERT(wqueue.max_length == 4); |
| 86 | msg = msgb_alloc(4096, "msg6"); |
| 87 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 88 | OSMO_ASSERT(rc == 0); |
| 89 | OSMO_ASSERT(wqueue.current_length == 1); |
| 90 | msg = msgb_alloc(4096, "msg7"); |
| 91 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 92 | OSMO_ASSERT(rc == 0); |
| 93 | OSMO_ASSERT(wqueue.current_length == 2); |
| 94 | msg = msgb_alloc(4096, "msg8"); |
| 95 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 96 | OSMO_ASSERT(wqueue.current_length == 3); |
| 97 | dropped_msgs = osmo_wqueue_set_maxlen(&wqueue, 1); |
| 98 | OSMO_ASSERT(dropped_msgs == 2); |
| 99 | osmo_wqueue_clear(&wqueue); |
| 100 | |
| 101 | /* Add three, reduce limit to 3 from 6 */ |
| 102 | OSMO_ASSERT(osmo_wqueue_set_maxlen(&wqueue, 6) == 0); |
| 103 | OSMO_ASSERT(wqueue.max_length == 6); |
| 104 | msg = msgb_alloc(4096, "msg9"); |
| 105 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 106 | OSMO_ASSERT(rc == 0); |
| 107 | OSMO_ASSERT(wqueue.current_length == 1); |
| 108 | msg = msgb_alloc(4096, "msg10"); |
| 109 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 110 | OSMO_ASSERT(rc == 0); |
| 111 | OSMO_ASSERT(wqueue.current_length == 2); |
| 112 | msg = msgb_alloc(4096, "msg11"); |
| 113 | rc = osmo_wqueue_enqueue(&wqueue, msg); |
| 114 | OSMO_ASSERT(wqueue.current_length == 3); |
| 115 | dropped_msgs = osmo_wqueue_set_maxlen(&wqueue, 3); |
| 116 | OSMO_ASSERT(dropped_msgs == 0); |
| 117 | osmo_wqueue_clear(&wqueue); |
Holger Hans Peter Freyther | c7f52c4 | 2016-11-12 21:25:21 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | int main(int argc, char **argv) |
| 121 | { |
| 122 | struct log_target *stderr_target; |
| 123 | |
| 124 | log_init(&log_info, NULL); |
| 125 | stderr_target = log_target_create_stderr(); |
| 126 | log_add_target(stderr_target); |
Pau Espin Pedrol | 01e0d3e | 2021-02-18 19:25:44 +0100 | [diff] [blame] | 127 | log_set_print_filename2(stderr_target, LOG_FILENAME_NONE); |
Pau Espin Pedrol | 690b661 | 2021-02-18 19:10:28 +0100 | [diff] [blame] | 128 | log_set_print_category_hex(stderr_target, 0); |
| 129 | log_set_print_category(stderr_target, 0); |
Holger Hans Peter Freyther | c7f52c4 | 2016-11-12 21:25:21 +0100 | [diff] [blame] | 130 | |
| 131 | test_wqueue_limit(); |
| 132 | |
| 133 | printf("Done\n"); |
| 134 | return 0; |
| 135 | } |