index_cache_test.rb
1.2 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
require_relative "test_helper"
class IndexCacheTest < Minitest::Test
def setup
Product.class_variable_get(:@@searchkick_index_cache).clear
end
def test_default
object_id = Product.search_index.object_id
3.times do
assert_equal object_id, Product.search_index.object_id
end
end
def test_max_size
starting_ids = object_ids(20)
assert_equal starting_ids, object_ids(20)
Product.search_index(name: "other")
refute_equal starting_ids, object_ids(20)
end
def test_thread_safe
object_ids = with_threads { object_ids(20) }
assert_equal object_ids[0], object_ids[1]
assert_equal object_ids[0], object_ids[2]
end
# object ids can differ since threads progress at different speeds
# test to make sure doesn't crash
def test_thread_safe_max_size
with_threads { object_ids(1000) }
end
private
def object_ids(count)
count.times.map { |i| Product.search_index(name: "index#{i}").object_id }
end
def with_threads
previous = Thread.report_on_exception
begin
Thread.report_on_exception = true
3.times.map { Thread.new { yield } }.map(&:join).map(&:value)
ensure
Thread.report_on_exception = previous
end
end
end