| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
file_section_list.c - description |
/* |
| 3 |
------------------- |
* test_section_list |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - tester for data models and basic operations of section and article |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 by Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* This program is free software; you can redistribute it and/or modify * |
|
|
* it under the terms of the GNU General Public License as published by * |
|
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
|
#include "section_list.h" |
|
|
#include "trie_dict.h" |
|
| 9 |
#include "bbs.h" |
#include "bbs.h" |
| 10 |
#include "log.h" |
#include "log.h" |
| 11 |
|
#include "section_list.h" |
| 12 |
|
#include "trie_dict.h" |
| 13 |
|
#include "user_list.h" |
| 14 |
|
#include <errno.h> |
| 15 |
#include <stdio.h> |
#include <stdio.h> |
| 16 |
#include <unistd.h> |
#include <unistd.h> |
|
#include <errno.h> |
|
| 17 |
|
|
| 18 |
#define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat" |
#define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat" |
| 19 |
#define SECTION_LIST_SHM_FILE "~section_list_shm.dat" |
#define SECTION_LIST_SHM_FILE "~section_list_shm.dat" |
| 20 |
#define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat" |
#define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat" |
| 21 |
|
#define USER_LIST_SHM_FILE "~user_list_shm.dat" |
| 22 |
|
|
| 23 |
const char *sname[] = { |
const char *sname[] = { |
| 24 |
"Test", |
"Test", |
| 27 |
|
|
| 28 |
const char *stitle[] = { |
const char *stitle[] = { |
| 29 |
" Test Section ", |
" Test Section ", |
| 30 |
"ĸABC", |
"字母组合ABC", |
| 31 |
"__123"}; |
"_数字_123"}; |
| 32 |
|
|
| 33 |
const char *master_name[] = { |
const char *master_name[] = { |
| 34 |
"sysadm", |
"sysadm", |
| 64 |
return -1; |
return -1; |
| 65 |
} |
} |
| 66 |
|
|
| 67 |
log_std_redirect(STDOUT_FILENO); |
log_common_redir(STDOUT_FILENO); |
| 68 |
log_err_redirect(STDERR_FILENO); |
log_error_redir(STDERR_FILENO); |
| 69 |
|
|
| 70 |
// - 1 to make blocks allocated is less than required, to trigger error handling |
// - 1 to make blocks allocated is less than required, to trigger error handling |
| 71 |
block_count = BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK; |
block_count = BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK; |
| 91 |
} |
} |
| 92 |
fclose(fp); |
fclose(fp); |
| 93 |
|
|
| 94 |
|
if ((fp = fopen(USER_LIST_SHM_FILE, "w")) == NULL) |
| 95 |
|
{ |
| 96 |
|
log_error("fopen(%s) error\n", USER_LIST_SHM_FILE); |
| 97 |
|
return -1; |
| 98 |
|
} |
| 99 |
|
fclose(fp); |
| 100 |
|
|
| 101 |
if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0) |
if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0) |
| 102 |
{ |
{ |
| 103 |
printf("trie_dict_init failed\n"); |
printf("trie_dict_init failed\n"); |
| 116 |
return -2; |
return -2; |
| 117 |
} |
} |
| 118 |
|
|
| 119 |
|
// Load user_list and online_user_list |
| 120 |
|
if (user_list_pool_init(USER_LIST_SHM_FILE) < 0) |
| 121 |
|
{ |
| 122 |
|
log_error("user_list_pool_init() error\n"); |
| 123 |
|
return -2; |
| 124 |
|
} |
| 125 |
|
|
| 126 |
printf("Testing #1 ...\n"); |
printf("Testing #1 ...\n"); |
| 127 |
|
|
| 128 |
last_aid = 0; |
last_aid = 0; |
| 129 |
|
|
| 130 |
if (section_list_try_rw_lock(NULL, 1) < 0) |
if (section_list_rw_lock(NULL) < 0) |
| 131 |
{ |
{ |
| 132 |
printf("section_list_try_rw_lock(sid = %d) error\n", 0); |
printf("section_list_rw_lock(sid = %d) error\n", 0); |
| 133 |
} |
} |
| 134 |
|
|
| 135 |
for (i = 0; i < section_count; i++) |
for (i = 0; i < section_count; i++) |
| 187 |
article.tid = 0; |
article.tid = 0; |
| 188 |
article.sid = i * 3 + 1; |
article.sid = i * 3 + 1; |
| 189 |
article.cid = article.aid; |
article.cid = article.aid; |
| 190 |
article.uid = 1; // TODO: randomize |
article.uid = 1; |
| 191 |
article.visible = 1; |
article.visible = 1; |
| 192 |
article.excerption = 0; |
article.excerption = 0; |
| 193 |
article.ontop = 0; |
article.ontop = 0; |
| 194 |
article.lock = 0; |
article.lock = 0; |
| 195 |
|
article.transship = 0; |
| 196 |
|
|
| 197 |
if (section_list_try_rw_lock(p_section[i], 1) < 0) |
if (section_list_rw_lock(p_section[i]) < 0) |
| 198 |
{ |
{ |
| 199 |
printf("section_list_try_rw_lock(sid = %d) error\n", p_section[i]->sid); |
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 200 |
break; |
break; |
| 201 |
} |
} |
| 202 |
|
|
| 219 |
// printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i); |
// printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i); |
| 220 |
} |
} |
| 221 |
|
|
| 222 |
|
if (last_aid != article_block_last_aid()) |
| 223 |
|
{ |
| 224 |
|
printf("last_aid != %d\n", article_block_last_aid()); |
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
if (article_block_article_count() != section_count * BBS_article_limit_per_section) |
| 228 |
|
{ |
| 229 |
|
printf("article_block_article_count() error %d != %d * %d\n", |
| 230 |
|
article_block_article_count(), section_count, BBS_article_limit_per_section); |
| 231 |
|
} |
| 232 |
|
|
| 233 |
last_aid = 0; |
last_aid = 0; |
| 234 |
|
|
| 235 |
for (j = 0; j < BBS_article_limit_per_section; j++) |
for (j = 0; j < BBS_article_limit_per_section; j++) |
| 244 |
printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid); |
printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid); |
| 245 |
} |
} |
| 246 |
|
|
| 247 |
if (section_list_try_rw_lock(p_section[i], 1) < 0) |
if (section_list_rw_lock(p_section[i]) < 0) |
| 248 |
{ |
{ |
| 249 |
printf("section_list_try_rw_lock(sid = %d) error\n", p_section[i]->sid); |
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 250 |
break; |
break; |
| 251 |
} |
} |
| 252 |
|
|
| 267 |
|
|
| 268 |
printf("Testing #2 ...\n"); |
printf("Testing #2 ...\n"); |
| 269 |
|
|
| 270 |
if (section_list_try_rw_lock(NULL, 1) < 0) |
if (section_list_rw_lock(NULL) < 0) |
| 271 |
{ |
{ |
| 272 |
printf("section_list_try_rw_lock(sid = %d) error\n", 0); |
printf("section_list_rw_lock(sid = %d) error\n", 0); |
| 273 |
} |
} |
| 274 |
|
|
| 275 |
if (article_block_reset() != 0) |
if (article_block_reset() != 0) |
| 294 |
{ |
{ |
| 295 |
section_first_aid = last_aid + 1; |
section_first_aid = last_aid + 1; |
| 296 |
|
|
| 297 |
if (section_list_try_rw_lock(p_section[i], 1) < 0) |
if (section_list_rw_lock(p_section[i]) < 0) |
| 298 |
{ |
{ |
| 299 |
printf("section_list_try_rw_lock(sid = %d) error\n", p_section[i]->sid); |
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 300 |
break; |
break; |
| 301 |
} |
} |
| 302 |
|
|
| 310 |
article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count)); |
article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count)); |
| 311 |
article.sid = i * 3 + 1; |
article.sid = i * 3 + 1; |
| 312 |
article.cid = article.aid; |
article.cid = article.aid; |
| 313 |
article.uid = 1; // TODO: randomize |
article.uid = 1; |
| 314 |
article.visible = 1; |
article.visible = 1; |
| 315 |
article.excerption = 0; |
article.excerption = 0; |
| 316 |
article.ontop = 0; |
article.ontop = 0; |
| 317 |
article.lock = 0; |
article.lock = 0; |
| 318 |
|
article.transship = 0; |
| 319 |
|
|
| 320 |
if (section_list_append_article(p_section[i], &article) < 0) |
if (section_list_append_article(p_section[i], &article) < 0) |
| 321 |
{ |
{ |
| 343 |
article_count = 0; |
article_count = 0; |
| 344 |
last_aid = 0; |
last_aid = 0; |
| 345 |
|
|
| 346 |
if (section_list_try_rd_lock(p_section[i], 1) < 0) |
if (section_list_rd_lock(p_section[i]) < 0) |
| 347 |
{ |
{ |
| 348 |
printf("section_list_try_rd_lock(sid = %d) error\n", p_section[i]->sid); |
printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid); |
| 349 |
break; |
break; |
| 350 |
} |
} |
| 351 |
|
|
| 387 |
continue; |
continue; |
| 388 |
} |
} |
| 389 |
|
|
| 390 |
if (section_list_try_rd_lock(p_section[i], 1) < 0) |
if (section_list_rd_lock(p_section[i]) < 0) |
| 391 |
{ |
{ |
| 392 |
printf("section_list_try_rd_lock(sid = %d) error\n", p_section[i]->sid); |
printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid); |
| 393 |
break; |
break; |
| 394 |
} |
} |
| 395 |
|
|
| 469 |
{ |
{ |
| 470 |
last_aid = 0; |
last_aid = 0; |
| 471 |
|
|
| 472 |
if (section_list_try_rd_lock(p_section[i], 1) < 0) |
if (section_list_rd_lock(p_section[i]) < 0) |
| 473 |
{ |
{ |
| 474 |
printf("section_list_try_rd_lock(sid = %d) error\n", p_section[i]->sid); |
printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid); |
| 475 |
break; |
break; |
| 476 |
} |
} |
| 477 |
|
|
| 511 |
|
|
| 512 |
for (i = 0; i < section_count; i++) |
for (i = 0; i < section_count; i++) |
| 513 |
{ |
{ |
| 514 |
if (section_list_try_rw_lock(p_section[i], 1) < 0) |
if (section_list_rw_lock(p_section[i]) < 0) |
| 515 |
{ |
{ |
| 516 |
printf("section_list_try_rw_lock(sid = %d) error\n", p_section[i]->sid); |
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 517 |
break; |
break; |
| 518 |
} |
} |
| 519 |
|
|
| 554 |
(p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) != |
(p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) != |
| 555 |
p_section[i]->page_count) |
p_section[i]->page_count) |
| 556 |
{ |
{ |
| 557 |
printf("Inconsistent page count in section %d offset %d, %d != %d, " |
printf("#1 Inconsistent page count in section %d offset %d, %d != %d, " |
| 558 |
"visible_article_count = %d, last_page_visible_count = %d\n", |
"visible_article_count = %d, last_page_visible_count = %d\n", |
| 559 |
i, j, |
i, j, |
| 560 |
p_section[i]->visible_article_count / BBS_article_limit_per_page + |
p_section[i]->visible_article_count / BBS_article_limit_per_page + |
| 637 |
(p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) != |
(p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) != |
| 638 |
p_section[i]->page_count) |
p_section[i]->page_count) |
| 639 |
{ |
{ |
| 640 |
printf("Inconsistent page count in section %d offset %d, %d != %d, " |
printf("#2 Inconsistent page count in section %d offset %d, %d != %d, " |
| 641 |
"visible_article_count = %d, last_page_visible_count = %d\n", |
"visible_article_count = %d, last_page_visible_count = %d\n", |
| 642 |
i, j, |
i, j, |
| 643 |
p_section[i]->visible_article_count / BBS_article_limit_per_page + |
p_section[i]->visible_article_count / BBS_article_limit_per_page + |
| 656 |
|
|
| 657 |
for (i = 0; i < BBS_max_section; i++) |
for (i = 0; i < BBS_max_section; i++) |
| 658 |
{ |
{ |
| 659 |
if (section_list_try_rw_lock(p_section[i], 1) < 0) |
if (section_list_rw_lock(p_section[i]) < 0) |
| 660 |
{ |
{ |
| 661 |
printf("section_list_try_rw_lock(sid = %d) error\n", p_section[i]->sid); |
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 662 |
break; |
break; |
| 663 |
} |
} |
| 664 |
|
|
| 706 |
(p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) != |
(p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) != |
| 707 |
p_section[i]->page_count) |
p_section[i]->page_count) |
| 708 |
{ |
{ |
| 709 |
printf("Inconsistent page count in section %d offset %d, %d != %d, " |
printf("#3 Inconsistent page count in section %d offset %d, %d != %d, " |
| 710 |
"visible_article_count = %d, last_page_visible_count = %d\n", |
"visible_article_count = %d, last_page_visible_count = %d\n", |
| 711 |
i, j, |
i, j, |
| 712 |
p_section[i]->visible_article_count / BBS_article_limit_per_page + |
p_section[i]->visible_article_count / BBS_article_limit_per_page + |
| 725 |
|
|
| 726 |
printf("Testing #5 ...\n"); |
printf("Testing #5 ...\n"); |
| 727 |
|
|
| 728 |
if (section_list_try_rw_lock(NULL, 1) < 0) |
if (section_list_rw_lock(NULL) < 0) |
| 729 |
{ |
{ |
| 730 |
printf("section_list_try_rw_lock(sid = %d) error\n", 0); |
printf("section_list_rw_lock(sid = %d) error\n", 0); |
| 731 |
} |
} |
| 732 |
|
|
| 733 |
if (article_block_reset() != 0) |
if (article_block_reset() != 0) |
| 750 |
|
|
| 751 |
for (i = 0; i < section_count / 2; i++) |
for (i = 0; i < section_count / 2; i++) |
| 752 |
{ |
{ |
| 753 |
if (section_list_try_rw_lock(p_section[i], 1) < 0) |
if (section_list_rw_lock(p_section[i]) < 0) |
| 754 |
{ |
{ |
| 755 |
printf("section_list_try_rw_lock(sid = %d) error\n", p_section[i]->sid); |
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 756 |
break; |
break; |
| 757 |
} |
} |
| 758 |
|
|
| 768 |
article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count)); |
article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count)); |
| 769 |
article.sid = i * 3 + 1; |
article.sid = i * 3 + 1; |
| 770 |
article.cid = article.aid; |
article.cid = article.aid; |
| 771 |
article.uid = 1; // TODO: randomize |
article.uid = 1; |
| 772 |
article.visible = 1; |
article.visible = 1; |
| 773 |
article.excerption = 0; |
article.excerption = 0; |
| 774 |
article.ontop = 0; |
article.ontop = 0; |
| 775 |
article.lock = 0; |
article.lock = 0; |
| 776 |
|
article.transship = 0; |
| 777 |
|
|
| 778 |
if (section_list_append_article(p_section[i], &article) < 0) |
if (section_list_append_article(p_section[i], &article) < 0) |
| 779 |
{ |
{ |
| 793 |
|
|
| 794 |
for (i = 0; i < section_count / 2; i++) |
for (i = 0; i < section_count / 2; i++) |
| 795 |
{ |
{ |
| 796 |
if (section_list_try_rw_lock(p_section[i], 1) < 0) |
if (section_list_rw_lock(p_section[i]) < 0) |
| 797 |
{ |
{ |
| 798 |
printf("section_list_try_rw_lock(sid = %d) error\n", p_section[i]->sid); |
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 799 |
break; |
break; |
| 800 |
} |
} |
| 801 |
|
|
| 826 |
|
|
| 827 |
for (i = 0; i < section_count / 2; i++) |
for (i = 0; i < section_count / 2; i++) |
| 828 |
{ |
{ |
| 829 |
if (section_list_try_rw_lock(p_section[i], 1) < 0) |
if (section_list_rw_lock(p_section[i]) < 0) |
| 830 |
{ |
{ |
| 831 |
printf("section_list_try_rw_lock(sid = %d) error\n", p_section[i]->sid); |
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 832 |
break; |
break; |
| 833 |
} |
} |
| 834 |
|
|
| 835 |
if (section_list_try_rw_lock(p_section[section_count / 2 + i], 1) < 0) |
if (section_list_rw_lock(p_section[section_count / 2 + i]) < 0) |
| 836 |
{ |
{ |
| 837 |
printf("section_list_try_rw_lock(sid = %d) error\n", p_section[section_count / 2 + i]->sid); |
printf("section_list_rw_lock(sid = %d) error\n", p_section[section_count / 2 + i]->sid); |
| 838 |
|
|
| 839 |
if (section_list_rw_unlock(p_section[i]) < 0) |
if (section_list_rw_unlock(p_section[i]) < 0) |
| 840 |
{ |
{ |
| 884 |
|
|
| 885 |
for (i = 0; i < section_count; i++) |
for (i = 0; i < section_count; i++) |
| 886 |
{ |
{ |
| 887 |
if (section_list_try_rd_lock(p_section[i], 1) < 0) |
if (section_list_rd_lock(p_section[i]) < 0) |
| 888 |
{ |
{ |
| 889 |
printf("section_list_try_rd_lock(sid = %d) error\n", p_section[i]->sid); |
printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid); |
| 890 |
break; |
break; |
| 891 |
} |
} |
| 892 |
|
|
| 932 |
} |
} |
| 933 |
} |
} |
| 934 |
|
|
| 935 |
|
printf("Testing #6 ...\n"); |
| 936 |
|
|
| 937 |
|
for (i = 0; i < section_count; i++) |
| 938 |
|
{ |
| 939 |
|
if (section_list_rd_lock(p_section[i]) < 0) |
| 940 |
|
{ |
| 941 |
|
printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid); |
| 942 |
|
break; |
| 943 |
|
} |
| 944 |
|
} |
| 945 |
|
|
| 946 |
|
printf("Try rw_lock for 5 sec...\n"); |
| 947 |
|
if (section_list_try_rw_lock(NULL, 5) == 0) |
| 948 |
|
{ |
| 949 |
|
printf("section_list_try_rw_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid); |
| 950 |
|
} |
| 951 |
|
|
| 952 |
|
for (i = 0; i < section_count; i++) |
| 953 |
|
{ |
| 954 |
|
if (section_list_rd_unlock(p_section[i]) < 0) |
| 955 |
|
{ |
| 956 |
|
printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid); |
| 957 |
|
break; |
| 958 |
|
} |
| 959 |
|
} |
| 960 |
|
|
| 961 |
|
if (section_list_try_rw_lock(NULL, 5) < 0) |
| 962 |
|
{ |
| 963 |
|
printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid); |
| 964 |
|
} |
| 965 |
|
|
| 966 |
|
for (i = 0; i < section_count; i++) |
| 967 |
|
{ |
| 968 |
|
if (section_list_try_rd_lock(p_section[i], 0) == 0) |
| 969 |
|
{ |
| 970 |
|
printf("section_list_try_rd_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid); |
| 971 |
|
break; |
| 972 |
|
} |
| 973 |
|
} |
| 974 |
|
|
| 975 |
|
if (section_list_rw_unlock(NULL) < 0) |
| 976 |
|
{ |
| 977 |
|
printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid); |
| 978 |
|
} |
| 979 |
|
|
| 980 |
printf("Press ENTER to exit..."); |
printf("Press ENTER to exit..."); |
| 981 |
getchar(); |
getchar(); |
| 982 |
|
|
| 983 |
|
user_list_pool_cleanup(); |
| 984 |
section_list_cleanup(); |
section_list_cleanup(); |
| 985 |
article_block_cleanup(); |
article_block_cleanup(); |
| 986 |
trie_dict_cleanup(); |
trie_dict_cleanup(); |
| 987 |
|
|
| 988 |
|
if (unlink(USER_LIST_SHM_FILE) < 0) |
| 989 |
|
{ |
| 990 |
|
log_error("unlink(%s) error\n", USER_LIST_SHM_FILE); |
| 991 |
|
return -1; |
| 992 |
|
} |
| 993 |
|
|
| 994 |
if (unlink(TRIE_DICT_SHM_FILE) < 0) |
if (unlink(TRIE_DICT_SHM_FILE) < 0) |
| 995 |
{ |
{ |
| 996 |
log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE); |
log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE); |