Skip to content

Commit 724cc81

Browse files
committed
[kvdb] add db size scale up testcase
1 parent 3c80fa5 commit 724cc81

File tree

1 file changed

+168
-23
lines changed

1 file changed

+168
-23
lines changed

tests/fdb_kvdb_tc.c

Lines changed: 168 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,25 @@
1616
#include <flashdb.h>
1717
#include <stdio.h>
1818
#include <stdlib.h>
19+
20+
#define RT_USING_UTEST
21+
22+
#if RT_VER_NUM <= 0x40003
23+
#include <dfs_posix.h>
24+
#else
25+
#include <dfs_file.h>
1926
#include <unistd.h>
27+
#endif
2028

2129
#define TEST_TS_PART_NAME "fdb_kvdb1"
2230
#define TEST_KV_BLOB_NAME "kv_blob_test"
2331
#define TEST_KV_NAME "kv_test"
2432
#define TEST_KV_VALUE_LEN 1200 /* only save 3 KVs in a 4096 sector */
25-
#define TEST_KV_NUM 4
33+
#define TEST_KV_MAX_NUM 8
2634
#define TEST_KVDB_SECTOR_SIZE 4096
35+
#define TEST_KVDB_SECTOR_NUM 4
36+
37+
#define FDB_ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
2738

2839
#if defined(RT_USING_UTEST) && defined(FDB_USING_KVDB)
2940

@@ -39,14 +50,76 @@ static struct fdb_kvdb test_kvdb;
3950

4051
static void test_fdb_kvdb_deinit(void);
4152

42-
static void test_fdb_kvdb_init(void)
53+
static rt_err_t dir_delete(const char* path)
54+
{
55+
DIR* dir = NULL;
56+
struct dirent* dirent = NULL;
57+
char* full_path;
58+
rt_err_t res = RT_EOK;
59+
60+
if (path == RT_NULL || path[0] == '\0')
61+
return -ENOENT;
62+
63+
full_path = (char*)rt_malloc(DFS_PATH_MAX);
64+
if (full_path == RT_NULL)
65+
return -ENOMEM;
66+
67+
dir = opendir(path);
68+
if (dir == RT_NULL)
69+
{
70+
rt_free(full_path);
71+
return -ENOENT;
72+
}
73+
74+
while (1)
75+
{
76+
dirent = readdir(dir);
77+
if (dirent == RT_NULL)
78+
break;
79+
if (rt_strcmp(".", dirent->d_name) != 0 &&
80+
rt_strcmp("..", dirent->d_name) != 0)
81+
{
82+
rt_snprintf(full_path, DFS_PATH_MAX, "%s/%s", path, dirent->d_name);
83+
if (dirent->d_type == DT_REG)
84+
{
85+
if (unlink(full_path) != 0)
86+
{
87+
LOG_W("cannot remove '%s'", full_path);
88+
res = -RT_ERROR;
89+
}
90+
}
91+
else if (dirent->d_type == DT_DIR)
92+
{
93+
if (dir_delete(full_path) != RT_EOK)
94+
{
95+
res = -RT_ERROR;
96+
}
97+
}
98+
}
99+
}
100+
closedir(dir);
101+
rt_free(full_path);
102+
103+
if (path[rt_strlen(path) - 1] != '/')
104+
{
105+
if (unlink(path) != 0)
106+
{
107+
LOG_W("cannot remove '%s'", path);
108+
res = -RT_ERROR;
109+
}
110+
}
111+
112+
return res;
113+
}
114+
115+
static void test_fdb_kvdb_init_by_sector_num(size_t sector_num)
43116
{
44117
if (access(TEST_TS_PART_NAME, 0) < 0)
45118
{
46119
mkdir(TEST_TS_PART_NAME, 0);
47120
}
48121

49-
uint32_t sec_size = TEST_KVDB_SECTOR_SIZE, db_size = sec_size * 4;
122+
uint32_t sec_size = TEST_KVDB_SECTOR_SIZE, db_size = sec_size * sector_num;
50123
rt_bool_t file_mode = true;
51124

52125
fdb_kvdb_control(&(test_kvdb), FDB_KVDB_CTRL_SET_SEC_SIZE, &sec_size);
@@ -56,12 +129,27 @@ static void test_fdb_kvdb_init(void)
56129
uassert_true(fdb_kvdb_init(&test_kvdb, "test_kv", TEST_TS_PART_NAME, NULL, NULL) == FDB_NO_ERR);
57130
}
58131

132+
static void test_fdb_kvdb_init(void)
133+
{
134+
test_fdb_kvdb_init_by_sector_num(TEST_KVDB_SECTOR_NUM);
135+
}
136+
137+
static void test_fdb_kvdb_init_by_8_sectors(void)
138+
{
139+
test_fdb_kvdb_init_by_sector_num(8);
140+
}
141+
59142
static void test_fdb_kvdb_init_check(void)
60143
{
61144
/* check the oldest address */
62145
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 0);
63146
}
64147

