From c7f52c4c84d6a8898048738c4db9266289c40b45 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sat, 12 Nov 2016 21:25:21 +0100 Subject: wqueue: Reject messges if queue is considered full The write queue was always meant to not queue more than the max_length messages but the implementation never rejected a message. Begin to log and enforce the queue size limit, add a testcase to verify the code and initialize except_cb as part of a fix for that new test case. Real applications might now run into the queue limit and drop messages where they just queued them before. It is unfortunate but I still think it is good to implement the routine as it was intended. We need to review osmo_wqueue_enqueue once more to see that no msgb is leaked. Change-Id: I1e6aef30f3e73d4bcf2967bc49f0783aa65395ae --- tests/write_queue/wqueue_test.c | 81 ++++++++++++++++++++++++++++++++++++++++ tests/write_queue/wqueue_test.ok | 1 + 2 files changed, 82 insertions(+) create mode 100644 tests/write_queue/wqueue_test.c create mode 100644 tests/write_queue/wqueue_test.ok (limited to 'tests/write_queue') diff --git a/tests/write_queue/wqueue_test.c b/tests/write_queue/wqueue_test.c new file mode 100644 index 00000000..827e4e84 --- /dev/null +++ b/tests/write_queue/wqueue_test.c @@ -0,0 +1,81 @@ +#include +#include +#include + +static const struct log_info_cat default_categories[] = { +}; + +static const struct log_info log_info = { + .cat = default_categories, + .num_cat = ARRAY_SIZE(default_categories), +}; + +static void test_wqueue_limit(void) +{ + struct msgb *msg; + struct osmo_wqueue wqueue; + int rc; + + osmo_wqueue_init(&wqueue, 0); + OSMO_ASSERT(wqueue.max_length == 0); + OSMO_ASSERT(wqueue.current_length == 0); + OSMO_ASSERT(wqueue.read_cb == NULL); + OSMO_ASSERT(wqueue.write_cb == NULL); + OSMO_ASSERT(wqueue.except_cb == NULL); + + /* try to add and fail */ + msg = msgb_alloc(4096, "msg1"); + rc = osmo_wqueue_enqueue(&wqueue, msg); + OSMO_ASSERT(rc < 0); + + /* add one and fail on the second */ + wqueue.max_length = 1; + rc = osmo_wqueue_enqueue(&wqueue, msg); + OSMO_ASSERT(rc == 0); + OSMO_ASSERT(wqueue.current_length == 1); + msg = msgb_alloc(4096, "msg2"); + rc = osmo_wqueue_enqueue(&wqueue, msg); + OSMO_ASSERT(rc < 0); + + /* add one more */ + wqueue.max_length = 2; + rc = osmo_wqueue_enqueue(&wqueue, msg); + OSMO_ASSERT(rc == 0); + OSMO_ASSERT(wqueue.current_length == 2); + + /* release everything */ + osmo_wqueue_clear(&wqueue); + OSMO_ASSERT(wqueue.current_length == 0); + OSMO_ASSERT(wqueue.max_length == 2); + + /* Add two, fail on the third, free it and the queue */ + msg = msgb_alloc(4096, "msg3"); + rc = osmo_wqueue_enqueue(&wqueue, msg); + OSMO_ASSERT(rc == 0); + OSMO_ASSERT(wqueue.current_length == 1); + msg = msgb_alloc(4096, "msg4"); + rc = osmo_wqueue_enqueue(&wqueue, msg); + OSMO_ASSERT(rc == 0); + OSMO_ASSERT(wqueue.current_length == 2); + msg = msgb_alloc(4096, "msg5"); + rc = osmo_wqueue_enqueue(&wqueue, msg); + OSMO_ASSERT(rc < 0); + OSMO_ASSERT(wqueue.current_length == 2); + msgb_free(msg); + osmo_wqueue_clear(&wqueue); +} + +int main(int argc, char **argv) +{ + struct log_target *stderr_target; + + log_init(&log_info, NULL); + stderr_target = log_target_create_stderr(); + log_add_target(stderr_target); + log_set_print_filename(stderr_target, 0); + + test_wqueue_limit(); + + printf("Done\n"); + return 0; +} diff --git a/tests/write_queue/wqueue_test.ok b/tests/write_queue/wqueue_test.ok new file mode 100644 index 00000000..a965a70e --- /dev/null +++ b/tests/write_queue/wqueue_test.ok @@ -0,0 +1 @@ +Done -- cgit v1.2.3