[修改] 增加freeRTOS
1. 版本FreeRTOSv202212.01,命名为kernel;
This commit is contained in:
@ -0,0 +1,102 @@
|
||||
cmake_minimum_required( VERSION 3.13.0 )
|
||||
project( "backoffAlgorithm unit test"
|
||||
VERSION 1.0.0
|
||||
LANGUAGES C )
|
||||
|
||||
# Allow the project to be organized into folders.
|
||||
set_property( GLOBAL PROPERTY USE_FOLDERS ON )
|
||||
|
||||
# Use C90.
|
||||
set( CMAKE_C_STANDARD 90 )
|
||||
set( CMAKE_C_STANDARD_REQUIRED ON )
|
||||
|
||||
# Do not allow in-source build.
|
||||
if( ${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR} )
|
||||
message( FATAL_ERROR "In-source build is not allowed. Please build in a separate directory, such as ${PROJECT_SOURCE_DIR}/build." )
|
||||
endif()
|
||||
|
||||
# Set global path variables.
|
||||
get_filename_component(__MODULE_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
|
||||
set( MODULE_ROOT_DIR ${__MODULE_ROOT_DIR} CACHE INTERNAL "backoffAlgorithm source root." )
|
||||
set( UNIT_TEST_DIR ${MODULE_ROOT_DIR}/test/unit-test CACHE INTERNAL "backoffAlgorithm unit test directory." )
|
||||
set( UNITY_DIR ${UNIT_TEST_DIR}/Unity CACHE INTERNAL "Unity library source directory." )
|
||||
|
||||
# Configure options to always show in CMake GUI.
|
||||
option( BUILD_UNIT_TESTS
|
||||
"Set this to ON to build unit tests. This will clone the required Unity test framework submodule if it is not cloned already."
|
||||
OFF )
|
||||
|
||||
# Set output directories.
|
||||
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
|
||||
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
|
||||
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
|
||||
|
||||
# ================================ Coverity Analysis Configuration =================================
|
||||
|
||||
# Include filepaths for source and include.
|
||||
include( ${MODULE_ROOT_DIR}/backoffAlgorithmFilePaths.cmake )
|
||||
|
||||
# Target for Coverity analysis that builds the library.
|
||||
add_library( coverity_analysis
|
||||
${BACKOFF_ALGORITHM_SOURCES} )
|
||||
|
||||
# Backoff Algorithm library public include path.
|
||||
target_include_directories( coverity_analysis
|
||||
PUBLIC
|
||||
${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} )
|
||||
|
||||
# Disable logging/assert() calls when building the Coverity analysis target
|
||||
target_compile_options(coverity_analysis PUBLIC -DNDEBUG )
|
||||
|
||||
# ==================================== Unit Test Configuration ====================================
|
||||
|
||||
if(${BUILD_CODE_EXAMPLE})
|
||||
|
||||
# Target for code example binary.
|
||||
add_executable( code_example_posix
|
||||
${MODULE_ROOT_DIR}/docs/doxygen/code_examples/backoff_algorithm_posix.c )
|
||||
|
||||
target_link_libraries( code_example_posix coverity_analysis )
|
||||
|
||||
endif()
|
||||
|
||||
# ==================================== Unit Test Configuration ====================================
|
||||
|
||||
if(${BUILD_UNIT_TESTS})
|
||||
|
||||
# Include Unity build configuration.
|
||||
include( unit-test/unity_build.cmake )
|
||||
|
||||
# Check if the Unity source directory exists, and if not present, clone the submodule
|
||||
# if BUILD_CLONE_SUBMODULES configuration is enabled.
|
||||
if( NOT EXISTS ${UNITY_DIR}/src )
|
||||
# Attempt to clone Unity.
|
||||
clone_unity()
|
||||
endif()
|
||||
|
||||
# Add unit test and coverage configuration.
|
||||
|
||||
# Use CTest utility for managing test runs. This has to be added BEFORE
|
||||
# defining test targets with add_test()
|
||||
enable_testing()
|
||||
|
||||
# Add build targets for Unity and Unit, required for unit testing.
|
||||
add_unity_targets()
|
||||
|
||||
# Add function to enable Unity based tests and coverage.
|
||||
include( ${MODULE_ROOT_DIR}/tools/unity/create_test.cmake )
|
||||
|
||||
# Include build configuration for unit tests.
|
||||
add_subdirectory( unit-test )
|
||||
|
||||
# ==================================== Coverage Analysis configuration ============================
|
||||
|
||||
# Add a target for running coverage on tests.
|
||||
add_custom_target( coverage
|
||||
COMMAND ${CMAKE_COMMAND} -DUNITY_DIR=${UNITY_DIR}
|
||||
-P ${MODULE_ROOT_DIR}/tools/unity/coverage.cmake
|
||||
DEPENDS unity backoff_algorithm_utest
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
)
|
||||
|
||||
endif()
|
||||
@ -0,0 +1,71 @@
|
||||
# Include file path configuration for backoff algorithm library.
|
||||
include(${MODULE_ROOT_DIR}/backoffAlgorithmFilePaths.cmake)
|
||||
|
||||
project ("backoff algorithm unit test")
|
||||
cmake_minimum_required (VERSION 3.2.0)
|
||||
|
||||
# ==================== Define your project name (edit) ========================
|
||||
set(project_name "backoff_algorithm")
|
||||
|
||||
# ===================== Create your mock here (edit) ========================
|
||||
|
||||
# list the files to mock here
|
||||
list(APPEND mock_list
|
||||
""
|
||||
)
|
||||
|
||||
# list the directories your mocks need
|
||||
list(APPEND mock_include_list
|
||||
""
|
||||
)
|
||||
#list the definitions of your mocks to control what to be included
|
||||
list(APPEND mock_define_list
|
||||
""
|
||||
)
|
||||
|
||||
# ================= Create the library under test here (edit) ==================
|
||||
|
||||
# list the files you would like to test here
|
||||
list(APPEND real_source_files
|
||||
${BACKOFF_ALGORITHM_SOURCES}
|
||||
)
|
||||
# list the directories the module under test includes
|
||||
list(APPEND real_include_directories
|
||||
${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS}
|
||||
)
|
||||
|
||||
# ===================== Create UnitTest Code here (edit) =====================
|
||||
|
||||
# list the directories your test needs to include
|
||||
list(APPEND test_include_directories
|
||||
${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS}
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
# ============================= (end edit) ===================================
|
||||
|
||||
set(mock_name "${project_name}_mock")
|
||||
set(real_name "${project_name}_real")
|
||||
|
||||
create_real_library(${real_name}
|
||||
"${real_source_files}"
|
||||
"${real_include_directories}"
|
||||
""
|
||||
)
|
||||
|
||||
list(APPEND utest_link_list
|
||||
lib${real_name}.a
|
||||
)
|
||||
|
||||
list(APPEND utest_dep_list
|
||||
${real_name}
|
||||
)
|
||||
|
||||
set(utest_name "${project_name}_utest")
|
||||
set(utest_source "${project_name}_utest.c")
|
||||
create_test(${utest_name}
|
||||
${utest_source}
|
||||
"${utest_link_list}"
|
||||
"${utest_dep_list}"
|
||||
"${test_include_directories}"
|
||||
)
|
||||
@ -0,0 +1,350 @@
|
||||
/*
|
||||
* backoffAlgorithm v1.3.0
|
||||
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Unity include. */
|
||||
#include "unity.h"
|
||||
|
||||
/* Include utility for catching assert failures. */
|
||||
#include "catch_assert.h"
|
||||
|
||||
/* Backoff Algorithm library include */
|
||||
#include "backoff_algorithm.h"
|
||||
|
||||
#define TEST_BACKOFF_BASE_VALUE ( 1000 )
|
||||
#define TEST_BACKOFF_MAX_VALUE ( 10000 )
|
||||
#define TEST_MAX_ATTEMPTS ( 5 )
|
||||
/* Parameters to track the next max jitter or number of attempts done. */
|
||||
static BackoffAlgorithmContext_t retryParams;
|
||||
/* Return value of #BackoffAlgorithm_GetNextBackoff. */
|
||||
static BackoffAlgorithmStatus_t BackoffAlgorithmStatus;
|
||||
static uint16_t nextBackoff;
|
||||
static uint32_t testRandomVal;
|
||||
|
||||
/* ============================ UNITY FIXTURES ============================ */
|
||||
|
||||
/* Called before each test method. */
|
||||
void setUp()
|
||||
{
|
||||
/* Initialize context. */
|
||||
BackoffAlgorithm_InitializeParams( &retryParams,
|
||||
TEST_BACKOFF_BASE_VALUE,
|
||||
TEST_BACKOFF_MAX_VALUE,
|
||||
TEST_MAX_ATTEMPTS );
|
||||
}
|
||||
|
||||
/* Called after each test method. */
|
||||
void tearDown()
|
||||
{
|
||||
testRandomVal = 0;
|
||||
BackoffAlgorithmStatus = BackoffAlgorithmSuccess;
|
||||
nextBackoff = 0;
|
||||
}
|
||||
|
||||
/* Called at the beginning of the whole suite. */
|
||||
void suiteSetUp()
|
||||
{
|
||||
}
|
||||
|
||||
/* Called at the end of the whole suite. */
|
||||
int suiteTearDown( int numFailures )
|
||||
{
|
||||
return numFailures;
|
||||
}
|
||||
|
||||
/* ========================================================================== */
|
||||
|
||||
/**
|
||||
* @brief Helper method to make assertions on data of the context object.
|
||||
*/
|
||||
static void verifyContextData( BackoffAlgorithmContext_t * pContext,
|
||||
uint32_t expectedAttemptsDone,
|
||||
uint16_t expectedNextJitterMax,
|
||||
uint16_t expectedMaxBackoff,
|
||||
uint32_t expectedMaxAttempts )
|
||||
{
|
||||
TEST_ASSERT_EQUAL( expectedNextJitterMax, pContext->nextJitterMax );
|
||||
TEST_ASSERT_EQUAL( expectedAttemptsDone, pContext->attemptsDone );
|
||||
TEST_ASSERT_EQUAL( expectedMaxAttempts, pContext->maxRetryAttempts );
|
||||
TEST_ASSERT_EQUAL( expectedMaxBackoff, pContext->maxBackoffDelay );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test that #BackoffAlgorithm_InitializeParams encounters an assert
|
||||
* failure when a NULL context parameter is passed.
|
||||
*/
|
||||
void test_BackoffAlgorithm_InitializeParams_Invalid_Context( void )
|
||||
{
|
||||
catch_assert( BackoffAlgorithm_InitializeParams( NULL /* Invalid context */,
|
||||
TEST_BACKOFF_BASE_VALUE,
|
||||
TEST_BACKOFF_MAX_VALUE,
|
||||
TEST_MAX_ATTEMPTS ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test that #BackoffAlgorithm_InitializeParams initializes the context
|
||||
* data as expected.
|
||||
*/
|
||||
void test_BackoffAlgorithm_InitializeParams_Sets_Jitter_Correctly( void )
|
||||
{
|
||||
BackoffAlgorithm_InitializeParams( &retryParams,
|
||||
TEST_BACKOFF_BASE_VALUE,
|
||||
TEST_BACKOFF_MAX_VALUE,
|
||||
TEST_MAX_ATTEMPTS );
|
||||
verifyContextData( &retryParams,
|
||||
0,
|
||||
TEST_BACKOFF_BASE_VALUE,
|
||||
TEST_BACKOFF_MAX_VALUE,
|
||||
TEST_MAX_ATTEMPTS );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test that #BackoffAlgorithm_GetNextBackoff returns the expected next back-off
|
||||
* value and updates the contents of the context as expected when the random value
|
||||
* generated is lower than the max jitter value for the retry attempts.
|
||||
*
|
||||
* This test assumes that the max jitter value has not reached the configured
|
||||
* maximum back-off value.
|
||||
*/
|
||||
void test_BackoffAlgorithm_GetNextBackoff_Success_RandVal_Less_Than_Jitter_Max( void )
|
||||
{
|
||||
int iter = 1;
|
||||
uint16_t expectedAttemptsDone = 0;
|
||||
uint16_t expectedNextJitterMax = TEST_BACKOFF_BASE_VALUE;
|
||||
uint16_t expectedNextBackoff = 0;
|
||||
|
||||
for( ; iter < 2; iter++ )
|
||||
{
|
||||
/* Set the random value as a value lower than
|
||||
* the jitter max value for the next retry attempt. */
|
||||
testRandomVal = retryParams.nextJitterMax / 2;
|
||||
|
||||
/* As the random value is less than the max jitter value, the expected
|
||||
* next backoff value should remain the same as the random value. */
|
||||
expectedNextBackoff = testRandomVal;
|
||||
|
||||
/* The jitter max value should double with the above call for use in next call. */
|
||||
expectedNextJitterMax *= 2;
|
||||
|
||||
/* The number of attempts should be updated by the above API call. */
|
||||
expectedAttemptsDone++;
|
||||
|
||||
TEST_ASSERT_LESS_THAN( TEST_BACKOFF_MAX_VALUE, expectedNextJitterMax );
|
||||
|
||||
/* Call the BackoffAlgorithm_GetNextBackoff API a couple times. */
|
||||
TEST_ASSERT_EQUAL( BackoffAlgorithmSuccess,
|
||||
BackoffAlgorithm_GetNextBackoff( &retryParams, testRandomVal, &nextBackoff ) );
|
||||
TEST_ASSERT_EQUAL( expectedNextBackoff, nextBackoff );
|
||||
|
||||
/* Verify that the context data for expected data after the API call. */
|
||||
verifyContextData( &retryParams,
|
||||
expectedAttemptsDone,
|
||||
expectedNextJitterMax,
|
||||
TEST_BACKOFF_MAX_VALUE,
|
||||
TEST_MAX_ATTEMPTS );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test that #BackoffAlgorithm_GetNextBackoff returns the expected next back-off
|
||||
* value and updates the contents of the context when the random value generated
|
||||
* is higher than the max jitter value for the retry attempts.
|
||||
*
|
||||
* This test assumes that the max jitter value has not reached the configured
|
||||
* maximum back-off value.
|
||||
*/
|
||||
void test_BackoffAlgorithm_GetNextBackoff_Success_RandVal_More_Than_Jitter_Max( void )
|
||||
{
|
||||
int iter = 1;
|
||||
uint16_t expectedAttemptsDone = 0;
|
||||
uint16_t expectedNextJitterMax = TEST_BACKOFF_BASE_VALUE;
|
||||
|
||||
for( ; iter < 2; iter++ )
|
||||
{
|
||||
/* Set the random value as a value greater than
|
||||
* the jitter max value for the next retry attempt. */
|
||||
testRandomVal = retryParams.nextJitterMax + 1;
|
||||
|
||||
/* The jitter max value should double with the above call for use in next call. */
|
||||
expectedNextJitterMax *= 2;
|
||||
TEST_ASSERT_LESS_THAN( TEST_BACKOFF_MAX_VALUE, expectedNextJitterMax );
|
||||
|
||||
/* The number of attempts should be updated by the above API call. */
|
||||
expectedAttemptsDone++;
|
||||
|
||||
/* As the random value is greater than the jitter max value, the expected
|
||||
* next backoff value should be truncated to a value within the jitter window
|
||||
* for the retry attempt. */
|
||||
uint16_t expectedNextBackoff = ( testRandomVal % ( retryParams.nextJitterMax + 1U ) );
|
||||
|
||||
/* Call the BackoffAlgorithm_GetNextBackoff API a couple times. */
|
||||
TEST_ASSERT_EQUAL( BackoffAlgorithmSuccess,
|
||||
BackoffAlgorithm_GetNextBackoff( &retryParams, testRandomVal, &nextBackoff ) );
|
||||
TEST_ASSERT_EQUAL( expectedNextBackoff, nextBackoff );
|
||||
|
||||
/* Verify that the context data for expected data after the API call. */
|
||||
verifyContextData( &retryParams,
|
||||
expectedAttemptsDone,
|
||||
expectedNextJitterMax,
|
||||
TEST_BACKOFF_MAX_VALUE,
|
||||
TEST_MAX_ATTEMPTS );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Tests the #BackoffAlgorithm_GetNextBackoff API when the next back-off value
|
||||
* is requested for exhausted retry attempts.
|
||||
*/
|
||||
void test_BackoffAlgorithm_GetNextBackoff_Attempts_Exhausted()
|
||||
{
|
||||
/* Update the context data to represent that maximum configured number of
|
||||
* retry attempts have been completed. */
|
||||
retryParams.attemptsDone = TEST_MAX_ATTEMPTS;
|
||||
|
||||
/* Call the BackoffAlgorithm_GetNextBackoff API. */
|
||||
TEST_ASSERT_EQUAL( BackoffAlgorithmRetriesExhausted,
|
||||
BackoffAlgorithm_GetNextBackoff( &retryParams, testRandomVal, &nextBackoff ) );
|
||||
/* Make sure that the value of the output parameter has not changed. */
|
||||
TEST_ASSERT_EQUAL( 0, nextBackoff );
|
||||
|
||||
/* Make sure that the context data has not changed as the call to
|
||||
* BackoffAlgorithm_GetNextBackoff failed. */
|
||||
verifyContextData( &retryParams,
|
||||
TEST_MAX_ATTEMPTS /* Number of attempts shouldn't change */,
|
||||
TEST_BACKOFF_BASE_VALUE,
|
||||
TEST_BACKOFF_MAX_VALUE,
|
||||
TEST_MAX_ATTEMPTS );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Tests that the #BackoffAlgorithm_GetNextBackoff API does not calculate a backoff period
|
||||
* beyond the configured maximum backoff period.
|
||||
*/
|
||||
void test_BackoffAlgorithm_GetNextBackoff_Returns_Cap_Backoff( void )
|
||||
{
|
||||
/* Initialize to 0 attempts, so the max value of next jitter will increase. */
|
||||
retryParams.attemptsDone = 0U;
|
||||
|
||||
/* Update the next jitter value to greater than half the maximum backoff so
|
||||
* that the BackoffAlgorithm_GetNextBackoff API updates the next jitter value to
|
||||
* the configured maximum backoff value. */
|
||||
retryParams.nextJitterMax = ( TEST_BACKOFF_MAX_VALUE / 2U ) + 1;
|
||||
|
||||
/* Set the random value equal to the current jitter max value.
|
||||
* Thus, the BackoffAlgorithm_GetNextBackoff API should return the random value as
|
||||
* the next back-off value. */
|
||||
testRandomVal = retryParams.nextJitterMax;
|
||||
uint16_t expectedBackoffVal = testRandomVal;
|
||||
|
||||
/* Call the BackoffAlgorithm_GetNextBackoff API. */
|
||||
TEST_ASSERT_EQUAL( BackoffAlgorithmSuccess,
|
||||
BackoffAlgorithm_GetNextBackoff( &retryParams, testRandomVal, &nextBackoff ) );
|
||||
/* Make sure that the expected value is returned for the next backoff. */
|
||||
TEST_ASSERT_EQUAL( expectedBackoffVal, nextBackoff );
|
||||
|
||||
/* Verify that the next jitter max value has been set to the cap back-off value
|
||||
* configured in the context. */
|
||||
verifyContextData( &retryParams,
|
||||
1,
|
||||
TEST_BACKOFF_MAX_VALUE /* New jitter max */,
|
||||
TEST_BACKOFF_MAX_VALUE,
|
||||
TEST_MAX_ATTEMPTS );
|
||||
|
||||
|
||||
/* Now, set the random value as the maximum back-off value to
|
||||
* expect that the next back-off value returned by the API is the maximum
|
||||
* back-off value.*/
|
||||
testRandomVal = TEST_BACKOFF_MAX_VALUE;
|
||||
|
||||
/* Call BackoffAlgorithm_GetNextBackoff API again to verify that it now returns the
|
||||
* cap value as the next back-off value. */
|
||||
TEST_ASSERT_EQUAL( BackoffAlgorithmSuccess,
|
||||
BackoffAlgorithm_GetNextBackoff( &retryParams, testRandomVal, &nextBackoff ) );
|
||||
/* Make sure that the capped backoff value is returned as the next backoff value . */
|
||||
TEST_ASSERT_EQUAL( TEST_BACKOFF_MAX_VALUE, nextBackoff );
|
||||
|
||||
/* Verify that the context data for expected data after the API call. */
|
||||
verifyContextData( &retryParams,
|
||||
2,
|
||||
TEST_BACKOFF_MAX_VALUE /* jitter max remains unchanged */,
|
||||
TEST_BACKOFF_MAX_VALUE,
|
||||
TEST_MAX_ATTEMPTS );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Tests the #BackoffAlgorithm_GetNextBackoff API when the next jitter max value
|
||||
* is one lower than half of the maximum backoff value. This tests that the API does not
|
||||
* update the next jitter max to the maximum backoff value in this case.
|
||||
*/
|
||||
void test_BackoffAlgorithm_GetNextBackoff_NextJitterMax_Below_Cap_Backoff( void )
|
||||
{
|
||||
/* Initialize context.
|
||||
* Use the configuration constant of retrying forever to achieve branch coverage in tests
|
||||
* for the configuration. */
|
||||
BackoffAlgorithm_InitializeParams( &retryParams,
|
||||
TEST_BACKOFF_BASE_VALUE,
|
||||
TEST_BACKOFF_MAX_VALUE,
|
||||
BACKOFF_ALGORITHM_RETRY_FOREVER );
|
||||
|
||||
/* Initialize to 0 attempts, so the max value of next jitter will increase. */
|
||||
retryParams.attemptsDone = 0U;
|
||||
|
||||
/* Set the random value to zero to test that the BackoffAlgorithm_GetNextBackoff
|
||||
* API will return zero as the next back-off value.*/
|
||||
testRandomVal = 0;
|
||||
|
||||
/* Update the next jitter max value to one less than half of max backoff
|
||||
* to make sure that the BackoffAlgorithm_GetNextBackoff API does not update it
|
||||
* to the cap value in the call.*/
|
||||
retryParams.nextJitterMax = ( TEST_BACKOFF_MAX_VALUE / 2U ) - 1;
|
||||
|
||||
/* Call the BackoffAlgorithm_GetNextBackoff API. */
|
||||
TEST_ASSERT_EQUAL( BackoffAlgorithmSuccess,
|
||||
BackoffAlgorithm_GetNextBackoff( &retryParams, testRandomVal, &nextBackoff ) );
|
||||
/* Make sure that zero is returned as the next backoff value . */
|
||||
TEST_ASSERT_EQUAL( 0, nextBackoff );
|
||||
|
||||
/* Verify that the context data for expected data after the API call. */
|
||||
verifyContextData( &retryParams,
|
||||
1,
|
||||
TEST_BACKOFF_MAX_VALUE - 2U /* next jitter max value */,
|
||||
TEST_BACKOFF_MAX_VALUE,
|
||||
BACKOFF_ALGORITHM_RETRY_FOREVER );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Tests that the #BackoffAlgorithm_GetNextBackoff API encounters assert failures
|
||||
* when called with invalid parameters.
|
||||
*/
|
||||
void test_BackoffAlgorithm_GetNextBackoff_Invalid_Params()
|
||||
{
|
||||
/* Invalid context. */
|
||||
catch_assert( BackoffAlgorithm_GetNextBackoff( NULL, testRandomVal, &nextBackoff ) );
|
||||
|
||||
/* Invalid output parameter for next back-off. */
|
||||
catch_assert( BackoffAlgorithm_GetNextBackoff( &retryParams, testRandomVal, NULL ) );
|
||||
}
|
||||
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* backoffAlgorithm v1.3.0
|
||||
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* How to catch an assert:
|
||||
* - save a jump buffer where execution will resume after the assert
|
||||
* - setup a handler for the abort signal, call longjmp within
|
||||
* - optional - close stderr ( fd 2 ) to discard the assert message
|
||||
*
|
||||
* Unity also does a longjmp within its TEST_ASSERT* macros,
|
||||
* so the macro below restores stderr and the prior abort handler
|
||||
* before calling the Unity macro.
|
||||
*/
|
||||
|
||||
#ifndef CATCH_ASSERT_H_
|
||||
#define CATCH_ASSERT_H_
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifndef CATCH_JMPBUF
|
||||
#define CATCH_JMPBUF waypoint_
|
||||
#endif
|
||||
|
||||
jmp_buf CATCH_JMPBUF;
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-function"
|
||||
static void catchHandler_( int signal )
|
||||
{
|
||||
longjmp( CATCH_JMPBUF, signal );
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#define catch_assert( x ) \
|
||||
do { \
|
||||
int try = 0, catch = 0; \
|
||||
int saveFd = dup( 2 ); \
|
||||
struct sigaction sa = { 0 }, saveSa; \
|
||||
sa.sa_handler = catchHandler_; \
|
||||
sigaction( SIGABRT, &sa, &saveSa ); \
|
||||
close( 2 ); \
|
||||
if( setjmp( CATCH_JMPBUF ) == 0 ) \
|
||||
{ \
|
||||
try++; \
|
||||
x; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
catch++; \
|
||||
} \
|
||||
sigaction( SIGABRT, &saveSa, NULL ); \
|
||||
dup2( saveFd, 2 ); \
|
||||
close( saveFd ); \
|
||||
TEST_ASSERT_EQUAL( try, catch ); \
|
||||
} while( 0 )
|
||||
|
||||
#endif /* ifndef CATCH_ASSERT_H_ */
|
||||
@ -0,0 +1,38 @@
|
||||
# Macro utility to clone the Unity submodule.
|
||||
macro( clone_unity )
|
||||
find_package( Git REQUIRED )
|
||||
message( "Cloning submodule Unity." )
|
||||
execute_process( COMMAND rm -rf ${UNITY_DIR}
|
||||
COMMAND ${GIT_EXECUTABLE} submodule update --checkout --init --recursive ${UNITY_DIR}
|
||||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
|
||||
RESULT_VARIABLE UNITY_CLONE_RESULT )
|
||||
|
||||
if( NOT ${UNITY_CLONE_RESULT} STREQUAL "0" )
|
||||
message( FATAL_ERROR "Failed to clone Unity submodule." )
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
# Macro utility to add library targets for Unity and Unity to build configuration.
|
||||
macro( add_unity_targets )
|
||||
# Build Configuration for Unity and Unity libraries.
|
||||
list( APPEND UNITY_INCLUDE_DIRS
|
||||
"${UNITY_DIR}/src/"
|
||||
"${UNITY_DIR}/extras/fixture/src"
|
||||
"${UNITY_DIR}/extras/memory/src"
|
||||
)
|
||||
|
||||
add_library( unity STATIC
|
||||
"${UNITY_DIR}/src/unity.c"
|
||||
"${UNITY_DIR}/extras/fixture/src/unity_fixture.c"
|
||||
"${UNITY_DIR}/extras/memory/src/unity_memory.c"
|
||||
)
|
||||
|
||||
set_target_properties( unity PROPERTIES
|
||||
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
|
||||
POSITION_INDEPENDENT_CODE ON
|
||||
)
|
||||
|
||||
target_include_directories( unity PUBLIC
|
||||
${UNITY_INCLUDE_DIRS}
|
||||
)
|
||||
endmacro()
|
||||
Reference in New Issue
Block a user