148+
static void test_fdb_kvdb_deinit(void)
149+
{
150+
uassert_true(fdb_kvdb_deinit(&test_kvdb) == FDB_NO_ERR);
151+
}
152+
65153
static void test_fdb_create_kv_blob(void)
66154
{
67155
fdb_err_t result = FDB_NO_ERR;
@@ -168,7 +256,6 @@ static void test_fdb_change_kv(void)
168256
/* check the oldest address is already right when kvdb reinit */
169257
static void fdb_reboot(void)
170258
{
171-
extern void test_fdb_kvdb_deinit(void);
172259
test_fdb_kvdb_deinit();
173260
test_fdb_kvdb_init();
174261
}
@@ -197,7 +284,7 @@ static void test_fdb_del_kv(void)
197284
}
198285
}
199286

200-
static int iter_all_kv(fdb_kvdb_t db, struct test_kv *kv_tbl)
287+
static int iter_all_kv(fdb_kvdb_t db, struct test_kv *kv_tbl, size_t len)
201288
{
202289
struct fdb_kv_iterator iterator;
203290
fdb_kv_t cur_kv;
@@ -206,7 +293,7 @@ static int iter_all_kv(fdb_kvdb_t db, struct test_kv *kv_tbl)
206293
struct fdb_blob fdb_blob;
207294

208295
fdb_kv_iterator_init(db, &iterator);
209-
while (fdb_kv_iterate(db, &iterator) == RT_TRUE)
296+
while (fdb_kv_iterate(db, &iterator) == RT_TRUE && index < len)
210297
{
211298
/* get data len */
212299
cur_kv = &(iterator.curr_kv);
@@ -222,15 +309,12 @@ static int iter_all_kv(fdb_kvdb_t db, struct test_kv *kv_tbl)
222309
index++;
223310
}
224311

225-
uassert_true(index == TEST_KV_NUM);
226-
227312
return index;
228313
}
229314

230-
static void test_fdb_by_kvs(const struct test_kv *kv_tbl, size_t len)
315+
static void test_save_fdb_by_kvs(const struct test_kv *kv_tbl, size_t len)
231316
{
232317
struct fdb_blob blob_obj, * blob = &blob_obj;
233-
static struct test_kv saved_kv_tbl[TEST_KV_NUM] = { 0 };
234318

235319
for (size_t i = 0; i < len; i++)
236320
{
@@ -239,17 +323,41 @@ static void test_fdb_by_kvs(const struct test_kv *kv_tbl, size_t len)
239323
fdb_kv_set_blob(&test_kvdb, kv_tbl[i].name, fdb_blob_make(blob, kv_tbl[i].value, TEST_KV_VALUE_LEN));
240324
}
241325
}
326+
}
327+
328+
static void test_check_fdb_by_kvs(const struct test_kv *kv_tbl, size_t len)
329+
{
330+
static struct test_kv saved_kv_tbl[TEST_KV_MAX_NUM] = { 0 };
242331

243-
iter_all_kv(&test_kvdb, saved_kv_tbl);
332+
iter_all_kv(&test_kvdb, saved_kv_tbl, FDB_ARRAY_SIZE(saved_kv_tbl));
244333

245-
for (size_t i = 0; i < len; i++)
334+
for (size_t i = 0, j = 0; i < len; i++)
246335
{
247-
uassert_str_equal(saved_kv_tbl[i].name, kv_tbl[i].name);
248-
uassert_str_equal(saved_kv_tbl[i].value, kv_tbl[i].value);
249-
uassert_true(RT_ALIGN_DOWN(saved_kv_tbl[i].addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * kv_tbl[i].addr);
336+
for (j = 0; j < FDB_ARRAY_SIZE(saved_kv_tbl); j++)
337+
{
338+
if (rt_strcmp(saved_kv_tbl[j].name, kv_tbl[i].name) == 0)
339+
break;
340+
}
341+
if (j < FDB_ARRAY_SIZE(saved_kv_tbl))
342+
{
343+
uassert_str_equal(saved_kv_tbl[j].name, kv_tbl[i].name);
344+
uassert_str_equal(saved_kv_tbl[j].value, kv_tbl[i].value);
345+
uassert_true(RT_ALIGN_DOWN(saved_kv_tbl[j].addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * kv_tbl[i].addr);
346+
}
347+
else
348+
{
349+
/* kv not found */
350+
uassert_true(0);
351+
}
250352
}
251353
}
252354

355+
static void test_fdb_by_kvs(const struct test_kv *kv_tbl, size_t len)
356+
{
357+
test_save_fdb_by_kvs(kv_tbl, len);
358+
test_check_fdb_by_kvs(kv_tbl, len);
359+
}
360+
253361
static void test_fdb_gc(void)
254362
{
255363
fdb_kv_set_default(&test_kvdb);
@@ -284,7 +392,7 @@ static void test_fdb_gc(void)
284392
{"kv3", "3", 1, 0, 1},
285393
};
286394

287-
test_fdb_by_kvs(kv_tbl, sizeof(kv_tbl) / sizeof(kv_tbl[0]));
395+
test_fdb_by_kvs(kv_tbl, FDB_ARRAY_SIZE(kv_tbl));
288396
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 0);
289397
fdb_reboot();
290398
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 0);
@@ -320,7 +428,7 @@ static void test_fdb_gc(void)
320428
{"kv3", "33", 1, 0, 1},
321429
};
322430

