|
14 | 14 | use Laravel\Pulse\Support\RedisServerException;
|
15 | 15 | use Tests\StorageFake;
|
16 | 16 |
|
| 17 | +$drivers = ['predis', 'phpredis', 'relay']; |
| 18 | + |
| 19 | +function skipWhenExtensionMissing($driver) |
| 20 | +{ |
| 21 | + $extension = match ($driver) { |
| 22 | + 'phpredis' => 'redis', |
| 23 | + default => $driver, |
| 24 | + }; |
| 25 | + |
| 26 | + match ($extension) { |
| 27 | + 'predis' => null, |
| 28 | + 'redis', 'relay' => ! extension_loaded($extension) |
| 29 | + ? test()->markTestSkipped("PHP extension [{$extension}] missing for Redis driver [{$driver}].") |
| 30 | + : null, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
17 | 34 | beforeEach(function () {
|
18 | 35 | try {
|
19 | 36 | Process::timeout(1)->run('redis-cli -p '.Config::get('database.redis.default.port').' FLUSHALL')->throw();
|
|
23 | 40 | });
|
24 | 41 |
|
25 | 42 | it('runs the same commands while ingesting entries', function ($driver) {
|
| 43 | + skipWhenExtensionMissing($driver); |
| 44 | + |
26 | 45 | Config::set('database.redis.client', $driver);
|
27 | 46 |
|
28 | 47 | $commands = captureRedisCommands(fn () => App::make(RedisIngest::class)->ingest(collect([
|
29 | 48 | new Entry(timestamp: 1700752211, type: 'foo', key: 'bar', value: 123),
|
30 | 49 | ])));
|
31 | 50 |
|
32 | 51 | expect($commands)->toContain('"XADD" "laravel_database_laravel:pulse:ingest" "*" "data" "O:19:\"Laravel\\\\Pulse\\\\Entry\\":6:{s:15:\"\x00*\x00aggregations\";a:0:{}s:14:\"\x00*\x00onlyBuckets\";b:0;s:9:\"timestamp\";i:1700752211;s:4:\"type\";s:3:\"foo\";s:3:\"key\";s:3:\"bar\";s:5:\"value\";i:123;}"');
|
33 |
| -})->with(['predis', 'phpredis', 'relay']); |
| 52 | +})->with($drivers); |
34 | 53 |
|
35 | 54 | it('keeps 7 days of data, by default, when trimming', function ($driver) {
|
| 55 | + skipWhenExtensionMissing($driver); |
| 56 | + |
36 | 57 | Config::set('database.redis.client', $driver);
|
37 | 58 | Date::setTestNow(Date::parse('2000-01-02 03:04:05')->startOfSecond());
|
38 | 59 |
|
39 | 60 | $commands = captureRedisCommands(fn () => App::make(RedisIngest::class)->trim());
|
40 | 61 |
|
41 | 62 | expect($commands)->toContain('"XTRIM" "laravel_database_laravel:pulse:ingest" "MINID" "~" "946177445000"');
|
42 |
| -})->with(['predis', 'phpredis', 'relay']); |
| 63 | +})->with($drivers); |
43 | 64 |
|
44 | 65 | it('can configure days of data to keep when trimming', function ($driver) {
|
| 66 | + skipWhenExtensionMissing($driver); |
| 67 | + |
45 | 68 | Config::set('database.redis.client', $driver);
|
46 | 69 | Date::setTestNow(Date::parse('2000-01-02 03:04:05')->startOfSecond());
|
47 | 70 | Config::set('pulse.ingest.trim.keep', '1 day');
|
48 | 71 |
|
49 | 72 | $commands = captureRedisCommands(fn () => App::make(RedisIngest::class)->trim());
|
50 | 73 |
|
51 | 74 | expect($commands)->toContain('"XTRIM" "laravel_database_laravel:pulse:ingest" "MINID" "~" "946695845000"');
|
52 |
| -})->with(['predis', 'phpredis', 'relay']); |
| 75 | +})->with($drivers); |
53 | 76 |
|
54 | 77 | it('can configure the number of entries to keep when trimming', function ($driver) {
|
| 78 | + skipWhenExtensionMissing($driver); |
| 79 | + |
55 | 80 | Config::set('database.redis.client', $driver);
|
56 | 81 | Date::setTestNow(Date::parse('2000-01-02 03:04:05')->startOfSecond());
|
57 | 82 | Config::set('pulse.ingest.trim.keep', 54321);
|
58 | 83 |
|
59 | 84 | $commands = captureRedisCommands(fn () => App::make(RedisIngest::class)->trim());
|
60 | 85 |
|
61 | 86 | expect($commands)->toContain('"XTRIM" "laravel_database_laravel:pulse:ingest" "MAXLEN" "~" "54321"');
|
62 |
| -})->with(['predis', 'phpredis', 'relay']); |
| 87 | +})->with($drivers); |
63 | 88 |
|
64 | 89 | it('runs the same commands while storing', function ($driver) {
|
| 90 | + skipWhenExtensionMissing($driver); |
| 91 | + |
65 | 92 | Config::set('database.redis.client', $driver);
|
66 | 93 | Config::set('pulse.ingest.redis.chunk', 567);
|
67 | 94 | Date::setTestNow(Date::parse('2000-01-02 03:04:05')->startOfSecond());
|
|
80 | 107 |
|
81 | 108 | expect($commands)->toContain('"XRANGE" "laravel_database_laravel:pulse:ingest" "-" "+" "COUNT" "567"');
|
82 | 109 | expect($commands)->toContain('"XDEL" "laravel_database_laravel:pulse:ingest" "'.$firstEntryKey.'" "'.$lastEntryKey.'"');
|
83 |
| -})->with(['predis', 'phpredis', 'relay']); |
| 110 | +})->with($drivers); |
84 | 111 |
|
85 | 112 | it('has consistent return for xadd', function ($driver) {
|
| 113 | + skipWhenExtensionMissing($driver); |
| 114 | + |
86 | 115 | Config::set('database.redis.client', $driver);
|
87 | 116 | $redis = new RedisAdapter(Redis::connection(), App::make('config'));
|
88 | 117 |
|
|
96 | 125 | expect($parts)->toHaveCount(2);
|
97 | 126 | expect($parts[0])->toEqualWithDelta(now()->getTimestampMs(), 50);
|
98 | 127 | expect($parts[1])->toBe('0');
|
99 |
| -})->with(['predis', 'phpredis', 'relay']); |
| 128 | +})->with($drivers); |
100 | 129 |
|
101 | 130 | it('has consistent return for xrange', function ($driver) {
|
| 131 | + skipWhenExtensionMissing($driver); |
| 132 | + |
102 | 133 | Config::set('database.redis.client', $driver);
|
103 | 134 | $redis = new RedisAdapter(Redis::connection(), App::make('config'));
|
104 | 135 | $redis->xadd('stream-name', [
|
|
125 | 156 | expect($parts[1])->toBeIn(['0', '1']);
|
126 | 157 | expect($value)->toBe(array_shift($values));
|
127 | 158 | }
|
128 |
| -})->with(['predis', 'phpredis', 'relay']); |
| 159 | +})->with($drivers); |
129 | 160 |
|
130 | 161 | it('has consistent return for xtrim', function ($driver) {
|
| 162 | + skipWhenExtensionMissing($driver); |
| 163 | + |
131 | 164 | Config::set('database.redis.client', $driver);
|
132 | 165 | $redis = new RedisAdapter(Redis::connection(), App::make('config'));
|
133 | 166 |
|
|
150 | 183 | $result = $redis->xtrim('stream-name', 'MINID', '=', Str::before($lastKey, '-'));
|
151 | 184 |
|
152 | 185 | expect($result)->toBe(2);
|
153 |
| -})->with(['predis', 'phpredis', 'relay']); |
| 186 | +})->with($drivers); |
154 | 187 |
|
155 | 188 | it('throws exception on failure', function ($driver) {
|
| 189 | + skipWhenExtensionMissing($driver); |
| 190 | + |
156 | 191 | Config::set('database.redis.client', $driver);
|
157 | 192 | $redis = new RedisAdapter(Redis::connection(), App::make('config'));
|
158 | 193 |
|
159 | 194 | $redis->xtrim('stream-name', 'FOO', 'a', 'xyz');
|
160 |
| -})->with(['predis', 'phpredis', 'relay'])->throws(RedisServerException::class, 'The Redis version does not support the command or some of its arguments [XTRIM laravel_database_stream-name FOO a xyz]. Redis error: [ERR syntax error].'); |
| 195 | +})->with($drivers)->throws(RedisServerException::class, 'The Redis version does not support the command or some of its arguments [XTRIM laravel_database_stream-name FOO a xyz]. Redis error: [ERR syntax error].'); |
161 | 196 |
|
162 | 197 | it('prepends the error message with the run command', function () {
|
163 | 198 | throw RedisServerException::whileRunningCommand('FOO BAR', 'Something happened');
|
|
0 commit comments