/[LeafOK_CVS]/lbbs/src/test_section_list.c
ViewVC logotype

Diff of /lbbs/src/test_section_list.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.22 by sysadm, Sun May 25 10:43:27 2025 UTC Revision 1.36 by sysadm, Mon Nov 3 15:05:02 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                                          file_section_list.c  -  description                                          test_section_list.c  -  description
3                                                           -------------------                                                           -------------------
4          Copyright            : (C) 2004-2025 by Leaflet          Copyright            : (C) 2004-2025 by Leaflet
5          Email                : leaflet@leafok.com          Email                : leaflet@leafok.com
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "section_list.h"  
 #include "trie_dict.h"  
17  #include "bbs.h"  #include "bbs.h"
18  #include "log.h"  #include "log.h"
19    #include "section_list.h"
20    #include "trie_dict.h"
21    #include "user_list.h"
22    #include <errno.h>
23  #include <stdio.h>  #include <stdio.h>
24  #include <unistd.h>  #include <unistd.h>
 #include <errno.h>  
25    
26  #define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat"  #define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat"
27  #define SECTION_LIST_SHM_FILE "~section_list_shm.dat"  #define SECTION_LIST_SHM_FILE "~section_list_shm.dat"
28  #define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat"  #define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat"
29    #define USER_LIST_SHM_FILE "~user_list_shm.dat"
30    
31  const char *sname[] = {  const char *sname[] = {
32          "Test",          "Test",
# Line 33  const char *sname[] = { Line 35  const char *sname[] = {
35    
36  const char *stitle[] = {  const char *stitle[] = {
37          " Test Section ",          " Test Section ",
38          "ĸABC",          "字母组合ABC",
39          "__123"};          "_数字_123"};
40    
41  const char *master_name[] = {  const char *master_name[] = {
42          "sysadm",          "sysadm",
# Line 70  int main(int argc, char *argv[]) Line 72  int main(int argc, char *argv[])
72                  return -1;                  return -1;
73          }          }
74    
75          log_std_redirect(STDOUT_FILENO);          log_common_redir(STDOUT_FILENO);
76          log_err_redirect(STDERR_FILENO);          log_error_redir(STDERR_FILENO);
77    
78          // - 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
79          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;
# Line 97  int main(int argc, char *argv[]) Line 99  int main(int argc, char *argv[])
99          }          }
100          fclose(fp);          fclose(fp);
101    
102            if ((fp = fopen(USER_LIST_SHM_FILE, "w")) == NULL)
103            {
104                    log_error("fopen(%s) error\n", USER_LIST_SHM_FILE);
105                    return -1;
106            }
107            fclose(fp);
108    
109          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)
110          {          {
111                  printf("trie_dict_init failed\n");                  printf("trie_dict_init failed\n");
# Line 115  int main(int argc, char *argv[]) Line 124  int main(int argc, char *argv[])
124                  return -2;                  return -2;
125          }          }
126    
127            // Load user_list and online_user_list
128            if (user_list_pool_init(USER_LIST_SHM_FILE) < 0)
129            {
130                    log_error("user_list_pool_init() error\n");
131                    return -2;
132            }
133    
134          printf("Testing #1 ...\n");          printf("Testing #1 ...\n");
135    
136          last_aid = 0;          last_aid = 0;
137    
138          if (section_list_try_rw_lock(NULL, 1) < 0)          if (section_list_rw_lock(NULL) < 0)
139          {          {
140                  printf("section_list_try_rw_lock(sid = %d) error\n", 0);                  printf("section_list_rw_lock(sid = %d) error\n", 0);
141          }          }
142    
143          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
# Line 179  int main(int argc, char *argv[]) Line 195  int main(int argc, char *argv[])
195                          article.tid = 0;                          article.tid = 0;
196                          article.sid = i * 3 + 1;                          article.sid = i * 3 + 1;
197                          article.cid = article.aid;                          article.cid = article.aid;
198                          article.uid = 1; // TODO: randomize                          article.uid = 1;
199                          article.visible = 1;                          article.visible = 1;
200                          article.excerption = 0;                          article.excerption = 0;
201                          article.ontop = 0;                          article.ontop = 0;
202                          article.lock = 0;                          article.lock = 0;
203                            article.transship = 0;
204    
205                          if (section_list_try_rw_lock(p_section[i], 1) < 0)                          if (section_list_rw_lock(p_section[i]) < 0)
206                          {                          {
207                                  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);
208                                  break;                                  break;
209                          }                          }
210    
# Line 210  int main(int argc, char *argv[]) Line 227  int main(int argc, char *argv[])
227                  // 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);
228          }          }
229    
230            if (last_aid != article_block_last_aid())
231            {
232                    printf("last_aid != %d\n", article_block_last_aid());
233            }
234    
235            if (article_block_article_count() != section_count * BBS_article_limit_per_section)
236            {
237                    printf("article_block_article_count() error %d != %d * %d\n",
238                               article_block_article_count(), section_count, BBS_article_limit_per_section);
239            }
240    
241          last_aid = 0;          last_aid = 0;
242    
243          for (j = 0; j < BBS_article_limit_per_section; j++)          for (j = 0; j < BBS_article_limit_per_section; j++)
# Line 224  int main(int argc, char *argv[]) Line 252  int main(int argc, char *argv[])
252                                  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);
253                          }                          }
254    
255                          if (section_list_try_rw_lock(p_section[i], 1) < 0)                          if (section_list_rw_lock(p_section[i]) < 0)
256                          {                          {
257                                  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);
258                                  break;                                  break;
259                          }                          }
260    
# Line 247  int main(int argc, char *argv[]) Line 275  int main(int argc, char *argv[])
275    
276          printf("Testing #2 ...\n");          printf("Testing #2 ...\n");
277    
278          if (section_list_try_rw_lock(NULL, 1) < 0)          if (section_list_rw_lock(NULL) < 0)
279          {          {
280                  printf("section_list_try_rw_lock(sid = %d) error\n", 0);                  printf("section_list_rw_lock(sid = %d) error\n", 0);
281          }          }
282    
283          if (article_block_reset() != 0)          if (article_block_reset() != 0)
# Line 274  int main(int argc, char *argv[]) Line 302  int main(int argc, char *argv[])
302          {          {
303                  section_first_aid = last_aid + 1;                  section_first_aid = last_aid + 1;
304    
305                  if (section_list_try_rw_lock(p_section[i], 1) < 0)                  if (section_list_rw_lock(p_section[i]) < 0)
306                  {                  {
307                          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);
308                          break;                          break;
309                  }                  }
310    
# Line 290  int main(int argc, char *argv[]) Line 318  int main(int argc, char *argv[])
318                          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));
319                          article.sid = i * 3 + 1;                          article.sid = i * 3 + 1;
320                          article.cid = article.aid;                          article.cid = article.aid;
321                          article.uid = 1; // TODO: randomize                          article.uid = 1;
322                          article.visible = 1;                          article.visible = 1;
323                          article.excerption = 0;                          article.excerption = 0;
324                          article.ontop = 0;                          article.ontop = 0;
325                          article.lock = 0;                          article.lock = 0;
326                            article.transship = 0;
327    
328                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
329                          {                          {
# Line 322  int main(int argc, char *argv[]) Line 351  int main(int argc, char *argv[])
351                  article_count = 0;                  article_count = 0;
352                  last_aid = 0;                  last_aid = 0;
353    
354                  if (section_list_try_rd_lock(p_section[i], 1) < 0)                  if (section_list_rd_lock(p_section[i]) < 0)
355                  {                  {
356                          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);
357                          break;                          break;
358                  }                  }
359    
# Line 366  int main(int argc, char *argv[]) Line 395  int main(int argc, char *argv[])
395                          continue;                          continue;
396                  }                  }
397    
398                  if (section_list_try_rd_lock(p_section[i], 1) < 0)                  if (section_list_rd_lock(p_section[i]) < 0)
399                  {                  {
400                          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);
401                          break;                          break;
402                  }                  }
403    
# Line 448  int main(int argc, char *argv[]) Line 477  int main(int argc, char *argv[])
477          {          {
478                  last_aid = 0;                  last_aid = 0;
479    
480                  if (section_list_try_rd_lock(p_section[i], 1) < 0)                  if (section_list_rd_lock(p_section[i]) < 0)
481                  {                  {
482                          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);
483                          break;                          break;
484                  }                  }
485    
# Line 490  int main(int argc, char *argv[]) Line 519  int main(int argc, char *argv[])
519    
520          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
521          {          {
522                  if (section_list_try_rw_lock(p_section[i], 1) < 0)                  if (section_list_rw_lock(p_section[i]) < 0)
523                  {                  {
524                          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);
525                          break;                          break;
526                  }                  }
527    
# Line 533  int main(int argc, char *argv[]) Line 562  int main(int argc, char *argv[])
562                                  (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) !=
563                          p_section[i]->page_count)                          p_section[i]->page_count)
564                  {                  {
565                          printf("Inconsistent page count in section %d offset %d, %d != %d, "                          printf("#1 Inconsistent page count in section %d offset %d, %d != %d, "
566                                     "visible_article_count = %d, last_page_visible_count = %d\n",                                     "visible_article_count = %d, last_page_visible_count = %d\n",
567                                     i, j,                                     i, j,
568                                     p_section[i]->visible_article_count / BBS_article_limit_per_page +                                     p_section[i]->visible_article_count / BBS_article_limit_per_page +
# Line 616  int main(int argc, char *argv[]) Line 645  int main(int argc, char *argv[])
645                                  (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) !=
646                          p_section[i]->page_count)                          p_section[i]->page_count)
647                  {                  {
648                          printf("Inconsistent page count in section %d offset %d, %d != %d, "                          printf("#2 Inconsistent page count in section %d offset %d, %d != %d, "
649                                     "visible_article_count = %d, last_page_visible_count = %d\n",                                     "visible_article_count = %d, last_page_visible_count = %d\n",
650                                     i, j,                                     i, j,
651                                     p_section[i]->visible_article_count / BBS_article_limit_per_page +                                     p_section[i]->visible_article_count / BBS_article_limit_per_page +
# Line 635  int main(int argc, char *argv[]) Line 664  int main(int argc, char *argv[])
664    
665          for (i = 0; i < BBS_max_section; i++)          for (i = 0; i < BBS_max_section; i++)
666          {          {
667                  if (section_list_try_rw_lock(p_section[i], 1) < 0)                  if (section_list_rw_lock(p_section[i]) < 0)
668                  {                  {
669                          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);
670                          break;                          break;
671                  }                  }
672    
# Line 685  int main(int argc, char *argv[]) Line 714  int main(int argc, char *argv[])
714                                  (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) !=
715                          p_section[i]->page_count)                          p_section[i]->page_count)
716                  {                  {
717                          printf("Inconsistent page count in section %d offset %d, %d != %d, "                          printf("#3 Inconsistent page count in section %d offset %d, %d != %d, "
718                                     "visible_article_count = %d, last_page_visible_count = %d\n",                                     "visible_article_count = %d, last_page_visible_count = %d\n",
719                                     i, j,                                     i, j,
720                                     p_section[i]->visible_article_count / BBS_article_limit_per_page +                                     p_section[i]->visible_article_count / BBS_article_limit_per_page +
# Line 704  int main(int argc, char *argv[]) Line 733  int main(int argc, char *argv[])
733    
734          printf("Testing #5 ...\n");          printf("Testing #5 ...\n");
735    
736          if (section_list_try_rw_lock(NULL, 1) < 0)          if (section_list_rw_lock(NULL) < 0)
737          {          {
738                  printf("section_list_try_rw_lock(sid = %d) error\n", 0);                  printf("section_list_rw_lock(sid = %d) error\n", 0);
739          }          }
740    
741          if (article_block_reset() != 0)          if (article_block_reset() != 0)
# Line 729  int main(int argc, char *argv[]) Line 758  int main(int argc, char *argv[])
758    
759          for (i = 0; i < section_count / 2; i++)          for (i = 0; i < section_count / 2; i++)
760          {          {
761                  if (section_list_try_rw_lock(p_section[i], 1) < 0)                  if (section_list_rw_lock(p_section[i]) < 0)
762                  {                  {
763                          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);
764                          break;                          break;
765                  }                  }
766    
# Line 747  int main(int argc, char *argv[]) Line 776  int main(int argc, char *argv[])
776                          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));
777                          article.sid = i * 3 + 1;                          article.sid = i * 3 + 1;
778                          article.cid = article.aid;                          article.cid = article.aid;
779                          article.uid = 1; // TODO: randomize                          article.uid = 1;
780                          article.visible = 1;                          article.visible = 1;
781                          article.excerption = 0;                          article.excerption = 0;
782                          article.ontop = 0;                          article.ontop = 0;
783                          article.lock = 0;                          article.lock = 0;
784                            article.transship = 0;
785    
786                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
787                          {                          {
# Line 771  int main(int argc, char *argv[]) Line 801  int main(int argc, char *argv[])
801    
802          for (i = 0; i < section_count / 2; i++)          for (i = 0; i < section_count / 2; i++)
803          {          {
804                  if (section_list_try_rw_lock(p_section[i], 1) < 0)                  if (section_list_rw_lock(p_section[i]) < 0)
805                  {                  {
806                          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);
807                          break;                          break;
808                  }                  }
809    
# Line 804  int main(int argc, char *argv[]) Line 834  int main(int argc, char *argv[])
834    
835          for (i = 0; i < section_count / 2; i++)          for (i = 0; i < section_count / 2; i++)
836          {          {
837                  if (section_list_try_rw_lock(p_section[i], 1) < 0)                  if (section_list_rw_lock(p_section[i]) < 0)
838                  {                  {
839                          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);
840                          break;                          break;
841                  }                  }
842    
843                  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)
844                  {                  {
845                          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);
846    
847                          if (section_list_rw_unlock(p_section[i]) < 0)                          if (section_list_rw_unlock(p_section[i]) < 0)
848                          {                          {
# Line 862  int main(int argc, char *argv[]) Line 892  int main(int argc, char *argv[])
892    
893          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
894          {          {
895                  if (section_list_try_rd_lock(p_section[i], 1) < 0)                  if (section_list_rd_lock(p_section[i]) < 0)
896                  {                  {
897                          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);
898                          break;                          break;
899                  }                  }
900    
# Line 910  int main(int argc, char *argv[]) Line 940  int main(int argc, char *argv[])
940                  }                  }
941          }          }
942    
943            printf("Testing #6 ...\n");
944    
945            for (i = 0; i < section_count; i++)
946            {
947                    if (section_list_rd_lock(p_section[i]) < 0)
948                    {
949                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
950                            break;
951                    }
952            }
953    
954            printf("Try rw_lock for 5 sec...\n");
955            if (section_list_try_rw_lock(NULL, 5) == 0)
956            {
957                    printf("section_list_try_rw_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
958            }
959    
960            for (i = 0; i < section_count; i++)
961            {
962                    if (section_list_rd_unlock(p_section[i]) < 0)
963                    {
964                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
965                            break;
966                    }
967            }
968    
969            if (section_list_try_rw_lock(NULL, 5) < 0)
970            {
971                    printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
972            }
973    
974            for (i = 0; i < section_count; i++)
975            {
976                    if (section_list_try_rd_lock(p_section[i], 0) == 0)
977                    {
978                            printf("section_list_try_rd_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
979                            break;
980                    }
981            }
982    
983            if (section_list_rw_unlock(NULL) < 0)
984            {
985                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
986            }
987    
988          printf("Press ENTER to exit...");          printf("Press ENTER to exit...");
989          getchar();          getchar();
990    
991            user_list_pool_cleanup();
992          section_list_cleanup();          section_list_cleanup();
993          article_block_cleanup();          article_block_cleanup();
994          trie_dict_cleanup();          trie_dict_cleanup();
995    
996            if (unlink(USER_LIST_SHM_FILE) < 0)
997            {
998                    log_error("unlink(%s) error\n", USER_LIST_SHM_FILE);
999                    return -1;
1000            }
1001    
1002          if (unlink(TRIE_DICT_SHM_FILE) < 0)          if (unlink(TRIE_DICT_SHM_FILE) < 0)
1003          {          {
1004                  log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);                  log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1