/[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.20 by sysadm, Sat May 24 13:52:44 2025 UTC Revision 1.30 by sysadm, Sat Jun 7 07:35:06 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 15  Line 15 
15   ***************************************************************************/   ***************************************************************************/
16    
17  #include "section_list.h"  #include "section_list.h"
18    #include "trie_dict.h"
19  #include "bbs.h"  #include "bbs.h"
20  #include "log.h"  #include "log.h"
21  #include <stdio.h>  #include <stdio.h>
# Line 23  Line 24 
24    
25  #define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat"  #define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat"
26  #define SECTION_LIST_SHM_FILE "~section_list_shm.dat"  #define SECTION_LIST_SHM_FILE "~section_list_shm.dat"
27    #define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat"
28    
29  const char *sname[] = {  const char *sname[] = {
30          "Test",          "Test",
# Line 68  int main(int argc, char *argv[]) Line 70  int main(int argc, char *argv[])
70                  return -1;                  return -1;
71          }          }
72    
73          log_std_redirect(STDOUT_FILENO);          log_common_redir(STDOUT_FILENO);
74          log_err_redirect(STDERR_FILENO);          log_error_redir(STDERR_FILENO);
75    
76          // - 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
77          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 88  int main(int argc, char *argv[]) Line 90  int main(int argc, char *argv[])
90          }          }
91          fclose(fp);          fclose(fp);
92    
93            if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL)
94            {
95                    log_error("fopen(%s) error\n", TRIE_DICT_SHM_FILE);
96                    return -1;
97            }
98            fclose(fp);
99    
100            if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0)
101            {
102                    printf("trie_dict_init failed\n");
103                    return -1;
104            }
105    
106          if (article_block_init(ARTICLE_BLOCK_SHM_FILE, block_count) < 0)          if (article_block_init(ARTICLE_BLOCK_SHM_FILE, block_count) < 0)
107          {          {
108                  log_error("article_block_init(%s, %d) error\n", ARTICLE_BLOCK_SHM_FILE, block_count);                  log_error("article_block_init(%s, %d) error\n", ARTICLE_BLOCK_SHM_FILE, block_count);
109                  return -2;                  return -2;
110          }          }
111    
112          if (section_list_pool_init(SECTION_LIST_SHM_FILE) < 0)          if (section_list_init(SECTION_LIST_SHM_FILE) < 0)
113          {          {
114                  log_error("section_list_pool_init(%s) error\n", SECTION_LIST_SHM_FILE);                  log_error("section_list_pool_init(%s) error\n", SECTION_LIST_SHM_FILE);
115                  return -2;                  return -2;
# Line 104  int main(int argc, char *argv[]) Line 119  int main(int argc, char *argv[])
119    
120          last_aid = 0;          last_aid = 0;
121    
122          if (section_list_try_rw_lock(NULL, 1) < 0)          if (section_list_rw_lock(NULL) < 0)
123          {          {
124                  printf("section_list_try_rw_lock(sid = %d) error\n", 0);                  printf("section_list_rw_lock(sid = %d) error\n", 0);
125          }          }
126    
127          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
# Line 164  int main(int argc, char *argv[]) Line 179  int main(int argc, char *argv[])
179                          article.tid = 0;                          article.tid = 0;
180                          article.sid = i * 3 + 1;                          article.sid = i * 3 + 1;
181                          article.cid = article.aid;                          article.cid = article.aid;
182                          article.uid = 1; // TODO: randomize                          article.uid = 1;
183                          article.visible = 1;                          article.visible = 1;
184                          article.excerption = 0;                          article.excerption = 0;
185                          article.ontop = 0;                          article.ontop = 0;
186                          article.lock = 0;                          article.lock = 0;
187                            article.transship = 0;
188    
189                          if (section_list_try_rw_lock(p_section[i], 1) < 0)                          if (section_list_rw_lock(p_section[i]) < 0)
190                          {                          {
191                                  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);
192                                  break;                                  break;
193                          }                          }
194    
# Line 195  int main(int argc, char *argv[]) Line 211  int main(int argc, char *argv[])
211                  // 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);
212          }          }
213    
214            if (last_aid != article_block_last_aid())
215            {
216                    printf("last_aid != %d\n", article_block_last_aid());
217            }
218    
219            if (article_block_article_count() != section_count * BBS_article_limit_per_section)
220            {
221                    printf("article_block_article_count() error %d != %d * %d\n",
222                               article_block_article_count(), section_count, BBS_article_limit_per_section);
223            }
224    
225          last_aid = 0;          last_aid = 0;
226    
227          for (j = 0; j < BBS_article_limit_per_section; j++)          for (j = 0; j < BBS_article_limit_per_section; j++)
# Line 209  int main(int argc, char *argv[]) Line 236  int main(int argc, char *argv[])
236                                  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);
237                          }                          }
238    
239                          if (section_list_try_rw_lock(p_section[i], 1) < 0)                          if (section_list_rw_lock(p_section[i]) < 0)
240                          {                          {
241                                  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);
242                                  break;                                  break;
243                          }                          }
244    
# Line 232  int main(int argc, char *argv[]) Line 259  int main(int argc, char *argv[])
259    
260          printf("Testing #2 ...\n");          printf("Testing #2 ...\n");
261    
262          if (section_list_try_rw_lock(NULL, 1) < 0)          if (section_list_rw_lock(NULL) < 0)
263          {          {
264                  printf("section_list_try_rw_lock(sid = %d) error\n", 0);                  printf("section_list_rw_lock(sid = %d) error\n", 0);
265          }          }
266    
267          if (article_block_reset() != 0)          if (article_block_reset() != 0)
# Line 259  int main(int argc, char *argv[]) Line 286  int main(int argc, char *argv[])
286          {          {
287                  section_first_aid = last_aid + 1;                  section_first_aid = last_aid + 1;
288    
289                  if (section_list_try_rw_lock(p_section[i], 1) < 0)                  if (section_list_rw_lock(p_section[i]) < 0)
290                  {                  {
291                          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);
292                          break;                          break;
293                  }                  }
294    
# Line 275  int main(int argc, char *argv[]) Line 302  int main(int argc, char *argv[])
302                          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));
303                          article.sid = i * 3 + 1;                          article.sid = i * 3 + 1;
304                          article.cid = article.aid;                          article.cid = article.aid;
305                          article.uid = 1; // TODO: randomize                          article.uid = 1;
306                          article.visible = 1;                          article.visible = 1;
307                          article.excerption = 0;                          article.excerption = 0;
308                          article.ontop = 0;                          article.ontop = 0;
309                          article.lock = 0;                          article.lock = 0;
310                            article.transship = 0;
311    
312                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
313                          {                          {
# Line 307  int main(int argc, char *argv[]) Line 335  int main(int argc, char *argv[])
335                  article_count = 0;                  article_count = 0;
336                  last_aid = 0;                  last_aid = 0;
337    
338                  if (section_list_try_rd_lock(p_section[i], 1) < 0)                  if (section_list_rd_lock(p_section[i]) < 0)
339                  {                  {
340                          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);
341                          break;                          break;
342                  }                  }
343    
# Line 351  int main(int argc, char *argv[]) Line 379  int main(int argc, char *argv[])
379                          continue;                          continue;
380                  }                  }
381    
382                  if (section_list_try_rd_lock(p_section[i], 1) < 0)                  if (section_list_rd_lock(p_section[i]) < 0)
383                  {                  {
384                          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);
385                          break;                          break;
386                  }                  }
387    
# Line 433  int main(int argc, char *argv[]) Line 461  int main(int argc, char *argv[])
461          {          {
462                  last_aid = 0;                  last_aid = 0;
463    
464                  if (section_list_try_rd_lock(p_section[i], 1) < 0)                  if (section_list_rd_lock(p_section[i]) < 0)
465                  {                  {
466                          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);
467                          break;                          break;
468                  }                  }
469    
# Line 475  int main(int argc, char *argv[]) Line 503  int main(int argc, char *argv[])
503    
504          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
505          {          {
506                  if (section_list_try_rw_lock(p_section[i], 1) < 0)                  if (section_list_rw_lock(p_section[i]) < 0)
507                  {                  {
508                          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);
509                          break;                          break;
510                  }                  }
511    
# Line 620  int main(int argc, char *argv[]) Line 648  int main(int argc, char *argv[])
648    
649          for (i = 0; i < BBS_max_section; i++)          for (i = 0; i < BBS_max_section; i++)
650          {          {
651                  if (section_list_try_rw_lock(p_section[i], 1) < 0)                  if (section_list_rw_lock(p_section[i]) < 0)
652                  {                  {
653                          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);
654                          break;                          break;
655                  }                  }
656    
# Line 689  int main(int argc, char *argv[]) Line 717  int main(int argc, char *argv[])
717    
718          printf("Testing #5 ...\n");          printf("Testing #5 ...\n");
719    
720          if (section_list_try_rw_lock(NULL, 1) < 0)          if (section_list_rw_lock(NULL) < 0)
721          {          {
722                  printf("section_list_try_rw_lock(sid = %d) error\n", 0);                  printf("section_list_rw_lock(sid = %d) error\n", 0);
723          }          }
724    
725          if (article_block_reset() != 0)          if (article_block_reset() != 0)
# Line 714  int main(int argc, char *argv[]) Line 742  int main(int argc, char *argv[])
742    
743          for (i = 0; i < section_count / 2; i++)          for (i = 0; i < section_count / 2; i++)
744          {          {
745                  if (section_list_try_rw_lock(p_section[i], 1) < 0)                  if (section_list_rw_lock(p_section[i]) < 0)
746                  {                  {
747                          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);
748                          break;                          break;
749                  }                  }
750    
# Line 732  int main(int argc, char *argv[]) Line 760  int main(int argc, char *argv[])
760                          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));
761                          article.sid = i * 3 + 1;                          article.sid = i * 3 + 1;
762                          article.cid = article.aid;                          article.cid = article.aid;
763                          article.uid = 1; // TODO: randomize                          article.uid = 1;
764                          article.visible = 1;                          article.visible = 1;
765                          article.excerption = 0;                          article.excerption = 0;
766                          article.ontop = 0;                          article.ontop = 0;
767                          article.lock = 0;                          article.lock = 0;
768                            article.transship = 0;
769    
770                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
771                          {                          {
# Line 756  int main(int argc, char *argv[]) Line 785  int main(int argc, char *argv[])
785    
786          for (i = 0; i < section_count / 2; i++)          for (i = 0; i < section_count / 2; i++)
787          {          {
788                  if (section_list_try_rw_lock(p_section[i], 1) < 0)                  if (section_list_rw_lock(p_section[i]) < 0)
789                  {                  {
790                          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);
791                          break;                          break;
792                  }                  }
793    
# Line 789  int main(int argc, char *argv[]) Line 818  int main(int argc, char *argv[])
818    
819          for (i = 0; i < section_count / 2; i++)          for (i = 0; i < section_count / 2; i++)
820          {          {
821                  if (section_list_try_rw_lock(p_section[i], 1) < 0)                  if (section_list_rw_lock(p_section[i]) < 0)
822                  {                  {
823                          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);
824                          break;                          break;
825                  }                  }
826    
827                  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)
828                  {                  {
829                          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);
830    
831                          if (section_list_rw_unlock(p_section[i]) < 0)                          if (section_list_rw_unlock(p_section[i]) < 0)
832                          {                          {
# Line 847  int main(int argc, char *argv[]) Line 876  int main(int argc, char *argv[])
876    
877          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
878          {          {
879                  if (section_list_try_rd_lock(p_section[i], 1) < 0)                  if (section_list_rd_lock(p_section[i]) < 0)
880                  {                  {
881                          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);
882                          break;                          break;
883                  }                  }
884    
# Line 895  int main(int argc, char *argv[]) Line 924  int main(int argc, char *argv[])
924                  }                  }
925          }          }
926    
927            printf("Testing #6 ...\n");
928    
929            for (i = 0; i < section_count; i++)
930            {
931                    if (section_list_rd_lock(p_section[i]) < 0)
932                    {
933                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
934                            break;
935                    }
936            }
937    
938            printf("Try rw_lock for 5 sec...\n");
939            if (section_list_try_rw_lock(NULL, 5) == 0)
940            {
941                    printf("section_list_try_rw_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
942            }
943    
944            for (i = 0; i < section_count; i++)
945            {
946                    if (section_list_rd_unlock(p_section[i]) < 0)
947                    {
948                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
949                            break;
950                    }
951            }
952    
953            if (section_list_try_rw_lock(NULL, 5) < 0)
954            {
955                    printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
956            }
957    
958            for (i = 0; i < section_count; i++)
959            {
960                    if (section_list_try_rd_lock(p_section[i], 0) == 0)
961                    {
962                            printf("section_list_try_rd_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
963                            break;
964                    }
965            }
966    
967            if (section_list_rw_unlock(NULL) < 0)
968            {
969                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
970            }
971    
972          printf("Press ENTER to exit...");          printf("Press ENTER to exit...");
973          getchar();          getchar();
974    
975            section_list_cleanup();
976          article_block_cleanup();          article_block_cleanup();
977          section_list_pool_cleanup();          trie_dict_cleanup();
978    
979            if (unlink(TRIE_DICT_SHM_FILE) < 0)
980            {
981                    log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);
982                    return -1;
983            }
984    
985          if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)          if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)
986          {          {


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

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