[svn-commits] mmichelson: branch mmichelson/rls-subscribe r417733 - /team/mmichelson/rls-su...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Jul 1 17:42:09 CDT 2014


Author: mmichelson
Date: Tue Jul  1 17:42:06 2014
New Revision: 417733

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=417733
Log:
Tweak test code and add documentatin to unit test methods.


Modified:
    team/mmichelson/rls-subscribe/res/res_pjsip_pubsub.c

Modified: team/mmichelson/rls-subscribe/res/res_pjsip_pubsub.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/rls-subscribe/res/res_pjsip_pubsub.c?view=diff&rev=417733&r1=417732&r2=417733
==============================================================================
--- team/mmichelson/rls-subscribe/res/res_pjsip_pubsub.c (original)
+++ team/mmichelson/rls-subscribe/res/res_pjsip_pubsub.c Tue Jul  1 17:42:06 2014
@@ -2730,6 +2730,11 @@
 
 #ifdef TEST_FRAMEWORK
 
+/*!
+ * \brief "bad" resources
+ *
+ * These are resources that the test handler will reject subscriptions to.
+ */
 const char *bad_resources[] = {
 	"coconut",
 	"cilantro",
@@ -2737,6 +2742,11 @@
 	"cheese",
 };
 
+/*!
+ * \brief new_subscribe callback for unit tests
+ *
+ * Will give a 200 OK response to any resource except the "bad" ones.
+ */
 static int test_new_subscribe(struct ast_sip_endpoint *endpoint, const char *resource)
 {
 	int i;
@@ -2750,15 +2760,34 @@
 	return 200;
 }
 
+/*!
+ * \brief Subscription notifier for unit tests.
+ *
+ * Since unit tests are only concerned with building a resource tree,
+ * only the new_subscribe callback needs to be defined.
+ */
 struct ast_sip_notifier test_notifier = {
 	.new_subscribe = test_new_subscribe,
 };
 
+/*!
+ * \brief Subscription handler for unit tests.
+ */
 struct ast_sip_subscription_handler test_handler = {
 	.event_name = "test",
 	.notifier = &test_notifier,
 };
 
+/*!
+ * \brief Set properties on an allocated resource list
+ *
+ * \param list The list to set details on.
+ * \param event The list's event.
+ * \param resources Array of resources to add to the list.
+ * \param num_resources Number of resources in the array.
+ * \retval 0 Success
+ * \retval non-zero Failure
+ */
 static int populate_list(struct resource_list *list, const char *event, const char **resources, size_t num_resources)
 {
 	int i;
@@ -2773,6 +2802,9 @@
 	return 0;
 }
 
+/*!
+ * \brief RAII callback to destroy a resource list
+ */
 static void cleanup_resource_list(struct resource_list *list)
 {
 	if (!list) {
@@ -2783,8 +2815,19 @@
 	ao2_cleanup(list);
 }
 
+/*!
+ * \brief allocate a resource list, store it in sorcery, and set its details
+ *
+ * \param test The unit test. Used for logging status messages.
+ * \param list_name The name of the list to create.
+ * \param event The event the list services
+ * \param resources Array of resources to apply to the list
+ * \param num_resources The number of resources in the array
+ * \retval NULL Failed to allocate or populate list
+ * \retval non-NULL The created list
+ */
 static struct resource_list *create_resource_list(struct ast_test *test,
-		const char *list_name, const char *event,const char **resources, size_t size)
+		const char *list_name, const char *event, const char **resources, size_t num_resources)
 {
 	struct resource_list *list;
 
@@ -2800,7 +2843,7 @@
 		return NULL;
 	}
 
