forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_cache_spec.rb
65 lines (55 loc) · 1.86 KB
/
multi_cache_spec.rb
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
# Copyright (C) 2012 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
require 'lib/multi_cache'
describe MultiCache do
it "should use the same node for read and write during a fetch" do
mock_class = Class.new do
attr_reader :gets, :sets
def initialize
@gets = @sets = 0
end
def get(*args)
@gets += 1
nil
end
def set(*args)
@sets += 1
end
end
nodes = (0..100).map { mock_class.new }
store = MultiCache.new(nodes)
store.fetch('a') { 1 }
# a total of one get and one set
expect(nodes.map(&:gets).sum).to eq 1
expect(nodes.map(&:sets).sum).to eq 1
# each node either got 0 and 0, or 1 and 1
nodes.each do |node|
expect(node.gets).to eq node.sets
end
end
it "should delete from _all_ nodes" do
ring = [mock, mock]
ring[0].expects(:del).with('key').returns(true)
ring[1].expects(:del).with('key').returns(false)
# TODO remove this when removing the shim from active_support.rb
ring[0].expects(:del).with('rails4:key').returns(false)
ring[1].expects(:del).with('rails4:key').returns(false)
store = MultiCache.new(ring)
expect(store.delete('key')).to eq true
end
end