|  | 
| 22 | 22 | use Hyperf\Redis\Redis; | 
| 23 | 23 | use Hyperf\Utils\ApplicationContext; | 
| 24 | 24 | use Hyperf\Utils\Context; | 
|  | 25 | +use Hyperf\Utils\Coroutine; | 
| 25 | 26 | use HyperfTest\Redis\Stub\RedisPoolFailedStub; | 
| 26 | 27 | use HyperfTest\Redis\Stub\RedisPoolStub; | 
| 27 | 28 | use Mockery; | 
| @@ -73,6 +74,52 @@ public function testRedisSelect() | 
| 73 | 74 |         $this->assertSame('db:0 name:get argument:xxxx', $res[0]); | 
| 74 | 75 |     } | 
| 75 | 76 | 
 | 
|  | 77 | +    public function testHasAlreadyBeenBoundToAnotherCoroutine() | 
|  | 78 | +    { | 
|  | 79 | +        $chan = new \Swoole\Coroutine\Channel(1); | 
|  | 80 | +        $redis = $this->getRedis(); | 
|  | 81 | +        $ref = new \ReflectionClass($redis); | 
|  | 82 | +        $method = $ref->getMethod('getConnection'); | 
|  | 83 | +        $method->setAccessible(true); | 
|  | 84 | + | 
|  | 85 | +        go(function () use ($chan, $redis, $method) { | 
|  | 86 | +            $id = null; | 
|  | 87 | +            defer(function () use ($redis, $chan, &$id, $method) { | 
|  | 88 | +                $chan->push(true); | 
|  | 89 | +                Coroutine::sleep(0.01); | 
|  | 90 | +                $hasContextConnection = Context::has('redis.connection.default'); | 
|  | 91 | +                $this->assertFalse($hasContextConnection); | 
|  | 92 | +                $connection = $method->invoke($redis, [$hasContextConnection]); | 
|  | 93 | +                $this->assertNotEquals($connection->id, $id); | 
|  | 94 | +                $redis->getConnection(); | 
|  | 95 | +                $chan->push(true); | 
|  | 96 | +            }); | 
|  | 97 | + | 
|  | 98 | +            $redis->keys('*'); | 
|  | 99 | + | 
|  | 100 | +            $hasContextConnection = Context::has('redis.connection.default'); | 
|  | 101 | +            $this->assertFalse($hasContextConnection); | 
|  | 102 | + | 
|  | 103 | +            $redis->multi(); | 
|  | 104 | +            $redis->set('id', uniqid()); | 
|  | 105 | +            $redis->exec(); | 
|  | 106 | + | 
|  | 107 | +            $hasContextConnection = Context::has('redis.connection.default'); | 
|  | 108 | +            $this->assertTrue($hasContextConnection); | 
|  | 109 | + | 
|  | 110 | +            $connection = $method->invoke($redis, [$hasContextConnection]); | 
|  | 111 | +            $id = $connection->id; | 
|  | 112 | +        }); | 
|  | 113 | + | 
|  | 114 | +        $chan->pop(); | 
|  | 115 | +        $factory = $ref->getProperty('factory'); | 
|  | 116 | +        $factory->setAccessible(true); | 
|  | 117 | +        $factory = $factory->getValue($redis); | 
|  | 118 | +        $pool = $factory->getPool('default'); | 
|  | 119 | +        $pool->flushAll(); | 
|  | 120 | +        $chan->pop(); | 
|  | 121 | +    } | 
|  | 122 | + | 
| 76 | 123 |     public function testRedisReuseAfterThrowable() | 
| 77 | 124 |     { | 
| 78 | 125 |         $container = $this->getContainer(); | 
| @@ -108,7 +155,7 @@ private function getContainer() | 
| 108 | 155 |         $container->shouldReceive('get')->once()->with(ConfigInterface::class)->andReturn(new Config([ | 
| 109 | 156 |             'redis' => [ | 
| 110 | 157 |                 'default' => [ | 
| 111 |  | -                    'host' => 'localhost', | 
|  | 158 | +                    'host' => '127.0.0.1', | 
| 112 | 159 |                     'auth' => null, | 
| 113 | 160 |                     'port' => 6379, | 
| 114 | 161 |                     'db' => 0, | 
|  | 
0 commit comments