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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# frozen_string_literal: true
require "abstract_unit"
require "active_support/cache"
require "active_support/cache/redis_cache_store"
require_relative "../behaviors"
module ActiveSupport::Cache::RedisCacheStoreTests
DRIVER = %w[ ruby hiredis ].include?(ENV["REDIS_DRIVER"]) ? ENV["REDIS_DRIVER"] : "hiredis"
class LookupTest < ActiveSupport::TestCase
test "may be looked up as :redis_cache_store" do
assert_kind_of ActiveSupport::Cache::RedisCacheStore,
ActiveSupport::Cache.lookup_store(:redis_cache_store)
end
end
class InitializationTest < ActiveSupport::TestCase
test "omitted URL uses Redis client with default settings" do
assert_called_with Redis, :new, [
url: nil,
connect_timeout: 20, read_timeout: 1, write_timeout: 1,
reconnect_attempts: 0, driver: DRIVER
] do
build
end
end
test "no URLs uses Redis client with default settings" do
assert_called_with Redis, :new, [
url: nil,
connect_timeout: 20, read_timeout: 1, write_timeout: 1,
reconnect_attempts: 0, driver: DRIVER
] do
build url: []
end
end
test "singular URL uses Redis client" do
assert_called_with Redis, :new, [
url: "redis://localhost:6379/0",
connect_timeout: 20, read_timeout: 1, write_timeout: 1,
reconnect_attempts: 0, driver: DRIVER
] do
build url: "redis://localhost:6379/0"
end
end
test "one URL uses Redis client" do
assert_called_with Redis, :new, [
url: "redis://localhost:6379/0",
connect_timeout: 20, read_timeout: 1, write_timeout: 1,
reconnect_attempts: 0, driver: DRIVER
] do
build url: %w[ redis://localhost:6379/0 ]
end
end
test "multiple URLs uses Redis::Distributed client" do
assert_called_with Redis, :new, [
[ url: "redis://localhost:6379/0",
connect_timeout: 20, read_timeout: 1, write_timeout: 1,
reconnect_attempts: 0, driver: DRIVER ],
[ url: "redis://localhost:6379/1",
connect_timeout: 20, read_timeout: 1, write_timeout: 1,
reconnect_attempts: 0, driver: DRIVER ],
], returns: Redis.new do
@cache = build url: %w[ redis://localhost:6379/0 redis://localhost:6379/1 ]
assert_kind_of ::Redis::Distributed, @cache.redis
end
end
test "block argument uses yielded client" do
block = -> { :custom_redis_client }
assert_called block, :call do
build redis: block
end
end
private
def build(**kwargs)
ActiveSupport::Cache::RedisCacheStore.new(driver: DRIVER, **kwargs).tap do |cache|
cache.redis
end
end
end
class StoreTest < ActiveSupport::TestCase
setup do
@namespace = "namespace"
@cache = ActiveSupport::Cache::RedisCacheStore.new(timeout: 0.1, namespace: @namespace, expires_in: 60, driver: DRIVER)
# @cache.logger = Logger.new($stdout) # For test debugging
# For LocalCacheBehavior tests
@peek = ActiveSupport::Cache::RedisCacheStore.new(timeout: 0.1, namespace: @namespace, driver: DRIVER)
end
teardown do
@cache.clear
@cache.redis.disconnect!
end
end
class RedisCacheStoreCommonBehaviorTest < StoreTest
include CacheStoreBehavior
include CacheStoreVersionBehavior
include LocalCacheBehavior
include CacheIncrementDecrementBehavior
include CacheInstrumentationBehavior
include AutoloadingCacheBehavior
def test_fetch_multi_uses_redis_mget
assert_called(@cache.redis, :mget, returns: []) do
@cache.fetch_multi("a", "b", "c") do |key|
key * 2
end
end
end
end
# Separate test class so we can omit the namespace which causes expected,
# appropriate complaints about incompatible string encodings.
class KeyEncodingSafetyTest < StoreTest
include EncodedKeyCacheBehavior
setup do
@cache = ActiveSupport::Cache::RedisCacheStore.new(timeout: 0.1, driver: DRIVER)
@cache.logger = nil
end
end
class StoreAPITest < StoreTest
end
class UnavailableRedisClient < Redis::Client
def ensure_connected
raise Redis::BaseConnectionError
end
end
class FailureSafetyTest < StoreTest
include FailureSafetyBehavior
private
def emulating_unavailability
old_client = Redis.send(:remove_const, :Client)
Redis.const_set(:Client, UnavailableRedisClient)
yield ActiveSupport::Cache::RedisCacheStore.new
ensure
Redis.send(:remove_const, :Client)
Redis.const_set(:Client, old_client)
end
end
class DeleteMatchedTest < StoreTest
test "deletes keys matching glob" do
@cache.write("foo", "bar")
@cache.write("fu", "baz")
@cache.delete_matched("foo*")
assert !@cache.exist?("foo")
assert @cache.exist?("fu")
end
test "fails with regexp matchers" do
assert_raise ArgumentError do
@cache.delete_matched(/OO/i)
end
end
end
end
|