forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas_spec.rb
145 lines (129 loc) · 5.71 KB
/
canvas_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
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
#
# Copyright (C) 2011 - 2013 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')
describe Canvas do
describe ".timeout_protection" do
it "should wrap the block in a timeout" do
Setting.set("service_generic_timeout", "2")
Timeout.expects(:timeout).with(2).yields
ran = false
Canvas.timeout_protection("spec") { ran = true }
expect(ran).to eq true
# service-specific timeout
Setting.set("service_spec_timeout", "1")
Timeout.expects(:timeout).with(1).yields
ran = false
Canvas.timeout_protection("spec") { ran = true }
expect(ran).to eq true
end
it "should raise on timeout if raise_on_timeout option is specified" do
Timeout.expects(:timeout).raises(Timeout::Error)
expect { Canvas.timeout_protection("spec", raise_on_timeout: true) {} }.to raise_error(Timeout::Error)
end
it "should use the timeout argument over the generic default" do
Timeout.expects(:timeout).with(23)
Canvas.timeout_protection("foo", fallback_timeout_length: 23)
end
it "should use the settings timeout over the timeout argument" do
Setting.set("service_foo_timeout", "1")
Timeout.expects(:timeout).with(1)
Canvas.timeout_protection("foo", fallback_timeout_length: 23)
end
if Canvas.redis_enabled?
it "should skip calling the block after X failures" do
Setting.set("service_spec_cutoff", "2")
Timeout.expects(:timeout).with(15).twice.raises(Timeout::Error)
Canvas.timeout_protection("spec") {}
Canvas.timeout_protection("spec") {}
ran = false
# third time, won't call timeout
Canvas.timeout_protection("spec") { ran = true }
expect(ran).to eq false
# verify the redis key has a ttl
key = "service:timeouts:spec"
expect(Canvas.redis.get(key)).to eq "2"
expect(Canvas.redis.ttl(key)).to be_present
# delete the redis key and it'll try again
Canvas.redis.del(key)
Timeout.expects(:timeout).with(15).yields
Canvas.timeout_protection("spec") { ran = true }
expect(ran).to eq true
end
it "should raise on cutoff if raise_on_timeout option is specified" do
Canvas.redis.set("service:timeouts:spec", 42)
expect { Canvas.timeout_protection("spec", raise_on_timeout: true) {} }.to raise_error(Timeout::Error)
expect(Canvas.redis.get("service:timeouts:spec")).to eq "42"
end
end
end
if Canvas.redis_enabled?
describe ".short_circuit_timeout" do
it "should wrap the block in a timeout" do
Timeout.expects(:timeout).with(15).yields
ran = false
Canvas.short_circuit_timeout(Canvas.redis, "spec", 15, 2, 1) { ran = true }
expect(ran).to eq true
end
it "should raise Timeout::Error on timeout" do
Timeout.expects(:timeout).raises(Timeout::Error)
expect { Canvas.short_circuit_timeout(Canvas.redis, "spec", 15, 2, 1) {} }.to raise_error(Timeout::Error)
end
it "should skip calling the block after X failures" do
Timeout.expects(:timeout).with(15).twice.raises(Timeout::Error)
expect { Canvas.short_circuit_timeout(Canvas.redis, "timeouts:spec", 15, 2, 1) {} }.to raise_error(Timeout::Error)
expect { Canvas.short_circuit_timeout(Canvas.redis, "timeouts:spec", 15, 2, 1) {} }.to raise_error(Timeout::Error)
ran = false
# third time, won't call timeout
expect { Canvas.short_circuit_timeout(Canvas.redis, "timeouts:spec", 15, 2, 1) { ran = true } }.to raise_error(Timeout::Error)
expect(ran).to eq false
# verify the redis key has a ttl
key = "timeouts:spec"
expect(Canvas.redis.get(key)).to eq "2"
expect(Canvas.redis.ttl(key)).to be_present
# delete the redis key and it'll try again
Canvas.redis.del(key)
Timeout.expects(:timeout).with(15).yields
Canvas.short_circuit_timeout(Canvas.redis, "timeouts:spec", 15, 2, 1) { ran = true }
expect(ran).to eq true
end
it "should raise TimeoutCutoff when the cutoff is reached" do
Canvas.redis.set("timeouts:spec", 42)
expect { Canvas.short_circuit_timeout(Canvas.redis, "timeouts:spec", 15, 2, 1) { ran = true} }.to raise_error(Canvas::TimeoutCutoff)
expect(Canvas.redis.get("timeouts:spec")).to eq "42"
end
end
end
describe ".cache_stores" do
before do
@old_cache_stores = Canvas.instance_variable_get(:@cache_stores)
Canvas.instance_variable_set(:@cache_stores, nil)
end
after do
Canvas.instance_variable_set(:@cache_stores, @old_cache_stores)
end
it "should pass through string links" do
ConfigFile.stubs(:load).returns('other' => { 'cache_store' => 'redis_store', 'servers' => ['localhost:6379'] }, 'db1' => 'other')
stores = Canvas.cache_stores
expect(stores.keys.sort).to eq ['db1', 'other', 'test']
expect(stores['other']).to be_a(Array)
expect(stores['other'].first).to eq :redis_store
expect(stores['db1']).to eq 'other'
expect(stores['test']).to eq :null_store
end
end
end