323-
test_fdb_by_kvs(kv_tbl, sizeof(kv_tbl) / sizeof(kv_tbl[0]));
431+
test_fdb_by_kvs(kv_tbl, FDB_ARRAY_SIZE(kv_tbl));
324432
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 0);
325433
fdb_reboot();
326434
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 0);
@@ -400,7 +508,7 @@ static void test_fdb_gc(void)
400508
{"kv3", "333", 3, 0, 1},
401509
};
402510

403-
test_fdb_by_kvs(kv_tbl, sizeof(kv_tbl) / sizeof(kv_tbl[0]));
511+
test_fdb_by_kvs(kv_tbl, FDB_ARRAY_SIZE(kv_tbl));
404512
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 1);
405513
fdb_reboot();
406514
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 1);
@@ -500,26 +608,62 @@ static void test_fdb_gc(void)
500608
{"kv3", "3333", 0, 0, 1},
501609
};
502610

503-
test_fdb_by_kvs(kv_tbl, sizeof(kv_tbl) / sizeof(kv_tbl[0]));
611+
test_fdb_by_kvs(kv_tbl, FDB_ARRAY_SIZE(kv_tbl));
504612
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 2);
505613
fdb_reboot();
506614
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 2);
507615

508616
}
509617
}
510618

511-
static void test_fdb_kvdb_set_default(void)
619+
static void test_fdb_scale_up(void)
512620
{
513-
uassert_true(fdb_kv_set_default(&test_kvdb) == FDB_NO_ERR);
621+
fdb_kv_set_default(&test_kvdb);
622+
623+
const struct test_kv old_kv_tbl[] = {
624+
{"kv0", "0", 0, 0, 1},
625+
{"kv1", "1", 0, 0, 1},
626+
{"kv2", "2", 0, 0, 1},
627+
{"kv3", "3", 1, 0, 1},
628+
};
629+
/* save some data */
630+
test_save_fdb_by_kvs(old_kv_tbl, FDB_ARRAY_SIZE(old_kv_tbl));
631+
632+
/* reboot, scale up from 4 sectors to 8 sectors */
633+
test_fdb_kvdb_deinit();
634+
test_fdb_kvdb_init_by_8_sectors();
635+
636+
/* check old data */
637+
test_check_fdb_by_kvs(old_kv_tbl, FDB_ARRAY_SIZE(old_kv_tbl));
638+
639+
/* save some new data */
640+
const struct test_kv new_kv_tbl[] = {
641+
{"kv4", "4", 4, 0, 1},
642+
{"kv5", "5", 4, 0, 1},
643+
{"kv6", "6", 4, 0, 1},
644+
{"kv7", "7", 5, 0, 1},
645+
};
646+
/* kv4: sector1, kv5: sector1, kv6: sector2, kv7: sector2 */
647+
test_save_fdb_by_kvs(new_kv_tbl, FDB_ARRAY_SIZE(new_kv_tbl));
648+
/* kv4: sector2, kv5: sector3, kv6: sector3, kv7: sector3 */
649+
test_save_fdb_by_kvs(new_kv_tbl, FDB_ARRAY_SIZE(new_kv_tbl));
650+
/* kv4: sector4, kv5: sector4, kv6: sector4, kv7: sector5 */
651+
test_save_fdb_by_kvs(new_kv_tbl, FDB_ARRAY_SIZE(new_kv_tbl));
652+
/* check new data */
653+
test_check_fdb_by_kvs(new_kv_tbl, FDB_ARRAY_SIZE(new_kv_tbl));
654+
/* check old data */
655+
test_check_fdb_by_kvs(old_kv_tbl, FDB_ARRAY_SIZE(old_kv_tbl));
514656
}
515657

516-
static void test_fdb_kvdb_deinit(void)
658+
static void test_fdb_kvdb_set_default(void)
517659
{
518-
uassert_true(fdb_kvdb_deinit(&test_kvdb) == FDB_NO_ERR);
660+
uassert_true(fdb_kv_set_default(&test_kvdb) == FDB_NO_ERR);
519661
}
520662

663+
521664
static rt_err_t utest_tc_init(void)
522665
{
666+
dir_delete(TEST_TS_PART_NAME);
523667
return RT_EOK;
524668
}
525669

@@ -545,6 +689,7 @@ static void testcase(void)
545689
UTEST_UNIT_RUN(test_fdb_change_kv);
546690
UTEST_UNIT_RUN(test_fdb_del_kv);
547691
UTEST_UNIT_RUN(test_fdb_gc);
692+
UTEST_UNIT_RUN(test_fdb_scale_up);
548693
UTEST_UNIT_RUN(test_fdb_kvdb_set_default);
549694
UTEST_UNIT_RUN(test_fdb_kvdb_deinit);
550695
}

0 commit comments

Comments
 (0)