-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathfetch_test.rb
More file actions
498 lines (391 loc) · 16.1 KB
/
Copy pathfetch_test.rb
File metadata and controls
498 lines (391 loc) · 16.1 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# frozen_string_literal: true
require "test_helper"
class FetchTest < IdentityCache::TestCase
NAMESPACE = IdentityCache::CacheKeyGeneration::DEFAULT_NAMESPACE
def setup
super
Item.cache_index(:title, unique: true)
Item.cache_index(:id, :title, unique: true)
@record = Item.new
@record.id = 1
@record.title = "bob"
@cached_value = { attributes: @record.attributes_before_type_cast }
attrs = "created_at:datetime,id:integer,item_id:integer,title:string,updated_at:datetime"
@blob_key = "#{NAMESPACE}blob:Item:#{cache_hash(attrs)}:1"
@index_key = "#{NAMESPACE}attr:Item:id:title:#{cache_hash('"bob"')}"
end
def test_fetch_with_garbage_input
assert_nil(Item.fetch_by_id("garbage"))
end
def test_fetch_cache_hit
IdentityCache.cache.expects(:fetch).with(@blob_key, {}).returns(@cached_value)
result = Item.fetch(1)
assert_instance_of(Item, result)
assert_equal(1, result.id)
end
def test_fetch_cache_hit_publishes_hydration_notification
IdentityCache.cache.expects(:fetch).with(@blob_key, {}).returns(@cached_value)
events = 0
subscriber = ActiveSupport::Notifications.subscribe("hydration.identity_cache") do |_, _, _, _, payload|
events += 1
assert_equal("Item", payload[:class])
end
Item.fetch(1)
assert_equal(1, events)
ensure
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end
def test_fetch_cache_hit_publishes_cache_notification
expected_kwargs = {}
IdentityCache.cache.cache_fetcher.expects(:fetch).with(@blob_key, **expected_kwargs).returns(@cached_value)
expected = { memoizing: false, resolve_miss_time: 0, memo_hits: 0, cache_hits: 1, cache_misses: 0 }
events = 0
subscriber = ActiveSupport::Notifications.subscribe("cache_fetch.identity_cache") do |_, _, _, _, payload|
events += 1
assert_equal(expected, payload)
end
Item.fetch(1)
assert_equal(1, events)
ensure
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end
def test_fetch_memoized_hit_publishes_cache_notification
subscriber = nil
expected_kwargs = {}
IdentityCache.cache.cache_fetcher.expects(:fetch).with(@blob_key, **expected_kwargs).returns(@cached_value)
expected = { memoizing: true, resolve_miss_time: 0, memo_hits: 1, cache_hits: 0, cache_misses: 0 }
IdentityCache.cache.with_memoization do
Item.fetch(1)
events = 0
subscriber = ActiveSupport::Notifications.subscribe("cache_fetch.identity_cache") do |_, _, _, _, payload|
events += 1
assert_equal(expected, payload)
end
Item.fetch(1)
assert_equal(1, events)
end
ensure
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end
def test_fetch_hit_cache_namespace
old_ns = IdentityCache.cache_namespace
IdentityCache.cache_namespace = proc { |model| "#{model.table_name}:#{old_ns}" }
new_blob_key = "items:#{@blob_key}"
IdentityCache.cache.expects(:fetch).with(new_blob_key, {}).returns(@cached_value)
result = Item.fetch(1)
assert_instance_of(Item, result)
assert_equal(1, result.id)
ensure
IdentityCache.cache_namespace = old_ns
end
def test_exists_with_identity_cache_when_cache_hit
IdentityCache.cache.expects(:fetch).with(@blob_key, {}).returns(@cached_value)
assert(Item.exists_with_identity_cache?(1))
end
def test_exists_with_identity_cache_when_cache_miss_and_in_db
fetch = Spy.on(IdentityCache.cache, :fetch).and_call_through
Item.cached_primary_index.expects(:load_one_from_db).with(1).once.returns(@record)
assert(Item.exists_with_identity_cache?(1))
assert(fetch.has_been_called_with?(@blob_key, {}))
end
def test_exists_with_identity_cache_when_cache_miss_and_not_in_db
fetch = Spy.on(IdentityCache.cache, :fetch).and_call_through
Item.cached_primary_index.expects(:load_one_from_db).with(1).once.returns(nil)
assert(!Item.exists_with_identity_cache?(1))
assert(fetch.has_been_called_with?(@blob_key, {}))
end
def test_fetch_miss_published_dehydration_notification
Item.cached_primary_index.expects(:load_one_from_db).with(1).once.returns(@record)
events = 0
subscriber = ActiveSupport::Notifications.subscribe("dehydration.identity_cache") do |_, _, _, _, payload|
events += 1
assert_equal("Item", payload[:class])
end
Item.fetch(1)
assert_equal(1, events)
ensure
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end
def test_fetch_miss_publishes_cache_notification
Item.cached_primary_index.expects(:load_one_from_db).with(1).once.returns(@record)
expected = { memoizing: false, memo_hits: 0, cache_hits: 0, cache_misses: 1 }
events = 0
subscriber = ActiveSupport::Notifications.subscribe("cache_fetch.identity_cache") do |_, _, _, _, payload|
events += 1
assert(payload.delete(:resolve_miss_time) > 0)
assert_equal(expected, payload)
end
Item.fetch(1)
assert_equal(1, events)
ensure
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end
def test_fetch_miss
Item.cached_primary_index.expects(:load_one_from_db).with(1).once.returns(@record)
results = []
fetch = Spy.on(IdentityCache.cache, :fetch).and_return { |_, &block| block.call.tap { |result| results << result } }
assert_equal(@record, Item.fetch(1))
assert(fetch.has_been_called_with?(@blob_key, {}))
assert_equal([@cached_value], results)
end
def test_fetch_miss_with_non_id_primary_key
hashed_key = Zlib.crc32("foo") % (2**30 - 1)
fixture = KeyedRecord.create!(value: "foo") { |r| r.hashed_key = hashed_key }
assert_equal(fixture, KeyedRecord.fetch(hashed_key))
end
def test_fetch_conflict
load_one_from_db = Spy.on(Item.cached_primary_index, :load_one_from_db).and_return do
@record.expire_cache
@record
end
write = Spy.on(backend, :write).and_call_through
assert_equal(@record, Item.fetch(1))
assert(load_one_from_db.has_been_called_with?(1))
assert(write.has_been_called_with?(@blob_key, @cached_value, { unless_exist: true }))
assert_equal(IdentityCache::DELETED, backend.read(@record.primary_cache_index_key))
end
def test_fetch_conflict_after_delete
@record.expire_cache
assert_equal(IdentityCache::DELETED, backend.read(@record.primary_cache_index_key))
load_one_from_db = Spy.on(Item.cached_primary_index, :load_one_from_db).and_return do
@record.expire_cache
@record
end
write = Spy.on(backend, :write).and_call_through
assert_equal(@record, Item.fetch(1))
assert(load_one_from_db.has_been_called_with?(1))
refute(write.calls.any? { |call| call.kwargs[:unless_exist] || call.args[2]&.key?(unless_exist: true) })
assert_equal(IdentityCache::DELETED, backend.read(@record.primary_cache_index_key))
end
def test_fetch_by_id_not_found_should_return_nil
nonexistent_record_id = 10
fetcher.expects(:add).with(@blob_key + "0", IdentityCache::CACHED_NIL, {})
assert_nil(Item.fetch_by_id(nonexistent_record_id))
end
def test_fetch_not_found_should_raise
nonexistent_record_id = 10
fetcher.expects(:add).with(@blob_key + "0", IdentityCache::CACHED_NIL, {})
assert_raises(IdentityCache::RecordNotFound) { Item.fetch(nonexistent_record_id) }
end
def test_cached_nil_expiry_on_record_creation
key = @record.primary_cache_index_key
assert_nil(Item.fetch_by_id(@record.id))
assert_equal(IdentityCache::CACHED_NIL, backend.read(key))
@record.save!
assert_equal(IdentityCache::DELETED, backend.read(key))
end
def test_fetch_id_coercion
assert_nil(Item.fetch_by_id(@record.id.to_f))
@record.save!
assert_equal(@record, Item.fetch_by_id(@record.id.to_f))
end
def test_fetch_by_title_hit
values = []
fetch = Spy.on(IdentityCache.cache, :fetch, {}).and_return do |key, &block|
case key
# Read record with title bob
when @index_key then block.call.tap { |val| values << val }
# got id, do memcache lookup on that, hit -> done
when @blob_key then @cached_value
end
end
# Id not found, use sql, SELECT id FROM records WHERE title = '...' LIMIT 1"
Item.connection.expects(ar_query_method(Item.connection)).returns(ActiveRecord::Result.new(["id"], [[1]]))
result = Item.fetch_by_title("bob")
assert_instance_of(Item, result)
assert_equal(@record.id, result.id)
assert_equal([1], values)
assert(fetch.has_been_called_with?(@index_key, {}))
assert(fetch.has_been_called_with?(@blob_key, {}))
end
def test_fetch_by_title_cache_namespace
Item.send(:include, SwitchNamespace)
IdentityCache.cache.expects(:fetch).with("ns:#{@index_key}", {}).returns(1)
IdentityCache.cache.expects(:fetch).with("ns:#{@blob_key}", {}).returns(@cached_value)
result = Item.fetch_by_title("bob")
assert_instance_of(Item, result)
assert_equal(@record.id, result.id)
end
def test_fetch_by_title_caches_nil
assert_nil(Item.fetch_by_title("missing"))
assert_no_queries do
assert_nil(Item.fetch_by_title("missing"))
end
end
def test_fetch_by_bang_method
assert_raises(IdentityCache::RecordNotFound) do
Item.fetch_by_title!("missing")
end
end
def test_fetch_does_not_communicate_to_cache_with_nil_id
fetcher.expects(:fetch).never
fetcher.expects(:add).never
assert_raises(IdentityCache::RecordNotFound) { Item.fetch(nil) }
end
def test_fetch_cache_hit_does_not_checkout_database_connection
@record.save!
record = Item.fetch(@record.id)
ActiveRecord::Base.connection_handler.clear_active_connections!(ActiveRecord::Base.current_role)
assert_equal(record, Item.fetch(@record.id))
assert_equal(false, ActiveRecord::Base.connection_handler.active_connections?(ActiveRecord::Base.current_role))
end
def test_fetch_raises_when_called_on_a_scope
exception = assert_raises(IdentityCache::UnsupportedScopeError) do
Item.where(updated_at: nil).fetch(1)
end
assert_match(/Item/, exception.message)
end
def test_fetch_by_raises_when_called_on_a_scope
exception = assert_raises(IdentityCache::UnsupportedScopeError) do
Item.where(updated_at: nil).fetch_by_id(1)
end
assert_match(/Item/, exception.message)
end
def test_fetch_on_derived_model_raises
assert_raises(IdentityCache::DerivedModelError) do
StiRecordTypeA.fetch(1)
end
end
def test_returned_records_are_readonly_on_cache_hit
IdentityCache.with_fetch_read_only_records do
IdentityCache.cache.expects(:fetch).with(@blob_key, {}).returns(@cached_value)
assert(Item.fetch(1).readonly?)
end
end
def test_returned_records_are_readonly_on_cache_miss
IdentityCache.with_fetch_read_only_records do
fetch = Spy.on(IdentityCache.cache, :fetch).and_call_through
Item.cached_primary_index.expects(:load_one_from_db).with(1).once.returns(@record)
assert(Item.exists_with_identity_cache?(1))
assert(fetch.has_been_called_with?(@blob_key, {}))
assert(Item.fetch(1).readonly?)
end
end
def test_returned_records_are_not_readonly_with_open_transactions
IdentityCache.with_fetch_read_only_records do
@record.transaction do
fetch = Spy.on(IdentityCache.cache, :fetch).and_call_through
Item.cached_primary_index.expects(:load_one_from_db).with(1).once.returns(@record)
refute(IdentityCache.should_use_cache?)
refute(fetch.has_been_called_with?(@blob_key, {}))
refute(Item.fetch(1).readonly?, "Fetched item was read-only")
end
end
end
def test_should_use_cache_not_called_on_preloaded
Item.send(:cache_belongs_to, :item)
bob = Item.create!(title: "bob")
john = Item.create!(title: "john")
bob.update_column(:item_id, john.id)
from_db = Item.includes(:item).find(bob.id)
IdentityCache.expects(:should_use_cache?).never
from_db.fetch_item
end
def test_should_use_cache_not_called_on_prefetched
Item.send(:cache_belongs_to, :item)
bob = Item.create!(title: "bob")
john = Item.create!(title: "john")
bob.update_column(:item_id, john.id)
item = Item.fetch(bob.id, includes: [:item])
IdentityCache.expects(:should_use_cache?).never
item.fetch_item
end
def test_should_use_cache_not_called_on_prefetched_multi
Item.send(:cache_belongs_to, :item)
bob = Item.create!(title: "bob")
john = Item.create!(title: "john")
bob.update_column(:item_id, john.id)
items = Item.fetch_multi([bob.id], includes: [:item])
IdentityCache.expects(:should_use_cache?).never
items.first.fetch_item
end
def test_should_use_cache_not_called_on_preloaded_has_many
Item.send(:cache_has_many, :associated_records, embed: true)
bob = Item.create!(title: "bob")
bob.associated_records.create!(name: "foo")
bob.associated_records.create!(name: "bar")
from_db = Item.includes(:associated_records).find(bob.id)
IdentityCache.expects(:should_use_cache?).never
records = from_db.fetch_associated_records
assert_equal(2, records.size)
end
def test_should_use_cache_not_called_on_embedded_has_many
Item.send(:cache_has_many, :associated_records, embed: true)
bob = Item.create!(title: "bob")
bob.associated_records.create!(name: "foo")
bob.associated_records.create!(name: "bar")
item = Item.fetch(bob.id)
IdentityCache.expects(:should_use_cache?).never
records = item.fetch_associated_records
assert_equal(2, records.size)
end
def test_should_use_cache_not_called_on_embedded_has_one
Item.send(:cache_has_one, :related_item, embed: true)
bob = Item.create!(title: "bob")
bob.build_related_item.save!
item = Item.fetch(bob.id)
IdentityCache.expects(:should_use_cache?).never
assert_predicate(item.fetch_related_item, :present?)
end
def test_should_use_cache_called_on_has_many_embed_ids
Item.send(:cache_has_many, :associated_records, embed: :ids)
bob = Item.create!(title: "bob")
bob.associated_records.create!(name: "foo")
bob.associated_records.create!(name: "bar")
item = Item.fetch(bob.id)
IdentityCache.expects(:should_use_cache?).at_least_once.returns(true)
records = item.fetch_associated_records
assert_equal(2, records.size)
end
def test_should_use_cache_called_on_has_one_embed_id
Item.send(:cache_has_one, :related_item, embed: :id)
bob = Item.create!(title: "bob")
bob.build_related_item.save!
item = Item.fetch(bob.id)
IdentityCache.expects(:should_use_cache?).at_least_once.returns(true)
assert_predicate(item.fetch_related_item, :present?)
end
def test_respects_should_use_cache_on_record
@record.save
Item.stubs(:should_use_cache?).returns(false)
assert_memcache_operations(0) do
assert_queries(1) do
Item.fetch_by_id(@record.id)
end
end
end
def test_with_should_use_cache_false_fetched_record_has_embedded_associations_preloaded
Item.cache_has_many(:associated_records, embed: true)
Item.cache_has_many(:normalized_associated_records)
@record.associated_records.build
@record.associated_records.build
@record.normalized_associated_records.build
@record.save
Item.stubs(:should_use_cache?).returns(false)
item = assert_memcache_operations(0) do
assert_queries(2) do
Item.fetch_by_id(@record.id)
end
end
assert(item.association(:associated_records).loaded?)
refute(item.association(:normalized_associated_records).loaded?)
end
def test_fetch_supports_lock_wait_options
@record.save
# only one client reads from the database on concurrent fetch with lock wait
assert_queries(1) do
# other client takes fill lock
cache_key = Item.cached_primary_index.cache_key(@record.id)
lock = IdentityCache::CacheFetcher::FillLock.new(client_id: SecureRandom.uuid, data_version: SecureRandom.uuid)
IdentityCache.cache.cache_fetcher.write(cache_key, lock.cache_value)
# fetch waits on lock
IdentityCache.cache.cache_fetcher.expects(:sleep).with do |duration|
# other client fills cache
assert_queries(1) { Item.fetch(@record.id) }
duration == 0.1
end
Item.fetch(@record.id, fill_lock_duration: 0.1, lock_wait_tries: 1)
assert_equal(@record, Item.fetch(@record.id))
end
end
end