ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

181 lines
4.7 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include <cppcms/defs.h>
  9. #include <cppcms/config.h>
  10. #include "test.h"
  11. #include <cppcms/session_storage.h>
  12. #include "session_memory_storage.h"
  13. #ifdef CPPCMS_WIN_NATIVE
  14. #include "session_win32_file_storage.h"
  15. #include <windows.h>
  16. #else
  17. #include "session_posix_file_storage.h"
  18. #include <sys/types.h>
  19. #include <dirent.h>
  20. #endif
  21. #ifndef CPPCMS_NO_TCP_CACHE
  22. #include "tcp_cache_server.h"
  23. #include "session_tcp_storage.h"
  24. #endif
  25. #include <booster/function.h>
  26. #include <booster/backtrace.h>
  27. #include <string.h>
  28. #include <booster/auto_ptr_inc.h>
  29. #include <iostream>
  30. #include <vector>
  31. #include <stdio.h>
  32. #include <time.h>
  33. std::string dir = "./sessions";
  34. std::string bs="0123456789abcdef0123456789abcde";
  35. void do_nothing() {}
  36. void test(booster::shared_ptr<cppcms::sessions::session_storage> storage,cppcms::sessions::session_storage_factory &f)
  37. {
  38. time_t now=time(0)+3;
  39. storage->save(bs+"1",now,"");
  40. std::string out="xx";
  41. time_t tout;
  42. TEST(storage->load(bs+"1",tout,out));
  43. TEST(out.empty());
  44. TEST(tout==now);
  45. storage->remove(bs+"1");
  46. TEST(!storage->load(bs+"1",tout,out));
  47. storage->save(bs+"1",now-4,"hello world");
  48. TEST(!storage->load(bs+"1",tout,out));
  49. storage->save(bs+"1",now,"hello world");
  50. TEST(storage->load(bs+"1",tout,out));
  51. TEST(out=="hello world");
  52. storage->save(bs+"2",now,"x");
  53. storage->remove(bs+"2");
  54. TEST(storage->load(bs+"1",tout,out));
  55. TEST(out=="hello world");
  56. storage->remove(bs+"1");
  57. storage->remove(bs+"2");
  58. f.gc_job();
  59. }
  60. int count_files()
  61. {
  62. #ifndef CPPCMS_WIN_NATIVE
  63. DIR *d=opendir(dir.c_str());
  64. TEST(d);
  65. int counter = 0;
  66. struct dirent *de;
  67. while((de=readdir(d))!=0) {
  68. if(strlen(de->d_name)==32)
  69. counter++;
  70. }
  71. closedir(d);
  72. return counter;
  73. #else
  74. WIN32_FIND_DATA entry;
  75. HANDLE d=FindFirstFile((dir+"/*").c_str(),&entry);
  76. int counter=0;
  77. if(d==INVALID_HANDLE_VALUE) {
  78. return 0;
  79. }
  80. do {
  81. if(strlen(entry.cFileName)==32)
  82. counter++;
  83. }while(FindNextFile(d,&entry));
  84. FindClose(d);
  85. return counter;
  86. #endif
  87. }
  88. void test_files(booster::shared_ptr<cppcms::sessions::session_storage> storage,
  89. cppcms::sessions::session_storage_factory &f)
  90. {
  91. test(storage,f);
  92. TEST(f.requires_gc());
  93. time_t now=time(0);
  94. storage->save(bs+"1",now,"test");
  95. TEST(count_files()==1);
  96. storage->remove(bs+"1");
  97. TEST(count_files()==0);
  98. storage->save(bs+"1",now-1,"test");
  99. storage->save(bs+"2",now+1,"test2");
  100. TEST(count_files()==2);
  101. f.gc_job();
  102. TEST(count_files()==1);
  103. std::string tstr;
  104. time_t ttime;
  105. TEST(!storage->load(bs+"1",ttime,tstr));
  106. TEST(storage->load(bs+"2",ttime,tstr));
  107. TEST(ttime==now+1 && tstr=="test2");
  108. storage->save(bs+"2",now-1,"test2");
  109. TEST(count_files()==1);
  110. f.gc_job();
  111. TEST(count_files()==0);
  112. }
  113. int main()
  114. {
  115. try {
  116. booster::shared_ptr<cppcms::sessions::session_storage> storage;
  117. std::auto_ptr<cppcms::sessions::session_storage_factory> storage_factory;
  118. using namespace cppcms::sessions;
  119. std::cout << "Testing memory storage" << std::endl;
  120. session_memory_storage_factory mem;
  121. storage=mem.get();
  122. test(storage,mem);
  123. std::cout << "Testing file storage" << std::endl;
  124. #ifndef CPPCMS_NO_GZIP
  125. #ifndef CPPCMS_WIN_NATIVE
  126. std::cout << "Testing single process" << std::endl;
  127. session_file_storage_factory f1(dir,5,1,false);
  128. storage=f1.get();
  129. test_files(storage,f1);
  130. std::cout << "Testing multiple process" << std::endl;
  131. session_file_storage_factory f2(dir,5,5,false);
  132. storage=f2.get();
  133. test_files(storage,f2);
  134. std::cout << "Testing single process over NFS" << std::endl;
  135. session_file_storage_factory f3(dir,5,1,true);
  136. storage=f3.get();
  137. test_files(storage,f3);
  138. #else
  139. session_file_storage_factory f(dir);
  140. storage=f.get();
  141. test_files(storage,f);
  142. #endif
  143. #endif
  144. #ifndef CPPCMS_NO_TCP_CACHE
  145. std::cout << "Testing network backend" << std::endl;
  146. std::vector<std::string> ips;
  147. ips.push_back("127.0.0.1");
  148. std::vector<int> ports;
  149. ports.push_back(8080);
  150. tcp_factory f4(ips,ports);
  151. booster::shared_ptr<cppcms::sessions::session_storage_factory>
  152. mem_ptr(new session_memory_storage_factory());
  153. cppcms::impl::tcp_cache_service service(0,mem_ptr,1,"127.0.0.1",8080);
  154. storage=f4.get();
  155. test(storage,f4);
  156. service.stop();
  157. mem_ptr.reset();
  158. #endif
  159. }
  160. catch(std::exception const &e) {
  161. std::cerr <<"Fail: " << e.what() << std::endl;
  162. std::cerr << booster::trace(e) << std::endl;
  163. return 1;
  164. }
  165. std::cout << "Ok" << std::endl;
  166. return 0;
  167. }