<p>Jenkins2 <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/8607">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Sean Bright: Looks good to me, but someone else must approve
Kevin Harwell: Looks good to me, approved
Jenkins2: Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">test_data_buffer.c: Add unit tests for data buffer API.<br><br>Added unit tests for the data buffer API. These tests include creating a<br>data buffer, putting payloads into the buffer, resizing the buffer, and<br>the nominal case for data buffer usage, which consists of adding<br>the max number of payloads to the buffer, checking to see if the correct<br>payloads are present, then adding more payloads and checking again to<br>see if the previous payloads were replaced or not.<br><br>For more information, refer to the wiki page:<br>https://wiki.asterisk.org/wiki/display/AST/WebRTC+User+Experience+Improvements<br><br>Change-Id: Id5b599aa15a5e61d0ec080f97cd0c57bd07e6f8f<br>---<br>A tests/test_data_buffer.c<br>1 file changed, 313 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">diff --git a/tests/test_data_buffer.c b/tests/test_data_buffer.c<br>new file mode 100644<br>index 0000000..11fdc7b<br>--- /dev/null<br>+++ b/tests/test_data_buffer.c<br>@@ -0,0 +1,313 @@<br>+/*<br>+ * Asterisk -- An open source telephony toolkit.<br>+ *<br>+ * Copyright (C) 2018, Digium, Inc.<br>+ *<br>+ * Ben Ford <bford@digium.com><br>+ *<br>+ * See http://www.asterisk.org for more information about<br>+ * the Asterisk project. Please do not directly contact<br>+ * any of the maintainers of this project for assistance;<br>+ * the project provides a web site, mailing lists and IRC<br>+ * channels for your use.<br>+ *<br>+ * This program is free software, distributed under the terms of<br>+ * the GNU General Public License Version 2. See the LICENSE file<br>+ * at the top of the source tree.<br>+ */<br>+<br>+/*!<br>+ * \file<br>+ * \brief Media Stream API Unit Tests<br>+ *<br>+ * \author Ben Ford <bford@digium.com><br>+ *<br>+ */<br>+<br>+/*** MODULEINFO<br>+ <depend>TEST_FRAMEWORK</depend><br>+ <support_level>core</support_level><br>+ ***/<br>+<br>+#include "asterisk.h"<br>+<br>+#include "asterisk/module.h"<br>+#include "asterisk/test.h"<br>+#include "asterisk/data_buffer.h"<br>+<br>+#define BUFFER_MAX_NOMINAL 10<br>+<br>+struct mock_payload<br>+{<br>+ int id;<br>+};<br>+<br>+/* Ensures that RAII_VAR will not trip ast_assert(buffer != NULL) in the callback */<br>+static void ast_data_buffer_free_wrapper(struct ast_data_buffer *buffer)<br>+{<br>+ if (!buffer) {<br>+ return;<br>+ }<br>+<br>+ ast_data_buffer_free(buffer);<br>+}<br>+<br>+AST_TEST_DEFINE(buffer_create)<br>+{<br>+ RAII_VAR(struct ast_data_buffer *, buffer, NULL, ast_data_buffer_free_wrapper);<br>+<br>+ switch (cmd) {<br>+ case TEST_INIT:<br>+ info->name = "buffer_create";<br>+ info->category = "/main/data_buffer/";<br>+ info->summary = "buffer create unit test";<br>+ info->description =<br>+ "Test that creating a data buffer results in a buffer with the expected values";<br>+ return AST_TEST_NOT_RUN;<br>+ case TEST_EXECUTE:<br>+ break;<br>+ }<br>+<br>+ buffer = ast_data_buffer_alloc(ast_free_ptr, BUFFER_MAX_NOMINAL);<br>+<br>+ ast_test_validate(test, buffer != NULL,<br>+ "Failed to create buffer with valid arguments");<br>+ ast_test_validate(test, ast_data_buffer_count(buffer) == 0,<br>+ "Newly created buffer does not have the expected payload count");<br>+ ast_test_validate(test, ast_data_buffer_max(buffer) == BUFFER_MAX_NOMINAL,<br>+ "Newly created buffer does not have the expected max size");<br>+<br>+ return AST_TEST_PASS;<br>+}<br>+<br>+AST_TEST_DEFINE(buffer_put)<br>+{<br>+ RAII_VAR(struct ast_data_buffer *, buffer, NULL, ast_data_buffer_free_wrapper);<br>+ struct mock_payload *payload;<br>+ struct mock_payload *fetched_payload;<br>+ int ret;<br>+<br>+ switch (cmd) {<br>+ case TEST_INIT:<br>+ info->name = "buffer_put";<br>+ info->category = "/main/data_buffer/";<br>+ info->summary = "buffer put unit test";<br>+ info->description =<br>+ "Test that putting payloads in the buffer yields the expected results";<br>+ return AST_TEST_NOT_RUN;<br>+ case TEST_EXECUTE:<br>+ break;<br>+ }<br>+<br>+ buffer = ast_data_buffer_alloc(ast_free_ptr, 2);<br>+<br>+ ast_test_validate(test, buffer != NULL,<br>+ "Failed to create buffer with valid arguments");<br>+ ast_test_validate(test, ast_data_buffer_count(buffer) == 0,<br>+ "Newly created buffer is not empty");<br>+<br>+ payload = ast_calloc(1, sizeof(*payload));<br>+<br>+ ast_test_validate(test, payload != NULL,<br>+ "Failed to allocate memory for first payload");<br>+<br>+ payload->id = 2;<br>+ ret = ast_data_buffer_put(buffer, 2, payload);<br>+<br>+ ast_test_validate(test, ret == 0,<br>+ "Adding a payload to an empty buffer did not return the expected value");<br>+ ast_test_validate(test, ast_data_buffer_count(buffer) == 1,<br>+ "Adding a payload to an empty buffer did not update count to the expected value");<br>+<br>+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 2);<br>+<br>+ ast_test_validate(test, fetched_payload != NULL,<br>+ "Failed to get only payload from buffer given valid arguments");<br>+<br>+ ast_data_buffer_put(buffer, 2, payload);<br>+<br>+ ast_test_validate(test, ast_data_buffer_count(buffer) == 1,<br>+ "Adding a payload that is already in the buffer should not do anything");<br>+<br>+ payload = ast_calloc(1, sizeof(*payload));<br>+<br>+ ast_test_validate(test, payload != NULL,<br>+ "Failed to allocate memory for second payload");<br>+<br>+ payload->id = 1;<br>+ ast_data_buffer_put(buffer, 1, payload);<br>+ fetched_payload = ast_data_buffer_get(buffer, 1);<br>+<br>+ ast_test_validate(test, fetched_payload != NULL,<br>+ "Failed to get a payload from buffer given valid arguments");<br>+ ast_test_validate(test, ast_data_buffer_count(buffer) == 2,<br>+ "Buffer does not have the expected count after removing a payload");<br>+ ast_test_validate(test, fetched_payload->id == 1,<br>+ "Did not get the expected payload from the buffer");<br>+<br>+ payload = ast_calloc(1, sizeof(*payload));<br>+<br>+ ast_test_validate(test, payload != NULL,<br>+ "Failed to allocate memory for third payload");<br>+<br>+ payload->id = 3;<br>+ ret = ast_data_buffer_put(buffer, 3, payload);<br>+<br>+ ast_test_validate(test, ret == 0,<br>+ "Failed to replace a payload in the buffer");<br>+ ast_test_validate(test, ast_data_buffer_count(buffer) <= 2,<br>+ "Buffer count exceeded the max");<br>+<br>+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 3);<br>+<br>+ ast_test_validate(test, fetched_payload != NULL,<br>+ "Failed to get a payload from buffer at position 3 given valid arguments");<br>+ ast_test_validate(test, fetched_payload->id == 3,<br>+ "Did not get the expected payload at position 3 from the buffer");<br>+<br>+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 2);<br>+<br>+ ast_test_validate(test, fetched_payload != NULL,<br>+ "Failed to get a payload from buffer at position 2 given valid arguments");<br>+ ast_test_validate(test, fetched_payload->id == 2,<br>+ "Did not get the expected payload at position 2 from the buffer");<br>+<br>+ return AST_TEST_PASS;<br>+}<br>+<br>+AST_TEST_DEFINE(buffer_resize)<br>+{<br>+ RAII_VAR(struct ast_data_buffer *, buffer, NULL, ast_data_buffer_free_wrapper);<br>+<br>+ switch (cmd) {<br>+ case TEST_INIT:<br>+ info->name = "buffer_resize";<br>+ info->category = "/main/data_buffer/";<br>+ info->summary = "buffer resize unit test";<br>+ info->description =<br>+ "Tests resizing a data buffer to make sure it has the expected outcome";<br>+ return AST_TEST_NOT_RUN;<br>+ case TEST_EXECUTE:<br>+ break;<br>+ }<br>+<br>+ buffer = ast_data_buffer_alloc(ast_free_ptr, BUFFER_MAX_NOMINAL);<br>+<br>+ ast_test_validate(test, buffer != NULL,<br>+ "Failed to create buffer with valid arguments");<br>+<br>+ ast_data_buffer_resize(buffer, BUFFER_MAX_NOMINAL);<br>+<br>+ ast_test_validate(test, ast_data_buffer_max(buffer) == BUFFER_MAX_NOMINAL,<br>+ "Trying to resize buffer to same size should not change its max size");<br>+<br>+ ast_data_buffer_resize(buffer, BUFFER_MAX_NOMINAL + 2);<br>+<br>+ ast_test_validate(test, ast_data_buffer_max(buffer) == BUFFER_MAX_NOMINAL + 2,<br>+ "Increasing buffer size did not return the expected max");<br>+<br>+ ast_data_buffer_resize(buffer, 1);<br>+<br>+ ast_test_validate(test, ast_data_buffer_max(buffer) == 1,<br>+ "Decreasing buffer size did not return the expected max");<br>+<br>+ return AST_TEST_PASS;<br>+}<br>+<br>+AST_TEST_DEFINE(buffer_nominal)<br>+{<br>+ RAII_VAR(struct ast_data_buffer *, buffer, NULL, ast_data_buffer_free_wrapper);<br>+ struct mock_payload *payload;<br>+ struct mock_payload *fetched_payload;<br>+ int ret;<br>+ int i;<br>+<br>+ switch (cmd) {<br>+ case TEST_INIT:<br>+ info->name = "buffer_nominal";<br>+ info->category = "/main/data_buffer/";<br>+ info->summary = "buffer nominal unit test";<br>+ info->description =<br>+ "Tests the normal usage of a data buffer to ensure the expected payloads "<br>+ "are present after multiple insertions";<br>+ return AST_TEST_NOT_RUN;<br>+ case TEST_EXECUTE:<br>+ break;<br>+ }<br>+<br>+ buffer = ast_data_buffer_alloc(ast_free_ptr, BUFFER_MAX_NOMINAL);<br>+<br>+ ast_test_validate(test, buffer != NULL,<br>+ "Failed to create buffer with valid arguments");<br>+<br>+ for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {<br>+ payload = ast_calloc(1, sizeof(*payload));<br>+<br>+ ast_test_validate(test, payload != NULL,<br>+ "Failed to allocate memory for payload %d", i);<br>+<br>+ ret = ast_data_buffer_put(buffer, i, payload);<br>+<br>+ ast_test_validate(test, ret == 0,<br>+ "Failed to add payload %d to buffer", i);<br>+ }<br>+<br>+ ast_test_validate(test, ast_data_buffer_count(buffer) == BUFFER_MAX_NOMINAL,<br>+ "Buffer does not have the expected count after adding payloads");<br>+<br>+ for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {<br>+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i);<br>+<br>+ ast_test_validate(test, fetched_payload != NULL,<br>+ "Failed to get payload at position %d during first loop", i);<br>+ }<br>+<br>+ for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {<br>+ payload = ast_calloc(1, sizeof(*payload));<br>+<br>+ ast_test_validate(test, payload != NULL,<br>+ "Failed to allocate memory for payload %d", i + BUFFER_MAX_NOMINAL);<br>+<br>+ ret = ast_data_buffer_put(buffer, i + BUFFER_MAX_NOMINAL, payload);<br>+<br>+ ast_test_validate(test, ret == 0,<br>+ "Failed to add payload %d to buffer", i + BUFFER_MAX_NOMINAL);<br>+ }<br>+<br>+ ast_test_validate(test, ast_data_buffer_count(buffer) == BUFFER_MAX_NOMINAL,<br>+ "Buffer does not have the expected count after replacing payloads");<br>+<br>+ for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {<br>+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i);<br>+<br>+ ast_test_validate(test, fetched_payload == NULL,<br>+ "Got an unexpected payload at position %d", i);<br>+<br>+ fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i + BUFFER_MAX_NOMINAL);<br>+<br>+ ast_test_validate(test, fetched_payload != NULL,<br>+ "Failed to get payload at position %d during second loop", i + BUFFER_MAX_NOMINAL);<br>+ }<br>+<br>+ return AST_TEST_PASS;<br>+}<br>+<br>+static int unload_module(void)<br>+{<br>+ AST_TEST_UNREGISTER(buffer_create);<br>+ AST_TEST_UNREGISTER(buffer_put);<br>+ AST_TEST_UNREGISTER(buffer_resize);<br>+ AST_TEST_UNREGISTER(buffer_nominal);<br>+ return 0;<br>+}<br>+<br>+static int load_module(void)<br>+{<br>+ AST_TEST_REGISTER(buffer_create);<br>+ AST_TEST_REGISTER(buffer_put);<br>+ AST_TEST_REGISTER(buffer_resize);<br>+ AST_TEST_REGISTER(buffer_nominal);<br>+ return AST_MODULE_LOAD_SUCCESS;<br>+}<br>+<br>+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Data buffer API test module");<br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/8607">change 8607</a>. To unsubscribe, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/8607"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: Id5b599aa15a5e61d0ec080f97cd0c57bd07e6f8f </div>
<div style="display:none"> Gerrit-Change-Number: 8607 </div>
<div style="display:none"> Gerrit-PatchSet: 23 </div>
<div style="display:none"> Gerrit-Owner: Benjamin Keith Ford <bford@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins2 </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Matthew Fredrickson <creslin@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Sean Bright <sean.bright@gmail.com> </div>