/
execache_spec.rb
121 lines (109 loc) · 2.5 KB
/
execache_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
require 'spec_helper'
describe Execache do
def client_exec(options={})
@client.exec(
{
:ttl => 60,
:some_binary => {
:args => 'preliminary_arg',
:groups => [
'arg1a arg1b',
'arg2a arg2b'
]
}
}.merge(options)
)
end
before(:all) do
@thread = Thread.new do
Execache.new("#{$root}/spec/fixtures/execache.yml")
end
@client = Execache::Client.new("localhost:6379/0")
@client.redis_1.keys("execache:*").each do |key|
@client.redis_1.del(key)
end
end
after(:all) do
@thread.kill
end
it "should return proper results" do
client_exec.should == {
"some_binary" => [
["arg1_result_1", "arg1_result_2"],
["arg2_result_1", "arg2_result_2"]
]
}
end
it "should write to cache" do
keys = @client.redis_1.keys("execache:cache:*")
keys.length.should == 2
end
it "should read from cache" do
keys = @client.redis_1.keys("execache:cache:*")
@client.redis_1.set(keys[0], "[\"cached!\"]")
@client.redis_1.set(keys[1], "[\"cached!\"]")
client_exec.should == {
"some_binary" => [
["cached!"],
["cached!"]
]
}
end
it "should read from cache for individual groups" do
@client.exec(
:ttl => 60,
:some_binary => {
:args => 'preliminary_arg',
:groups => [ 'arg2a arg2b' ]
}
).should == {
"some_binary" => [
["cached!"]
]
}
@client.exec(
:ttl => 60,
:some_binary => {
:args => 'preliminary_arg',
:groups => [ 'arg1a arg1b' ]
}
).should == {
"some_binary" => [
["cached!"]
]
}
end
it "should not read cache if preliminary arg changes" do
@client.exec(
:ttl => 60,
:some_binary => {
:args => 'preliminary_arg2',
:groups => [ 'arg2a arg2b' ]
}
).should == {
"some_binary" => [
["arg1_result_1", "arg1_result_2"]
]
}
end
it "should still read from original cache" do
client_exec.should == {
"some_binary" => [
["cached!"],
["cached!"]
]
}
end
it "should respect wait option" do
@client.redis_1.keys("execache:cache:*").each do |key|
@client.redis_1.del(key)
end
client_exec(:wait => false).should == false
client_exec.should == {
"some_binary" => [
["arg1_result_1", "arg1_result_2"],
["arg2_result_1", "arg2_result_2"]
]
}
end
end