2
2
3
3
module LocalCacheBehavior
4
4
def test_instrumentation_with_local_cache
5
+ key = SecureRandom . uuid
5
6
events = with_instrumentation "write" do
6
- @cache . write ( "foo" , "bar" )
7
+ @cache . write ( key , SecureRandom . uuid )
7
8
end
8
9
assert_equal @cache . class . name , events [ 0 ] . payload [ :store ]
9
10
10
11
@cache . with_local_cache do
11
12
events = with_instrumentation "read" do
12
- @cache . read ( "foo" )
13
- @cache . read ( "foo" )
13
+ @cache . read ( key )
14
+ @cache . read ( key )
14
15
end
15
16
16
17
expected = [ @cache . class . name , @cache . send ( :local_cache ) . class . name ]
@@ -19,21 +20,24 @@ def test_instrumentation_with_local_cache
19
20
end
20
21
21
22
def test_local_writes_are_persistent_on_the_remote_cache
23
+ key = SecureRandom . uuid
24
+ value = SecureRandom . alphanumeric
22
25
retval = @cache . with_local_cache do
23
- @cache . write ( "foo" , "bar" )
26
+ @cache . write ( key , value )
24
27
end
25
28
assert retval
26
- assert_equal "bar" , @cache . read ( "foo" )
29
+ assert_equal value , @cache . read ( key )
27
30
end
28
31
29
32
def test_clear_also_clears_local_cache
33
+ key = SecureRandom . uuid
30
34
@cache . with_local_cache do
31
- @cache . write ( "foo" , "bar" )
35
+ @cache . write ( key , SecureRandom . alphanumeric )
32
36
@cache . clear
33
- assert_nil @cache . read ( "foo" )
37
+ assert_nil @cache . read ( key )
34
38
end
35
39
36
- assert_nil @cache . read ( "foo" )
40
+ assert_nil @cache . read ( key )
37
41
end
38
42
39
43
def test_cleanup_clears_local_cache_but_not_remote_cache
@@ -43,79 +47,98 @@ def test_cleanup_clears_local_cache_but_not_remote_cache
43
47
skip
44
48
end
45
49
50
+ key = SecureRandom . uuid
51
+ value = SecureRandom . alphanumeric
52
+ other_value = SecureRandom . alphanumeric
53
+
46
54
@cache . with_local_cache do
47
- @cache . write ( "foo" , "bar" )
48
- assert_equal "bar" , @cache . read ( "foo" )
55
+ @cache . write ( key , value )
56
+ assert_equal value , @cache . read ( key )
49
57
50
- @cache . send ( :bypass_local_cache ) { @cache . write ( "foo" , "baz" ) }
51
- assert_equal "bar" , @cache . read ( "foo" )
58
+ @cache . send ( :bypass_local_cache ) { @cache . write ( key , other_value ) }
59
+ assert_equal value , @cache . read ( key )
52
60
53
61
@cache . cleanup
54
- assert_equal "baz" , @cache . read ( "foo" )
62
+ assert_equal other_value , @cache . read ( key )
55
63
end
56
64
end
57
65
58
66
def test_local_cache_of_write
67
+ key = SecureRandom . uuid
68
+ value = SecureRandom . alphanumeric
59
69
@cache . with_local_cache do
60
- @cache . write ( "foo" , "bar" )
61
- @peek . delete ( "foo" )
62
- assert_equal "bar" , @cache . read ( "foo" )
70
+ @cache . write ( key , value )
71
+ @peek . delete ( key )
72
+ assert_equal value , @cache . read ( key )
63
73
end
64
74
end
65
75
66
76
def test_local_cache_of_read_returns_a_copy_of_the_entry
77
+ key = SecureRandom . alphanumeric . to_sym
78
+ value = SecureRandom . alphanumeric
67
79
@cache . with_local_cache do
68
- @cache . write ( :foo , type : "bar" )
69
- value = @cache . read ( :foo )
70
- assert_equal ( "bar" , value . delete ( :type ) )
71
- assert_equal ( { type : "bar" } , @cache . read ( :foo ) )
80
+ @cache . write ( key , type : value )
81
+ local_value = @cache . read ( key )
82
+ assert_equal ( value , local_value . delete ( :type ) )
83
+ assert_equal ( { type : value } , @cache . read ( key ) )
72
84
end
73
85
end
74
86
75
87
def test_local_cache_of_read
76
- @cache . write ( "foo" , "bar" )
88
+ key = SecureRandom . uuid
89
+ value = SecureRandom . alphanumeric
90
+ @cache . write ( key , value )
77
91
@cache . with_local_cache do
78
- assert_equal "bar" , @cache . read ( "foo" )
92
+ assert_equal value , @cache . read ( key )
79
93
end
80
94
end
81
95
82
96
def test_local_cache_of_read_nil
97
+ key = SecureRandom . uuid
98
+ value = SecureRandom . alphanumeric
83
99
@cache . with_local_cache do
84
- assert_nil @cache . read ( "foo" )
85
- @cache . send ( :bypass_local_cache ) { @cache . write "foo" , "bar" }
86
- assert_nil @cache . read ( "foo" )
100
+ assert_nil @cache . read ( key )
101
+ @cache . send ( :bypass_local_cache ) { @cache . write ( key , value ) }
102
+ assert_nil @cache . read ( key )
87
103
end
88
104
end
89
105
90
106
def test_local_cache_fetch
107
+ key = SecureRandom . uuid
108
+ value = SecureRandom . alphanumeric
91
109
@cache . with_local_cache do
92
- @cache . send ( :local_cache ) . write_entry "foo" , "bar"
93
- assert_equal "bar" , @cache . send ( :local_cache ) . fetch_entry ( "foo" )
110
+ @cache . send ( :local_cache ) . write_entry ( key , value )
111
+ assert_equal value , @cache . send ( :local_cache ) . fetch_entry ( key )
94
112
end
95
113
end
96
114
97
115
def test_local_cache_of_write_nil
116
+ key = SecureRandom . uuid
117
+ value = SecureRandom . alphanumeric
98
118
@cache . with_local_cache do
99
- assert @cache . write ( "foo" , nil )
100
- assert_nil @cache . read ( "foo" )
101
- @peek . write ( "foo" , "bar" )
102
- assert_nil @cache . read ( "foo" )
119
+ assert @cache . write ( key , nil )
120
+ assert_nil @cache . read ( key )
121
+ @peek . write ( key , value )
122
+ assert_nil @cache . read ( key )
103
123
end
104
124
end
105
125
106
126
def test_local_cache_of_write_with_unless_exist
127
+ key = SecureRandom . uuid
128
+ value = SecureRandom . alphanumeric
107
129
@cache . with_local_cache do
108
- @cache . write ( "foo" , "bar" )
109
- @cache . write ( "foo" , "baz" , unless_exist : true )
110
- assert_equal @peek . read ( "foo" ) , @cache . read ( "foo" )
130
+ @cache . write ( key , value )
131
+ @cache . write ( key , SecureRandom . alphanumeric , unless_exist : true )
132
+ assert_equal @peek . read ( key ) , @cache . read ( key )
111
133
end
112
134
end
113
135
114
136
def test_local_cache_of_delete
137
+ key = SecureRandom . uuid
115
138
@cache . with_local_cache do
116
- @cache . write ( "foo" , "bar" )
117
- @cache . delete ( "foo" )
118
- assert_nil @cache . read ( "foo" )
139
+ @cache . write ( key , SecureRandom . alphanumeric )
140
+ @cache . delete ( key )
141
+ assert_nil @cache . read ( key )
119
142
end
120
143
end
121
144
@@ -126,94 +149,112 @@ def test_local_cache_of_delete_matched
126
149
skip
127
150
end
128
151
152
+ prefix = SecureRandom . alphanumeric
153
+ key = "#{ prefix } #{ SecureRandom . uuid } "
154
+ other_key = "#{ prefix } #{ SecureRandom . uuid } "
155
+ third_key = SecureRandom . uuid
156
+ value = SecureRandom . alphanumeric
129
157
@cache . with_local_cache do
130
- @cache . write ( "foo" , "bar" )
131
- @cache . write ( "fop" , "bar" )
132
- @cache . write ( "bar" , "foo" )
133
- @cache . delete_matched ( "fo *" )
134
- assert_not @cache . exist? ( "foo" )
135
- assert_not @cache . exist? ( "fop" )
136
- assert_equal "foo" , @cache . read ( "bar" )
158
+ @cache . write ( key , SecureRandom . alphanumeric )
159
+ @cache . write ( other_key , SecureRandom . alphanumeric )
160
+ @cache . write ( third_key , value )
161
+ @cache . delete_matched ( "#{ prefix } *" )
162
+ assert_not @cache . exist? ( key )
163
+ assert_not @cache . exist? ( other_key )
164
+ assert_equal value , @cache . read ( third_key )
137
165
end
138
166
end
139
167
140
168
def test_local_cache_of_exist
169
+ key = SecureRandom . uuid
141
170
@cache . with_local_cache do
142
- @cache . write ( "foo" , "bar" )
143
- @peek . delete ( "foo" )
144
- assert @cache . exist? ( "foo" )
171
+ @cache . write ( key , SecureRandom . alphanumeric )
172
+ @peek . delete ( key )
173
+ assert @cache . exist? ( key )
145
174
end
146
175
end
147
176
148
177
def test_local_cache_of_increment
178
+ key = SecureRandom . uuid
149
179
@cache . with_local_cache do
150
- @cache . write ( "foo" , 1 , raw : true )
151
- @peek . write ( "foo" , 2 , raw : true )
152
- @cache . increment ( "foo" )
180
+ @cache . write ( key , 1 , raw : true )
181
+ @peek . write ( key , 2 , raw : true )
182
+ @cache . increment ( key )
153
183
154
- expected = @peek . read ( "foo" , raw : true )
184
+ expected = @peek . read ( key , raw : true )
155
185
assert_equal 3 , Integer ( expected )
156
- assert_equal expected , @cache . read ( "foo" , raw : true )
186
+ assert_equal expected , @cache . read ( key , raw : true )
157
187
end
158
188
end
159
189
160
190
def test_local_cache_of_decrement
191
+ key = SecureRandom . uuid
161
192
@cache . with_local_cache do
162
- @cache . write ( "foo" , 1 , raw : true )
163
- @peek . write ( "foo" , 3 , raw : true )
193
+ @cache . write ( key , 1 , raw : true )
194
+ @peek . write ( key , 3 , raw : true )
164
195
165
- @cache . decrement ( "foo" )
166
- expected = @peek . read ( "foo" , raw : true )
196
+ @cache . decrement ( key )
197
+ expected = @peek . read ( key , raw : true )
167
198
assert_equal 2 , Integer ( expected )
168
- assert_equal expected , @cache . read ( "foo" , raw : true )
199
+ assert_equal expected , @cache . read ( key , raw : true )
169
200
end
170
201
end
171
202
172
203
def test_local_cache_of_fetch_multi
204
+ key = SecureRandom . uuid
205
+ other_key = SecureRandom . uuid
173
206
@cache . with_local_cache do
174
- @cache . fetch_multi ( "foo" , "bar" ) { |_key | true }
175
- @peek . delete ( "foo" )
176
- @peek . delete ( "bar" )
177
- assert_equal true , @cache . read ( "foo" )
178
- assert_equal true , @cache . read ( "bar" )
207
+ @cache . fetch_multi ( key , other_key ) { |_key | true }
208
+ @peek . delete ( key )
209
+ @peek . delete ( other_key )
210
+ assert_equal true , @cache . read ( key )
211
+ assert_equal true , @cache . read ( other_key )
179
212
end
180
213
end
181
214
182
215
def test_local_cache_of_read_multi
216
+ key = SecureRandom . uuid
217
+ value = SecureRandom . alphanumeric
218
+ other_key = SecureRandom . uuid
219
+ other_value = SecureRandom . alphanumeric
183
220
@cache . with_local_cache do
184
- @cache . write ( "foo" , "foo" , raw : true )
185
- @cache . write ( "bar" , "bar" , raw : true )
186
- values = @cache . read_multi ( "foo" , "bar" , raw : true )
187
- assert_equal "foo" , @cache . read ( "foo" , raw : true )
188
- assert_equal "bar" , @cache . read ( "bar" , raw : true )
189
- assert_equal "foo" , values [ "foo" ]
190
- assert_equal "bar" , values [ "bar" ]
221
+ @cache . write ( key , value , raw : true )
222
+ @cache . write ( other_key , other_value , raw : true )
223
+ values = @cache . read_multi ( key , other_key , raw : true )
224
+ assert_equal value , @cache . read ( key , raw : true )
225
+ assert_equal other_value , @cache . read ( other_key , raw : true )
226
+ assert_equal value , values [ key ]
227
+ assert_equal other_value , values [ other_key ]
191
228
end
192
229
end
193
230
194
231
def test_initial_object_mutation_after_write
232
+ key = SecureRandom . uuid
195
233
@cache . with_local_cache do
196
234
initial = +"bar"
197
- @cache . write ( "foo" , initial )
235
+ @cache . write ( key , initial )
198
236
initial << "baz"
199
- assert_equal "bar" , @cache . read ( "foo" )
237
+ assert_equal "bar" , @cache . read ( key )
200
238
end
201
239
end
202
240
203
241
def test_initial_object_mutation_after_fetch
242
+ key = SecureRandom . uuid
204
243
@cache . with_local_cache do
205
244
initial = +"bar"
206
- @cache . fetch ( "foo" ) { initial }
245
+ @cache . fetch ( key ) { initial }
207
246
initial << "baz"
208
- assert_equal "bar" , @cache . read ( "foo" )
209
- assert_equal "bar" , @cache . fetch ( "foo" )
247
+ assert_equal "bar" , @cache . read ( key )
248
+ assert_equal "bar" , @cache . fetch ( key )
210
249
end
211
250
end
212
251
213
252
def test_middleware
253
+ key = SecureRandom . uuid
254
+ value = SecureRandom . alphanumeric
214
255
app = lambda { |env |
215
- result = @cache . write ( "foo" , "bar" )
216
- assert_equal "bar" , @cache . read ( "foo" ) # make sure 'foo' was written
256
+ result = @cache . write ( key , value )
257
+ assert_equal value , @cache . read ( key ) # make sure 'foo' was written
217
258
assert result
218
259
[ 200 , { } , [ ] ]
219
260
}
@@ -222,23 +263,27 @@ def test_middleware
222
263
end
223
264
224
265
def test_local_race_condition_protection
266
+ key = SecureRandom . uuid
267
+ value = SecureRandom . alphanumeric
268
+ other_value = SecureRandom . alphanumeric
225
269
@cache . with_local_cache do
226
270
time = Time . now
227
- @cache . write ( "foo" , "bar" , expires_in : 60 )
271
+ @cache . write ( key , value , expires_in : 60 )
228
272
Time . stub ( :now , time + 61 ) do
229
- result = @cache . fetch ( "foo" , race_condition_ttl : 10 ) do
230
- assert_equal "bar" , @cache . read ( "foo" )
231
- "baz"
273
+ result = @cache . fetch ( key , race_condition_ttl : 10 ) do
274
+ assert_equal value , @cache . read ( key )
275
+ other_value
232
276
end
233
- assert_equal "baz" , result
277
+ assert_equal other_value , result
234
278
end
235
279
end
236
280
end
237
281
238
282
def test_local_cache_should_read_and_write_false
283
+ key = SecureRandom . uuid
239
284
@cache . with_local_cache do
240
- assert @cache . write ( "foo" , false )
241
- assert_equal false , @cache . read ( "foo" )
285
+ assert @cache . write ( key , false )
286
+ assert_equal false , @cache . read ( key )
242
287
end
243
288
end
244
289
end
0 commit comments