Skip to content

Commit 1674634

Browse files
authored
[1.x] Cache testing improvements (#363)
* Remove redis extension requirements * Fix code styling --------- Co-authored-by: timacdonald <[email protected]>
1 parent 16949d5 commit 1674634

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ jobs:
5757

5858
- name: Install dependencies
5959
run: |
60+
composer require cachewerk/relay --no-interaction --no-update
6061
composer update --prefer-dist --no-interaction --no-progress --${{ matrix.stability }}
6162
6263
- name: Execute tests
@@ -115,6 +116,7 @@ jobs:
115116

116117
- name: Install dependencies
117118
run: |
119+
composer require cachewerk/relay --no-interaction --no-update
118120
composer update --prefer-dist --no-interaction --no-progress --${{ matrix.stability }}
119121
120122
- name: Execute tests
@@ -172,6 +174,7 @@ jobs:
172174
- name: Install dependencies
173175
run: |
174176
composer require "illuminate/contracts=^${{ matrix.laravel }}" --dev --no-update
177+
composer require cachewerk/relay --no-interaction --no-update
175178
composer update --prefer-dist --no-interaction --no-progress
176179
177180
- name: Execute tests
@@ -219,6 +222,7 @@ jobs:
219222

220223
- name: Install dependencies
221224
run: |
225+
composer require cachewerk/relay --no-interaction --no-update
222226
composer update --prefer-dist --no-interaction --no-progress --${{ matrix.stability }}
223227
224228
- name: Execute tests

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"nesbot/carbon": "^2.67|^3.0"
3737
},
3838
"require-dev": {
39-
"cachewerk/relay": "^0.7.0",
4039
"guzzlehttp/guzzle": "^7.7",
4140
"mockery/mockery": "^1.0",
4241
"orchestra/testbench": "^8.23.1|^9.0",

tests/Feature/RedisTest.php

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@
1414
use Laravel\Pulse\Support\RedisServerException;
1515
use Tests\StorageFake;
1616

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+
1734
beforeEach(function () {
1835
try {
1936
Process::timeout(1)->run('redis-cli -p '.Config::get('database.redis.default.port').' FLUSHALL')->throw();
@@ -23,45 +40,55 @@
2340
});
2441

2542
it('runs the same commands while ingesting entries', function ($driver) {
43+
skipWhenExtensionMissing($driver);
44+
2645
Config::set('database.redis.client', $driver);
2746

2847
$commands = captureRedisCommands(fn () => App::make(RedisIngest::class)->ingest(collect([
2948
new Entry(timestamp: 1700752211, type: 'foo', key: 'bar', value: 123),
3049
])));
3150

3251
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);
3453

3554
it('keeps 7 days of data, by default, when trimming', function ($driver) {
55+
skipWhenExtensionMissing($driver);
56+
3657
Config::set('database.redis.client', $driver);
3758
Date::setTestNow(Date::parse('2000-01-02 03:04:05')->startOfSecond());
3859

3960
$commands = captureRedisCommands(fn () => App::make(RedisIngest::class)->trim());
4061

4162
expect($commands)->toContain('"XTRIM" "laravel_database_laravel:pulse:ingest" "MINID" "~" "946177445000"');
42-
})->with(['predis', 'phpredis', 'relay']);
63+
})->with($drivers);
4364

4465
it('can configure days of data to keep when trimming', function ($driver) {
66+
skipWhenExtensionMissing($driver);
67+
4568
Config::set('database.redis.client', $driver);
4669
Date::setTestNow(Date::parse('2000-01-02 03:04:05')->startOfSecond());
4770
Config::set('pulse.ingest.trim.keep', '1 day');
4871

4972
$commands = captureRedisCommands(fn () => App::make(RedisIngest::class)->trim());
5073

5174
expect($commands)->toContain('"XTRIM" "laravel_database_laravel:pulse:ingest" "MINID" "~" "946695845000"');
52-
})->with(['predis', 'phpredis', 'relay']);
75+
})->with($drivers);
5376

5477
it('can configure the number of entries to keep when trimming', function ($driver) {
78+
skipWhenExtensionMissing($driver);
79+
5580
Config::set('database.redis.client', $driver);
5681
Date::setTestNow(Date::parse('2000-01-02 03:04:05')->startOfSecond());
5782
Config::set('pulse.ingest.trim.keep', 54321);
5883

5984
$commands = captureRedisCommands(fn () => App::make(RedisIngest::class)->trim());
6085

6186
expect($commands)->toContain('"XTRIM" "laravel_database_laravel:pulse:ingest" "MAXLEN" "~" "54321"');
62-
})->with(['predis', 'phpredis', 'relay']);
87+
})->with($drivers);
6388

6489
it('runs the same commands while storing', function ($driver) {
90+
skipWhenExtensionMissing($driver);
91+
6592
Config::set('database.redis.client', $driver);
6693
Config::set('pulse.ingest.redis.chunk', 567);
6794
Date::setTestNow(Date::parse('2000-01-02 03:04:05')->startOfSecond());
@@ -80,9 +107,11 @@
80107

81108
expect($commands)->toContain('"XRANGE" "laravel_database_laravel:pulse:ingest" "-" "+" "COUNT" "567"');
82109
expect($commands)->toContain('"XDEL" "laravel_database_laravel:pulse:ingest" "'.$firstEntryKey.'" "'.$lastEntryKey.'"');
83-
})->with(['predis', 'phpredis', 'relay']);
110+
})->with($drivers);
84111

85112
it('has consistent return for xadd', function ($driver) {
113+
skipWhenExtensionMissing($driver);
114+
86115
Config::set('database.redis.client', $driver);
87116
$redis = new RedisAdapter(Redis::connection(), App::make('config'));
88117

@@ -96,9 +125,11 @@
96125
expect($parts)->toHaveCount(2);
97126
expect($parts[0])->toEqualWithDelta(now()->getTimestampMs(), 50);
98127
expect($parts[1])->toBe('0');
99-
})->with(['predis', 'phpredis', 'relay']);
128+
})->with($drivers);
100129

101130
it('has consistent return for xrange', function ($driver) {
131+
skipWhenExtensionMissing($driver);
132+
102133
Config::set('database.redis.client', $driver);
103134
$redis = new RedisAdapter(Redis::connection(), App::make('config'));
104135
$redis->xadd('stream-name', [
@@ -125,9 +156,11 @@
125156
expect($parts[1])->toBeIn(['0', '1']);
126157
expect($value)->toBe(array_shift($values));
127158
}
128-
})->with(['predis', 'phpredis', 'relay']);
159+
})->with($drivers);
129160

130161
it('has consistent return for xtrim', function ($driver) {
162+
skipWhenExtensionMissing($driver);
163+
131164
Config::set('database.redis.client', $driver);
132165
$redis = new RedisAdapter(Redis::connection(), App::make('config'));
133166

@@ -150,14 +183,16 @@
150183
$result = $redis->xtrim('stream-name', 'MINID', '=', Str::before($lastKey, '-'));
151184

152185
expect($result)->toBe(2);
153-
})->with(['predis', 'phpredis', 'relay']);
186+
})->with($drivers);
154187

155188
it('throws exception on failure', function ($driver) {
189+
skipWhenExtensionMissing($driver);
190+
156191
Config::set('database.redis.client', $driver);
157192
$redis = new RedisAdapter(Redis::connection(), App::make('config'));
158193

159194
$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].');
161196

162197
it('prepends the error message with the run command', function () {
163198
throw RedisServerException::whileRunningCommand('FOO BAR', 'Something happened');

0 commit comments

Comments
 (0)