From: Ivan Grokhotkov Date: Mon, 15 Apr 2019 13:07:38 +0000 (+0800) Subject: test: clean up "leaks" tags from most unit tests X-Git-Tag: v4.0-beta1~395^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bb4f5617ab82a5d36ed753e53ca8eed93478594d;p=esp-idf test: clean up "leaks" tags from most unit tests --- diff --git a/components/driver/test/test_uart.c b/components/driver/test/test_uart.c index 045c993b7d..55a41f43bf 100644 --- a/components/driver/test/test_uart.c +++ b/components/driver/test/test_uart.c @@ -118,7 +118,7 @@ static void uart_config(uint32_t baud_rate, bool use_ref_tick) uart_driver_install(UART_NUM1, BUF_SIZE * 2, BUF_SIZE * 2, 20, NULL, 0); } -TEST_CASE("test uart get baud-rate","[uart]") +TEST_CASE("test uart get baud-rate", "[uart]") { uint32_t baud_rate1 = 0; uint32_t baud_rate2 = 0; @@ -136,13 +136,13 @@ TEST_CASE("test uart get baud-rate","[uart]") ESP_LOGI(UART_TAG, "get baud-rate test passed ....\n"); } -TEST_CASE("test uart tx data with break","[uart][leaks=2192]") +TEST_CASE("test uart tx data with break", "[uart]") { const int buf_len = 200; const int send_len = 128; const int brk_len = 10; char *psend = (char *)malloc(buf_len); - TEST_ASSERT( psend != NULL); + TEST_ASSERT(psend != NULL); memset(psend, '0', buf_len); uart_config(UART_BAUD_115200, false); printf("Uart%d send %d bytes with break\n", UART_NUM1, send_len); @@ -151,6 +151,7 @@ TEST_CASE("test uart tx data with break","[uart][leaks=2192]") //If the code is running here, it means the test passed, otherwise it will crash due to the interrupt wdt timeout. printf("Send data with break test passed\n"); free(psend); + uart_driver_delete(UART_NUM1); } // Calculate buffer checksum using tables diff --git a/components/esp_event/test/test_event.c b/components/esp_event/test/test_event.c index c0876657a2..b26877342a 100644 --- a/components/esp_event/test/test_event.c +++ b/components/esp_event/test/test_event.c @@ -868,7 +868,7 @@ TEST_CASE("performance test - dedicated task", "[event]") performance_test(true); } -TEST_CASE("performance test - no dedicated task", "[event][leaks=2736]") +TEST_CASE("performance test - no dedicated task", "[event]") { performance_test(false); } diff --git a/components/esp_wifi/test/test_phy_rtc.c b/components/esp_wifi/test/test_phy_rtc.c index e755ccb418..9506599248 100644 --- a/components/esp_wifi/test/test_phy_rtc.c +++ b/components/esp_wifi/test/test_phy_rtc.c @@ -44,6 +44,7 @@ static void test_phy_rtc_init(void) //must run here, not blocking in above code TEST_ASSERT(1); + nvs_flash_deinit(); } static IRAM_ATTR void test_phy_rtc_cache_task(void *arg) @@ -77,7 +78,7 @@ static IRAM_ATTR void test_phy_rtc_cache_task(void *arg) vTaskDelete(NULL); } -TEST_CASE("Test PHY/RTC functions called when cache is disabled", "[phy_rtc][cache_disabled][leaks=1216]") +TEST_CASE("Test PHY/RTC functions called when cache is disabled", "[phy_rtc][cache_disabled]") { semphr_done = xSemaphoreCreateCounting(1, 0); diff --git a/components/fatfs/test/test_fatfs_rawflash.c b/components/fatfs/test/test_fatfs_rawflash.c index 717a314ac0..5e60bb2965 100644 --- a/components/fatfs/test/test_fatfs_rawflash.c +++ b/components/fatfs/test/test_fatfs_rawflash.c @@ -69,7 +69,7 @@ TEST_CASE("(raw) can read file", "[fatfs]") test_teardown(); } -TEST_CASE("(raw) can open maximum number of files", "[fatfs][leaks=2028]") +TEST_CASE("(raw) can open maximum number of files", "[fatfs]") { size_t max_files = FOPEN_MAX - 3; /* account for stdin, stdout, stderr */ test_setup(max_files); diff --git a/components/fatfs/test/test_fatfs_sdmmc.c b/components/fatfs/test/test_fatfs_sdmmc.c index b0b2d05139..e1a6cfb3bd 100644 --- a/components/fatfs/test/test_fatfs_sdmmc.c +++ b/components/fatfs/test/test_fatfs_sdmmc.c @@ -158,7 +158,7 @@ TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs] test_teardown(); } -TEST_CASE("(SD) multiple tasks can use same volume", "[fatfs][test_env=UT_T1_SDMODE][leaks=1264]") +TEST_CASE("(SD) multiple tasks can use same volume", "[fatfs][test_env=UT_T1_SDMODE]") { test_setup(); test_fatfs_concurrent("/sdcard/f"); @@ -167,7 +167,7 @@ TEST_CASE("(SD) multiple tasks can use same volume", "[fatfs][test_env=UT_T1_SDM static void speed_test(void* buf, size_t buf_size, size_t file_size, bool write); -TEST_CASE("(SD) write/read speed test", "[fatfs][sd][test_env=UT_T1_SDMODE][timeout=60][leaks=1080]") +TEST_CASE("(SD) write/read speed test", "[fatfs][sd][test_env=UT_T1_SDMODE][timeout=60]") { size_t heap_size; HEAP_SIZE_CAPTURE(heap_size); diff --git a/components/fatfs/test/test_fatfs_spiflash.c b/components/fatfs/test/test_fatfs_spiflash.c index 9b95ddba8b..6c33559395 100644 --- a/components/fatfs/test/test_fatfs_spiflash.c +++ b/components/fatfs/test/test_fatfs_spiflash.c @@ -70,7 +70,7 @@ TEST_CASE("(WL) can read file", "[fatfs][wear_levelling]") test_teardown(); } -TEST_CASE("(WL) can open maximum number of files", "[fatfs][wear_levelling][leaks=2460]") +TEST_CASE("(WL) can open maximum number of files", "[fatfs][wear_levelling]") { size_t max_files = FOPEN_MAX - 3; /* account for stdin, stdout, stderr */ esp_vfs_fat_sdmmc_mount_config_t mount_config = { @@ -152,14 +152,14 @@ TEST_CASE("(WL) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs] test_teardown(); } -TEST_CASE("(WL) multiple tasks can use same volume", "[fatfs][wear_levelling][leaks=1340]") +TEST_CASE("(WL) multiple tasks can use same volume", "[fatfs][wear_levelling]") { test_setup(); test_fatfs_concurrent("/spiflash/f"); test_teardown(); } -TEST_CASE("(WL) write/read speed test", "[fatfs][wear_levelling][timeout=60][leaks=1156]") +TEST_CASE("(WL) write/read speed test", "[fatfs][wear_levelling][timeout=60]") { /* Erase partition before running the test to get consistent results */ const esp_partition_t* part = get_test_data_partition(); diff --git a/components/newlib/platform_include/esp_newlib.h b/components/newlib/platform_include/esp_newlib.h index 31adf7d847..2c51051693 100644 --- a/components/newlib/platform_include/esp_newlib.h +++ b/components/newlib/platform_include/esp_newlib.h @@ -25,6 +25,11 @@ */ void esp_reent_init(struct _reent* r); +/** + * Clean up some of lazily allocated buffers in REENT structures. + */ +void esp_reent_cleanup(); + /** * Function which sets up syscall table used by newlib functions in ROM. * diff --git a/components/newlib/reent_init.c b/components/newlib/reent_init.c index 638d10be29..412a793933 100644 --- a/components/newlib/reent_init.c +++ b/components/newlib/reent_init.c @@ -13,6 +13,9 @@ // limitations under the License. #include +#include +#include +#include #include #include "esp_attr.h" @@ -42,3 +45,64 @@ void IRAM_ATTR esp_reent_init(struct _reent* r) r->__sglue._niobs = 0; r->__sglue._iobs = NULL; } + +/* only declared in private stdio header file, local.h */ +extern void __sfp_lock_acquire(); +extern void __sfp_lock_release(); + +void esp_reent_cleanup() +{ + struct _reent* r = __getreent(); + /* Clean up storage used by mprec functions */ + if (r->_mp) { + if (_REENT_MP_FREELIST(r)) { + for (int i = 0; i < _Kmax; ++i) { + struct _Bigint *cur, *next; + next = _REENT_MP_FREELIST(r)[i]; + while (next) { + cur = next; + next = next->_next; + free(cur); + } + } + } + free(_REENT_MP_FREELIST(r)); + free(_REENT_MP_RESULT(r)); + } + + /* Clean up "glue" (lazily-allocated FILE objects) */ + struct _glue* prev = &_GLOBAL_REENT->__sglue; + for (struct _glue* cur = _GLOBAL_REENT->__sglue._next; cur != NULL;) { + if (cur->_niobs == 0) { + cur = cur->_next; + continue; + } + bool has_open_files = false; + for (int i = 0; i < cur->_niobs; ++i) { + FILE* fp = &cur->_iobs[i]; + if (fp->_flags != 0) { + has_open_files = true; + break; + } + } + if (has_open_files) { + prev = cur; + cur = cur->_next; + continue; + } + struct _glue* next = cur->_next; + prev->_next = next; + free(cur); + cur = next; + } + + /* Clean up various other buffers */ + free(r->_mp); + r->_mp = NULL; + free(r->_r48); + r->_r48 = NULL; + free(r->_localtime_buf); + r->_localtime_buf = NULL; + free(r->_asctime_buf); + r->_asctime_buf = NULL; +} diff --git a/components/spiffs/test/test_spiffs.c b/components/spiffs/test/test_spiffs.c index 32ab922db8..b5dc129e31 100644 --- a/components/spiffs/test/test_spiffs.c +++ b/components/spiffs/test/test_spiffs.c @@ -531,7 +531,7 @@ TEST_CASE("can read file", "[spiffs]") test_teardown(); } -TEST_CASE("can open maximum number of files", "[spiffs][leaks=2244]") +TEST_CASE("can open maximum number of files", "[spiffs]") { size_t max_files = FOPEN_MAX - 3; /* account for stdin, stdout, stderr */ esp_vfs_spiffs_conf_t conf = { @@ -602,7 +602,7 @@ TEST_CASE("readdir with large number of files", "[spiffs][timeout=30]") test_teardown(); } -TEST_CASE("multiple tasks can use same volume", "[spiffs][leaks=1128]") +TEST_CASE("multiple tasks can use same volume", "[spiffs]") { test_setup(); test_spiffs_concurrent("/spiffs/f"); diff --git a/tools/unit-test-app/components/test_utils/test_runner.c b/tools/unit-test-app/components/test_utils/test_runner.c index 7f1fdb607c..33247904c3 100644 --- a/tools/unit-test-app/components/test_utils/test_runner.c +++ b/tools/unit-test-app/components/test_utils/test_runner.c @@ -19,6 +19,7 @@ #include "freertos/task.h" #include "unity.h" #include "test_utils.h" +#include "esp_newlib.h" #ifdef CONFIG_HEAP_TRACING #include "esp_heap_trace.h" @@ -121,6 +122,9 @@ void tearDown(void) /* some FreeRTOS stuff is cleaned up by idle task */ vTaskDelay(5); + /* clean up some of the newlib's lazy allocations */ + esp_reent_cleanup(); + size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); /* We want the teardown to have this file in the printout if TEST_ASSERT fails */