Skip to content

Commit 4b7d71c

Browse files
committed
Add Redis integration tests and update CI workflow to include Redis service
1 parent c8079f2 commit 4b7d71c

4 files changed

Lines changed: 66 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ on:
77
jobs:
88
test:
99
runs-on: ubuntu-latest
10+
services:
11+
redis:
12+
image: redis:7-alpine
13+
ports:
14+
- 6379:6379
15+
options: >-
16+
--health-cmd "redis-cli ping"
17+
--health-interval 10s
18+
--health-timeout 5s
19+
--health-retries 5
1020
1121
strategy:
1222
matrix:
@@ -35,6 +45,8 @@ jobs:
3545
run: shards install
3646

3747
- name: Run tests
48+
env:
49+
REDIS_URL: redis://localhost:6379/0
3850
run: crystal spec
3951

4052
- name: Check code formatting

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ crystal spec
335335
crystal tool format --check
336336
```
337337

338+
To run the real Redis integration spec locally, start Redis and set `REDIS_URL`:
339+
340+
```bash
341+
REDIS_URL=redis://localhost:6379/0 crystal spec
342+
```
343+
338344
## Contributing
339345

340346
1. Fork it (<https://github.com/kemalcr/kemal-cache/fork>)

spec/redis_integration_spec.cr

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require "./spec_helper"
2+
3+
describe Kemal::Cache::RedisStore do
4+
it "works against a real Redis instance" do
5+
redis_url = real_redis_url
6+
pending!("REDIS_URL is not set") unless redis_url
7+
8+
namespace = unique_redis_namespace("kemal-cache-integration")
9+
other_namespace = unique_redis_namespace("kemal-cache-integration-other")
10+
store = Kemal::Cache::RedisStore.new(redis_url, namespace)
11+
client = Redis::Client.new(URI.parse(redis_url))
12+
13+
begin
14+
store.clear
15+
client.del("#{other_namespace}:keep")
16+
17+
store.set("greeting", "hello", 50.milliseconds)
18+
store.get("greeting").should eq("hello")
19+
20+
store.delete("greeting")
21+
store.get("greeting").should be_nil
22+
23+
store.set("one", "1", 50.milliseconds)
24+
store.set("two", "2", 50.milliseconds)
25+
client.set("#{other_namespace}:keep", "3", ex: 50.milliseconds)
26+
store.clear
27+
28+
store.get("one").should be_nil
29+
store.get("two").should be_nil
30+
client.get("#{other_namespace}:keep").should eq("3")
31+
32+
store.set("short", "ttl", 5.milliseconds)
33+
sleep 10.milliseconds
34+
store.get("short").should be_nil
35+
ensure
36+
store.clear
37+
client.del("#{other_namespace}:keep")
38+
end
39+
end
40+
end

spec/spec_helper.cr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,11 @@ def mount_cache(config : Kemal::Cache::Config = Kemal::Cache::Config.new, &)
115115
yield
116116
Kemal.config.setup
117117
end
118+
119+
def real_redis_url : String?
120+
ENV["REDIS_URL"]?
121+
end
122+
123+
def unique_redis_namespace(prefix : String = "kemal-cache-spec") : String
124+
"#{prefix}-#{Random.rand(1_000_000)}-#{Time.utc.to_unix_ms}"
125+
end

0 commit comments

Comments
 (0)