Skip to content

Commit 7d2f005

Browse files
author
Veijo Pesonen
committed
tests-integration-fs: makes tests independent
Cleans up the created files between test cases to avoid out of space situations.
1 parent dba3962 commit 7d2f005

File tree

1 file changed

+77
-30
lines changed

1 file changed

+77
-30
lines changed

TESTS/integration/fs-threaded/main.cpp

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222
*/
2323

2424
#if !INTEGRATION_TESTS
25-
#error [NOT_SUPPORTED] integration tests not enabled for this target
25+
#error [NOT_SUPPORTED] integration tests not enabled
2626
#elif !MBED_CONF_RTOS_PRESENT
2727
#error [NOT_SUPPORTED] integration tests require RTOS
28-
#else
28+
#endif
2929

3030
#include "mbed.h"
31-
#include "FATFileSystem.h"
3231
#include "LittleFileSystem.h"
3332
#include "utest/utest.h"
3433
#include "unity/unity.h"
@@ -59,38 +58,38 @@ void led_thread()
5958
#endif
6059

6160
BlockDevice *bd = BlockDevice::get_default_instance();
62-
SlicingBlockDevice sd(bd, 0, MBED_CONF_APP_TESTS_FS_SIZE);
63-
#if TEST_USE_FILESYSTEM == FS_FAT
64-
FATFileSystem fs("sd");
65-
#else
61+
SlicingBlockDevice sbd(bd, 0, MBED_CONF_APP_TESTS_FS_SIZE);
6662
LittleFileSystem fs("sd");
67-
#endif
6863

69-
static control_t test_format(const size_t call_count)
70-
{
71-
int format_err = fs.format(&sd);
72-
TEST_ASSERT_EQUAL_INT_MESSAGE(0, format_err, "could not format block device");
64+
constexpr char fname_prefix[] = "mbed-file-test-";
65+
constexpr char fname_postfix[] = ".txt";
7366

74-
int mount_err = fs.mount(&sd);
75-
TEST_ASSERT_EQUAL_INT_MESSAGE(0, mount_err, "could not mount block device");
67+
static uint32_t thread_counter = 0;
7668

77-
return CaseNext;
69+
void print_memory_info()
70+
{
71+
#if defined MBED_HEAP_STATS_ENABLED && MBED_MEM_TRACING_ENABLED
72+
// Grab the heap statistics
73+
mbed_stats_heap_t heap_stats;
74+
mbed_stats_heap_get(&heap_stats);
75+
printf("Heap size: %lu / %lu bytes\r\n",
76+
heap_stats.current_size,
77+
heap_stats.reserved_size);
78+
#endif
7879
}
7980

80-
static uint32_t thread_counter = 0;
81-
8281
void file_fn(size_t *block_size)
8382
{
8483
uint32_t thread_id = core_util_atomic_incr_u32(&thread_counter, 1);
8584
char filename[255] = { 0 };
86-
snprintf(filename, 255, "mbed-file-test-%" PRIu32 ".txt", thread_id);
85+
snprintf(filename, 255, "%s%" PRIu32 "%s", fname_prefix, thread_id, fname_postfix);
8786
file_test_write(filename, 0, story, sizeof(story), *block_size);
8887
file_test_read(filename, 0, story, sizeof(story), *block_size);
88+
print_memory_info();
8989
}
9090

9191
static control_t file_2_threads(const size_t call_count)
9292
{
93-
thread_counter = 0;
9493
size_t block_size1 = 4;
9594
size_t block_size2 = 256;
9695

@@ -104,39 +103,88 @@ static control_t file_2_threads(const size_t call_count)
104103
return CaseNext;
105104
}
106105

107-
static control_t file_3_threads(const size_t call_count)
106+
static control_t file_4_threads(const size_t call_count)
108107
{
109-
thread_counter = 0;
110-
size_t block_size1 = 256;
111-
size_t block_size2 = 1024;
112-
size_t block_size3 = 4096;
108+
size_t block_size1 = 4;
109+
size_t block_size2 = 256;
110+
size_t block_size3 = 1024;
111+
size_t block_size4 = 4096;
113112

114113
Thread t1;
115114
Thread t2;
116115
Thread t3;
116+
Thread t4;
117117
t1.start(callback(file_fn, &block_size1));
118118
t2.start(callback(file_fn, &block_size2));
119119
t3.start(callback(file_fn, &block_size3));
120+
t4.start(callback(file_fn, &block_size4));
120121
t1.join();
121122
t2.join();
122123
t3.join();
124+
t4.join();
123125

124126
return CaseNext;
125127
}
126128

127-
utest::v1::status_t greentea_setup(const size_t number_of_cases)
129+
utest::v1::status_t test_setup_handler(const size_t number_of_cases)
128130
{
129131
GREENTEA_SETUP(10 * 60, "default_auto");
132+
133+
/* Format only once per each test run */
134+
int format_err = fs.format(&sbd);
135+
TEST_ASSERT_EQUAL_INT_MESSAGE(0, format_err, "could not format block device");
136+
137+
int mount_err = fs.mount(&sbd);
138+
TEST_ASSERT_EQUAL_INT_MESSAGE(0, mount_err, "could not mount block device");
139+
130140
return greentea_test_setup_handler(number_of_cases);
131141
}
132142

143+
void test_teardown_handler(const size_t passed, const size_t failed, const failure_t failure)
144+
{
145+
int mount_err = fs.unmount();
146+
TEST_ASSERT_EQUAL_INT_MESSAGE(0, mount_err, "could not unmount block device");
147+
148+
greentea_test_teardown_handler(passed, failed, failure);
149+
}
150+
151+
utest::v1::status_t case_setup_handler(const Case *const source, const size_t index_of_case)
152+
{
153+
thread_counter = 0;
154+
155+
return greentea_case_setup_handler(source, index_of_case);
156+
}
157+
158+
utest::v1::status_t case_teardown_handler(const Case *const source,
159+
const size_t passed,
160+
const size_t failed,
161+
const failure_t reason)
162+
{
163+
char filename[255] = { 0 };
164+
165+
for (uint32_t idx = 1; idx <= thread_counter; idx++) {
166+
snprintf(filename, 255, "%s%" PRIu32 "%s", fname_prefix, idx, fname_postfix);
167+
int removal_err = fs.remove(filename);
168+
TEST_ASSERT_EQUAL_INT_MESSAGE(0, removal_err, "could not remove file");
169+
}
170+
171+
return greentea_case_teardown_handler(source, passed, failed, reason);
172+
}
173+
174+
175+
133176
Case cases[] = {
134-
Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " format", test_format),
135-
Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 2 files, buff 4b/256b", file_2_threads),
136-
Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 3 files, buff 256b/1kb/4kb", file_3_threads),
177+
Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 2 files, block size 4B/256B",
178+
case_setup_handler,
179+
file_2_threads,
180+
case_teardown_handler),
181+
Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 4 files, block size 4B/256B/1KiB/4KiB",
182+
case_setup_handler,
183+
file_4_threads,
184+
case_teardown_handler),
137185
};
138186

139-
Specification specification(greentea_setup, cases);
187+
Specification specification(test_setup_handler, cases, test_teardown_handler);
140188

141189
int main()
142190
{
@@ -148,4 +196,3 @@ int main()
148196

149197
return !Harness::run(specification);
150198
}
151-
#endif // !INTEGRATION_TESTS

0 commit comments

Comments
 (0)