refactor (test_utils)!: separate file for memory check functions

Memory check (leaks and heap tracing) functions for unit tests
now have a separate file now and are renamed for more consistency.

BREAKING CHANGE: renamed memory check function names which may be used
                 in unit tests outside IDF.
This commit is contained in:
Jakob Hasse
2021-11-18 14:27:30 +08:00
parent 2e1c7d876c
commit 16514f93f0
12 changed files with 307 additions and 231 deletions
@@ -0,0 +1,91 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Leak for components
*/
typedef enum {
ESP_COMP_LEAK_GENERAL = 0, /**< Leak by default */
ESP_COMP_LEAK_LWIP, /**< Leak for LWIP */
ESP_COMP_LEAK_NVS, /**< Leak for NVS */
ESP_COMP_LEAK_ALL, /**< Use for getting the summary leak level */
} esp_comp_leak_t;
/**
* @brief Type of a leak threshold
*/
typedef enum {
ESP_LEAK_TYPE_WARNING = 0, /**< Warning level of leak */
ESP_LEAK_TYPE_CRITICAL, /**< Critical level of leak */
ESP_LEAK_TYPE_MAX, /**< Max number of leak levels for all components/levels */
} esp_type_leak_t;
/**
* @brief Adjust the memory leak thresholds for unit tests.
*
* Usually, unit tests will check if memory is leaked. Some functionality used by unit tests may unavoidably
* leak memory. This is why there is a default threshold for memory leaks (currently 1024 bytes).
* Within this range, the number of bytes leaked will be visually reported on the terminal, but no test failure will
* be triggered. Any memory leak above the default threshold will trigger a unit test failure.
* This function allows to adjust that memory leak threshold.
*
* @param leak_level Maximum allowed memory leak which will not trigger a unit test failure.
* @param type_of_leak There are two types of leak thresholds: critical and warning. Only the
* critical threshold will trigger a unit test failure if exceeded.
* @param component Thresholds can be set in general or for specific components. Note that this argument
* is not checked.
*
* @return ESP_OK on success, ESP_INVALID_ARG if type_of_leak is invalid. \c component is unchecked.
*/
esp_err_t test_utils_set_leak_level(size_t leak_level, esp_type_leak_t type_of_leak, esp_comp_leak_t component);
/**
* @brief Return the memory leak thresholds for unit tests for a leak type and component.
*
* For more information, see \c test_utils_set_leak_level above.
*
* @param type_of_leak Warning or Critical
* @param component The component for which to return the leak threshold.
*/
size_t test_utils_get_leak_level(esp_type_leak_t type_of_leak, esp_comp_leak_t component);
/**
* @brief Start/Restart memory leak checking.
*
* Records the current free memory values at time of calling. After the test case, it may be checked with
* \c test_utils_finish_and_evaluate_leaks.
*
* If this function is called repeatedly, only the free memory values at the last time of calling will prevail
* as reference.
*/
void test_utils_record_free_mem(void);
/**
* @brief Evaluate memory leak checking according to the provided thresholds.
*
* If the current memory leak level (counted from the last time calling \c test_utils_record_free_mem() ) exceeds
* \c critical_threshold, a unit test failure will be triggered. If it exceeds only the warning,
* a warning message will be issued.
*/
void test_utils_finish_and_evaluate_leaks(size_t warn_threshold, size_t critical_threshold);
/**
* @brief Helper function to setup and initialize heap tracing.
*/
void setup_heap_record(void);
#ifdef __cplusplus
}
#endif
@@ -1,16 +1,9 @@
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
// Utilities for esp-idf unit tests
@@ -118,18 +111,6 @@ uint64_t ref_clock_get(void);
*/
void test_main(void);
/**
* @brief Reset automatic leak checking which happens in unit tests.
*
* Updates recorded "before" free memory values to the free memory values
* at time of calling. Resets leak checker if tracing is enabled in
* config.
*
* This can be called if a test case does something which allocates
* memory on first use, for example.
*
* @note Use with care as this can mask real memory leak problems.
*/
void unity_reset_leak_checks(void);
@@ -232,47 +213,6 @@ static inline void unity_send_signal(const char* signal_name)
*/
bool unity_util_convert_mac_from_string(const char* mac_str, uint8_t *mac_addr);
/**
* @brief Leak for components
*/
typedef enum {
COMP_LEAK_GENERAL = 0, /**< Leak by default */
COMP_LEAK_LWIP, /**< Leak for LWIP */
COMP_LEAK_NVS, /**< Leak for NVS */
COMP_LEAK_ALL, /**< Use for getting the summary leak level */
} esp_comp_leak_t;
/**
* @brief Type of leak
*/
typedef enum {
TYPE_LEAK_WARNING = 0, /**< Warning level of leak */
TYPE_LEAK_CRITICAL, /**< Critical level of leak */
TYPE_LEAK_MAX, /**< Max number of leak levels */
} esp_type_leak_t;
/**
* @brief Set a leak level for the required type and component.
*
* @param[in] leak_level Level of leak
* @param[in] type Type of leak
* @param[in] component Name of component
*
* return ESP_OK: Successful.
* ESP_ERR_INVALID_ARG: Invalid argument.
*/
esp_err_t test_utils_set_leak_level(size_t leak_level, esp_type_leak_t type, esp_comp_leak_t component);
/**
* @brief Get a leak level for the required type and component.
*
* @param[in] type Type of leak.
* @param[in] component Name of component. If COMP_LEAK_ALL, then the level will be summarized for all components.
* return Leak level
*/
size_t test_utils_get_leak_level(esp_type_leak_t type, esp_comp_leak_t component);
typedef struct test_utils_exhaust_memory_record_s *test_utils_exhaust_memory_rec;
/**