-	if (populate_list(list, event, resources, size)) {
+	if (populate_list(list, event, resources, num_resources)) {
 		ast_test_status_update(test, "Could not add resources to the resource list\n");
 		cleanup_resource_list(list);
 		return NULL;
@@ -2809,20 +2852,34 @@
 	return list;
 }
 
+/*!
+ * \brief Check the integrity of a tree node against a set of resources.
+ *
+ * The tree node's resources must be in the same order as the resources in
+ * the supplied resources array. Because of this constraint, tests can misrepresent
+ * the size of the resources array as being smaller than it really is if resources
+ * at the end of the array should not be present in the tree node.
+ *
+ * \param test The unit test. Used for printing status messages.
+ * \param node The constructed tree node whose integrity is under question.
+ * \param resources Array of expected resource values
+ * \param num_resources The number of resources to check in the array.
+ */
 static int check_node(struct ast_test *test, struct tree_node *node,
-		const char **resources, size_t size)
+		const char **resources, size_t num_resources)
 {
 	int i;
 
-	if (AST_VECTOR_SIZE(&node->children) != size) {
+	if (AST_VECTOR_SIZE(&node->children) != num_resources) {
 		ast_test_status_update(test, "Unexpected number of resources in tree. Expected %d, got %d\n",
-				size, AST_VECTOR_SIZE(&node->children));
+				num_resources, AST_VECTOR_SIZE(&node->children));
 		return -1;
 	}
 
-	for (i = 0; i < size; ++i) {
+	for (i = 0; i < num_resources; ++i) {
 		if (strcmp(resources[i], AST_VECTOR_GET(&node->children, i)->resource)) {
-			ast_test_status_update(test, "Mismatched resources\n");
+			ast_test_status_update(test, "Mismatched resources. Expected '%s' but got '%s'\n",
+					resources[i], AST_VECTOR_GET(&node->children, i)->resource);
 			return -1;
 		}
 	}
@@ -2830,10 +2887,19 @@
 	return 0;
 }
 
+/*!
+ * \brief RAII_VAR callback to destroy an allocated resource tree
+ */
+static void test_resource_tree_destroy(struct resource_tree *tree)
+{
+	resource_tree_destroy(tree);
+	ast_free(tree);
+}
+
 AST_TEST_DEFINE(resource_tree)
 {
 	RAII_VAR(struct resource_list *, list, NULL, cleanup_resource_list);
-	RAII_VAR(struct resource_tree *, tree, NULL, resource_tree_destroy);
+	RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
 	const char *resources[] = {
 		"huey",
 		"dewey",
@@ -2881,7 +2947,7 @@
 {
 	RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list);
 	RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list);
-	RAII_VAR(struct resource_tree *, tree, NULL, resource_tree_destroy);
+	RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
 	const char *resources_1[] = {
 		"huey",
 		"dewey",
@@ -2939,6 +3005,7 @@
 		return AST_TEST_FAIL;
 	}
 
+	/* The embedded list is at index 3 in the root node's children */
 	node = AST_VECTOR_GET(&node->children, 3);
 	if (check_node(test, node, resources_2, ARRAY_LEN(resources_2))) {
 		return AST_TEST_FAIL;
@@ -2950,12 +3017,12 @@
 AST_TEST_DEFINE(bad_resource)
 {
 	RAII_VAR(struct resource_list *, list, NULL, cleanup_resource_list);
-	RAII_VAR(struct resource_tree *, tree, NULL, resource_tree_destroy);
+	RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
 	const char *resources[] = {
 		"huey",
 		"dewey",
 		"louie",
-		"coconut",
+		"coconut", /* A "bad" resource */
 	};
 	int resp;
 
@@ -2988,6 +3055,7 @@
 		return AST_TEST_FAIL;
 	}
 
+	/* We check against all but the final resource since we expect it not to be in the tree */
 	if (check_node(test, tree->root, resources, ARRAY_LEN(resources) - 1)) {
 		return AST_TEST_FAIL;
 	}
@@ -3000,13 +3068,14 @@
 {
 	RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list);
 	RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list);
-	RAII_VAR(struct resource_tree *, tree, NULL, resource_tree_destroy);
+	RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
 	const char *resources_1[] = {
 		"huey",
 		"dewey",
 		"louie",
 		"gross",
 	};
+	/* This list has nothing but bad resources */
 	const char *resources_2[] = {
 		"coconut",
 		"cilantro",
@@ -3049,6 +3118,9 @@
 		return AST_TEST_FAIL;
 	}
 
+	/* We check against all but the final resource of the list since the entire branch should
+	 * be pruned from the tree
+	 */
 	if (check_node(test, tree->root, resources_1, ARRAY_LEN(resources_1) - 1)) {
 		return AST_TEST_FAIL;
 	}
@@ -3061,7 +3133,7 @@
 {
 	RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list);
 	RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list);
-	RAII_VAR(struct resource_tree *, tree, NULL, resource_tree_destroy);
+	RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
 	const char *resources_1[] = {
 		"huey",
 		"ducks",
@@ -3122,7 +3194,8 @@
 	}
 
 	/* This node should have "donald", "daisy", "scrooge", "dewey", and "louie".
-	 * "huey" is not here since that was already encountered in the parent list */
+	 * "huey" is not here since that was already encountered in the parent list
+	 */
 	node = AST_VECTOR_GET(&node->children, 1);
 	if (check_node(test, node, resources_2, ARRAY_LEN(resources_2) - 1)) {
 		return AST_TEST_FAIL;
@@ -3135,7 +3208,7 @@
 {
 	RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list);
 	RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list);
-	RAII_VAR(struct resource_tree *, tree, NULL, resource_tree_destroy);
+	RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
 	const char *resources_1[] = {
 		"derp",
 	};
@@ -3179,7 +3252,7 @@
 AST_TEST_DEFINE(bad_event)
 {
 	RAII_VAR(struct resource_list *, list, NULL, cleanup_resource_list);
-	RAII_VAR(struct resource_tree *, tree, NULL, resource_tree_destroy);
+	RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
 	const char *resources[] = {
 		"huey",
 		"dewey",
@@ -3205,6 +3278,9 @@
 	}
 
 	tree = ast_calloc(1, sizeof(*tree));
+	/* Since the test_handler is for event "test", this should not build a list, but
+	 * instead result in a single resource being created, called "foo"
+	 */
 	resp = build_resource_tree(NULL, &test_handler, "foo", tree);
 	if (resp != 200) {
 		ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);




More information about the svn-commits mailing list