123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Tioncico
- * Date: 2019/9/24 0024
- * Time: 16:16
- */
- namespace Test;
- use EasySwoole\Redis\Client;
- use EasySwoole\Redis\Config\RedisConfig;
- use EasySwoole\Redis\Exception\RedisException;
- use EasySwoole\Redis\Redis;
- use PHPUnit\Framework\TestCase;
- use Swoole\Coroutine;
- class UnixSocketRedisTest extends TestCase
- {
- /**
- * @var $redis Redis
- */
- protected $redis;
- protected function setUp():void
- {
- parent::setUp(); // TODO: Change the autogenerated stub
- $this->redis = new Redis(new RedisConfig([
- 'unixSocket' => REDIS_UNIX_SOCKET,
- ]));
- $this->redis->connect();
- //测试原始rawCommand
- $redis = $this->redis;
- $data = $redis->rawCommand(['set','a','1']);
- $this->assertEquals('OK',$data->getData());
- $data = $redis->rawCommand(['get','a']);
- $this->assertEquals('1',$data->getData());
- $redis->del('a');
- }
- function testConnect()
- {
- $this->assertTrue($this->redis->connect());
- }
- public function testAuth()
- {
- if (!empty(REDIS_AUTH)) {
- $this->assertTrue($this->redis->auth(REDIS_AUTH));
- }
- $this->assertTrue(true);
- }
- /**
- * key值操作测试
- * testKey
- * @author Tioncico
- * Time: 10:02
- */
- function testKey()
- {
- $redis = $this->redis;
- $key = 'test123213Key';
- $redis->select(0);
- $redis->flushAll();
- $redis->set($key, 123);
- $data = $redis->dump($key);
- $this->assertTrue(!!$data);
- $data = $redis->dump($key . 'x');
- $this->assertNull($data);
- $data = $this->redis->exists($key);
- $this->assertEquals(1, $data);
- $data = $this->redis->expire($key, 1);
- $this->assertEquals(1, $data);
- Coroutine::sleep(2);
- $this->assertEquals(0, $this->redis->exists($key));
- $redis->set($key, 123);
- $data = $this->redis->pExpire($key, 1000);
- $this->assertEquals(1, $data);
- $this->assertEquals(123, $this->redis->get($key));
- Coroutine::sleep(2);
- $this->assertEquals(0, $this->redis->pExpire($key));
- $redis->expireAt($key, 1 * 100);
- Coroutine::sleep(0.1);
- $this->assertEquals(0, $this->redis->exists($key));
- $redis->set($key, 123);
- $data = $redis->keys("{$key}");
- $this->assertEquals($key, $data[0]);
- $redis->select(1);
- $redis->del($key);
- $redis->select(0);
- $data = $redis->move($key, 1);
- $this->assertEquals(1, $data);
- $data = $redis->exists($key);
- $this->assertEquals(0, $data);
- $redis->select(0);
- $redis->set($key, 123);
- $data = $redis->expire($key, 1);
- $this->assertEquals(1, $data);
- $data = $redis->persist($key);
- $this->assertEquals(1, $data);
- $redis->expire($key, 1);
- $data = $redis->pTTL($key);
- $this->assertLessThanOrEqual(1000, $data);
- $data = $redis->ttl($key);
- $this->assertLessThanOrEqual(1, $data);
- $data = $redis->randomKey();
- $this->assertTrue(!!$data);
- $data = $redis->rename($key, $key . 'new');
- $this->assertTrue($data);
- $redis->set($key.'old',1);
- $data = $redis->renameNx($key.'old', $key . 'new');
- $this->assertEquals(0, $data);
- $redis->renameNx($key . 'new', $key);
- $data = $redis->renameNx($key, $key . 'new');
- $this->assertEquals(1, $data);
- $data = $redis->type($key);
- $this->assertEquals('none', $data);
- $data = $redis->type($key . 'new');
- $this->assertEquals('string', $data);
- //del单元测试新增
- $keyArr = ['a1','a2','a3','a4'];
- $valueArr = ['a1','a2','a3','a4'];
- $redis->mSet([
- $keyArr[0]=>$valueArr[0],
- $keyArr[1]=>$valueArr[1],
- $keyArr[2]=>$valueArr[2],
- $keyArr[3]=>$valueArr[3],
- ]);
- $data = $redis->del($keyArr[0]);
- $this->assertEquals(1,$data);
- $data = $redis->del($keyArr[1],$keyArr[2],$keyArr[3]);
- $this->assertEquals(3,$data);
- $redis->mSet([
- $keyArr[0]=>$valueArr[0],
- $keyArr[1]=>$valueArr[1],
- $keyArr[2]=>$valueArr[2],
- $keyArr[3]=>$valueArr[3],
- ]);
- $data = $redis->del([$keyArr[1],$keyArr[2],$keyArr[3]]);
- $this->assertEquals(3,$data);
- //unlink命令新增
- $keyArr = ['a1','a2','a3','a4'];
- $valueArr = ['a1','a2','a3','a4'];
- $redis->mSet([
- $keyArr[0]=>$valueArr[0],
- $keyArr[1]=>$valueArr[1],
- $keyArr[2]=>$valueArr[2],
- $keyArr[3]=>$valueArr[3],
- ]);
- $data = $redis->unlink($keyArr[0]);
- $this->assertEquals(1,$data);
- $data = $redis->unlink($keyArr[0].'test');
- $this->assertEquals(0,$data);
- $data = $redis->unlink($keyArr[1],$keyArr[2],$keyArr[3]);
- $this->assertEquals(3,$data);
- $redis->mSet([
- $keyArr[0]=>$valueArr[0],
- $keyArr[1]=>$valueArr[1],
- $keyArr[2]=>$valueArr[2],
- $keyArr[3]=>$valueArr[3],
- ]);
- $data = $redis->unlink([$keyArr[1],$keyArr[2],$keyArr[3]]);
- $this->assertEquals(3,$data);
- }
- /**
- * 字符串单元测试
- * testString
- * @author tioncico
- * Time: 下午9:41
- */
- function testString()
- {
- $redis = $this->redis;
- $key = 'test';
- $value = 1;
- $data = $redis->del($key);
- $this->assertNotFalse($data);
- $data = $redis->set($key, $value);
- $this->assertTrue($data);
- $redis->set($key, $value);
- $data = $redis->get($key);
- $this->assertEquals($data, $value);
- //set的其他作用测试(超时作用)
- $keyTTL=$key.'ttl';
- $redis->set($keyTTL,$value,20);
- $this->assertGreaterThan(18,$redis->ttl($keyTTL));
- $redis->set($keyTTL,$value,['EX'=>20]);
- $this->assertGreaterThan(18,$redis->ttl($keyTTL));
- $redis->set($keyTTL,$value,['PX'=>20000]);
- $this->assertGreaterThan(18000,$redis->pTTL($keyTTL));
- $this->assertGreaterThan(18,$redis->ttl($keyTTL));
- //set的其他作用测试(存在以及不存在时候设置)
- $keyExist = $key.'exist';
- $redis->del($keyExist);
- $data = $redis->set($keyExist,'1','XX');
- $this->assertNull($data);
- $data = $redis->set($keyExist,'1','NX');
- $this->assertTrue($data);
- $this->assertEquals('1',$redis->get($keyExist));
- $data = $redis->set($keyExist,'1','NX');
- $this->assertNull($data);
- $data = $redis->set($keyExist,'2','XX');
- $this->assertTrue($data);
- $this->assertEquals('2',$redis->get($keyExist));
- //set组合测试
- $data = $redis->set($keyExist,'3',['XX','EX'=>10]);
- $this->assertTrue($data);
- $this->assertEquals('3',$redis->get($keyExist));
- $this->assertGreaterThan(8,$redis->ttl($keyTTL));
- $data = $redis->set($keyExist,'3',['NX','EX'=>10]);
- $this->assertNull($data);
- $redis->del($keyExist);;
- $redis->set($keyExist,'4',['NX','EX'=>20]);
- $this->assertEquals('4',$redis->get($keyExist));
- $this->assertGreaterThan(18,$redis->ttl($keyTTL));
- $data = $redis->exists($key);
- $this->assertEquals(1, $data);
- $data = $redis->set($key, $value);
- $this->assertTrue($data);
- $value += 1;
- $data = $redis->incr($key);
- $this->assertEquals($value, $data);
- $value += 10;
- $data = $redis->incrBy($key, 10);
- $this->assertEquals($value, $data);
- $value -= 1;
- $data = $redis->decr($key);
- $this->assertEquals($value, $data);
- $value -= 10;
- $data = $redis->decrBy($key, 10);
- $this->assertEquals($value, $data);
- $key = 'stringTest';
- $value = 'tioncico';
- $redis->set($key, $value);
- $data = $redis->getRange($key, 1, 2);
- $this->assertEquals('io', $data);
- $data = $redis->getSet($key, $value . 'a');
- $this->assertEquals($data, $value);
- $redis->set($key, $value);
- $bitKey = 'testBit';
- $bitValue = 10000;
- $redis->set($bitKey, $bitValue);
- $data = $redis->setBit($bitKey, 1, 0);
- $this->assertEquals(0, $data);
- $data = $redis->getBit($key, 1);
- $this->assertEquals(1, $data);
- $field = [
- 'stringField1',
- 'stringField2',
- 'stringField3',
- 'stringField4',
- 'stringField5',
- ];
- $value = [
- 1,
- 2,
- 3,
- 4,
- 5,
- ];
- $data = $redis->mSet([
- "{$field[0]}" => $value[0],
- "{$field[1]}" => $value[1],
- "{$field[2]}" => $value[2],
- "{$field[3]}" => $value[3],
- "{$field[4]}" => $value[4],
- ]);
- $this->assertTrue($data);
- $data = $redis->mGet([$field[3], $field[2], $field[1]]);
- $this->assertEquals([$value[3], $value[2], $value[1]], $data);
- $data = $redis->setEx($key, 1, $value[0] . $value[0]);
- $this->assertTrue($data);
- $this->assertEquals($value[0] . $value[0], $redis->get($key));
- $data = $redis->pSetEx($key, 1, $value[0]);
- $this->assertTrue($data);
- $this->assertEquals($value[0], $redis->get($key));
- $redis->del($key);
- $data = $redis->setNx($key, 1);
- $this->assertEquals(1, $data);
- $redis->del($field[0]);
- $data = $redis->mSetNx([
- "{$field[0]}" => $value[0],
- "{$field[1]}" => $value[1],
- ]);
- $this->assertEquals(0, $data);
- $this->assertEquals($value[1], $redis->get($field[1]));
- $redis->del($field[1]);
- $data = $redis->mSetNx([
- "{$field[0]}" => $value[0] + 1,
- "{$field[1]}" => $value[1] + 1,
- ]);
- $this->assertEquals(1, $data);
- $this->assertEquals($value[0] + 1, $redis->get($field[0]));
- $data = $redis->setRange($field[0], 1, 1);
- $this->assertEquals(2, $data);
- $this->assertEquals('2' . $value[0], $redis->get($field[0]));
- $data = $redis->strLen($field[0]);
- $this->assertEquals(2, $data);
- $redis->set($key, 1);
- $data = $redis->incrByFloat($key, 0.1);
- $this->assertEquals(1.1, $data);
- $data = $redis->appEnd($field[0], '1');
- $this->assertEquals($redis->strLen($field[0]), $data);
- $this->assertEquals('2' . $value[0] . '1', $redis->get($field[0]));
- //迭代测试
- $cursor = 0;
- $redis->flushAll();
- $redis->set('xxxa', '仙士可');
- $redis->set('xxxb', '仙士可');
- $redis->set('xxxc', '仙士可');
- $redis->set('xxxd', '仙士可');
- $data = [];
- do {
- $keys = $redis->scan($cursor, 'xxx*', 1);
- $data = array_merge($data, $keys);
- } while ($cursor);
- $this->assertEquals(4, count($data));
- }
- /**
- * testHash
- * @author Tioncico
- * Time: 11:54
- */
- function testHash()
- {
- $key = 'hKey';
- $field = [
- 'hField1',
- 'hField2',
- 'hField3',
- 'hField4',
- 'hField5',
- ];
- $value = [
- 1,
- 2,
- 3,
- 4,
- 5,
- ];
- $redis = $this->redis;
- $redis->del($key);
- $data = $redis->hSet($key, $field[0], $value[0]);
- $this->assertNotFalse($data);
- $data = $redis->hGet($key, $field[0]);
- $this->assertEquals($data, $value[0]);
- $data = $redis->hExists($key, $field[0]);
- $this->assertEquals(1, $data);
- $data = $redis->hDel($key, $field[0]);
- $this->assertEquals(1, $data, $redis->getErrorMsg());
- $data = $redis->hExists($key, $field[0]);
- $this->assertEquals(0, $data);
- $data = $redis->hMSet($key, [
- "{$field[0]}" => $value[0],
- "{$field[1]}" => $value[1],
- "{$field[2]}" => $value[2],
- "{$field[3]}" => $value[3],
- "{$field[4]}" => $value[4],
- ]);
- $this->assertTrue($data);
- $data = $redis->hValS($key);
- sort($data);
- $this->assertEquals($value, $data);
- $data = $redis->hGetAll($key);
- $keyTmp = array_keys($data);
- sort($keyTmp);
- $this->assertEquals($field, $keyTmp);
- $valueTmp = array_values($data);
- sort($valueTmp);
- $this->assertEquals($value, $valueTmp);
- $this->assertEquals($value, [
- $data[$field[0]],
- $data[$field[1]],
- $data[$field[2]],
- $data[$field[3]],
- $data[$field[4]],
- ]);
- $data = $redis->hKeys($key);
- sort($data);
- $this->assertEquals($field, $data);
- $data = $redis->hLen($key);
- $this->assertEquals(count($field), $data);
- $data = $redis->hMGet($key, [$field[0], $field[1], $field[2]]);
- $this->assertEquals([1, 2, 3], $data);
- $data = $redis->hIncrBy($key, $field[4], 1);
- $this->assertEquals($value[4] + 1, $data);
- $data = $redis->hIncrByFloat($key, $field[1], 1.1);
- $this->assertEquals($value[1] + 1.1, $data);
- $data = $redis->hSetNx($key, $field[0], 1);
- $this->assertEquals(0, $data);
- $data = $redis->hSetNx($key, $field[0] . 'a', 1);
- $this->assertEquals(1, $data);
- $this->assertEquals(1, $redis->hGet($key, $field[0] . 'a'));
- $cursor = 0;
- $redis->del('a');
- $redis->hMSet('a', [
- 'a' => 'tioncico',
- 'b' => 'tioncico',
- 'c' => 'tioncico',
- 'd' => 'tioncico',
- 'e' => 'tioncico',
- 'f' => 'tioncico',
- 'g' => 'tioncico',
- 'h' => 'tioncico',
- ]);
- $data = [];
- do {
- $keys = $redis->hScan('a', $cursor);
- $data = array_merge($data, $keys);
- } while ($cursor);
- $this->assertEquals(8, count($data));
- }
- /**
- * testList
- * @author tioncico
- * Time: 下午8:17
- */
- function testList()
- {
- $redis = $this->redis;
- $key = [
- 'listKey1',
- 'listKey2',
- 'listKey3',
- ];
- $value = [
- 'a', 'b', 'c', 'd'
- ];
- $redis->flushAll();
- //测试null的时候
- $data = $redis->bLPop([$key[0], $key[1]], 1);
- $this->assertNull($data);
- $data = $redis->lPush($key[0], $value[0], $value[1]);
- $this->assertEquals(2, $data);
- //测试null的时候
- $data = $redis->bLPop([$key[1]], 1);
- $this->assertNull($data);
- $data = $redis->bRPop([$key[1]], 1);
- $this->assertNull($data);
- $data = $redis->bLPop([$key[0], $key[1]], 1);
- $this->assertTrue(!!$data);
- $data = $redis->bRPop([$key[0], $key[1]], 1);
- $this->assertTrue(!!$data);
- $redis->del($key[0]);
- $redis->lPush($key[0], $value[0], $value[1]);
- $data = $redis->bRPopLPush($key[0], $key[1], 1);
- $this->assertEquals($value[0], $data);
- $redis->del($key[0]);
- $redis->lPush($key[0], $value[0], $value[1]);
- $data = $redis->rPopLPush($key[0], $key[1]);
- $this->assertEquals($value[0], $data);
- $redis->del($key[0]);
- $redis->lPush($key[0], $value[0], $value[1]);
- $data = $redis->lIndex($key[0], 1);
- $this->assertEquals($value[0], $data);
- $data = $redis->lLen($key[0]);
- $this->assertEquals(2, $data);
- $data = $redis->lInsert($key[0], true, 'b', 'c');
- $this->assertEquals($redis->lLen($key[0]), $data);
- $data = $redis->lInsert($key[0], true, 'd', 'c');
- $this->assertEquals(-1, $data);
- $redis->del($key[1]);
- $data = $redis->rPush($key[1], $value[0], $value[2], $value[1]);
- $this->assertEquals(3, $data);
- $data = $redis->lRange($key[1], 0, 3);
- $this->assertEquals(count($data), 3);
- $data = $redis->lPop($key[1]);
- $this->assertEquals($value[0], $data);
- $data = $redis->rPop($key[1]);
- $this->assertEquals($value[1], $data);
- $data = $redis->lPuShx($key[1], 'x');
- $this->assertEquals($redis->lLen($key[1]), $data);
- $this->assertEquals('x', $redis->lPop($key[1]));
- $data = $redis->rPuShx($key[1], 'z');
- $this->assertEquals($redis->lLen($key[1]), $data);
- $this->assertEquals('z', $redis->rPop($key[1]));
- $redis->del($key[1]);
- $redis->rPush($key[1], $value[0], $value[0], $value[0]);
- $data = $redis->lRem($key[1], 1, $value[0]);
- $this->assertEquals(1, $data);
- $data = $redis->lSet($key[1], 0, 'xx');
- $this->assertTrue($data);
- $this->assertEquals('xx', $redis->lPop($key[1]));
- $data = $redis->lTrim($key[1], 0, 2);
- $this->assertTrue($data);
- $this->assertEquals(1, $redis->lLen($key[1]));
- }
- /**
- * 集合测试
- * testMuster
- * @author Tioncico
- * Time: 9:10
- */
- function testMuster()
- {
- $redis = $this->redis;
- $key = [
- 'muster1',
- 'muster2',
- 'muster3',
- 'muster4',
- 'muster5',
- ];
- $value = [
- '1',
- '2',
- '3',
- '4',
- ];
- $redis->del($key[0]);
- $redis->del($key[1]);
- $data = $redis->sAdd($key[0], $value[0], $value[1]);
- $this->assertEquals(2, $data);
- $data = $redis->sCard($key[0]);
- $this->assertEquals(2, $data);
- $redis->sAdd($key[1], $value[0], $value[2]);
- $data = $redis->sDiff($key[0], $key[1]);
- $this->assertEquals([$value[1]], $data);
- $data = $redis->sDiff($key[1], $key[0]);
- $this->assertEquals([$value[2]], $data);
- $data = $redis->sMembers($key[0]);
- $this->assertEquals([$value[0], $value[1]], $data);
- $data = $redis->sMembers($key[1]);
- $this->assertEquals([$value[0], $value[2]], $data);
- $data = $redis->sDiffStore($key[2], $key[0], $key[1]);
- $this->assertEquals(1, $data);
- $data = $redis->sMembers($key[2]);
- $this->assertEquals([$value[1]], $data);
- $data = $redis->sInter($key[0], $key[1]);
- $this->assertEquals([$value[0]], $data);
- $data = $redis->sInterStore($key[3], $key[0], $key[1]);
- $this->assertEquals(1, $data);
- $this->assertEquals([$value[0]], $redis->sMembers($key[3]));
- $data = $redis->sIsMember($key[0], $value[0]);
- $this->assertEquals(1, $data);
- $data = $redis->sIsMember($key[0], $value[3]);
- $this->assertEquals(0, $data);
- $data = $redis->sMove($key[0], $key[1], $value[1]);
- $this->assertEquals(1, $data);
- $data = $redis->sPop($key[0]);
- $this->assertEquals(1, $data);
- $redis->del($key[3]);
- $redis->sAdd($key[3], $value[0], $value[1], $value[2], $value[3]);
- $data = $redis->sRandMember($key[3], 4);
- $this->assertEquals(4, count($data));
- $data = $redis->sRandMember($key[3].rand(1,1111));
- $this->assertEquals(null, $data);
- $data = $redis->sRandMember($key[3]);
- $this->assertTrue(in_array($data,$value));
- $data = $redis->sRem($key[3], $value[0], $value[1], $value[2], $value[3]);
- $this->assertEquals(4, $data);
- $this->assertEquals([], $redis->sMembers($key[3]));
- $data = $redis->sUnion($key[0], $key[1]);
- $this->assertEquals([$value[0], $value[1], $value[2]], $data);
- $redis->del($key[1]);
- $redis->del($key[2]);
- $redis->del($key[3]);
- $redis->del($key[4]);
- $redis->sAdd($key[1], 1, 2, 3, 4);
- $redis->sAdd($key[2], 5);
- $redis->sAdd($key[3], 6, 7);
- $data = $redis->sUnIonStore($key[4], $key[1], $key[2], $key[3]);
- $this->assertEquals(7, $data);
- $cursor = 0;
- $redis->del('a');
- $redis->sAdd('a', 'a1', 'a2', 'a3', 'a4', 'a5');
- $data = [];
- do {
- $keys = $redis->sScan('a', $cursor, '*', 1);
- $data = array_merge($data, $keys);
- } while ($cursor);
- $this->assertEquals(5, count($data));
- }
- /**
- * 有序集合测试
- * testSortMuster
- * @author Tioncico
- * Time: 14:17
- */
- function testSortMuster()
- {
- $redis = $this->redis;
- $key = [
- 'sortMuster1',
- 'sortMuster2',
- 'sortMuster3',
- 'sortMuster4',
- 'sortMuster5',
- ];
- $member = [
- 'member1',
- 'member2',
- 'member3',
- 'member4',
- 'member5',
- ];
- $score = [
- 1,
- 2,
- 3,
- 4,
- ];
- $redis->del($key[0]);
- $data = $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
- $this->assertEquals(2, $data);
- $data = $redis->zCard($key[0]);
- $this->assertEquals(2, $data);
- $data = $redis->zCount($key[0], 0, 3);
- $this->assertEquals(2, $data);
- $data = $redis->zInCrBy($key[0], 1, $member[1]);
- $this->assertEquals($score[1] + 1, $data);
- $redis->del($key[0]);
- $redis->del($key[1]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
- $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
- $data = $redis->zInTerStore($key[2], [$key[0], $key[1]], [1, 2]);
- $this->assertEquals(1, $data);
- $data = $redis->zLexCount($key[0], '-', '+');
- $this->assertEquals(2, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRange($key[0], 0, -1, true);
- $this->assertEquals([
- $member[0] => $score[0],
- $member[1] => $score[1],
- $member[2] => $score[2],
- ], $data);
- $data = $redis->zRange($key[0], 0, -1, false);
- $this->assertEquals([
- $member[0],
- $member[1],
- $member[2],
- ], $data);
- $data = $redis->zRangeByLex($key[0], '-', '+');
- $this->assertEquals(3, count($data));
- $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => true, 'limit' => array(0, 2)]);
- $this->assertEquals([
- $member[1] => $score[1],
- $member[2] => $score[2],
- ], $data);
- $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => false, 'limit' => array(0, 2)]);
- $this->assertEquals([
- $member[1],
- $member[2],
- ], $data);
- $data = $redis->zRank($key[0], $member[1]);
- $this->assertEquals(1, $data);
- $data = $redis->zRem($key[0], $member[1], $member[2]);
- $this->assertEquals(2, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRemRangeByLex($key[0], '-', '+');
- $this->assertEquals(3, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRemRangeByRank($key[0], 0, 2);
- $this->assertEquals(3, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRemRangeByScore($key[0], 0, 3);
- $this->assertEquals(3, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRevRange($key[0], 0, 3);
- $this->assertEquals([
- $member[2],
- $member[1],
- $member[0],
- ], $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRevRange($key[0], 0, 3, true);
- $this->assertEquals([
- $member[2] => $score[2],
- $member[1] => $score[1],
- $member[0] => $score[0],
- ], $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRevRangeByScore($key[0], 3, 0, ['withScores' => true, 'limit' => array(0, 3)]);
- $this->assertEquals([
- $member[2] => $score[2],
- $member[1] => $score[1],
- $member[0] => $score[0],
- ], $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zReVRangeByScore($key[0], 3, 0, ['withScores' => false, 'limit' => array(0, 3)]);
- $this->assertEquals([
- $member[2],
- $member[1],
- $member[0],
- ], $data);
- $data = $redis->zRevRank($key[0], $member[0]);
- $this->assertEquals(2, $data);
- $data = $redis->zScore($key[0], $member[0]);
- $this->assertEquals($score[0], $data);
- $redis->del($key[0]);
- $redis->del($key[1]);
- $redis->del($key[2]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
- $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
- $data = $redis->zUnionStore($key[2], [$key[1], $key[0]]);
- $this->assertEquals(3, $data);
- $cursor = 0;
- $redis->del('a');
- $redis->zAdd('a', 1, 'a1', 2, 'a2', 3, 'a3', 4, 'a4', 5, 'a5');
- $data = [];
- do {
- $keys = $redis->zScan('a', $cursor, '*', 1);
- $data = array_merge($data, $keys);
- } while ($cursor);
- $this->assertEquals(5, count($data));
- }
- /**
- * Stream 测试
- * @author gaobinzhan <gaobinzhan@gmail.com>
- */
- function testStream(){
- $redis = $this->redis;
- $id = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- $this->assertIsString($id);
- $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy'], 1);
- $length = $redis->xLen('test');
- $this->assertEquals(1, $length);
- $ids[] = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- $ids[] = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- $delNum = $redis->xDel('test',$ids);
- $this->assertEquals(2, $delNum);
- $result = $redis->xRange('test','-','+',0);
- $this->assertEmpty($result);
- $result = $redis->xRange('test','-','+');
- $this->assertIsArray($result);
- $result = $redis->xRevRange('test','+','-',0);
- $this->assertEmpty($result);
- $result = $redis->xRevRange('test','+','-');
- $this->assertIsArray($result);
- $redis->del('test1');
- $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
- $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
- $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
- $len = $redis->xTrim('test1',2);
- $this->assertEquals(1, $len);
- $result = $redis->xRead(['test1' => '0-0'],1,0);
- $this->assertIsArray($result);
- $this->assertCount(1,$result['test1']);
- $result = $redis->xRead(['test1' => '0-0'],2,0);
- $this->assertCount(2,$result['test1']);
- $result = $redis->xRead(['test' => '$'],1,1000);
- $this->assertFalse($result);
- go(function (){
- $result = (new Redis(new RedisConfig([
- 'host' => REDIS_HOST,
- 'port' => REDIS_PORT,
- 'auth' => REDIS_AUTH
- ])))->xRead(['test' => '$'],1,1000);
- $this->assertIsArray($result);
- });
- go(function (){
- (new Redis(new RedisConfig([
- 'host' => REDIS_HOST,
- 'port' => REDIS_PORT,
- 'auth' => REDIS_AUTH
- ])))->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- });
- $array = $redis->xGroup('HELP');
- $this->assertIsArray($array);
- $groupName = time();
- $bool = $redis->xGroup('CREATE','mystream',$groupName,'$',true);
- $this->assertTrue($bool);
- $bool = $redis->xGroup('SETID','mystream',$groupName,0);
- $this->assertTrue($bool);
- $int = $redis->xGroup('DELCONSUMER','mystream',$groupName,1);
- $this->assertEquals(0,$int);
- $num = $redis->xGroup('DESTROY','mystream',$groupName);
- $this->assertEquals(1,$num);
- $array = $redis->xInfo('STREAM','mystream');
- $this->assertIsArray($array);
- $groupName =time();
- $redis->xGroup('CREATE','mystream',$groupName,'$',true);
- $array = $redis->xInfo('GROUPS','mystream');
- $this->assertIsArray($array);
- $array = $redis->xPending('mystream',$groupName);
- $this->assertIsArray($array);
- $array = $redis->xPending('mystream',$groupName,'-','+',10);
- $this->assertIsArray($array);
- $array = $redis->xPending('mystream',$groupName,'-','+',10,'consumer-123');
- $this->assertIsArray($array);
- $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
- $this->assertFalse($result);
- go(function () use ($groupName){
- $result = (new Redis(new RedisConfig([
- 'host' => REDIS_HOST,
- 'port' => REDIS_PORT,
- 'auth' => REDIS_AUTH
- ])))->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
- $this->assertIsArray($result);
- });
- go(function (){
- (new Redis(new RedisConfig([
- 'host' => REDIS_HOST,
- 'port' => REDIS_PORT,
- 'auth' => REDIS_AUTH
- ])))->xAdd('mystream','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- });
- $redis->xAdd('mystream','*',['name' => 1]);
- $num = $redis->xAck('mystream',$groupName,[0]);
- $this->assertEquals(0,$num);
- $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
- $num = $redis->xAck('mystream',$groupName,array_keys($result['mystream']));
- $this->assertEquals(1,$num);
- $redis->xAdd('mystream','*',['name' => 1]);
- $redis->xAdd('mystream','*',['name' => 1]);
- $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],2,0);
- $num = $redis->xAck('mystream',$groupName,array_keys($result['mystream']));
- $this->assertEquals(2,$num);
- $redis->xAdd('mystream','*',['name' => 1]);
- $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],2,0);
- $result = $redis->xPending('mystream',$groupName,'-','+',2);
- $ids[] = $result[0][0];
- $ids[] = $result[1][0];
- $array = $redis->xClaim('mystream',$groupName,'abc',2,$ids);
- $this->assertIsArray($array);
- }
- /**
- * 基数统计 测试
- * testHyperLog
- * @author Tioncico
- * Time: 14:59
- */
- function testHyperLog()
- {
- $redis = $this->redis;
- $key = [
- 'hp1',
- 'hp2',
- 'hp3',
- 'hp4',
- 'hp5',
- ];
- $redis->del($key[0]);
- $redis->del($key[1]);
- $data = $redis->pfAdd($key[0], [1, 2, 2, 3, 3]);
- $this->assertEquals(1, $data);
- $redis->pfAdd($key[1], [1, 2, 2, 3, 3]);
- $data = $redis->pfCount([$key[0], $key[1]]);
- $this->assertEquals(3, $data);
- $data = $redis->pfMerge($key[2], [$key[0], $key[1]]);
- $this->assertEquals(1, $data);
- }
- /**
- * 发布订阅测试
- * testSubscribe
- * @author Tioncico
- * Time: 14:59
- */
- function testSubscribe()
- {
- // $this->assertEquals(1,1);
- go(function () {
- $redis = new Redis(new RedisConfig([
- 'host' => REDIS_HOST,
- 'port' => REDIS_PORT,
- 'auth' => REDIS_AUTH
- ]));
- $redis->pSubscribe(function (Redis $redis, $pattern, $str) {
- $redis2 = new Redis(new RedisConfig([
- 'host' => REDIS_HOST,
- 'port' => REDIS_PORT,
- 'auth' => REDIS_AUTH
- ]));
- var_dump($redis2);
- var_dump($redis2->set('a',1));
- var_dump($redis2->get('a'));
- $this->assertEquals('test', $str);
- $data = $redis->unsubscribe('test');
- $this->assertTrue(!!$data);
- $redis->setSubscribeStop(true);
- }, 'test', 'test1', 'test2');
- });
- go(function () {
- $redis = new Redis(new RedisConfig([
- 'host' => REDIS_HOST,
- 'port' => REDIS_PORT,
- 'auth' => REDIS_AUTH
- ]));
- $redis->subscribe(function (Redis $redis, $pattern, $str) {
- $this->assertEquals('test', $str);
- $data = $redis->unsubscribe('test');
- $this->assertTrue(!!$data);
- $redis->setSubscribeStop(true);
- }, 'test', 'test1', 'test2');
- });
- $redis = $this->redis;
- $data = $redis->pubSub('CHANNELS');
- $this->assertIsArray($data);
- Coroutine::sleep(1);
- $data = $redis->publish('test2', 'test');
- $this->assertGreaterThan(0, $data);
- //
- $data = $redis->pUnSubscribe('test');
- $this->assertTrue(!!$data);
- }
- /**
- * 事务测试
- * testTransaction
- * @author tioncico
- * Time: 下午5:40
- */
- function testTransaction()
- {
- $redis = $this->redis;
- $data = $redis->multi();
- $this->assertTrue($data);
- $this->assertEquals(true, $redis->getTransaction()->isTransaction());
- $redis->del('ha');
- $data = $redis->hset('ha', 'a', 1);
- $this->assertEquals('QUEUED', $data);
- $data = $redis->hset('ha', 'b', '2');
- $this->assertEquals('QUEUED', $data);
- $data = $redis->hset('ha', 'c', '3');
- $this->assertEquals('QUEUED', $data);
- $data = $redis->hGetAll('ha');
- $this->assertEquals('QUEUED', $data);
- $data = $redis->exec();
- $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3], $data[4]);
- $this->assertEquals(false, $redis->getTransaction()->isTransaction());
- $redis->multi();
- $this->assertEquals(true, $redis->getTransaction()->isTransaction());
- $data = $redis->discard();
- $this->assertEquals(true, $data);
- $this->assertEquals(false, $redis->getTransaction()->isTransaction());
- $data = $redis->watch('a', 'b', 'c');
- $data = $redis->unwatch();
- $this->assertEquals(1, $data);
- }
- /**
- * 管道测试
- * testTransaction
- * @author tioncico
- * Time: 下午5:40
- */
- function testPipe()
- {
- $this->assertEquals(1, 1);
- $redis = $this->redis;
- $redis->get('a');
- $data = $redis->startPipe();
- $this->assertTrue($data);
- $this->assertEquals(true, $redis->getPipe()->isStartPipe());
- $redis->del('ha');
- $data = $redis->hset('ha', "a", "a\r\nb\r\nc");
- $this->assertEquals('PIPE', $data);
- $data = $redis->hset('ha', 'b', '2');
- $this->assertEquals('PIPE', $data);
- $data = $redis->hset('ha', 'c', '3');
- $this->assertEquals('PIPE', $data);
- $data = $redis->hGetAll('ha');
- $this->assertEquals('PIPE', $data);
- $data = $redis->execPipe();
- $this->assertEquals(['a' => "a\r\nb\r\nc", 'b' => 2, 'c' => 3], $data[4]);
- $this->assertEquals(false, $redis->getPipe()->isStartPipe());
- $redis->startPipe();
- $this->assertEquals(true, $redis->getPipe()->isStartPipe());
- $data = $redis->set("a", '1');
- $this->assertTrue($data);
- $this->assertEquals('Set', ($redis->getPipe()->getCommandLog()[0][0]));
- $data = $redis->discardPipe();
- $this->assertEquals(true, $data);
- $this->assertEquals(false, $redis->getPipe()->isStartPipe());
- }
- /**
- * 脚本执行测试
- * testScript
- * @author tioncico
- * Time: 下午5:41
- */
- function testScript()
- {
- $this->assertEquals(1, 1);
- $redis = $this->redis;
- // $data = $redis->eval('s','s','a','1','2','a');
- // $this->assertEquals(1,$data);
- //
- // $data = $redis->evalsha('a','g','g','1','a','a');
- // $this->assertEquals(1,$data);
- //
- // $data = $redis->scriptExists('a','f');
- // $this->assertEquals(1,$data);
- //
- // $data = $redis->scriptFlush();
- // $this->assertEquals(1,$data);
- //
- // $data = $redis->scriptKill();
- // $this->assertEquals(1,$data);
- //
- // $data = $redis->scriptLoad('a');
- // $this->assertEquals(1,$data);
- }
- /**
- * 服务器命令测试
- * testServer
- * @author tioncico
- * Time: 下午5:41
- */
- function testServer()
- {
- $redis = $this->redis;
- $data = $redis->bgRewriteAof();
- $this->assertEquals('Background append only file rewriting started', $data);
- Coroutine::sleep(1);
- $data = $redis->bgSave();
- $this->assertEquals('Background saving started', $data);
- $data = $redis->clientList();
- $this->assertIsArray($data);
- $data = $redis->clientSetName('test');
- $this->assertTrue($data);
- $data = $redis->clientGetName();
- $this->assertEquals('test', $data);
- $data = $redis->clientPause(1);
- $this->assertEquals(1, $data);
- $data = $redis->command();
- $this->assertIsArray($data);
- $data = $redis->commandCount();
- $this->assertGreaterThan(0, $data);
- $data = $redis->commandGetKeys('MSET', 'a', 'b', 'c', 'd');
- $this->assertEquals(['a', 'c'], $data);
- $data = $redis->time();
- $this->assertIsArray($data);
- $data = $redis->commandInfo('get', 'set');
- $this->assertIsArray($data);
- $data = $redis->configGet('*max-*-entries*');
- $this->assertIsArray($data);
- $data = $redis->configSet('appendonly', 'yes');
- $this->assertTrue($data);
- $data = $redis->configRewrite();
- $this->assertTrue($data);
- $data = $redis->configResetStat();
- $this->assertTrue($data);
- $data = $redis->dBSize();
- $this->assertGreaterThanOrEqual(0, $data);
- $redis->set('a', 1);
- $data = $redis->debugObject('a');
- $this->assertIsString($data);
- $data = $redis->flushAll();
- $this->assertTrue($data);
- $data = $redis->flushDb();
- $this->assertTrue($data);
- $data = $redis->info();
- $this->assertIsArray($data);
- $data = $redis->lastSave();
- $this->assertNotFalse($data);
- go(function () {
- $redis = new Redis(new RedisConfig([
- 'host' => REDIS_HOST,
- 'port' => REDIS_PORT,
- 'auth' => REDIS_AUTH
- ]));
- $redis->monitor(function (Redis $redis, $data) {
- $this->assertIsString($data);
- $redis->set('a', 1);
- $redis->setMonitorStop(true);
- });
- });
- go(function () {
- $redis = new Redis(new RedisConfig([
- 'host' => REDIS_HOST,
- 'port' => REDIS_PORT,
- 'auth' => REDIS_AUTH
- ]));
- Coroutine::sleep(1);
- $redis->set('a', 1);
- });
- $data = $redis->save();
- $this->assertTrue($data);
- $data = $redis->clientList();
- $data = $redis->clientKill($data[0]['addr']);
- $this->assertTrue($data);
- try {
- $redis->slowLog('get', 'a');
- }catch (RedisException $exception){
- $this->assertEquals('ERR value is not an integer or out of range',$exception->getMessage());
- }
- }
- /**
- * geohash测试
- * testGeohash
- * @author tioncico
- * Time: 下午6:11
- */
- function testGeohash()
- {
- $redis = $this->redis;
- $key = 'testGeohash';
- $redis->del($key);
- $data = $redis->geoAdd($key, [
- ['118.6197800000', '24.88849', 'user1',],
- ['118.6197800000', '24.88859', 'user2',],
- ['114.8197800000', '25.88849', 'user3'],
- ['118.8197800000', '22.88849', 'user4'],
- ]);
- $this->assertEquals(4, $data);
- $data = $redis->geoAdd($key, [
- ['118.6197800000', '24.88869', 'user5',],
- ]);
- $data = $redis->geoDist($key, 'user1', 'user2');
- $this->assertGreaterThan(10, $data);
- $data = $redis->geoHash($key, 'user1', 'user2');
- $this->assertIsArray($data);
- $data = $redis->geoPos($key, 'user1', 'user2');
- $this->assertIsArray($data);
- $data = $redis->geoRadius($key, '118.6197800000', '24.88849', 100, 'm', false, false, false, null,'desc');
- $this->assertEquals(['user5','user2', 'user1'], $data);
- $data = $redis->geoRadiusByMember($key, 'user1', 100, 'm', false, false, false, 2,'DESC');
- //限制为2,并且desc
- $this->assertEquals(['user5', 'user2'], $data);
- $data = $redis->geoRadiusByMember($key, 'user1', 100, 'm', false, false, false, null,'asc');
- //不限制并且asc
- $this->assertEquals(['user1', 'user2', 'user5'], $data);
- }
- }
|