|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of RedisClient. |
| 4 | + * git: https://github.com/cheprasov/php-redis-client |
| 5 | + * |
| 6 | + * (C) Alexander Cheprasov <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | +namespace Test\Integration\Version2x6; |
| 12 | + |
| 13 | +use RedisClient\Client\Version\RedisClient2x6; |
| 14 | +use RedisClient\Exception\ErrorResponseException; |
| 15 | + |
| 16 | +/** |
| 17 | + * @see PubSubCommandsTrait |
| 18 | + */ |
| 19 | +class PubSubCommandsTest extends \PHPUnit_Framework_TestCase { |
| 20 | + |
| 21 | + const TEST_REDIS_SERVER_1 = TEST_REDIS_SERVER_2x6_1; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var RedisClient2x6 |
| 25 | + */ |
| 26 | + protected static $Redis; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var RedisClient2x6 |
| 30 | + */ |
| 31 | + protected static $Redis2; |
| 32 | + |
| 33 | + /** |
| 34 | + * @inheritdoc |
| 35 | + */ |
| 36 | + public static function setUpBeforeClass() { |
| 37 | + static::$Redis = new RedisClient2x6([ |
| 38 | + 'server' => static::TEST_REDIS_SERVER_1, |
| 39 | + 'timeout' => 2, |
| 40 | + ]); |
| 41 | + static::$Redis2 = new RedisClient2x6([ |
| 42 | + 'server' => static::TEST_REDIS_SERVER_1, |
| 43 | + 'timeout' => 2, |
| 44 | + ]); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @inheritdoc |
| 49 | + */ |
| 50 | + public static function tearDownAfterClass() { |
| 51 | + static::$Redis->flushall(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @inheritdoc |
| 56 | + */ |
| 57 | + protected function setUp() { |
| 58 | + static::$Redis->flushall(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @see PubSubCommandsTrait::subscribe |
| 63 | + * @see PubSubCommandsTrait::unsubscribe |
| 64 | + */ |
| 65 | + public function test_subscribe_and_unsubscribe() { |
| 66 | + $Redis = static::$Redis; |
| 67 | + $Redis2 = static::$Redis2; |
| 68 | + |
| 69 | + $time = time(); |
| 70 | + $messages = []; |
| 71 | + $posts = []; |
| 72 | + |
| 73 | + $result = $Redis->subscribe('channel-foo', |
| 74 | + function($type, $channel, $message) use ($Redis2, $time, &$messages, &$posts) { |
| 75 | + if (time() - $time > 1 || count($messages) > 20) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + if (!isset($type)) { |
| 79 | + sleep(1); |
| 80 | + return true; |
| 81 | + } |
| 82 | + if ($type === 'message') { |
| 83 | + $messages[] = $message; |
| 84 | + } |
| 85 | + $Redis2->publish('channel-foo', $post = md5(rand(1, 9999))); |
| 86 | + $Redis2->publish('channel-bar', md5(rand(1, 9999))); |
| 87 | + $posts[] = $post; |
| 88 | + |
| 89 | + return true; |
| 90 | + }); |
| 91 | + |
| 92 | + $this->assertSame(['unsubscribe', 'channel-foo', 0], $result); |
| 93 | + array_pop($posts); |
| 94 | + $this->assertSame($posts, $messages); |
| 95 | + |
| 96 | + $this->assertSame(true, $Redis->set('foo', 'bar')); |
| 97 | + $this->assertSame('bar', $Redis->get('foo')); |
| 98 | + $this->assertSame(['foo'], $Redis->keys('*')); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @see PubSubCommandsTrait::psubscribe |
| 103 | + * @see PubSubCommandsTrait::punsubscribe |
| 104 | + */ |
| 105 | + public function test_psubscribe_and_punsubscribe() { |
| 106 | + $Redis = static::$Redis; |
| 107 | + $Redis2 = static::$Redis2; |
| 108 | + |
| 109 | + $time = time(); |
| 110 | + $messages = []; |
| 111 | + $posts = []; |
| 112 | + |
| 113 | + $result = $Redis->psubscribe('channel-f*', |
| 114 | + function($type, $pattern, $channel, $message) use ($Redis2, $time, &$messages, &$posts) { |
| 115 | + if (time() - $time > 1 || count($messages) > 20) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + if (!isset($type)) { |
| 119 | + sleep(1); |
| 120 | + return true; |
| 121 | + } |
| 122 | + if ($type === 'pmessage') { |
| 123 | + $messages[] = $message; |
| 124 | + } |
| 125 | + $Redis2->publish('channel-foo', $post = md5(rand(1, 9999))); |
| 126 | + $Redis2->publish('channel-bar', md5(rand(1, 9999))); |
| 127 | + $posts[] = $post; |
| 128 | + |
| 129 | + return true; |
| 130 | + }); |
| 131 | + |
| 132 | + $this->assertSame(['punsubscribe', 'channel-f*', 0], $result); |
| 133 | + array_pop($posts); |
| 134 | + $this->assertSame($posts, $messages); |
| 135 | + |
| 136 | + $this->assertSame(true, $Redis->set('foo', 'bar')); |
| 137 | + $this->assertSame('bar', $Redis->get('foo')); |
| 138 | + $this->assertSame(['foo'], $Redis->keys('*')); |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments