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.
 
 
 
 
 
 

193 lines
6.1 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 "test.h"
  9. #include "cache_storage.h"
  10. #include "tcp_cache_server.h"
  11. #include "cache_over_ip.h"
  12. #include "base_cache.h"
  13. #include <booster/intrusive_ptr.h>
  14. #include <cppcms/config.h>
  15. #include "test.h"
  16. #include <iostream>
  17. #include <booster/auto_ptr_inc.h>
  18. #include <time.h>
  19. #ifdef CPPCMS_WIN_NATIVE
  20. #include <windows.h>
  21. void ssleep(int x) { Sleep(x*1000); }
  22. #else
  23. #include <unistd.h>
  24. void ssleep(int x) { sleep(x); }
  25. #endif
  26. void test_cache(booster::intrusive_ptr<cppcms::impl::base_cache> cache,bool test_generators=true)
  27. {
  28. std::string tmp;
  29. std::set<std::string> tags;
  30. TEST(cache->fetch("foo",tmp,0)==false);
  31. time_t t_and_1=time(0)+1,ttmp=0;
  32. cache->store("foo","bar",tags,t_and_1);
  33. TEST(cache->fetch("foo",tmp,&tags)==true);
  34. TEST(tmp=="bar");
  35. TEST(tags.size()==1);
  36. TEST(tags.find("foo")!=tags.end());
  37. TEST(cache->fetch("foo",0,0,&ttmp)==true);
  38. TEST(ttmp==t_and_1);
  39. ssleep(2);
  40. TEST(cache->fetch("foo",tmp,0)==false);
  41. tags.clear();
  42. cache->clear();
  43. tags.insert("beep");
  44. cache->store("foo","bar",tags,time(0)+5);
  45. cache->store("bar","foo",tags,time(0)+5);
  46. tags.clear();
  47. cache->store("bee","bzzz",tags,time(0)+5);
  48. TEST(cache->fetch("foo",tmp,0) && tmp=="bar");
  49. TEST(cache->fetch("bar",tmp,0) && tmp=="foo");
  50. TEST(cache->fetch("bee",tmp,0) && tmp=="bzzz");
  51. unsigned keys=0,triggers=0;
  52. cache->stats(keys,triggers);
  53. TEST(keys==3 && triggers==5);
  54. cache->rise("beep");
  55. cache->stats(keys,triggers);
  56. TEST(keys==1 && triggers==1);
  57. TEST(cache->fetch("foo",tmp,0)==false);
  58. TEST(cache->fetch("bar",tmp,0)==false);
  59. TEST(cache->fetch("bee",tmp,0) && tmp=="bzzz");
  60. cache->clear();
  61. tags.clear();
  62. cache->store("fu","fu",tags,time(0)+25);
  63. // 25 - make sure it works on slow platforms like
  64. // ARM emulator
  65. for(unsigned i=0;i<1000;i++) {
  66. std::ostringstream key;
  67. key << i;
  68. cache->store(key.str(),key.str(),tags,time(0)+5);
  69. TEST(cache->fetch("fu",tmp,0) && tmp=="fu");
  70. }
  71. TEST(cache->fetch("fu",tmp,0) && tmp=="fu");
  72. TEST(cache->fetch("1",tmp,0)==false);
  73. TEST(cache->fetch("999",tmp,0)==true && tmp=="999");
  74. cache->clear();
  75. tags.clear();
  76. if(test_generators) {
  77. cache->store("test","test",tags,time(0)+5);
  78. cppcms::uint64_t my_gen=2;
  79. cache->store("test2","x",tags,time(0)+5,&my_gen);
  80. cppcms::uint64_t g1=0,g2=0,g3=0;
  81. TEST(cache->fetch("test",0,0,0,&g1));
  82. cache->store("test","test2",tags,time(0)+5);
  83. TEST(cache->fetch("test",&tmp,0,0,&g2));
  84. TEST(tmp=="test2");
  85. TEST(g1!=0 && g2!=0 && g1!=g2);
  86. TEST(cache->fetch("test2",0,0,0,&g3));
  87. TEST(g3==my_gen);
  88. cache->clear();
  89. }
  90. }
  91. void test_two_clients(booster::intrusive_ptr<cppcms::impl::base_cache> c1,booster::intrusive_ptr<cppcms::impl::base_cache> c2)
  92. {
  93. std::string tmp;
  94. std::set<std::string> tags;
  95. c1->store("foo","test",tags,time(0)+1);
  96. TEST(c2->fetch("foo",tmp,0));
  97. TEST(tmp=="test");
  98. c2->store("foo","test2",tags,time(0)+1);
  99. TEST(c1->fetch("foo",tmp,0) && tmp=="test2");
  100. c2->rise("foo");
  101. TEST(c1->fetch("foo",tmp,0)==false);
  102. c2->store("foo","test3",tags,time(0)+1);
  103. TEST(c1->fetch("foo",tmp,0) && tmp=="test3");
  104. c2->clear();
  105. TEST(c1->fetch("foo",tmp,0)==false);
  106. }
  107. int main()
  108. {
  109. typedef booster::shared_ptr<cppcms::sessions::session_storage_factory> sfact_type;
  110. try {
  111. std::cout << "Testing thread cache... "<< std::flush;
  112. test_cache(cppcms::impl::thread_cache_factory(20));
  113. std::cout << "Ok" << std::endl;
  114. #if !defined(CPPCMS_WIN32) && !defined(CPPCMS_NO_PREFOK_CACHE)
  115. std::cout << "Testing process cache... " << std::flush;
  116. test_cache(cppcms::impl::process_cache_factory(16*1024*1024,20));
  117. std::cout << "Ok" << std::endl;
  118. #endif
  119. std::auto_ptr<cppcms::impl::tcp_cache_service> srv1,srv2;
  120. {
  121. try {
  122. std::cout << "Testing cache over ip, single server... "<<std::flush;
  123. std::vector<std::string> ips;
  124. std::vector<int> ports;
  125. ips.push_back("127.0.0.1");
  126. ports.push_back(6001);
  127. srv1.reset(new cppcms::impl::tcp_cache_service(
  128. cppcms::impl::thread_cache_factory(20),
  129. sfact_type(),
  130. 1,"127.0.0.1",6001));
  131. test_cache(cppcms::impl::tcp_cache_factory(ips,ports,0),false);
  132. std::cout << "Ok" << std::endl;
  133. std::cout << "Testing cache over ip, single server with L1 cache... "<<std::flush;
  134. test_cache(cppcms::impl::tcp_cache_factory(ips,ports,cppcms::impl::thread_cache_factory(5)),false);
  135. std::cout << "Ok" << std::endl;
  136. srv2.reset(new cppcms::impl::tcp_cache_service(
  137. cppcms::impl::thread_cache_factory(20),
  138. sfact_type(),
  139. 1,"127.0.0.1",6002));
  140. ips.push_back("127.0.0.1");
  141. ports.push_back(6002);
  142. std::cout << "Testing cache over ip, multiple server... "<<std::flush;
  143. test_cache(cppcms::impl::tcp_cache_factory(ips,ports,0),false);
  144. std::cout << "Ok" << std::endl;
  145. std::cout << "Testing cache over ip, multiple server with L1 cache... "<<std::flush;
  146. test_cache(cppcms::impl::tcp_cache_factory(ips,ports,cppcms::impl::thread_cache_factory(5)),false);
  147. std::cout << "Ok" <<std::endl;
  148. ips.resize(1);
  149. ports.resize(1);
  150. std::cout << "Testing two clients... "<<std::flush;
  151. test_two_clients(
  152. cppcms::impl::tcp_cache_factory(ips,ports,cppcms::impl::thread_cache_factory(5)),
  153. cppcms::impl::tcp_cache_factory(ips,ports,cppcms::impl::thread_cache_factory(5)));
  154. std::cout << "Ok" <<std::endl;
  155. std::cout << "Testing two clients with L1 cache... "<<std::flush;
  156. test_two_clients(
  157. cppcms::impl::tcp_cache_factory(ips,ports,0),
  158. cppcms::impl::tcp_cache_factory(ips,ports,0));
  159. std::cout << "Ok" <<std::endl;
  160. }
  161. catch(...) {
  162. if(srv1.get()) {
  163. srv1->stop();
  164. srv1.reset();
  165. }
  166. if(srv2.get()) {
  167. srv2->stop();
  168. srv2.reset();
  169. }
  170. throw;
  171. }
  172. srv1->stop();
  173. srv2->stop();
  174. }
  175. }
  176. catch(std::exception const &e) {
  177. std::cerr << "\nFail " << e.what() << std::endl;
  178. return 1;
  179. }
  180. return 0;
  181. }