Repository navigation
Expand file tree
/
Copy pathRedisDriverTest.php
More file actions
157 lines (132 loc) · 5.14 KB
/
Copy pathRedisDriverTest.php
File metadata and controls
157 lines (132 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\AsyncQueue;
use Hyperf\AsyncQueue\Driver\ChannelConfig;
use Hyperf\AsyncQueue\Driver\RedisDriver;
use Hyperf\AsyncQueue\JobMessage;
use Hyperf\Codec\Packer\PhpSerializerPacker;
use Hyperf\Context\ApplicationContext;
use Hyperf\Context\Context;
use Hyperf\Di\Container;
use Hyperf\Redis\RedisFactory;
use Hyperf\Stringable\Str;
use HyperfTest\AsyncQueue\Stub\DemoJob;
use HyperfTest\AsyncQueue\Stub\DemoModel;
use HyperfTest\AsyncQueue\Stub\DemoModelMeta;
use HyperfTest\AsyncQueue\Stub\Redis;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class RedisDriverTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
}
public function testDriverPush()
{
$container = $this->getContainer();
$packer = $container->get(PhpSerializerPacker::class);
$driver = new RedisDriver($container, [
'channel' => 'test',
]);
$id = uniqid();
$driver->push(new DemoJob($id));
/** @var JobMessage $class */
$class = $packer->unpack((string) Context::get('test.async-queue.lpush.value'));
$this->assertSame($id, $class->job()->id);
$key = Context::get('test.async-queue.lpush.key');
$this->assertSame('test:waiting', $key);
$id = uniqid();
$driver->push(new DemoJob($id), 5);
/** @var JobMessage $class */
$class = $packer->unpack((string) Context::get('test.async-queue.zadd.value'));
$this->assertSame($id, $class->job()->id);
$key = Context::get('test.async-queue.zadd.key');
$this->assertSame('test:delayed', $key);
$time = Context::get('test.async-queue.zadd.delay');
$this->assertSame(time() + 5, $time);
}
public function testDemoModelGenerate()
{
$content = Str::random(1000);
$model = new DemoModel(1, 'Hyperf', 1, $content);
$s1 = serialize($model);
$this->assertSame(1128, strlen($s1));
$meta = $model->compress();
$s2 = serialize($meta);
$this->assertSame(65, strlen($s2));
$this->assertInstanceOf(DemoModelMeta::class, $meta);
$model2 = $meta->uncompress();
$this->assertEquals($model, $model2);
}
public function testDemoModelUnCompressToNull()
{
$content = Str::random(1000);
$model = new DemoModel(9999, 'Hyperf', 1, $content);
$s1 = serialize($model);
$this->assertSame(1131, strlen($s1));
$meta = $model->compress();
$s2 = serialize($meta);
$this->assertSame(68, strlen($s2));
$this->assertInstanceOf(DemoModelMeta::class, $meta);
$this->assertNull($meta->uncompress());
}
public function testAsyncQueueJobGenerate()
{
$container = $this->getContainer();
$packer = $container->get(PhpSerializerPacker::class);
$driver = new RedisDriver($container, [
'channel' => 'test',
]);
$id = uniqid();
$content = Str::random(1000);
$model = new DemoModel(1, 'Hyperf', 1, $content);
$driver->push(new DemoJob($id, $model));
$serialized = (string) Context::get('test.async-queue.lpush.value');
$this->assertSame(264, strlen($serialized));
/** @var JobMessage $class */
$class = $packer->unpack($serialized);
$this->assertSame($id, $class->job()->id);
$this->assertEquals($model, $class->job()->model);
$key = Context::get('test.async-queue.lpush.key');
$this->assertSame('test:waiting', $key);
$this->assertSame(true, $class->attempts());
$this->assertSame(false, $class->attempts());
}
protected function getContainer()
{
$packer = new PhpSerializerPacker();
$container = Mockery::mock(Container::class);
$container->shouldReceive('get')->with(PhpSerializerPacker::class)->andReturn($packer);
$container->shouldReceive('get')->once()->with(EventDispatcherInterface::class)->andReturn(null);
$container->shouldReceive('get')->with(\Redis::class)->andReturn(new Redis());
$container->shouldReceive('make')->with(ChannelConfig::class, Mockery::any())->andReturnUsing(function ($class, $args) {
return new ChannelConfig($args['channel']);
});
$container->shouldReceive('make')->with(JobMessage::class, Mockery::any())->andReturnUsing(function ($class, $args) {
return new JobMessage(...$args);
});
$container->shouldReceive('get')->with(RedisFactory::class)->andReturnUsing(function ($_) {
$factory = Mockery::mock(RedisFactory::class);
$factory->shouldReceive('get')->with('default')->andReturn(new Redis());
return $factory;
});
ApplicationContext::setContainer($container);
return $container;
}
}