TestRedis.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php declare(strict_types=0);
  2. use PHPUnit\Framework\TestCase;
  3. define('APP_ID', 'test');
  4. define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
  5. require_once(BASE_ROOT_PATH . '/global.php');
  6. require_once(BASE_CORE_PATH . '/lrlz.php');
  7. require_once(BASE_ROOT_PATH . '/fooder.php');
  8. require_once (BASE_ROOT_PATH . '/helper/util_helper.php');
  9. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  10. require_once(BASE_HELPER_PATH . '/refill/policy/rlock.php');
  11. require_once(BASE_HELPER_PATH . '/refill/policy/rstorage.php');
  12. function sub_callback($redis, $chan, $msg)
  13. {
  14. switch($chan) {
  15. case 'chan-1':
  16. print "get $msg from $chan\n";
  17. break;
  18. case 'chan-2':
  19. print "get $msg FROM $chan\n";
  20. break;
  21. case 'chan-3':
  22. break;
  23. }
  24. }
  25. function hello(&$name = '')
  26. {
  27. Log::record("name = {$name}",Log::DEBUG);
  28. $name = "yes";
  29. }
  30. class TestRedis extends TestCase
  31. {
  32. public static function setUpBeforeClass() : void
  33. {
  34. Base::run_util();
  35. }
  36. public function testRef()
  37. {
  38. hello($val);
  39. }
  40. public function testIncr()
  41. {
  42. $key = '100905';
  43. $ins = Cache::getInstance('cacheredis');
  44. $ret = $ins->incr($key);
  45. $ret = $ins->incrby($key, 6);
  46. $ret = $ins->decr($key);
  47. $ret = $ins->decrby($key, 10);
  48. $ret = $ins->get_org($key);
  49. $ret = $ins->get_org('yyyyyyxxxx');
  50. }
  51. public function testzIncr()
  52. {
  53. for ($i = 0; $i < 300; ++$i) {
  54. $x = util::mem_no();
  55. printf("%d \r\n",$x);
  56. }
  57. }
  58. public function testMember()
  59. {
  60. $mod = Model('member');
  61. $mobile = $mod->getMemberInfoByID(36490,'member_no');
  62. }
  63. public function testGetgid()
  64. {
  65. $url = 'http://images.shobserver.com/news/news/2016/12/24/0370ef86-7cea-4094-bfd4-2d6a284f8bf8.jpg?goods_id=3000';
  66. $exp = '/goods_id=(\d{1,9})$/';
  67. $x = preg_match_all($exp, $url, $match);
  68. }
  69. public function testRelation()
  70. {
  71. $x = 'a:335:{i:0;N;i:1;i:36490;i:2;i:36662;i:3;i:37260;i:4;i:37378;i:5;i:38606;i:6;i:38610;i:7;i:39254;i:8;i:39258;i:9;i:39259;i:10;i:39260;i:11;i:39265;i:12;i:39266;i:13;i:39282;i:14;i:39283;i:15;i:39285;i:16;i:39296;i:17;i:39324;i:18;i:39326;i:19;i:39348;i:20;i:39441;i:21;i:39446;i:22;i:39458;i:23;i:39478;i:24;i:39486;i:25;i:39487;i:26;i:39488;i:27;i:39489;i:28;i:39492;i:29;i:39496;i:30;i:39505;i:31;i:39520;i:32;i:39523;i:33;i:39524;i:34;i:39525;i:35;i:39526;i:36;i:39527;i:37;i:39528;i:38;i:39529;i:39;i:39531;i:40;i:39535;i:41;i:39536;i:42;i:39537;i:43;i:39538;i:44;i:39541;i:45;i:39546;i:46;i:39579;i:47;i:39585;i:48;i:39586;i:49;i:39587;i:50;i:39588;i:51;i:39604;i:52;i:39605;i:53;i:39614;i:54;i:39615;i:55;i:39616;i:56;i:39637;i:57;i:39641;i:58;i:39647;i:59;i:39648;i:60;i:39649;i:61;i:39654;i:62;i:39655;i:63;i:39656;i:64;i:39657;i:65;i:39658;i:66;i:39659;i:67;i:39660;i:68;i:39661;i:69;i:39662;i:70;i:39663;i:71;i:39664;i:72;i:39665;i:73;i:39668;i:74;i:39670;i:75;i:39673;i:76;i:39675;i:77;i:39680;i:78;i:39681;i:79;i:39682;i:80;i:39683;i:81;i:39684;i:82;i:39685;i:83;i:39686;i:84;i:39687;i:85;i:39688;i:86;i:39689;i:87;i:39690;i:88;i:39691;i:89;i:39692;i:90;i:39693;i:91;i:39694;i:92;i:39695;i:93;i:39696;i:94;i:39697;i:95;i:39698;i:96;i:39699;i:97;i:39700;i:98;i:39701;i:99;i:39702;i:100;i:39703;i:101;i:39704;i:102;i:39705;i:103;i:39706;i:104;i:39707;i:105;i:39708;i:106;i:39709;i:107;i:39710;i:108;i:39714;i:109;i:39715;i:110;i:39716;i:111;i:39717;i:112;i:39718;i:113;i:39719;i:114;i:39720;i:115;i:39733;i:116;i:39734;i:117;i:39784;i:118;i:39786;i:119;i:39797;i:120;i:39799;i:121;i:39803;i:122;i:39805;i:123;i:39807;i:124;i:39809;i:125;i:39813;i:126;i:39814;i:127;i:39834;i:128;i:39838;i:129;i:39839;i:130;i:39840;i:131;i:39842;i:132;i:39845;i:133;i:39846;i:134;i:39847;i:135;i:39848;i:136;i:39849;i:137;i:39850;i:138;i:39851;i:139;i:39852;i:140;i:39853;i:141;i:39854;i:142;i:39855;i:143;i:39856;i:144;i:39858;i:145;i:39859;i:146;i:39864;i:147;i:39865;i:148;i:39867;i:149;i:39868;i:150;i:39869;i:151;i:39870;i:152;i:39871;i:153;i:39872;i:154;i:39874;i:155;i:39875;i:156;i:39877;i:157;i:39880;i:158;i:39881;i:159;i:39884;i:160;i:39885;i:161;i:39886;i:162;i:39889;i:163;i:39897;i:164;i:39898;i:165;i:39912;i:166;i:39913;i:167;i:39914;i:168;i:39915;i:169;i:39916;i:170;i:39917;i:171;i:39922;i:172;i:39923;i:173;i:39924;i:174;i:39925;i:175;i:39938;i:176;i:39942;i:177;i:39946;i:178;i:39951;i:179;i:39953;i:180;i:39964;i:181;i:39972;i:182;i:39976;i:183;i:39978;i:184;i:39979;i:185;i:39980;i:186;i:39981;i:187;i:39983;i:188;i:39984;i:189;i:39986;i:190;i:39992;i:191;i:39993;i:192;i:39994;i:193;i:39995;i:194;i:39996;i:195;i:39998;i:196;i:39999;i:197;i:40001;i:198;i:40002;i:199;i:40004;i:200;i:40005;i:201;i:40006;i:202;i:40007;i:203;i:40008;i:204;i:40009;i:205;i:40010;i:206;i:40011;i:207;i:40012;i:208;i:40015;i:209;i:40016;i:210;i:40017;i:211;i:40019;i:212;i:40020;i:213;i:40021;i:214;i:40022;i:215;i:40023;i:216;i:40024;i:217;i:40025;i:218;i:40026;i:219;i:40027;i:220;i:40028;i:221;i:40029;i:222;i:40030;i:223;i:40031;i:224;i:40032;i:225;i:40033;i:226;i:40034;i:227;i:40035;i:228;i:40036;i:229;i:40037;i:230;i:40038;i:231;i:40039;i:232;i:40040;i:233;i:40041;i:234;i:40042;i:235;i:40043;i:236;i:40045;i:237;i:40047;i:238;i:40048;i:239;i:40049;i:240;i:40051;i:241;i:40052;i:242;i:40054;i:243;i:40055;i:244;i:40056;i:245;i:40057;i:246;i:40059;i:247;i:40060;i:248;i:40061;i:249;i:40065;i:250;i:40066;i:251;i:40071;i:252;i:40072;i:253;i:40073;i:254;i:40074;i:255;i:40075;i:256;i:40076;i:257;i:40077;i:258;i:40078;i:259;i:40079;i:260;i:40080;i:261;i:40081;i:262;i:40082;i:263;i:40083;i:264;i:40088;i:265;i:40090;i:266;i:40091;i:267;i:40094;i:268;i:40097;i:269;i:40098;i:270;i:40101;i:271;i:40102;i:272;i:40107;i:273;i:40109;i:274;i:40111;i:275;i:40114;i:276;i:40115;i:277;i:40116;i:278;i:40117;i:279;i:40118;i:280;i:40120;i:281;i:40122;i:282;i:40124;i:283;i:40125;i:284;i:40126;i:285;i:40127;i:286;i:40128;i:287;i:40130;i:288;i:40132;i:289;i:40135;i:290;i:40136;i:291;i:40140;i:292;i:40142;i:293;i:40144;i:294;i:40145;i:295;i:40150;i:296;i:40154;i:297;i:40155;i:298;i:40161;i:299;i:40164;i:300;i:40165;i:301;i:40166;i:302;i:40179;i:303;i:40180;i:304;i:40181;i:305;i:40182;i:306;i:40184;i:307;i:40185;i:308;i:40186;i:309;i:40187;i:310;i:40189;i:311;i:40190;i:312;i:40192;i:313;i:40193;i:314;i:40196;i:315;i:40213;i:316;i:40219;i:317;i:40223;i:318;i:40231;i:319;i:40233;i:320;i:40237;i:321;i:40238;i:322;i:40246;i:323;i:40247;i:324;i:40248;i:325;i:40249;i:326;i:40250;i:327;i:40251;i:328;i:40255;i:329;i:40256;i:330;i:40257;i:331;i:40258;i:332;i:40259;i:333;i:40261;i:334;i:40264;}';
  72. $y = unserialize($x);
  73. }
  74. public function testJson()
  75. {
  76. $x = '[{"comment":"njhhjks","dispalyImages":[{"imagePath":"/storage/emulated/0/Yoho/YohoBuy/Img/Entry/11.11_n@3x.png","imageState":1,"imageUrl":"/Users/stanley-king/work/PHPProject/shopnc/data/upload/upfile/20170104043357-7ab582c6-67ec-e941-4234-8fdb01d37921.png","primaryKey":"F88154BE695749FC9FAC4B984B113F40"}],"goods_id":1285,"goods_image":"http://192.168.1.200/data/upload/shop/store/goods/6/6_05065322566829042_240.jpg","images":["/Users/stanley-king/work/PHPProject/shopnc/data/upload/upfile/20170104043357-7ab582c6-67ec-e941-4234-8fdb01d37921.png"],"score":5}]';
  77. $y = json_decode($x,true);
  78. }
  79. public function testHome()
  80. {
  81. $y = 'a:1:{s:4:"item";a:5:{s:24:"s0_05360612357121233.jpg";a:4:{s:5:"image";s:24:"s0_05398626403134398.jpg";s:4:"type";s:3:"url";s:4:"data";s:156:"http://activity.m.yiguo.com/activity/201702060.html?cityid=312d0556-0671-4f2e-8bac-7b8873b5a03a&amp;preview=1&amp;utm_source=20170206&amp;utm_medium=lrlzqrj";s:5:"title";s:24:"遇见你的不二情书";}s:24:"s0_05363274022351304.jpg";a:4:{s:5:"image";s:24:"s0_05399573176047649.jpg";s:4:"type";s:7:"special";s:4:"data";s:3:"235";s:5:"title";s:24:"皮肤身材管理专场";}s:24:"s0_05373018466048170.jpg";a:4:{s:5:"image";s:24:"s0_05394487095218686.jpg";s:4:"type";s:7:"special";s:4:"data";s:3:"231";s:5:"title";s:27:"开工不能顶着熊猫眼";}s:24:"s0_05398626270780316.jpg";a:4:{s:5:"image";s:24:"s0_05398626270780316.jpg";s:4:"type";s:7:"special";s:4:"data";s:3:"230";s:5:"title";s:23:"2017 “红”运当头";}s:24:"s0_05399573071346216.jpg";a:4:{s:5:"image";s:24:"s0_05399573071346216.jpg";s:4:"type";s:7:"special";s:4:"data";s:3:"232";s:5:"title";s:21:"直男最爱桃花色";}}}';
  82. $v = unserialize($y);
  83. }
  84. public function testPublishTags()
  85. {
  86. $redis = new Redis();
  87. $redis->pconnect('127.0.0.1',6379);
  88. $redis->publish('ch_index',serialize(array('type'=>'tags','value' => 0))); // send message to channel 1.
  89. $redis->publish('ch_index',serialize(array('type'=>'special','value' => 0))); // send message to channel 1.
  90. $redis->close();
  91. }
  92. public function testPublishSpecial()
  93. {
  94. $redis = new Redis();
  95. $redis->pconnect('127.0.0.1',6379);
  96. $redis->publish('ch_index',serialize(array('type'=>'special','value' => 0))); // send message to channel 1.
  97. $redis->close();
  98. }
  99. public function testPublishSearch()
  100. {
  101. $redis = new Redis();
  102. $redis->pconnect('127.0.0.1',6379);
  103. $redis->publish('searcher',serialize(array('type'=>'init','value' => 0)));
  104. $redis->close();
  105. }
  106. public function testSubscribe()
  107. {
  108. $redis = new Redis();
  109. $redis->pconnect('127.0.0.1',6379);
  110. $data = $redis->pubsub('numsub');
  111. $data = $redis->pubsub('numsub',array('chan-1'));
  112. $redis->subscribe(array('chan-1', 'chan-2', 'chan-3'), 'sub_callback');
  113. $data = $redis->pubsub('numsub');
  114. $data = $redis->pubsub('numpat');
  115. }
  116. public function testFcode()
  117. {
  118. QueueClient::push('createGoodsFCode', array('goods_commonid' => 810, 'fc_count' => 100, 'fc_prefix' => 'NWY','fc_validate' => 10));
  119. }
  120. public function testUrl()
  121. {
  122. $x= htmlspecialchars('http://t.cn/RaVl0wQ');
  123. }
  124. public function testLogic()
  125. {
  126. $y = 5;
  127. $x = !isset($y) || is_object($y) == false;
  128. $z = !isset($y) || (is_object($y) == false);
  129. }
  130. public function testWRcache()
  131. {
  132. $t = current(['1000119000003695610' => time()]);
  133. wcache("card_expired",['1000119000003695610' => time()],'');
  134. $x = rcache('card_expired','','1000119000003695610');
  135. }
  136. public function testSpeed()
  137. {
  138. for ($i = 0; $i < 1000000; ++$i) {
  139. refill\util::incr_commit('lingzh',1,100,1,true);
  140. $ret = refill\util::hget_commit_sec('lingzh',1,100,1,time(),true);
  141. }
  142. }
  143. public function testOrderSn()
  144. {
  145. $order_sn = $this->_makeOrderSn();
  146. }
  147. private function _makeOrderSn()
  148. {
  149. static $prime_num = [11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97];
  150. static $factor = 0;
  151. if($factor == 0) {
  152. $pos = mt_rand(0,count($prime_num) - 1);
  153. $factor = $prime_num[$pos];
  154. if($factor < 100) {
  155. $factor += mt_rand(100,count($prime_num) - 1);
  156. }
  157. }
  158. $prefix = (getmypid() * $factor) % 1000;
  159. return sprintf('%03d', $prefix)
  160. . sprintf('%03d', mt_rand(100,999))
  161. . sprintf('%010d', time() - 946656000)
  162. . sprintf('%06d', (float)microtime() * 1000000);
  163. }
  164. public function testSHA1()
  165. {
  166. $params['supnumber'] = 'null';
  167. $y = strtolower($params['supnumber']) == 'null' ? '' : $params['supnumber'];
  168. $z = strtolower($params['supnumber']) ?? '';
  169. $params = [];
  170. $y = strtolower($params['supnumber']) == 'null' ? '' : $params['supnumber'];
  171. $z = strtolower($params['supnumber']) ?? '';
  172. }
  173. public function testJsonEmpty()
  174. {
  175. $resp = json_decode('',true);
  176. }
  177. public function testFormat()
  178. {
  179. $body = 'a2421040915130540';
  180. $ret = preg_match('/^\d{4}(?P<month>\d{2})(?P<day>\d{2})(?P<hour>\d{2})(?P<min>\d{2})\d{4}/u', $body, $matches);
  181. $short_no = substr('1000115100002086143',-6);
  182. $x = 'http://www.sina.com.cn';
  183. $y = urlencode($x);
  184. }
  185. public function testMillsec()
  186. {
  187. //获取毫秒的时间戳
  188. $cur = microtime (true);
  189. $cur = intval($cur * 1000);
  190. $time = explode ( " ", microtime () );
  191. $time = $time[1] . ($time[0] * 1000);
  192. $time2 = explode( ".", $time );
  193. $time = $time2[0];
  194. return $time;
  195. }
  196. public function testStorage()
  197. {
  198. $storage = new refill\rstorage();
  199. $storage->allow(1,1,200);
  200. }
  201. public function testRlock()
  202. {
  203. $val = refill\rlock::hget_mch_storage(1,1,200);
  204. $val = refill\rlock::incr_mch_storage(1,1,200,1);
  205. }
  206. public function testCancel()
  207. {
  208. $mod_refill = Model('refill_order');
  209. $logic_vr_order = Logic("vr_order");
  210. $order_sns = ['2329260675344960788706','0915120675344960799565'];
  211. foreach ($order_sns as $order_sn) {
  212. $order_info = Model('vr_order')->getOrderInfo(['order_sn' => $order_sn]);
  213. $logic_vr_order->changeOrderStateCancel($order_info, '', "充值失败");
  214. $mod_refill->edit(['order_id' => $order_info['order_id']], ['notify_state' =>1, 'notify_time' => time()]);
  215. }
  216. }
  217. public function testCancelPayed()
  218. {
  219. $mod_refill = Model('refill_order');
  220. $logic_vr_order = Logic("vr_order");
  221. $cond = [
  222. 'refill_order.inner_status' => 0,
  223. 'vr_order.order_state' => ORDER_STATE_PAY,
  224. 'refill_order.order_time' => ['lt', time() - 300]
  225. ];
  226. $items = Model('')->table('refill_order,vr_order')
  227. ->field('vr_order.order_state,vr_order.order_sn' )
  228. ->join('inner')
  229. ->on('refill_order.order_id=vr_order.order_id')
  230. ->where($cond)
  231. ->select();
  232. foreach ($items as $item)
  233. {
  234. $order_sn = $item['order_sn'];
  235. $order_info = Model('vr_order')->getOrderInfo(['order_sn' => $order_sn]);
  236. $logic_vr_order->changeOrderStateCancel($order_info, '', "充值失败");
  237. $mod_refill->edit(['order_id' => $order_info['order_id']], ['notify_state' =>1, 'notify_time' => time()]);
  238. }
  239. }
  240. public function testSec()
  241. {
  242. $cur_min = 0;
  243. while(true)
  244. {
  245. for ($i = 0; $i < 61; $i++) {
  246. $cur_time = time();
  247. $time_sec = $cur_time;
  248. $next_min = $time_sec - $time_sec % 60;
  249. if($next_min > $cur_min && $time_sec % 60 == 3) {
  250. $cur_min = $next_min;
  251. break;
  252. } else {
  253. sleep(1);
  254. }
  255. }
  256. $a = strftime("%Y-%m-%d %H:%M:%S",$cur_time);
  257. $b = strftime("%Y-%m-%d %H:%M:%S",$time_sec);
  258. $c = strftime("%Y-%m-%d %H:%M:%S",$cur_min);
  259. $d = strftime("%Y-%m-%d %H:%M:%S",$next_min);
  260. Log::record("cur_time={$a},time_sec={$b},cur_min={$c},next_min={$d}",Log::DEBUG);
  261. }
  262. }
  263. public function testValue()
  264. {
  265. $ret = refill\util::get_order_channels(1,"xxxxxfdasfdas");
  266. refill\util::del_order_channels(1,"xxxxxfdasfdas");
  267. }
  268. public function testPushQueue()
  269. {
  270. refill\util::push_queue_order(1,"a",ORDER_STATE_QUEUE);
  271. $ret = refill\util::query_queue_order(1,"a");
  272. refill\util::pop_queue_order(1,"a");
  273. $ret = refill\util::query_queue_order(1,"a");
  274. $ret = refill\util::query_queue_order(1,"a");
  275. $x = 1;
  276. }
  277. public function testPushReturn()
  278. {
  279. $ret = refill\util::push_add(['xxxx']);
  280. $ret = refill\util::push_notify('xxx',['xxxx']);
  281. }
  282. public function testMonitor()
  283. {
  284. include(BASE_CONFIG_PATH . CONFIG_PREFIX . '/refill.ini.php');
  285. require_once(BASE_HELPER_PATH . '/refill/policy/try_judge.php');
  286. $canceller = new refill\try_judge();
  287. $canceller->can_try(1092,1);
  288. }
  289. ////docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestRedis::testCancel)( .*)?$/" --test-suffix TestRedis.php /var/www/html/test
  290. ////docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestRedis::testCancelPayed)( .*)?$/" --test-suffix TestRedis.php /var/www/html/test
  291. public static function tearDownAfterClass() : void
  292. {
  293. }
  294. }