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