[asterisk-commits] mjordan: trunk r428947 - in /trunk: ./ main/manager.c main/test.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Dec 4 09:48:29 CST 2014


Author: mjordan
Date: Thu Dec  4 09:48:26 2014
New Revision: 428947

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=428947
Log:
main/test: Fix race condition between AMI topic and Test Suite topic

This patch fixes a race condition between the raising of test AMI events (which
drive many tests in the Asterisk Test Suite) and other AMI events. Prior to
this patch, the Stasis messages published to the test topic were not forwarded
to the AMI topic. Instead, the code in manager had a dedicated handler for test
messages that was independent of the topics forwarded to the AMI topic. This
results in no synchronization between the test messages and the rest of the
Stasis messages published out over AMI. In some test with very tight timing
constraints, this can result in out of order messages and spurious test
failures. Properly forwarding the Test Suite topic to the AMI topic ensures
that the messages are synchronized properly.

This patch does that, and moves the message handling to the Stasis definition
of the Test Suite message in test.c as well.

Review: https://reviewboard.asterisk.org/r/4221/
........

Merged revisions 428945 from http://svn.asterisk.org/svn/asterisk/branches/12
........

Merged revisions 428946 from http://svn.asterisk.org/svn/asterisk/branches/13

Modified:
    trunk/   (props changed)
    trunk/main/manager.c
    trunk/main/test.c

Propchange: trunk/
------------------------------------------------------------------------------
Binary property 'branch-13-merged' - no diff available.

Modified: trunk/main/manager.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/manager.c?view=diff&rev=428947&r1=428946&r2=428947
==============================================================================
--- trunk/main/manager.c (original)
+++ trunk/main/manager.c Thu Dec  4 09:48:26 2014
@@ -1375,7 +1375,8 @@
 static struct stasis_forward *security_topic_forwarder;
 
 #ifdef TEST_FRAMEWORK
-struct stasis_subscription *test_suite_sub;
+/*! \brief The \ref stasis_subscription for forwarding the Test topic to the AMI topic */
+static struct stasis_forward *test_suite_forwarder;
 #endif
 
 #define MGR_SHOW_TERMINAL_WIDTH 80
@@ -8360,49 +8361,6 @@
 	ast_channel_set_manager_vars(args.argc, args.vars);
 }
 
-#ifdef TEST_FRAMEWORK
-
-static void test_suite_event_cb(void *data, struct stasis_subscription *sub,
-		struct stasis_message *message)
-{
-	struct ast_test_suite_message_payload *payload;
-	struct ast_json *blob;
-	const char *type;
-
-	if (stasis_message_type(message) != ast_test_suite_message_type()) {
-		return;
-	}
-
-	payload = stasis_message_data(message);
-	if (!payload) {
-		return;
-	}
-	blob = ast_test_suite_get_blob(payload);
-	if (!blob) {
-		return;
-	}
-
-	type = ast_json_string_get(ast_json_object_get(blob, "type"));
-	if (ast_strlen_zero(type) || strcmp("testevent", type)) {
-		return;
-	}
-
-	manager_event(EVENT_FLAG_TEST, "TestEvent",
-		"Type: StateChange\r\n"
-		"State: %s\r\n"
-		"AppFile: %s\r\n"
-		"AppFunction: %s\r\n"
-		"AppLine: %jd\r\n"
-		"%s\r\n",
-		ast_json_string_get(ast_json_object_get(blob, "state")),
-		ast_json_string_get(ast_json_object_get(blob, "appfile")),
-		ast_json_string_get(ast_json_object_get(blob, "appfunction")),
-		ast_json_integer_get(ast_json_object_get(blob, "line")),
-		ast_json_string_get(ast_json_object_get(blob, "data")));
-}
-
-#endif
-
 /*!
  * \internal
  * \brief Free a user record.  Should already be removed from the list
@@ -8478,7 +8436,8 @@
 #endif
 
 #ifdef TEST_FRAMEWORK
-	stasis_unsubscribe(test_suite_sub);
+	stasis_forward_cancel(test_suite_forwarder);
+	test_suite_forwarder = NULL;
 #endif
 
 	if (stasis_router) {
@@ -8674,7 +8633,7 @@
 		ast_manager_register_xml_core("BlindTransfer", EVENT_FLAG_CALL, action_blind_transfer);
 
 #ifdef TEST_FRAMEWORK
-		test_suite_sub = stasis_subscribe(ast_test_suite_topic(), test_suite_event_cb, NULL);
+		test_suite_forwarder = stasis_forward_all(ast_test_suite_topic(), manager_topic);
 #endif
 
 		ast_cli_register_multiple(cli_manager, ARRAY_LEN(cli_manager));

Modified: trunk/main/test.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/test.c?view=diff&rev=428947&r1=428946&r2=428947
==============================================================================
--- trunk/main/test.c (original)
+++ trunk/main/test.c Thu Dec  4 09:48:26 2014
@@ -55,11 +55,6 @@
  * \brief The topic for test suite messages
  */
 struct stasis_topic *test_suite_topic;
-
-/*! \since 12
- * \brief The message type for test suite messages
- */
-STASIS_MESSAGE_TYPE_DEFN(ast_test_suite_message_type);
 
 /*! This array corresponds to the values defined in the ast_test_state enum */
 static const char * const test_result2str[] = {
@@ -1012,6 +1007,51 @@
 	return payload->blob;
 }
 
+static struct ast_manager_event_blob *test_suite_event_to_ami(struct stasis_message *msg)
+{
+	RAII_VAR(struct ast_str *, packet_string, ast_str_create(128), ast_free);
+	struct ast_test_suite_message_payload *payload;
+	struct ast_json *blob;
+	const char *type;
+
+	payload = stasis_message_data(msg);
+	if (!payload) {
+		return NULL;
+	}
+	blob = ast_test_suite_get_blob(payload);
+	if (!blob) {
+		return NULL;
+	}
+
+	type = ast_json_string_get(ast_json_object_get(blob, "type"));
+	if (ast_strlen_zero(type) || strcmp("testevent", type)) {
+		return NULL;
+	}
+
+	ast_str_append(&packet_string, 0, "Type: StateChange\r\n");
+	ast_str_append(&packet_string, 0, "State: %s\r\n",
+		ast_json_string_get(ast_json_object_get(blob, "state")));
+	ast_str_append(&packet_string, 0, "AppFile: %s\r\n",
+		ast_json_string_get(ast_json_object_get(blob, "appfile")));
+	ast_str_append(&packet_string, 0, "AppFunction: %s\r\n",
+		ast_json_string_get(ast_json_object_get(blob, "appfunction")));
+	ast_str_append(&packet_string, 0, "AppLine: %ld\r\n",
+		ast_json_integer_get(ast_json_object_get(blob, "line")));
+	ast_str_append(&packet_string, 0, "%s\r\n",
+		ast_json_string_get(ast_json_object_get(blob, "data")));
+
+	return ast_manager_event_blob_create(EVENT_FLAG_REPORTING,
+		"TestEvent",
+		"%s",
+		ast_str_buffer(packet_string));
+}
+
+/*! \since 12
+ * \brief The message type for test suite messages
+ */
+STASIS_MESSAGE_TYPE_DEFN(ast_test_suite_message_type,
+	.to_ami = test_suite_event_to_ami);
+
 void __ast_test_suite_event_notify(const char *file, const char *func, int line, const char *state, const char *fmt, ...)
 {
 	RAII_VAR(struct ast_test_suite_message_payload *, payload,




More information about the asterisk-commits mailing list