RedisClusterTest.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Tioncico
  5. * Date: 2019/9/24 0024
  6. * Time: 16:16
  7. */
  8. namespace Test;
  9. use EasySwoole\Redis\Client;
  10. use EasySwoole\Redis\ClusterClient;
  11. use EasySwoole\Redis\Config\RedisClusterConfig;
  12. use EasySwoole\Redis\Config\RedisConfig;
  13. use EasySwoole\Redis\Exception\RedisClusterException;
  14. use EasySwoole\Redis\Redis;
  15. use EasySwoole\Redis\RedisCluster;
  16. use PHPUnit\Framework\TestCase;
  17. use Swoole\Coroutine;
  18. class RedisClusterTest extends TestCase
  19. {
  20. /**
  21. * @var $redis RedisCluster
  22. */
  23. protected $redis;
  24. /**
  25. * @var $redisPHPSerialize RedisCluster
  26. */
  27. protected $redisPHPSerialize;
  28. protected function setUp():void
  29. {
  30. parent::setUp(); // TODO: Change the autogenerated stub
  31. $this->redis = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
  32. 'auth' => REDIS_CLUSTER_AUTH,
  33. ]));
  34. $this->redisPHPSerialize = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
  35. 'auth' => REDIS_CLUSTER_AUTH,
  36. 'serialize' => RedisConfig::SERIALIZE_PHP
  37. ]));
  38. $redis = $this->redis;
  39. $redis->flushAll();
  40. $data = $redis->rawCommand(['set', 'a', '1']);
  41. $this->assertEquals('OK', $data);
  42. $data = $redis->rawCommand(['get', 'a']);
  43. $this->assertEquals('1', $data);
  44. $redis->del('a');
  45. }
  46. function testConnect()
  47. {
  48. $this->assertTrue($this->redis->connect());
  49. $data = $this->redis->pingAll();
  50. foreach ($data as $value) {
  51. $this->assertEquals('PONG', $value);
  52. }
  53. }
  54. public function testAuth()
  55. {
  56. if (!empty(REDIS_CLUSTER_AUTH)) {
  57. $this->assertTrue($this->redis->auth(REDIS_CLUSTER_AUTH));
  58. }
  59. $this->assertTrue(true);
  60. }
  61. /**
  62. * 单元测试忽略方法,否则会出错
  63. * atestCluster
  64. * @throws \EasySwoole\Redis\Exception\RedisClusterException
  65. * @author Tioncico
  66. * Time: 15:20
  67. */
  68. function atestCluster()
  69. {
  70. $redis = $this->redis;
  71. $data = $redis->clusterNodes();
  72. $this->assertIsArray($data);
  73. $data = $redis->clusterKeySlot('key1');
  74. $this->assertGreaterThan(0, $data);
  75. // $data = $redis->clusterDelSlots([1,2,3]);
  76. // $this->assertTrue($data);
  77. // $data = $redis->clusterAddSlots([1,3]);
  78. // $this->assertTrue($data);
  79. $data = $redis->clusterCountFailureReports(current($redis->getNodeList())['name']);
  80. $this->assertIsInt($data);
  81. $data = $redis->clusterCountKeySinSlot(1);
  82. $this->assertIsInt($data);
  83. $data = $redis->clusterFailOver('FORCE');
  84. $this->assertFalse($data);
  85. $redis->tryConnectServerList();
  86. $data = $redis->clusterForget(array_column(($redis->getNodeList()), 'name')[0]);
  87. $this->assertTrue($data);
  88. $redis->set('a', 1);
  89. $data = $redis->clusterKeySlot('a');
  90. $data = $redis->clusterGetKeySinSlot($data, 1);
  91. $this->assertIsArray($data);
  92. $data = $redis->clusterInfo();
  93. $this->assertIsArray($data);
  94. $data = $redis->clusterKeySlot('b');
  95. $this->assertIsInt($data);
  96. $data = $redis->clusterMeet('172.16.253.156', '9005');
  97. $this->assertTrue($data);
  98. // $redis->tryConnectServerList();
  99. // $data = $redis->clusterReplicate(array_column(($redis->getNodeList()),'name')[0]);
  100. // var_dump($data,$redis->getErrorMsg());return;
  101. // $data = $redis->clusterReset();
  102. // var_dump($data,$redis->getErrorMsg());return;
  103. $data = $redis->clusterSaveConfig();
  104. $this->assertTrue($data);
  105. // $data = $redis->clusterSetConfigEpoch('1');
  106. // var_dump($data,$redis->getErrorMsg());return;
  107. $redis->tryConnectServerList();
  108. $data = $redis->clusterSetSlot(1, 'NODE', current($redis->getNodeList())['name']);
  109. $this->assertTrue($data);
  110. // $redis->tryConnectServerList();
  111. // $data = $redis->clusterSlaves(current($redis->getNodeList())['name']);
  112. // var_dump($data,$redis->getErrorMsg());
  113. // $this->assertIsArray($data);
  114. $data = $redis->clusterSlots();
  115. $this->assertIsArray($data);
  116. $data = $redis->readonly();
  117. $this->assertTrue($data);
  118. $data = $redis->readwrite();
  119. $this->assertTrue($data);
  120. }
  121. /**
  122. * 集群逻辑方法测试(单元测试忽略方法,否则会出错)
  123. * testClusterMethod
  124. * @author Tioncico
  125. * Time: 17:26
  126. */
  127. function atestClusterMethod()
  128. {
  129. $redis = $this->redis;
  130. $redis->tryConnectServerList();
  131. $client = $redis->getClient();
  132. $this->assertTrue($client->isConnected());
  133. $data = ['a' => 1, 'b' => 1, 'c' => 1, 'd' => 1,];
  134. $redis->mSet($data);
  135. $defaultClientPort = $redis->getDefaultClient()->getPort();
  136. $recv = [];
  137. $defaultKey = '';
  138. foreach ($data as $k => $va) {
  139. $client->sendCommand(['get', $k]);
  140. $response = $client->recv();
  141. if ($response->getErrorType() == 'MOVED') {
  142. $recv = $response;
  143. $defaultKey = $k;
  144. break;
  145. }
  146. }
  147. $this->assertTrue(!!$recv);
  148. $data = $redis->getMoveNodeId($recv);
  149. $this->assertTrue(!!$data);
  150. $client = $redis->getClient($data);
  151. //断言这个客户端和之前的不一样
  152. $this->assertNotEquals($client->getPort(), $defaultClientPort);
  153. $this->assertTrue($client instanceof ClusterClient);
  154. //进行一次get 命令的操作,还是默认的客户端
  155. $redis->get($defaultKey);
  156. $this->assertEquals($redis->getDefaultClient()->getPort(), $defaultClientPort);
  157. //重新设置默认客户端
  158. $redis->setDefaultClient($client);
  159. $this->assertNotEquals($redis->getDefaultClient()->getPort(), $defaultClientPort);
  160. $client = $redis->getDefaultClient();
  161. $client->sendCommand(['get', $defaultKey]);
  162. $response = $client->recv();
  163. $this->assertEquals($response, 1);
  164. // $data = $redis->getSlotNodeId(3300);
  165. $data = $redis->getNodeClientList();
  166. $this->assertIsArray($data);
  167. $data = $redis->getNodeList();
  168. $this->assertIsArray($data);
  169. $data = $redis->clientAuth($client, REDIS_CLUSTER_AUTH);
  170. }
  171. /**
  172. * key值操作测试
  173. * testKey
  174. * @author Tioncico
  175. * Time: 10:02
  176. */
  177. function testKey()
  178. {
  179. $redis = $this->redis;
  180. $key = 'test123213Key';
  181. $redis->select(0);
  182. $redis->set($key, 123);
  183. $data = $redis->dump($key);
  184. $this->assertTrue(!!$data);
  185. $data = $redis->dump($key . 'x');
  186. $this->assertNull($data);
  187. $data = $this->redis->exists($key);
  188. $this->assertEquals(1, $data);
  189. $data = $this->redis->expire($key, 1);
  190. $this->assertEquals(1, $data);
  191. Coroutine::sleep(2);
  192. $this->assertEquals(0, $this->redis->exists($key));
  193. $redis->set($key, 123);
  194. $data = $this->redis->pExpire($key, 1000);
  195. $this->assertEquals(1, $data);
  196. $this->assertEquals(123, $this->redis->get($key));
  197. Coroutine::sleep(2);
  198. $this->assertEquals(0, $this->redis->pExpire($key));
  199. $redis->expireAt($key, 1 * 100);
  200. Coroutine::sleep(0.1);
  201. $this->assertEquals(0, $this->redis->exists($key));
  202. $redis->set($key, 123);
  203. $data = $redis->keys("{$key}");
  204. $this->assertEquals($key, $data[0]);
  205. // $redis->select(1);
  206. $redis->del($key);
  207. $redis->select(0);
  208. try {
  209. $data = $redis->move($key, 1);
  210. $this->assertEquals(false, $data);
  211. }catch (RedisClusterException $exception){
  212. $this->assertEquals('ERR MOVE is not allowed in cluster mode', $redis->getErrorMsg());
  213. }
  214. // var_dump($data,$redis->recv());
  215. $data = $redis->exists($key);
  216. $this->assertEquals(0, $data);
  217. $redis->select(0);
  218. $redis->set($key, 123);
  219. $data = $redis->expire($key, 1);
  220. $this->assertEquals(1, $data);
  221. $data = $redis->persist($key);
  222. $this->assertEquals(1, $data);
  223. $redis->expire($key, 1);
  224. $data = $redis->pTTL($key);
  225. $this->assertLessThanOrEqual(1000, $data);
  226. $data = $redis->ttl($key);
  227. $this->assertLessThanOrEqual(1, $data);
  228. $data = $redis->randomKey();
  229. $this->assertTrue(!!$data);
  230. // $data = $redis->rename($key, $key . 'new');
  231. // $this->assertFalse($data);
  232. // $this->assertEquals(1, $redis->expire($key . 'new'));
  233. // $this->assertEquals(0, $redis->expire($key));
  234. // $data = $redis->renameNx($key, $key . 'new');
  235. // var_dump($data,$redis->getErrorMsg());
  236. // $this->assertEquals(0, $data);
  237. // $redis->renameNx($key . 'new', $key);
  238. // $data = $redis->renameNx($key, $key . 'new');
  239. // $this->assertEquals(1, $data);
  240. $redis->del($key);
  241. $data = $redis->type($key);
  242. $this->assertEquals('none', $data);
  243. $redis->set($key . 'new', 1);
  244. $data = $redis->type($key . 'new');
  245. $this->assertEquals('string', $data);
  246. //del单元测试新增
  247. $keyArr = ['a1', 'a2', 'a3', 'a4'];
  248. $valueArr = ['a1', 'a2', 'a3', 'a4'];
  249. $redis->mSet([
  250. $keyArr[0] => $valueArr[0],
  251. $keyArr[1] => $valueArr[1],
  252. $keyArr[2] => $valueArr[2],
  253. $keyArr[3] => $valueArr[3],
  254. ]);
  255. $data = $redis->del($keyArr[0]);
  256. $this->assertEquals(1, $data);
  257. $data = $redis->del($keyArr[1], $keyArr[2], $keyArr[3]);
  258. $this->assertEquals(3, $data);
  259. $redis->mSet([
  260. $keyArr[0] => $valueArr[0],
  261. $keyArr[1] => $valueArr[1],
  262. $keyArr[2] => $valueArr[2],
  263. $keyArr[3] => $valueArr[3],
  264. ]);
  265. $data = $redis->del([$keyArr[1], $keyArr[2], $keyArr[3]]);
  266. $this->assertEquals(3, $data);
  267. //unlink命令新增
  268. $keyArr = ['a1', 'a2', 'a3', 'a4'];
  269. $valueArr = ['a1', 'a2', 'a3', 'a4'];
  270. $redis->mSet([
  271. $keyArr[0] => $valueArr[0],
  272. $keyArr[1] => $valueArr[1],
  273. $keyArr[2] => $valueArr[2],
  274. $keyArr[3] => $valueArr[3],
  275. ]);
  276. $data = $redis->unlink($keyArr[0]);
  277. $this->assertEquals(1, $data);
  278. $data = $redis->unlink($keyArr[0] . 'test');
  279. $this->assertEquals(0, $data);
  280. $data = $redis->unlink($keyArr[1], $keyArr[2], $keyArr[3]);
  281. $this->assertEquals(3, $data);
  282. $redis->mSet([
  283. $keyArr[0] => $valueArr[0],
  284. $keyArr[1] => $valueArr[1],
  285. $keyArr[2] => $valueArr[2],
  286. $keyArr[3] => $valueArr[3],
  287. ]);
  288. $data = $redis->unlink([$keyArr[1], $keyArr[2], $keyArr[3]]);
  289. $this->assertEquals(3, $data);
  290. }
  291. /**
  292. * 字符串单元测试
  293. * testString
  294. * @author tioncico
  295. * Time: 下午9:41
  296. */
  297. function testString()
  298. {
  299. $redis = $this->redis;
  300. $key = 'test';
  301. $value = 1;
  302. $data = $redis->del($key);
  303. $this->assertNotFalse($data);
  304. $data = $redis->set($key, $value);
  305. $this->assertTrue($data);
  306. $redis->set($key, $value);
  307. $data = $redis->get($key);
  308. $this->assertEquals($data, $value);
  309. //set的其他作用测试(超时作用)
  310. $keyTTL = $key . 'ttl';
  311. $redis->set($keyTTL, $value, 20);
  312. $this->assertGreaterThan(18, $redis->ttl($keyTTL));
  313. $redis->set($keyTTL, $value, ['EX' => 20]);
  314. $this->assertGreaterThan(18, $redis->ttl($keyTTL));
  315. $redis->set($keyTTL, $value, ['PX' => 20000]);
  316. $this->assertGreaterThan(18000, $redis->pTTL($keyTTL));
  317. $this->assertGreaterThan(18, $redis->ttl($keyTTL));
  318. //set的其他作用测试(存在以及不存在时候设置)
  319. $keyExist = $key . 'exist';
  320. $redis->del($keyExist);
  321. $data = $redis->set($keyExist, '1', 'XX');
  322. $this->assertNull($data);
  323. $data = $redis->set($keyExist, '1', 'NX');
  324. $this->assertTrue($data);
  325. $this->assertEquals('1', $redis->get($keyExist));
  326. $data = $redis->set($keyExist, '1', 'NX');
  327. $this->assertNull($data);
  328. $data = $redis->set($keyExist, '2', 'XX');
  329. $this->assertTrue($data);
  330. $this->assertEquals('2', $redis->get($keyExist));
  331. //set组合测试
  332. $data = $redis->set($keyExist, '3', ['XX', 'EX' => 10]);
  333. $this->assertTrue($data);
  334. $this->assertEquals('3', $redis->get($keyExist));
  335. $this->assertGreaterThan(8, $redis->ttl($keyTTL));
  336. $data = $redis->set($keyExist, '3', ['NX', 'EX' => 10]);
  337. $this->assertNull($data);
  338. $redis->del($keyExist);;
  339. $redis->set($keyExist, '4', ['NX', 'EX' => 20]);
  340. $this->assertEquals('4', $redis->get($keyExist));
  341. $this->assertGreaterThan(18, $redis->ttl($keyTTL));
  342. $data = $redis->get($key);
  343. $this->assertEquals($data, $value);
  344. $data = $redis->exists($key);
  345. $this->assertEquals(1, $data);
  346. $data = $redis->set($key, $value);
  347. $this->assertTrue($data);
  348. $value += 1;
  349. $data = $redis->incr($key);
  350. $this->assertEquals($value, $data);
  351. $value += 10;
  352. $data = $redis->incrBy($key, 10);
  353. $this->assertEquals($value, $data);
  354. $value -= 1;
  355. $data = $redis->decr($key);
  356. $this->assertEquals($value, $data);
  357. $value -= 10;
  358. $data = $redis->decrBy($key, 10);
  359. $this->assertEquals($value, $data);
  360. $key = 'stringTest';
  361. $value = 'tioncico';
  362. $redis->set($key, $value);
  363. $data = $redis->getRange($key, 1, 2);
  364. $this->assertEquals('io', $data);
  365. $data = $redis->getSet($key, $value . 'a');
  366. $this->assertEquals($data, $value);
  367. $redis->set($key, $value);
  368. $bitKey = 'testBit';
  369. $bitValue = 10000;
  370. $redis->set($bitKey, $bitValue);
  371. $data = $redis->setBit($bitKey, 1, 0);
  372. $this->assertEquals(0, $data);
  373. $data = $redis->getBit($key, 1);
  374. $this->assertEquals(1, $data);
  375. $field = [
  376. 'stringField1',
  377. 'stringField2',
  378. 'stringField3',
  379. 'stringField4',
  380. 'stringField5',
  381. ];
  382. $value = [
  383. 1,
  384. 2,
  385. 3,
  386. 4,
  387. 5,
  388. ];
  389. $data = $redis->mSet([
  390. "{$field[0]}" => $value[0],
  391. "{$field[1]}" => $value[1],
  392. "{$field[2]}" => $value[2],
  393. "{$field[3]}" => $value[3],
  394. "{$field[4]}" => $value[4],
  395. ]);
  396. // var_dump($redis->getErrorMsg());
  397. $this->assertTrue($data);
  398. $data = $redis->mGet([$field[3], $field[2], $field[1]]);
  399. $this->assertEquals([$value[3], $value[2], $value[1]], $data);
  400. $data = $redis->setEx($key, 1, $value[0] . $value[0]);
  401. $this->assertTrue($data);
  402. $this->assertEquals($value[0] . $value[0], $redis->get($key));
  403. $data = $redis->pSetEx($key, 1, $value[0]);
  404. $this->assertTrue($data);
  405. $this->assertEquals($value[0], $redis->get($key));
  406. $redis->del($key);
  407. $data = $redis->setNx($key, 1);
  408. $this->assertEquals(1, $data);
  409. $redis->del($field[0]);
  410. $data = $redis->mSetNx([
  411. "{$field[0]}" => $value[0],
  412. "{$field[1]}" => $value[1],
  413. ]);
  414. $this->assertEquals([1, 0], $data);
  415. $this->assertEquals($value[1], $redis->get($field[1]));
  416. $redis->del($field[1]);
  417. $data = $redis->mSetNx([
  418. "{$field[0]}" => $value[0] + 1,
  419. "{$field[1]}" => $value[1] + 1,
  420. ]);
  421. $this->assertEquals([0, 1], $data);
  422. $this->assertEquals($value[1] + 1, $redis->get($field[1]));
  423. $redis->set($field[0], $value[0]);
  424. $data = $redis->setRange($field[0], 1, 1);
  425. $this->assertEquals(2, $data);
  426. $this->assertEquals('1' . $value[0], $redis->get($field[0]));
  427. $data = $redis->strLen($field[0]);
  428. $this->assertEquals(2, $data);
  429. $redis->set($key, 1);
  430. $data = $redis->incrByFloat($key, 0.1);
  431. $this->assertEquals(1.1, $data);
  432. $data = $redis->appEnd($field[0], '1');
  433. $this->assertEquals($redis->strLen($field[0]), $data);
  434. $this->assertEquals('1' . $value[0] . '1', $redis->get($field[0]));
  435. //迭代测试
  436. // $cursor = 0;
  437. // $redis->flushAll();
  438. // $redis->set('xxxa', '仙士可');
  439. // $redis->set('xxxb', '仙士可');
  440. // $redis->set('xxxc', '仙士可');
  441. // $redis->set('xxxd', '仙士可');
  442. // $data = [];
  443. // do {
  444. // $keys = $redis->scan($cursor, 'xxx*', 1);
  445. // $data = array_merge($data,$keys);
  446. // } while ($cursor);
  447. // $this->assertEquals(4,count($data));
  448. }
  449. /**
  450. * 序列化字符串单元测试
  451. * testString
  452. * @author tioncico
  453. * Time: 下午9:41
  454. */
  455. function testStringSerialize()
  456. {
  457. $redis = $this->redisPHPSerialize;
  458. $key = 'test';
  459. $value = 1;
  460. $data = $redis->del($key);
  461. $this->assertNotFalse($data);
  462. $data = $redis->set($key, $value);
  463. $this->assertTrue($data);
  464. $redis->set($key, $value);
  465. $data = $redis->get($key);
  466. $this->assertEquals($data, $value);
  467. //set的其他作用测试(超时作用)
  468. $keyTTL = $key . 'ttl';
  469. $redis->set($keyTTL, $value, 20);
  470. $this->assertGreaterThan(18, $redis->ttl($keyTTL));
  471. $redis->set($keyTTL, $value, ['EX' => 20]);
  472. $this->assertGreaterThan(18, $redis->ttl($keyTTL));
  473. $redis->set($keyTTL, $value, ['PX' => 20000]);
  474. $this->assertGreaterThan(18000, $redis->pTTL($keyTTL));
  475. $this->assertGreaterThan(18, $redis->ttl($keyTTL));
  476. //set的其他作用测试(存在以及不存在时候设置)
  477. $keyExist = $key . 'exist';
  478. $redis->del($keyExist);
  479. $data = $redis->set($keyExist, '1', 'XX');
  480. $this->assertNull($data);
  481. $data = $redis->set($keyExist, '1', 'NX');
  482. $this->assertTrue($data);
  483. $this->assertEquals('1', $redis->get($keyExist));
  484. $data = $redis->set($keyExist, '1', 'NX');
  485. $this->assertNull($data);
  486. $data = $redis->set($keyExist, '2', 'XX');
  487. $this->assertTrue($data);
  488. $this->assertEquals('2', $redis->get($keyExist));
  489. //set组合测试
  490. $data = $redis->set($keyExist, '3', ['XX', 'EX' => 10]);
  491. $this->assertTrue($data);
  492. $this->assertEquals('3', $redis->get($keyExist));
  493. $this->assertGreaterThan(8, $redis->ttl($keyTTL));
  494. $data = $redis->set($keyExist, '3', ['NX', 'EX' => 10]);
  495. $this->assertNull($data);
  496. $redis->del($keyExist);;
  497. $redis->set($keyExist, '4', ['NX', 'EX' => 20]);
  498. $this->assertEquals('4', $redis->get($keyExist));
  499. $this->assertGreaterThan(18, $redis->ttl($keyTTL));
  500. $data = $redis->get($key);
  501. $this->assertEquals($data, $value);
  502. $data = $redis->exists($key);
  503. $this->assertEquals(1, $data);
  504. $key = 'stringTest';
  505. $value = 'tioncico';
  506. $redis->set($key, $value);
  507. $data = $redis->getSet($key, $value . 'a');
  508. $this->assertEquals($data, $value);
  509. $redis->set($key, $value);
  510. $field = [
  511. 'stringField1',
  512. 'stringField2',
  513. 'stringField3',
  514. 'stringField4',
  515. 'stringField5',
  516. ];
  517. $value = [
  518. 1,
  519. 2,
  520. 3,
  521. 4,
  522. 5,
  523. ];
  524. $data = $redis->mSet([
  525. "{$field[0]}" => $value[0],
  526. "{$field[1]}" => $value[1],
  527. "{$field[2]}" => $value[2],
  528. "{$field[3]}" => $value[3],
  529. "{$field[4]}" => $value[4],
  530. ]);
  531. $this->assertTrue($data);
  532. $data = $redis->mGet([$field[3], $field[2], $field[1]]);
  533. $this->assertEquals([$value[3], $value[2], $value[1]], $data);
  534. $data = $redis->setEx($key, 1, $value[0] . $value[0]);
  535. $this->assertTrue($data);
  536. $this->assertEquals($value[0] . $value[0], $redis->get($key));
  537. $data = $redis->pSetEx($key, 1, $value[0]);
  538. $this->assertTrue($data);
  539. $this->assertEquals($value[0], $redis->get($key));
  540. $redis->del($key);
  541. $data = $redis->setNx($key, 1);
  542. $this->assertEquals(1, $data);
  543. $redis->del($field[0]);
  544. $data = $redis->mSetNx([
  545. "{$field[0]}" => $value[0],
  546. "{$field[1]}" => $value[1],
  547. ]);
  548. $this->assertEquals([1, 0], $data);
  549. $this->assertEquals($value[1], $redis->get($field[1]));
  550. $redis->del($field[1]);
  551. $data = $redis->mSetNx([
  552. "{$field[0]}" => $value[0] + 1,
  553. "{$field[1]}" => $value[1] + 1,
  554. ]);
  555. $this->assertEquals([0, 1], $data);
  556. $this->assertEquals($value[1] + 1, $redis->get($field[1]));
  557. //迭代测试
  558. // $cursor = 0;
  559. // $redis->flushAll();
  560. // $redis->set('xxxa', '仙士可');
  561. // $redis->set('xxxb', '仙士可');
  562. // $redis->set('xxxc', '仙士可');
  563. // $redis->set('xxxd', '仙士可');
  564. // $data = [];
  565. // do {
  566. // $keys = $redis->scan($cursor, 'xxx*', 1);
  567. // $data = array_merge($data,$keys);
  568. // } while ($cursor);
  569. // $this->assertEquals(4,count($data));
  570. // $this->assertTrue(in_array('xxxa',$data));
  571. }
  572. /**
  573. * testHash
  574. * @author Tioncico
  575. * Time: 11:54
  576. */
  577. function testHash()
  578. {
  579. $key = 'hKey';
  580. $field = [
  581. 'hField1',
  582. 'hField2',
  583. 'hField3',
  584. 'hField4',
  585. 'hField5',
  586. ];
  587. $value = [
  588. 1,
  589. 2,
  590. 3,
  591. 4,
  592. 5,
  593. ];
  594. $redis = $this->redis;
  595. $redis->del($key);
  596. $data = $redis->hSet($key, $field[0], $value[0]);
  597. $this->assertNotFalse($data);
  598. $data = $redis->hGet($key, $field[0]);
  599. $this->assertEquals($data, $value[0]);
  600. $data = $redis->hExists($key, $field[0]);
  601. $this->assertEquals(1, $data);
  602. $data = $redis->hDel($key, $field[0]);
  603. $this->assertEquals(1, $data, $redis->getErrorMsg());
  604. $data = $redis->hExists($key, $field[0]);
  605. $this->assertEquals(0, $data);
  606. $data = $redis->hMSet($key, [
  607. "{$field[0]}" => $value[0],
  608. "{$field[1]}" => $value[1],
  609. "{$field[2]}" => $value[2],
  610. "{$field[3]}" => $value[3],
  611. "{$field[4]}" => $value[4],
  612. ]);
  613. $this->assertTrue($data);
  614. $data = $redis->hValS($key);
  615. sort($data);
  616. $this->assertEquals($value, $data);
  617. $data = $redis->hGetAll($key);
  618. $keyTmp = array_keys($data);
  619. sort($keyTmp);
  620. $this->assertEquals($field, $keyTmp);
  621. $valueTmp = array_values($data);
  622. sort($valueTmp);
  623. $this->assertEquals($value, $valueTmp);
  624. $this->assertEquals($value, [
  625. $data[$field[0]],
  626. $data[$field[1]],
  627. $data[$field[2]],
  628. $data[$field[3]],
  629. $data[$field[4]],
  630. ]);
  631. $data = $redis->hKeys($key);
  632. sort($data);
  633. $this->assertEquals($field, $data);
  634. $data = $redis->hLen($key);
  635. $this->assertEquals(count($field), $data);
  636. $data = $redis->hMGet($key, [$field[0], $field[1], $field[2]]);
  637. $this->assertEquals([$field[0]=>1, $field[1]=>2, $field[2]=>3], $data);
  638. $data = $redis->hIncrBy($key, $field[4], 1);
  639. $this->assertEquals($value[4] + 1, $data);
  640. $data = $redis->hIncrByFloat($key, $field[1], 1.1);
  641. $this->assertEquals($value[1] + 1.1, $data);
  642. $data = $redis->hSetNx($key, $field[0], 1);
  643. $this->assertEquals(0, $data);
  644. $data = $redis->hSetNx($key, $field[0] . 'a', 1);
  645. $this->assertEquals(1, $data);
  646. $this->assertEquals(1, $redis->hGet($key, $field[0] . 'a'));
  647. $cursor = 0;
  648. $redis->del('a');
  649. $redis->hMSet('a', [
  650. 'a' => 'tioncico',
  651. 'b' => 'tioncico',
  652. 'c' => 'tioncico',
  653. 'd' => 'tioncico',
  654. 'e' => 'tioncico',
  655. 'f' => 'tioncico',
  656. 'g' => 'tioncico',
  657. 'h' => 'tioncico',
  658. ]);
  659. $data = [];
  660. do {
  661. $keys = $redis->hScan('a', $cursor);
  662. $data = array_merge($data, $keys);
  663. } while ($cursor);
  664. $this->assertEquals(8, count($data));
  665. }
  666. /**
  667. * testHash序列化测试
  668. * @author Tioncico
  669. * Time: 11:54
  670. */
  671. function testHashSerialize()
  672. {
  673. $key = 'hKey';
  674. $field = [
  675. 'hField1',
  676. 'hField2',
  677. 'hField3',
  678. 'hField4',
  679. 'hField5',
  680. ];
  681. $value = [
  682. 1,
  683. 2,
  684. 3,
  685. 4,
  686. 5,
  687. ];
  688. $redis = $this->redisPHPSerialize;
  689. $redis->del($key);
  690. $data = $redis->hSet($key, $field[0], $value[0]);
  691. $this->assertNotFalse($data);
  692. $data = $redis->hGet($key, $field[0]);
  693. $this->assertEquals($data, $value[0]);
  694. $data = $redis->hExists($key, $field[0]);
  695. $this->assertEquals(1, $data);
  696. $data = $redis->hDel($key, $field[0]);
  697. $this->assertEquals(1, $data, $redis->getErrorMsg());
  698. $data = $redis->hExists($key, $field[0]);
  699. $this->assertEquals(0, $data);
  700. $data = $redis->hMSet($key, [
  701. "{$field[0]}" => $value[0],
  702. "{$field[1]}" => $value[1],
  703. "{$field[2]}" => $value[2],
  704. "{$field[3]}" => $value[3],
  705. "{$field[4]}" => $value[4],
  706. ]);
  707. $this->assertTrue($data);
  708. $data = $redis->hValS($key);
  709. sort($data);
  710. $this->assertEquals($value, $data);
  711. $data = $redis->hGetAll($key);
  712. $keyTmp = array_keys($data);
  713. sort($keyTmp);
  714. $this->assertEquals($field, $keyTmp);
  715. $valueTmp = array_values($data);
  716. sort($valueTmp);
  717. $this->assertEquals($value, $valueTmp);
  718. $this->assertEquals($value, [
  719. $data[$field[0]],
  720. $data[$field[1]],
  721. $data[$field[2]],
  722. $data[$field[3]],
  723. $data[$field[4]],
  724. ]);
  725. $data = $redis->hKeys($key);
  726. sort($data);
  727. $this->assertEquals($field, $data);
  728. $data = $redis->hLen($key);
  729. $this->assertEquals(count($field), $data);
  730. $data = $redis->hMGet($key, [$field[0], $field[1], $field[2]]);
  731. $this->assertEquals([$field[0]=>1, $field[1]=>2, $field[2]=>3], $data);
  732. $data = $redis->hSetNx($key, $field[0], 1);
  733. $this->assertEquals(0, $data);
  734. $data = $redis->hSetNx($key, $field[0] . 'a', 1);
  735. $this->assertEquals(1, $data);
  736. $this->assertEquals(1, $redis->hGet($key, $field[0] . 'a'));
  737. $cursor = 0;
  738. $redis->del('a');
  739. $redis->hMSet('a', [
  740. 'a' => 'tioncico',
  741. 'b' => 'tioncico',
  742. 'c' => 'tioncico',
  743. 'd' => 'tioncico',
  744. 'e' => 'tioncico',
  745. 'f' => 'tioncico',
  746. 'g' => 'tioncico',
  747. 'h' => 'tioncico',
  748. ]);
  749. $data = [];
  750. do {
  751. $keys = $redis->hScan('a', $cursor);
  752. $data = array_merge($data, $keys);
  753. } while ($cursor);
  754. $this->assertEquals(8, count($data));
  755. $this->assertEquals('tioncico', $data['a']);
  756. }
  757. /**
  758. * testList
  759. * @author tioncico
  760. * Time: 下午8:17
  761. */
  762. function testList()
  763. {
  764. $redis = $this->redis;
  765. $key = [
  766. 'listKey1',
  767. 'listKey2',
  768. 'listKey3',
  769. ];
  770. $value = [
  771. 'a', 'b', 'c', 'd'
  772. ];
  773. $redis->del($key[0]);
  774. $data = $redis->lPush($key[0], $value[0], $value[1]);
  775. $this->assertEquals(2, $data);
  776. $data = $redis->bLPop($key[0], 1);
  777. $this->assertTrue(!!$data);
  778. $data = $redis->bRPop($key[0], 1);
  779. $this->assertTrue(!!$data);
  780. // $redis->del($key[0]);
  781. // $redis->lPush($key[0], $value[0], $value[1]);
  782. // $data = $redis->bRPopLPush($key[0], $key[1], 1);
  783. // var_dump($data,$redis->getErrorMsg());
  784. // $this->assertEquals($value[0], $data);
  785. // $redis->del($key[0]);
  786. // $redis->lPush($key[0], $value[0], $value[1]);
  787. // $data = $redis->rPopLPush($key[0], $key[1]);
  788. // $this->assertEquals($value[0], $data);
  789. $redis->del($key[0]);
  790. $redis->lPush($key[0], $value[0], $value[1]);
  791. $data = $redis->lIndex($key[0], 1);
  792. $this->assertEquals($value[0], $data);
  793. $data = $redis->lLen($key[0]);
  794. $this->assertEquals(2, $data);
  795. $data = $redis->lInsert($key[0], true, 'b', 'c');
  796. $this->assertEquals($redis->lLen($key[0]), $data);
  797. $data = $redis->lInsert($key[0], true, 'd', 'c');
  798. $this->assertEquals(-1, $data);
  799. $redis->del($key[1]);
  800. $data = $redis->rPush($key[1], $value[0], $value[2], $value[1]);
  801. $this->assertEquals(3, $data);
  802. $data = $redis->lRange($key[1], 0, 3);
  803. $this->assertEquals(count($data), 3);
  804. $data = $redis->lPop($key[1]);
  805. $this->assertEquals($value[0], $data);
  806. $data = $redis->rPop($key[1]);
  807. $this->assertEquals($value[1], $data);
  808. $data = $redis->lPuShx($key[1], 'x');
  809. $this->assertEquals($redis->lLen($key[1]), $data);
  810. $this->assertEquals('x', $redis->lPop($key[1]));
  811. $data = $redis->rPuShx($key[1], 'z');
  812. $this->assertEquals($redis->lLen($key[1]), $data);
  813. $this->assertEquals('z', $redis->rPop($key[1]));
  814. $redis->del($key[1]);
  815. $redis->rPush($key[1], $value[0], $value[0], $value[0]);
  816. $data = $redis->lRem($key[1], 1, $value[0]);
  817. $this->assertEquals(1, $data);
  818. $data = $redis->lSet($key[1], 0, 'xx');
  819. $this->assertTrue($data);
  820. $this->assertEquals('xx', $redis->lPop($key[1]));
  821. $data = $redis->lTrim($key[1], 0, 2);
  822. $this->assertTrue($data);
  823. $this->assertEquals(1, $redis->lLen($key[1]));
  824. }
  825. /**
  826. * testList序列化测试
  827. * @author tioncico
  828. * Time: 下午8:17
  829. */
  830. function testListSerialize()
  831. {
  832. $redis = $this->redisPHPSerialize;
  833. $key = [
  834. 'listKey1',
  835. 'listKey2',
  836. 'listKey3',
  837. ];
  838. $value = [
  839. 'a', 'b', 'c', 'd'
  840. ];
  841. $redis->del($key[0]);
  842. $data = $redis->lPush($key[0], $value[0], $value[1]);
  843. $this->assertEquals(2, $data);
  844. $data = $redis->bLPop($key[0], 1);
  845. $this->assertTrue(!!$data);
  846. $data = $redis->bRPop($key[0], 1);
  847. $this->assertTrue(!!$data);
  848. // $redis->del($key[0]);
  849. // $redis->lPush($key[0], $value[0], $value[1]);
  850. // $data = $redis->bRPopLPush($key[0], $key[1], 1);
  851. // $this->assertEquals($value[0], $data);
  852. //
  853. // $redis->del($key[0]);
  854. // $redis->lPush($key[0], $value[0], $value[1]);
  855. // $data = $redis->rPopLPush($key[0], $key[1]);
  856. // $this->assertEquals($value[0], $data);
  857. $redis->del($key[0]);
  858. $redis->lPush($key[0], $value[0], $value[1]);
  859. $data = $redis->lIndex($key[0], 1);
  860. $this->assertEquals($value[0], $data);
  861. $data = $redis->lLen($key[0]);
  862. $this->assertEquals(2, $data);
  863. $data = $redis->lInsert($key[0], true, 'b', 'c');
  864. $this->assertEquals($redis->lLen($key[0]), $data);
  865. $data = $redis->lInsert($key[0], true, 'd', 'c');
  866. $this->assertEquals(-1, $data);
  867. $redis->del($key[1]);
  868. $data = $redis->rPush($key[1], $value[0], $value[2], $value[1]);
  869. $this->assertEquals(3, $data);
  870. $data = $redis->lRange($key[1], 0, 3);
  871. $this->assertEquals([$value[0], $value[2], $value[1]], $data);
  872. $data = $redis->lPop($key[1]);
  873. $this->assertEquals($value[0], $data);
  874. $data = $redis->rPop($key[1]);
  875. $this->assertEquals($value[1], $data);
  876. $data = $redis->lPuShx($key[1], 'x');
  877. $this->assertEquals($redis->lLen($key[1]), $data);
  878. $this->assertEquals('x', $redis->lPop($key[1]));
  879. $data = $redis->rPuShx($key[1], 'z');
  880. $this->assertEquals($redis->lLen($key[1]), $data);
  881. $this->assertEquals('z', $redis->rPop($key[1]));
  882. $redis->del($key[1]);
  883. $redis->rPush($key[1], $value[0], $value[0], $value[0]);
  884. $data = $redis->lRem($key[1], 1, $value[0]);
  885. $this->assertEquals(1, $data);
  886. $data = $redis->lSet($key[1], 0, 'xx');
  887. $this->assertTrue($data);
  888. $this->assertEquals('xx', $redis->lPop($key[1]));
  889. $data = $redis->lTrim($key[1], 0, 2);
  890. $this->assertTrue($data);
  891. $this->assertEquals(1, $redis->lLen($key[1]));
  892. }
  893. /**
  894. * 集合测试
  895. * testMuster
  896. * @author Tioncico
  897. * Time: 9:10
  898. */
  899. function testMuster()
  900. {
  901. $redis = $this->redis;
  902. // $server_list = [
  903. // 'tcp://172.16.253.156:9001',
  904. // ];
  905. // $redis = new \RedisCluster(null,$server_list);
  906. $key = [
  907. 'muster1',
  908. 'muster2',
  909. 'muster3',
  910. 'muster4',
  911. 'muster5',
  912. ];
  913. $value = [
  914. '1',
  915. '2',
  916. '3',
  917. '4',
  918. ];
  919. $redis->del($key[0]);
  920. $redis->del($key[1]);
  921. $data = $redis->sAdd($key[0], $value[0], $value[1]);
  922. $this->assertEquals(2, $data);
  923. $data = $redis->sCard($key[0]);
  924. $this->assertEquals(2, $data);
  925. $redis->sAdd($key[1], $value[0], $value[2]);
  926. $data = $redis->sMembers($key[0]);
  927. $this->assertEquals([$value[0], $value[1]], $data);
  928. $data = $redis->sMembers($key[1]);
  929. $this->assertEquals([$value[0], $value[2]], $data);
  930. $redis->del($key[2]);
  931. $redis->sAdd($key[2], $value[1]);
  932. $data = $redis->sMembers($key[2]);
  933. $this->assertEquals([$value[1]], $data);
  934. $data = $redis->sIsMember($key[0], $value[0]);
  935. $this->assertEquals(1, $data);
  936. $data = $redis->sIsMember($key[0], $value[3]);
  937. $this->assertEquals(0, $data);
  938. $redis->del($key[0]);
  939. $redis->sAdd($key[0], $value[1]);
  940. $data = $redis->sPop($key[0]);
  941. $this->assertEquals(2, $data);
  942. $redis->del($key[3]);
  943. $redis->sAdd($key[3], $value[0], $value[1], $value[2], $value[3]);
  944. $data = $redis->sRandMember($key[3], 4);
  945. $this->assertEquals(4, count($data));
  946. $data = $redis->sRandMember($key[3] . rand(1, 1111));
  947. $this->assertEquals(null, $data);
  948. $data = $redis->sRandMember($key[3]);
  949. $this->assertTrue(in_array($data, $value));
  950. $data = $redis->sRem($key[3], $value[0], $value[1], $value[2], $value[3]);
  951. $this->assertEquals(4, $data);
  952. $this->assertEquals([], $redis->sMembers($key[3]));
  953. $cursor = 0;
  954. $redis->del('a');
  955. $redis->sAdd('a', 'a1', 'a2', 'a3', 'a4', 'a5');
  956. $data = [];
  957. do {
  958. $keys = $redis->sScan('a', $cursor, '*', 1);
  959. $data = array_merge($data, $keys);
  960. } while ($cursor);
  961. $this->assertEquals(5, count($data));
  962. }
  963. /**
  964. * 集合序列化测试
  965. * testMuster
  966. * @author Tioncico
  967. * Time: 9:10
  968. */
  969. function testMusterSerialize()
  970. {
  971. $redis = $this->redisPHPSerialize;
  972. $key = [
  973. 'muster1',
  974. 'muster2',
  975. 'muster3',
  976. 'muster4',
  977. 'muster5',
  978. ];
  979. $value = [
  980. '1',
  981. '2',
  982. '3',
  983. '4',
  984. ];
  985. // $server_list = [
  986. // 'tcp://172.16.253.156:9001',
  987. // ];
  988. // $redis = new \RedisCluster(null,$server_list);
  989. $redis->del($key[0]);
  990. $redis->del($key[1]);
  991. $data = $redis->sAdd($key[0], $value[0], $value[1]);
  992. $this->assertEquals(2, $data);
  993. $data = $redis->sCard($key[0]);
  994. $this->assertEquals(2, $data);
  995. $redis->sAdd($key[1], $value[0], $value[2]);
  996. // $data = $redis->sDiff($key[0], $key[1]);
  997. // $this->assertEquals([$value[1]], $data);
  998. // $data = $redis->sDiff($key[1], $key[0]);
  999. // $this->assertEquals([$value[2]], $data);
  1000. $redis->del($key[1]);
  1001. $redis->sAdd($key[1], $value[0], $value[2]);
  1002. $data = $redis->sMembers($key[1]);
  1003. $this->assertEquals([1,3], $data);
  1004. // $data = $redis->sDiffStore($key[2], $key[0], $key[1]);
  1005. // $this->assertEquals(1, $data);
  1006. $redis->del($key[2]);
  1007. $redis->sAdd($key[2], $value[1]);
  1008. $data = $redis->sMembers($key[2]);
  1009. $this->assertEquals([$value[1]], $data);
  1010. $data = $redis->sMembers($key[2]);
  1011. $this->assertEquals([$value[1]], $data);
  1012. // $data = $redis->sInter($key[0], $key[1]);
  1013. // $this->assertEquals([$value[0]], $data);
  1014. // $data = $redis->sInterStore($key[3], $key[0], $key[1]);
  1015. // $this->assertEquals(1, $data);
  1016. // $this->assertEquals([$value[0]], $redis->sMembers($key[3]));
  1017. $data = $redis->sIsMember($key[0], $value[0]);
  1018. $this->assertEquals(1, $data);
  1019. $data = $redis->sIsMember($key[0], $value[3]);
  1020. $this->assertEquals(0, $data);
  1021. // $data = $redis->sMove($key[0], $key[1], $value[1]);
  1022. // $this->assertEquals(1, $data);
  1023. $redis->del($key[0]);
  1024. $redis->sAdd($key[0], $value[1]);
  1025. $data = $redis->sPop($key[0]);
  1026. $this->assertEquals($value[1], $data);
  1027. $redis->del($key[3]);
  1028. $redis->sAdd($key[3], $value[0], $value[1], $value[2], $value[3]);
  1029. $data = $redis->sRandMember($key[3], 4);
  1030. $this->assertEquals(4, count($data));
  1031. $data = $redis->sRandMember($key[3] . rand(1, 1111));
  1032. $this->assertEquals(null, $data);
  1033. $data = $redis->sRandMember($key[3]);
  1034. $this->assertTrue(in_array($data, $value));
  1035. $data = $redis->sRem($key[3], $value[0], $value[1], $value[2], $value[3]);
  1036. $this->assertEquals(4, $data);
  1037. $this->assertEquals([], $redis->sMembers($key[3]));
  1038. // $data = $redis->sUnion($key[0], $key[1]);
  1039. // $this->assertTrue(!!$data);
  1040. $redis->del($key[1]);
  1041. $redis->del($key[2]);
  1042. $redis->del($key[3]);
  1043. $redis->del($key[4]);
  1044. $redis->sAdd($key[1], 1, 2, 3, 4);
  1045. $redis->sAdd($key[2], 5);
  1046. $redis->sAdd($key[3], 6, 7);
  1047. // $data = $redis->sUnIonStore($key[4], $key[1], $key[2], $key[3]);
  1048. // $this->assertEquals(7, $data);
  1049. $cursor = 0;
  1050. $redis->del('a');
  1051. $redis->sAdd('a', 'a1', 'a2', 'a3', 'a4', 'a5');
  1052. $data = [];
  1053. do {
  1054. $keys = $redis->sScan('a', $cursor, '*', 1);
  1055. $data = array_merge($data, $keys);
  1056. } while ($cursor);
  1057. $this->assertEquals(5, count($data));
  1058. $this->assertTrue(in_array('a1', $data));
  1059. }
  1060. /**
  1061. * 有序集合测试
  1062. * testSortMuster
  1063. * @author Tioncico
  1064. * Time: 14:17
  1065. */
  1066. function testSortMuster()
  1067. {
  1068. $redis = $this->redis;
  1069. // $server_list = [
  1070. // 'tcp://172.16.253.156:9001',
  1071. // ];
  1072. // $redis = new \RedisCluster(null,$server_list);
  1073. $key = [
  1074. 'sortMuster1',
  1075. 'sortMuster2',
  1076. 'sortMuster3',
  1077. 'sortMuster4',
  1078. 'sortMuster5',
  1079. ];
  1080. $member = [
  1081. 'member1',
  1082. 'member2',
  1083. 'member3',
  1084. 'member4',
  1085. 'member5',
  1086. ];
  1087. $score = [
  1088. 1,
  1089. 2,
  1090. 3,
  1091. 4,
  1092. ];
  1093. $redis->del($key[0]);
  1094. $data = $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  1095. $this->assertEquals(2, $data);
  1096. $data = $redis->zCard($key[0]);
  1097. $this->assertEquals(2, $data);
  1098. $data = $redis->zCount($key[0], 0, 3);
  1099. $this->assertEquals(2, $data);
  1100. $data = $redis->zInCrBy($key[0], 1, $member[1]);
  1101. $this->assertEquals($score[1] + 1, $data);
  1102. // $redis->del($key[0]);
  1103. // $redis->del($key[1]);
  1104. // $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  1105. // $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
  1106. // $data = $redis->zInTerStore($key[2], [$key[0], $key[1]],[1]);
  1107. // $this->assertEquals(1, $data);
  1108. $data = $redis->zLexCount($key[0], '-', '+');
  1109. $this->assertEquals(2, $data);
  1110. $redis->del($key[0]);
  1111. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1112. $data = $redis->zRange($key[0], 0, -1, true);
  1113. $this->assertEquals([
  1114. $member[0] => $score[0],
  1115. $member[1] => $score[1],
  1116. $member[2] => $score[2],
  1117. ], $data);
  1118. $data = $redis->zRange($key[0], 0, -1, false);
  1119. $this->assertEquals([
  1120. $member[0],
  1121. $member[1],
  1122. $member[2],
  1123. ], $data);
  1124. $data = $redis->zRangeByLex($key[0], '-', '+');
  1125. $this->assertEquals(3, count($data));
  1126. $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => true, 'limit' => array(0, 2)]);
  1127. $this->assertEquals([
  1128. $member[1] => $score[1],
  1129. $member[2] => $score[2],
  1130. ], $data);
  1131. $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => false, 'limit' => array(0, 2)]);
  1132. $this->assertEquals([
  1133. $member[1],
  1134. $member[2],
  1135. ], $data);
  1136. $data = $redis->zRank($key[0], $member[1]);
  1137. $this->assertEquals(1, $data);
  1138. $data = $redis->zRem($key[0], $member[1], $member[2]);
  1139. $this->assertEquals(2, $data);
  1140. $redis->del($key[0]);
  1141. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1142. $data = $redis->zRemRangeByLex($key[0], '-', '+');
  1143. $this->assertEquals(3, $data);
  1144. $redis->del($key[0]);
  1145. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1146. $data = $redis->zRemRangeByRank($key[0], 0, 2);
  1147. $this->assertEquals(3, $data);
  1148. $redis->del($key[0]);
  1149. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1150. $data = $redis->zRemRangeByScore($key[0], 0, 3);
  1151. $this->assertEquals(3, $data);
  1152. $redis->del($key[0]);
  1153. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1154. $data = $redis->zRevRange($key[0], 0, 3);
  1155. $this->assertEquals([
  1156. $member[2],
  1157. $member[1],
  1158. $member[0],
  1159. ], $data);
  1160. $redis->del($key[0]);
  1161. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1162. $data = $redis->zRevRange($key[0], 0, 3, true);
  1163. $this->assertEquals([
  1164. $member[2] => $score[2],
  1165. $member[1] => $score[1],
  1166. $member[0] => $score[0],
  1167. ], $data);
  1168. $redis->del($key[0]);
  1169. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1170. $data = $redis->zRevRangeByScore($key[0], 3, 0, ['withScores' => true, 'limit' => array(0, 3)]);
  1171. $this->assertEquals([
  1172. $member[2] => $score[2],
  1173. $member[1] => $score[1],
  1174. $member[0] => $score[0],
  1175. ], $data);
  1176. $redis->del($key[0]);
  1177. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1178. $data = $redis->zReVRangeByScore($key[0], 3, 0, ['withScores' => false, 'limit' => array(0, 3)]);
  1179. $this->assertEquals([
  1180. $member[2],
  1181. $member[1],
  1182. $member[0],
  1183. ], $data);
  1184. $data = $redis->zRevRank($key[0], $member[0]);
  1185. $this->assertEquals(2, $data);
  1186. $data = $redis->zScore($key[0], $member[0]);
  1187. $this->assertEquals($score[0], $data);
  1188. // $redis->del($key[0]);
  1189. // $redis->del($key[1]);
  1190. // $redis->del($key[2]);
  1191. // $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  1192. // $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
  1193. // $data = $redis->zUnionStore($key[2], [$key[1], $key[0]],[1,2]);
  1194. // $this->assertEquals(3, $data);
  1195. $cursor = 0;
  1196. $redis->del('a');
  1197. $redis->zAdd('a', 1, 'a1', 2, 'a2', 3, 'a3', 4, 'a4', 5, 'a5');
  1198. $data = [];
  1199. do {
  1200. $keys = $redis->zScan('a', $cursor, '*', 1);
  1201. $data = array_merge($data, $keys);
  1202. } while ($cursor);
  1203. $this->assertEquals(5, count($data));
  1204. }
  1205. /**
  1206. * 有序集合序列化测试
  1207. * testSortMuster
  1208. * @author Tioncico
  1209. * Time: 14:17
  1210. */
  1211. function testSortMusterSerialize()
  1212. {
  1213. $redis = $this->redisPHPSerialize;
  1214. // $server_list = [
  1215. // 'tcp://172.16.253.156:9001',
  1216. // ];
  1217. // $redis = new \RedisCluster(null,$server_list);
  1218. $key = [
  1219. 'sortMuster1',
  1220. 'sortMuster2',
  1221. 'sortMuster3',
  1222. 'sortMuster4',
  1223. 'sortMuster5',
  1224. ];
  1225. $member = [
  1226. 'member1',
  1227. 'member2',
  1228. 'member3',
  1229. 'member4',
  1230. 'member5',
  1231. ];
  1232. $score = [
  1233. 1,
  1234. 2,
  1235. 3,
  1236. 4,
  1237. ];
  1238. $redis->del($key[0]);
  1239. $data = $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  1240. $this->assertEquals(2, $data);
  1241. $data = $redis->zCard($key[0]);
  1242. $this->assertEquals(2, $data);
  1243. $data = $redis->zCount($key[0], 0, 3);
  1244. $this->assertEquals(2, $data);
  1245. // $redis->del($key[0]);
  1246. // $redis->del($key[1]);
  1247. // $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  1248. // $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
  1249. // $data = $redis->zInTerStore($key[2], [$key[0], $key[1]]);
  1250. // $this->assertEquals(1, $data);
  1251. $data = $redis->zLexCount($key[0], '-', '+');
  1252. $this->assertEquals(2, $data);
  1253. $redis->del($key[0]);
  1254. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1255. $data = $redis->zRange($key[0], 0, -1, true);
  1256. $this->assertEquals([
  1257. $member[0] => $score[0],
  1258. $member[1] => $score[1],
  1259. $member[2] => $score[2],
  1260. ], $data);
  1261. $data = $redis->zRange($key[0], 0, -1, false);
  1262. $this->assertEquals([
  1263. $member[0],
  1264. $member[1],
  1265. $member[2],
  1266. ], $data);
  1267. $data = $redis->zRangeByLex($key[0], '-', '+');
  1268. $this->assertEquals(3, count($data));
  1269. $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => true, 'limit' => array(0, 2)]);
  1270. $this->assertEquals([
  1271. $member[1] => $score[1],
  1272. $member[2] => $score[2],
  1273. ], $data);
  1274. $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => false, 'limit' => array(0, 2)]);
  1275. $this->assertEquals([
  1276. $member[1],
  1277. $member[2],
  1278. ], $data);
  1279. $data = $redis->zRank($key[0], $member[1]);
  1280. $this->assertEquals(1, $data);
  1281. $data = $redis->zRem($key[0], $member[1], $member[2]);
  1282. $this->assertEquals(2, $data);
  1283. $redis->del($key[0]);
  1284. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1285. $data = $redis->zRemRangeByLex($key[0], '-', '+');
  1286. $this->assertEquals(3, $data);
  1287. $redis->del($key[0]);
  1288. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1289. $data = $redis->zRemRangeByRank($key[0], 0, 2);
  1290. $this->assertEquals(3, $data);
  1291. $redis->del($key[0]);
  1292. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1293. $data = $redis->zRemRangeByScore($key[0], 0, 3);
  1294. $this->assertEquals(3, $data);
  1295. $redis->del($key[0]);
  1296. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1297. $data = $redis->zRevRange($key[0], 0, 3);
  1298. $this->assertEquals([
  1299. $member[2],
  1300. $member[1],
  1301. $member[0],
  1302. ], $data);
  1303. $redis->del($key[0]);
  1304. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1305. $data = $redis->zRevRange($key[0], 0, 3, true);
  1306. $this->assertEquals([
  1307. $member[2] => $score[2],
  1308. $member[1] => $score[1],
  1309. $member[0] => $score[0],
  1310. ], $data);
  1311. $redis->del($key[0]);
  1312. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1313. $data = $redis->zRevRangeByScore($key[0], 3, 0, ['withScores' => true, 'limit' => array(0, 3)]);
  1314. $this->assertEquals([
  1315. $member[2] => $score[2],
  1316. $member[1] => $score[1],
  1317. $member[0] => $score[0],
  1318. ], $data);
  1319. $redis->del($key[0]);
  1320. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1321. $data = $redis->zReVRangeByScore($key[0], 3, 0, ['withScores' => false, 'limit' => array(0, 3)]);
  1322. $this->assertEquals([
  1323. $member[2],
  1324. $member[1],
  1325. $member[0],
  1326. ], $data);
  1327. $data = $redis->zRevRank($key[0], $member[0]);
  1328. $this->assertEquals(2, $data);
  1329. $data = $redis->zScore($key[0], $member[0]);
  1330. $this->assertEquals($score[0], $data);
  1331. // $redis->del($key[0]);
  1332. // $redis->del($key[1]);
  1333. // $redis->del($key[2]);
  1334. // $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  1335. // $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
  1336. // $data = $redis->zUnionStore($key[2], 2, $key[1], $key[0]);
  1337. // $this->assertEquals(3, $data);
  1338. $cursor = 0;
  1339. $redis->del('a');
  1340. $redis->zAdd('a', 1, 'a1', 2, 'a2', 3, 'a3', 4, 'a4', 5, 'a5');
  1341. $data = [];
  1342. do {
  1343. $keys = $redis->zScan('a', $cursor, '*', 1);
  1344. $data = array_merge($data, $keys);
  1345. } while ($cursor);
  1346. $this->assertEquals(1, $data['a1']);
  1347. }
  1348. /**
  1349. * Stream 测试
  1350. * @author gaobinzhan <gaobinzhan@gmail.com>
  1351. */
  1352. function testStream(){
  1353. $redis = $this->redis;
  1354. $redis->del('test');
  1355. $id = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1356. $this->assertIsString($id);
  1357. $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1358. $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1359. $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1360. $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy'], 1);
  1361. $length = $redis->xLen('test');
  1362. $this->assertEquals(1, $length);
  1363. $ids[] = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1364. $ids[] = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1365. $delNum = $redis->xDel('test',$ids);
  1366. $this->assertEquals(2, $delNum);
  1367. $result = $redis->xRange('test','-','+',0);
  1368. $this->assertEmpty($result);
  1369. $result = $redis->xRange('test','-','+');
  1370. $this->assertIsArray($result);
  1371. $result = $redis->xRevRange('test','+','-',0);
  1372. $this->assertEmpty($result);
  1373. $result = $redis->xRevRange('test','+','-');
  1374. $this->assertIsArray($result);
  1375. $redis->del('test1');
  1376. $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
  1377. $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
  1378. $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
  1379. $len = $redis->xTrim('test1',2);
  1380. $this->assertEquals(1, $len);
  1381. $result = $redis->xRead(['test1' => '0-0'],1,0);
  1382. $this->assertIsArray($result);
  1383. $this->assertCount(1,$result['test1']);
  1384. $result = $redis->xRead(['test1' => '0-0'],2,0);
  1385. $this->assertCount(2,$result['test1']);
  1386. $result = $redis->xRead(['test' => '$'],1,1000);
  1387. $this->assertFalse($result);
  1388. $array = $redis->xGroup('HELP');
  1389. $this->assertIsArray($array);
  1390. $groupName = time();
  1391. $bool = $redis->xGroup('CREATE','mystream',$groupName,'$',true);
  1392. $this->assertTrue($bool);
  1393. $bool = $redis->xGroup('SETID','mystream',$groupName,0);
  1394. $this->assertTrue($bool);
  1395. $int = $redis->xGroup('DELCONSUMER','mystream',$groupName,1);
  1396. $this->assertEquals(0,$int);
  1397. $num = $redis->xGroup('DESTROY','mystream',$groupName);
  1398. $this->assertEquals(1,$num);
  1399. $array = $redis->xInfo('STREAM','mystream');
  1400. $this->assertIsArray($array);
  1401. $groupName =time();
  1402. $redis->xGroup('CREATE','mystream',$groupName,'$',true);
  1403. $array = $redis->xInfo('GROUPS','mystream');
  1404. $this->assertIsArray($array);
  1405. $array = $redis->xPending('mystream',$groupName);
  1406. $this->assertIsArray($array);
  1407. $array = $redis->xPending('mystream',$groupName,'-','+',10);
  1408. $this->assertIsArray($array);
  1409. $array = $redis->xPending('mystream',$groupName,'-','+',10,'consumer-123');
  1410. $this->assertIsArray($array);
  1411. $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
  1412. $this->assertFalse($result);
  1413. $redis->xAdd('mystream','*',['name' => 1]);
  1414. $num = $redis->xAck('mystream',$groupName,[0]);
  1415. $this->assertEquals(0,$num);
  1416. $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
  1417. $num = $redis->xAck('mystream',$groupName,array_keys($result['mystream']));
  1418. $this->assertEquals(1,$num);
  1419. $redis->xAdd('mystream','*',['name' => 1]);
  1420. $redis->xAdd('mystream','*',['name' => 1]);
  1421. $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],2,0);
  1422. $num = $redis->xAck('mystream',$groupName,array_keys($result['mystream']));
  1423. $this->assertEquals(2,$num);
  1424. $redis->xAdd('mystream','*',['name' => 1]);
  1425. $redis->xAdd('mystream','*',['name' => 1]);
  1426. $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],2,0);
  1427. $result = $redis->xPending('mystream',$groupName,'-','+',2);
  1428. $ids[] = $result[0][0];
  1429. $ids[] = $result[1][0];
  1430. $array = $redis->xClaim('mystream',$groupName,'abc',2,$ids);
  1431. $this->assertIsArray($array);
  1432. }
  1433. /**
  1434. * 基数统计 测试
  1435. * testHyperLog
  1436. * @author Tioncico
  1437. * Time: 14:59
  1438. */
  1439. function testHyperLog()
  1440. {
  1441. $redis = $this->redis;
  1442. // $server_list = [
  1443. // 'tcp://172.16.253.156:9001',
  1444. // ];
  1445. // $redis = new \RedisCluster(null,$server_list);
  1446. $key = [
  1447. 'hp1',
  1448. 'hp2',
  1449. 'hp3',
  1450. 'hp4',
  1451. 'hp5',
  1452. ];
  1453. $redis->del($key[0]);
  1454. $redis->del($key[1]);
  1455. $data = $redis->pfAdd($key[0], [1, 2, 2, 3, 3]);
  1456. $this->assertEquals(1, $data);
  1457. // $redis->pfAdd($key[1], [1, 2, 2, 3, 3]);
  1458. // $data = $redis->pfCount([$key[0], $key[1]]);
  1459. // $this->assertEquals(3, $data);
  1460. // $data = $redis->pfMerge($key[2], [$key[0], $key[1]]);
  1461. // $this->assertEquals(1, $data);
  1462. }
  1463. /**
  1464. * 发布订阅测试
  1465. * testSubscribe
  1466. * @author Tioncico
  1467. * Time: 14:59
  1468. */
  1469. function testSubscribe()
  1470. {
  1471. // $this->assertEquals(1,1);
  1472. go(function () {
  1473. $redis = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
  1474. 'auth' => REDIS_CLUSTER_AUTH,
  1475. ]));
  1476. $redis->pSubscribe(function (RedisCluster $redis, $pattern, $str) {
  1477. $this->assertEquals('test', $str);
  1478. $data = $redis->unsubscribe('test');
  1479. $this->assertTrue(!!$data);
  1480. $redis->setSubscribeStop(true);
  1481. }, 'test', 'test1', 'test2');
  1482. });
  1483. go(function () {
  1484. $redis = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
  1485. 'auth' => REDIS_CLUSTER_AUTH,
  1486. ]));
  1487. $redis->subscribe(function (RedisCluster $redis, $pattern, $str) {
  1488. $this->assertEquals('test', $str);
  1489. $data = $redis->unsubscribe('test');
  1490. $this->assertTrue(!!$data);
  1491. $redis->setSubscribeStop(true);
  1492. }, 'test', 'test1', 'test2');
  1493. });
  1494. $redis = $this->redis;
  1495. $data = $redis->pubSub('CHANNELS');
  1496. $this->assertIsArray($data);
  1497. Coroutine::sleep(1);
  1498. $data = $redis->publish('test2', 'test');
  1499. $this->assertGreaterThan(0, $data);
  1500. //
  1501. $data = $redis->pUnSubscribe('test');
  1502. $this->assertTrue(!!$data);
  1503. }
  1504. /**
  1505. * 脚本执行测试
  1506. * testScript
  1507. * @author tioncico
  1508. * Time: 下午5:41
  1509. */
  1510. function testScript()
  1511. {
  1512. $this->assertEquals(1, 1);
  1513. $redis = $this->redis;
  1514. // $data = $redis->eval('s','s','a','1','2','a');
  1515. // $this->assertEquals(1,$data);
  1516. //
  1517. // $data = $redis->evalsha('a','g','g','1','a','a');
  1518. // $this->assertEquals(1,$data);
  1519. //
  1520. // $data = $redis->scriptExists('a','f');
  1521. // $this->assertEquals(1,$data);
  1522. //
  1523. // $data = $redis->scriptFlush();
  1524. // $this->assertEquals(1,$data);
  1525. //
  1526. // $data = $redis->scriptKill();
  1527. // $this->assertEquals(1,$data);
  1528. //
  1529. // $data = $redis->scriptLoad('a');
  1530. // $this->assertEquals(1,$data);
  1531. }
  1532. /**
  1533. * 服务器命令测试
  1534. * testServer
  1535. * @author tioncico
  1536. * Time: 下午5:41
  1537. */
  1538. function testServer()
  1539. {
  1540. $redis = $this->redis;
  1541. $data = $redis->bgRewriteAof();
  1542. $this->assertEquals('Background append only file rewriting started', $data);
  1543. Coroutine::sleep(1);
  1544. $data = $redis->bgSave();
  1545. $this->assertEquals('Background saving started', $data);
  1546. $data = $redis->clientList();
  1547. $this->assertIsArray($data);
  1548. $data = $redis->clientSetName('test');
  1549. $this->assertTrue($data);
  1550. $data = $redis->clientGetName();
  1551. $this->assertEquals('test', $data);
  1552. $data = $redis->clientPause(1);
  1553. $this->assertEquals(1, $data);
  1554. $data = $redis->command();
  1555. $this->assertIsArray($data);
  1556. $data = $redis->commandCount();
  1557. $this->assertGreaterThan(0, $data);
  1558. $data = $redis->commandGetKeys('MSET', 'a', 'b', 'c', 'd');
  1559. $this->assertEquals(['a', 'c'], $data);
  1560. $data = $redis->time();
  1561. $this->assertIsArray($data);
  1562. $data = $redis->commandInfo('get', 'set');
  1563. $this->assertIsArray($data);
  1564. $data = $redis->configGet('*max-*-entries*');
  1565. $this->assertIsArray($data);
  1566. $data = $redis->configSet('appendonly', 'yes');
  1567. $this->assertTrue($data);
  1568. $data = $redis->configRewrite();
  1569. $this->assertTrue($data);
  1570. $data = $redis->configResetStat();
  1571. $this->assertTrue($data);
  1572. $data = $redis->dBSize();
  1573. $this->assertGreaterThanOrEqual(0, $data);
  1574. $redis->set('a', 1);
  1575. $data = $redis->debugObject('a');
  1576. $this->assertIsString($data);
  1577. $data = $redis->flushAll();
  1578. $this->assertTrue($data);
  1579. $data = $redis->flushDb();
  1580. $this->assertTrue($data);
  1581. $data = $redis->info();
  1582. $this->assertIsArray($data);
  1583. $data = $redis->lastSave();
  1584. $this->assertNotFalse($data);
  1585. go(function () {
  1586. $redis = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
  1587. 'auth' => REDIS_CLUSTER_AUTH,
  1588. ]));
  1589. $redis->monitor(function (RedisCluster $redis, $data) {
  1590. $this->assertIsString($data);
  1591. $redis->set('a', 1);
  1592. $redis->setMonitorStop(true);
  1593. });
  1594. });
  1595. go(function () {
  1596. $redis = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
  1597. 'auth' => REDIS_CLUSTER_AUTH,
  1598. ]));
  1599. Coroutine::sleep(1);
  1600. $redis->set('a', 1);
  1601. });
  1602. $data = $redis->save();
  1603. $this->assertEquals(1, $data);
  1604. // $data = $redis->clientKill($data[0]['addr']);
  1605. // $this->assertTrue($data);
  1606. // $data = $redis->slowLog('get', 'a');
  1607. // var_dump($data,$redis->getErrorMsg());
  1608. // $this->assertTrue(!!$data);
  1609. }
  1610. /**
  1611. * geohash测试
  1612. * testGeohash
  1613. * @author tioncico
  1614. * Time: 下午6:11
  1615. */
  1616. function testGeohash()
  1617. {
  1618. $redis = $this->redis;
  1619. $key = 'testGeohash';
  1620. $redis->del($key);
  1621. $redis->del($key . 'new');
  1622. $data = $redis->geoAdd($key, [
  1623. ['118.6197800000', '24.88849', 'user1',],
  1624. ['118.6197800000', '24.88859', 'user2',],
  1625. ['114.8197800000', '25.88849', 'user3'],
  1626. ['118.8197800000', '22.88849', 'user4'],
  1627. ]);
  1628. $this->assertEquals(4, $data);
  1629. $data = $redis->geoAdd($key . 'new', [
  1630. ['longitude' => '118.6197800000', 'latitude' => '24.88849', 'name' => 'user1',],
  1631. ['longitude' => '118.6197800000', 'latitude' => '24.88859', 'name' => 'user2',],
  1632. ['longitude' => '114.8197800000', 'latitude' => '25.88849', 'name' => 'user3'],
  1633. ['longitude' => '118.8197800000', 'latitude' => '22.88849', 'name' => 'user4'],
  1634. ]);
  1635. $this->assertEquals(4, $data);
  1636. $data = $redis->geoDist($key, 'user1', 'user2');
  1637. $this->assertGreaterThan(10, $data);
  1638. $data = $redis->geoHash($key, 'user1', 'user2');
  1639. $this->assertIsArray($data);
  1640. $data = $redis->geoPos($key, 'user1', 'user2');
  1641. $this->assertIsArray($data);
  1642. $data = $redis->geoRadius($key, '118.6197800000', '24.88849', 100, 'm', false, false, false, null, 'desc');
  1643. $this->assertEquals(['user2', 'user1'], $data);
  1644. $data = $redis->geoRadiusByMember($key, 'user1', 100, 'm', false, false, false, null, 'desc');
  1645. $this->assertEquals(['user2', 'user1'], $data);
  1646. }
  1647. }