]> granicus.if.org Git - esp-idf/commitdiff
test: clean up "leaks" tags from most unit tests
authorIvan Grokhotkov <ivan@espressif.com>
Mon, 15 Apr 2019 13:07:38 +0000 (21:07 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Tue, 16 Apr 2019 12:07:40 +0000 (20:07 +0800)
components/driver/test/test_uart.c
components/esp_event/test/test_event.c
components/esp_wifi/test/test_phy_rtc.c
components/fatfs/test/test_fatfs_rawflash.c
components/fatfs/test/test_fatfs_sdmmc.c
components/fatfs/test/test_fatfs_spiflash.c
components/newlib/platform_include/esp_newlib.h
components/newlib/reent_init.c
components/spiffs/test/test_spiffs.c
tools/unit-test-app/components/test_utils/test_runner.c

index 045c993b7d52139215068d962372760c59d23cb7..55a41f43bf80821995f3604382ac16f5bea205be 100644 (file)
@@ -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 
index c0876657a2d0c0ea8cffa2d8c322ecae876f7a50..b26877342ad4726c3fc303381d88f43264914f09 100644 (file)
@@ -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);
 }
index e755ccb4182ece712bd03c1673011e58d7347a23..950659924882e14d6b8d5d48ec55777ca65415d4 100644 (file)
@@ -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);
 
index 717a314ac02531ea5b76d47a721141d28f82d16d..5e60bb2965b3e42b88a48dc5e5b6c4c3bd8f7ec8 100644 (file)
@@ -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);
index b0b2d0513910da4f6df1cc48a206a5a495287e16..e1a6cfb3bd6c0333778a6ac157069104185427a0 100644 (file)
@@ -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);
index 9b95ddba8ba3aa30b67ee31d883add761391c6c4..6c33559395e9ecc7560654f00c56a1161b2fe330 100644 (file)
@@ -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();
index 31adf7d84737cca575e9ded0d98695765d51c7b3..2c510516935ca5e3aee4c75e1a3110ab069cd23f 100644 (file)
  */
 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.
  *
index 638d10be29da6950e436207b0835dc29bed962e5..412a79393377d5661e28590df46972bfa2730af6 100644 (file)
@@ -13,6 +13,9 @@
 // limitations under the License.
 
 #include <string.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
 #include <sys/reent.h>
 #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;
+}
index 32ab922db8bed13d78b3bb52c4605a7b085a3fb9..b5dc129e3119cf1605566a687f19288c48413299 100644 (file)
@@ -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");
index 7f1fdb607c03f93ad3966bfa4c40c92eef343878..33247904c38e5e34b689e21a75d99edbe5de32d5 100644 (file)
@@ -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 */