From 989e617e1f45f48764e830c7875114c7f6e1cb48 Mon Sep 17 00:00:00 2001
From: Russell Bryant <russell@russellbryant.com>
Date: Mon, 23 Feb 2009 17:29:16 +0000
Subject: [PATCH] Fix a regression in scheduler entry ordering, and add a
 regression test for it.

(closes issue #14522)
Reported by: pj
Tested by: russell


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@178022 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 main/sched.c       |   2 +-
 tests/test_sched.c | 119 ++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 118 insertions(+), 3 deletions(-)

diff --git a/main/sched.c b/main/sched.c
index e1d43baee2..86c01143fa 100644
--- a/main/sched.c
+++ b/main/sched.c
@@ -233,7 +233,7 @@ static unsigned int sched_hash(const void *obj)
 
 static int sched_time_cmp(void *a, void *b)
 {
-	return ast_tvcmp(((struct sched *) a)->when, ((struct sched *) b)->when);
+	return ast_tvcmp(((struct sched *) b)->when, ((struct sched *) a)->when);
 }
 
 struct sched_context *sched_context_create(void)
diff --git a/tests/test_sched.c b/tests/test_sched.c
index 6222cfc023..186cc4847b 100644
--- a/tests/test_sched.c
+++ b/tests/test_sched.c
@@ -40,6 +40,120 @@ static int sched_cb(const void *data)
 }
 
 static char *handle_cli_sched_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	struct sched_context *con;
+	char *res = CLI_FAILURE;
+	int id1, id2, id3, wait;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "sched test";
+		e->usage = ""
+			"Usage: sched test\n"
+			"   Test scheduler entry ordering.\n"
+			"";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != e->args) {
+		return CLI_SHOWUSAGE;
+	}
+
+	ast_cli(a->fd, "Testing scheduler entry ordering ...\n");
+
+	if (!(con = sched_context_create())) {
+		ast_cli(a->fd, "Test failed - could not create scheduler context\n");
+		return CLI_FAILURE;
+	}
+
+	/* Add 3 scheduler entries, and then remove them, ensuring that the result
+	 * of ast_sched_wait() looks appropriate at each step along the way. */
+
+	if ((wait = ast_sched_wait(con)) != -1) {
+		ast_cli(a->fd, "ast_sched_wait() should have returned -1, returned '%d'\n",
+				wait);
+		goto return_cleanup;
+	}
+
+	if ((id1 = ast_sched_add(con, 100000, sched_cb, NULL)) == -1) {
+		ast_cli(a->fd, "Failed to add scheduler entry\n");
+		goto return_cleanup;
+	}
+
+	if ((wait = ast_sched_wait(con)) > 100000) {
+		ast_cli(a->fd, "ast_sched_wait() should have returned <= 100000, returned '%d'\n",
+				wait);
+		goto return_cleanup;
+	}
+
+	if ((id2 = ast_sched_add(con, 10000, sched_cb, NULL)) == -1) {
+		ast_cli(a->fd, "Failed to add scheduler entry\n");
+		goto return_cleanup;
+	}
+
+	if ((wait = ast_sched_wait(con)) > 10000) {
+		ast_cli(a->fd, "ast_sched_wait() should have returned <= 10000, returned '%d'\n",
+				wait);
+		goto return_cleanup;
+	}
+
+	if ((id3 = ast_sched_add(con, 1000, sched_cb, NULL)) == -1) {
+		ast_cli(a->fd, "Failed to add scheduler entry\n");
+		goto return_cleanup;
+	}
+
+	if ((wait = ast_sched_wait(con)) > 1000) {
+		ast_cli(a->fd, "ast_sched_wait() should have returned <= 1000, returned '%d'\n",
+				wait);
+		goto return_cleanup;
+	}
+
+	if (ast_sched_del(con, id3) == -1) {
+		ast_cli(a->fd, "Failed to remove scheduler entry\n");
+		goto return_cleanup;
+	}
+
+	if ((wait = ast_sched_wait(con)) <= 1000) {
+		ast_cli(a->fd, "ast_sched_wait() should have returned > 1000, returned '%d'\n",
+				wait);
+		goto return_cleanup;
+	}
+
+	if (ast_sched_del(con, id2) == -1) {
+		ast_cli(a->fd, "Failed to remove scheduler entry\n");
+		goto return_cleanup;
+	}
+
+	if ((wait = ast_sched_wait(con)) <= 10000) {
+		ast_cli(a->fd, "ast_sched_wait() should have returned > 10000, returned '%d'\n",
+				wait);
+		goto return_cleanup;
+	}
+
+	if (ast_sched_del(con, id1) == -1) {
+		ast_cli(a->fd, "Failed to remove scheduler entry\n");
+		goto return_cleanup;
+	}
+
+	if ((wait = ast_sched_wait(con)) != -1) {
+		ast_cli(a->fd, "ast_sched_wait() should have returned -1, returned '%d'\n",
+				wait);
+		goto return_cleanup;
+	}
+
+	res = CLI_SUCCESS;
+
+	ast_cli(a->fd, "Test passed!\n");
+
+return_cleanup:
+	sched_context_destroy(con);
+
+	return res;
+}
+
+static char *handle_cli_sched_bench(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	struct sched_context *con;
 	struct timeval start;
@@ -48,7 +162,7 @@ static char *handle_cli_sched_test(struct ast_cli_entry *e, int cmd, struct ast_
 
 	switch (cmd) {
 	case CLI_INIT:
-		e->command = "sched test";
+		e->command = "sched benchmark";
 		e->usage = ""
 			"Usage: sched test <num>\n"
 			"";
@@ -114,7 +228,8 @@ return_cleanup:
 }
 
 static struct ast_cli_entry cli_sched[] = {
-	AST_CLI_DEFINE(handle_cli_sched_test, "Test ast_sched add/del performance"),
+	AST_CLI_DEFINE(handle_cli_sched_bench, "Benchmark ast_sched add/del performance"),
+	AST_CLI_DEFINE(handle_cli_sched_test, "Test scheduler entry ordering"),
 };
 
 static int unload_module(void)
-- 
GitLab