Review at  https://gerrit.osmocom.org/7636

ctrl: test deferred cmd, show current failure

Handling a deferred command currently deallocates the struct ctrl_cmd upon
exiting the initial command handling, while it should actually stay around for
the asynchronous/deferred handling of the ctrl command.

Show the current bug by means of a ctrl test. The test will be adjusted to
expect the correct result when the bug is fixed in a subsequent commit
(I24232be7dcf7be79f4def91ddc8b8f8005b56318).

Change-Id: Ibbc847fc583bdd8e5e53a008258805e634ea12b4
---
M tests/ctrl/ctrl_test.c
M tests/ctrl/ctrl_test.ok
2 files changed, 102 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/36/7636/1

diff --git a/tests/ctrl/ctrl_test.c b/tests/ctrl/ctrl_test.c
index a38591f..86c6a78 100644
--- a/tests/ctrl/ctrl_test.c
+++ b/tests/ctrl/ctrl_test.c
@@ -331,6 +331,101 @@
        talloc_free(ctrl);
 }
 
+CTRL_CMD_DEFINE(test_defer, "test-defer");
+static void ctrl_test_defer_cb(void *data)
+{
+       struct ctrl_cmd_def *cd = data;
+       struct ctrl_cmd *cmd = cd->cmd;
+       static int i = 0;
+       printf("%s called\n", __func__);
+
+       if (ctrl_cmd_def_is_zombie(cd)) {
+               printf("Is Zombie\n");
+               return;
+       }
+
+       cmd->reply = talloc_asprintf(cmd, "Test Defer #%d", i++);
+
+       ctrl_cmd_def_send(cd);
+       return;
+}
+
+static struct ctrl_cmd_def *test_defer_cd = NULL;
+static int get_test_defer(struct ctrl_cmd *cmd, void *data)
+{
+       void *ctx = cmd->node;
+       printf("%s called\n", __func__);
+
+       test_defer_cd = ctrl_cmd_def_make(ctx, cmd, NULL, 10);
+
+       return CTRL_CMD_HANDLED;
+}
+static int set_test_defer(struct ctrl_cmd *cmd, void *data)
+{
+       return CTRL_CMD_HANDLED;
+}
+static int verify_test_defer(struct ctrl_cmd *cmd, const char *value, void 
*data)
+{
+       return 0;
+}
+
+static void test_deferred_cmd()
+{
+       struct ctrl_handle *ctrl;
+       struct ctrl_connection *ccon;
+       struct ctrl_cmd *cmd;
+       struct msgb *msg;
+       int result;
+       int ctx_size_was;
+       int ctx_size_before_defer;
+
+       printf("\n%s\n", __func__);
+       ctx_size_was = talloc_total_size(ctx);
+
+       ctrl = ctrl_handle_alloc2(ctx, NULL, NULL, 0);
+       ccon = talloc_zero(ctx, struct ctrl_connection);
+       INIT_LLIST_HEAD(&ccon->def_cmds);
+
+       osmo_wqueue_init(&ccon->write_queue, 1);
+
+       ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_test_defer);
+
+       ctx_size_before_defer = talloc_total_size(ctx);
+
+       msg = msgb_from_string("GET 123 test-defer");
+
+       result = ctrl_handle_msg(ctrl, ccon, msg);
+       printf("ctrl_handle_msg() returned %d\n", result);
+
+       OSMO_ASSERT(result == CTRL_CMD_HANDLED);
+
+       /* Expecting a ctrl_cmd_def as well as the cmd to still be allocated */
+       if (talloc_total_size(ctx) <= ctx_size_before_defer) {
+               printf("deferred command apparently deallocated too soon\n");
+               /* ERROR -- showing current bug in handling deallocated cmds, 
hence exiting early */
+               goto exit_early;
+               talloc_report_full(ctx, stdout);
+               OSMO_ASSERT(false);
+       }
+
+       printf("invoking ctrl_test_defer_cb() asynchronously\n");
+       ctrl_test_defer_cb(test_defer_cd);
+
+       /* And now the deferred cmd should be cleaned up completely. */
+       if (talloc_total_size(ctx) != ctx_size_before_defer) {
+               printf("mem leak!\n");
+               talloc_report_full(ctx, stdout);
+               OSMO_ASSERT(false);
+       }
+
+       printf("success\n");
+
+exit_early:
+
+       talloc_free(ccon);
+       talloc_free(ctrl);
+}
+
 static struct log_info_cat test_categories[] = {
 };
 
@@ -357,5 +452,7 @@
 
        test_messages();
 
+       test_deferred_cmd();
+
        return 0;
 }
diff --git a/tests/ctrl/ctrl_test.ok b/tests/ctrl/ctrl_test.ok
index 087ebbc..2af708a 100644
--- a/tests/ctrl/ctrl_test.ok
+++ b/tests/ctrl/ctrl_test.ok
@@ -172,3 +172,8 @@
 reply = 'OK'
 handling:
 ok
+
+test_deferred_cmd
+get_test_defer called
+ctrl_handle_msg() returned 0
+deferred command apparently deallocated too soon

-- 
To view, visit https://gerrit.osmocom.org/7636
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibbc847fc583bdd8e5e53a008258805e634ea12b4
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofm...@sysmocom.de>

Reply via email to