Skip to content

Commit 0aacbf7

Browse files
authored
Merge pull request #12668 from VeijoPesonen/fix_tests-integration-fs_2
tests-integration-fs-threaded: makes tests independent from each other
2 parents 4b3cddf + 61d70ae commit 0aacbf7

File tree

1 file changed

+108
-30
lines changed

1 file changed

+108
-30
lines changed

TESTS/integration/fs-threaded/main.cpp

Lines changed: 108 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,119 @@ 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;
112+
113+
Thread t1;
114+
Thread t2;
115+
Thread t3;
116+
Thread t4;
117+
t1.start(callback(file_fn, &block_size1));
118+
t2.start(callback(file_fn, &block_size2));
119+
t3.start(callback(file_fn, &block_size3));
120+
t4.start(callback(file_fn, &block_size4));
121+
t1.join();
122+
t2.join();
123+
t3.join();
124+
t4.join();
125+
126+
return CaseNext;
127+
}
128+
129+
static control_t file_5_threads(const size_t call_count)
130+
{
131+
size_t block_size1 = 4;
132+
size_t block_size2 = 256;
133+
size_t block_size3 = 1024;
134+
size_t block_size4 = 4096;
135+
size_t block_size5 = 4096;
113136

114137
Thread t1;
115138
Thread t2;
116139
Thread t3;
140+
Thread t4;
141+
Thread t5;
117142
t1.start(callback(file_fn, &block_size1));
118143
t2.start(callback(file_fn, &block_size2));
119144
t3.start(callback(file_fn, &block_size3));
145+
t4.start(callback(file_fn, &block_size4));
146+
t5.start(callback(file_fn, &block_size5));
120147
t1.join();
121148
t2.join();
122149
t3.join();
150+
t4.join();
151+
t5.join();
123152

124153
return CaseNext;
125154
}
126155

127-
utest::v1::status_t greentea_setup(const size_t number_of_cases)
156+
utest::v1::status_t test_setup_handler(const size_t number_of_cases)
128157
{
129158
GREENTEA_SETUP(10 * 60, "default_auto");
159+
160+
/* Format only once per each test run */
161+
int format_err = fs.format(&sbd);
162+
TEST_ASSERT_EQUAL_INT_MESSAGE(0, format_err, "could not format block device");
163+
164+
int mount_err = fs.mount(&sbd);
165+
TEST_ASSERT_EQUAL_INT_MESSAGE(0, mount_err, "could not mount block device");
166+
130167
return greentea_test_setup_handler(number_of_cases);
131168
}
132169

170+
void test_teardown_handler(const size_t passed, const size_t failed, const failure_t failure)
171+
{
172+
int mount_err = fs.unmount();
173+
TEST_ASSERT_EQUAL_INT_MESSAGE(0, mount_err, "could not unmount block device");
174+
175+
greentea_test_teardown_handler(passed, failed, failure);
176+
}
177+
178+
utest::v1::status_t case_setup_handler(const Case *const source, const size_t index_of_case)
179+
{
180+
thread_counter = 0;
181+
182+
return greentea_case_setup_handler(source, index_of_case);
183+
}
184+
185+
utest::v1::status_t case_teardown_handler(const Case *const source,
186+
const size_t passed,
187+
const size_t failed,
188+
const failure_t reason)
189+
{
190+
char filename[255] = { 0 };
191+
192+
for (uint32_t idx = 1; idx <= thread_counter; idx++) {
193+
snprintf(filename, 255, "%s%" PRIu32 "%s", fname_prefix, idx, fname_postfix);
194+
int removal_err = fs.remove(filename);
195+
TEST_ASSERT_EQUAL_INT_MESSAGE(0, removal_err, "could not remove file");
196+
}
197+
198+
return greentea_case_teardown_handler(source, passed, failed, reason);
199+
}
200+
201+
202+
133203
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),
204+
Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 2 files, block size 4B/256B",
205+
case_setup_handler,
206+
file_2_threads,
207+
case_teardown_handler),
208+
Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 4 files, block size 4B/256B/1KiB/4KiB",
209+
case_setup_handler,
210+
file_4_threads,
211+
case_teardown_handler),
212+
Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 5 files, block size 4B/256B/1KiB/4KiB/4KiB",
213+
case_setup_handler,
214+
file_5_threads,
215+
case_teardown_handler),
137216
};
138217

139-
Specification specification(greentea_setup, cases);
218+
Specification specification(test_setup_handler, cases, test_teardown_handler);
140219

141220
int main()
142221
{
@@ -148,4 +227,3 @@ int main()
148227

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

0 commit comments

Comments
 (0)