dalli 2.7.2 → 2.7.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of dalli might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/History.md +14 -0
- data/README.md +18 -14
- data/Rakefile +1 -0
- data/lib/active_support/cache/dalli_store.rb +24 -15
- data/lib/dalli/client.rb +14 -10
- data/lib/dalli/ring.rb +1 -1
- data/lib/dalli/server.rb +48 -23
- data/lib/dalli/socket.rb +74 -40
- data/lib/dalli/version.rb +1 -1
- data/test/benchmark_test.rb +3 -2
- data/test/helper.rb +3 -2
- data/test/memcached_mock.rb +97 -17
- data/test/sasl/memcached.conf +1 -0
- data/test/{sasldb → sasl/sasldb} +0 -0
- data/test/test_active_support.rb +163 -61
- data/test/test_cas_client.rb +7 -7
- data/test/test_compressor.rb +4 -5
- data/test/test_dalli.rb +92 -35
- data/test/test_encoding.rb +2 -2
- data/test/test_failover.rb +44 -35
- data/test/test_network.rb +10 -0
- data/test/test_rack_session.rb +3 -3
- data/test/test_ring.rb +2 -2
- data/test/test_sasl.rb +9 -14
- data/test/test_serializer.rb +6 -7
- data/test/test_server.rb +30 -0
- metadata +7 -5
data/lib/dalli/socket.rb
CHANGED
@@ -13,17 +13,6 @@ begin
|
|
13
13
|
IO.select(nil, [self], nil, options[:socket_timeout]) || raise(Timeout::Error, "IO timeout")
|
14
14
|
end
|
15
15
|
|
16
|
-
def self.open(host, port, server, options = {})
|
17
|
-
addr = Socket.pack_sockaddr_in(port, host)
|
18
|
-
sock = start(addr)
|
19
|
-
sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
|
20
|
-
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) if options[:keepalive]
|
21
|
-
sock.options = options
|
22
|
-
sock.server = server
|
23
|
-
sock.kgio_wait_writable
|
24
|
-
sock
|
25
|
-
end
|
26
|
-
|
27
16
|
alias :write :kgio_write
|
28
17
|
|
29
18
|
def readfull(count)
|
@@ -50,7 +39,30 @@ begin
|
|
50
39
|
end
|
51
40
|
value
|
52
41
|
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class Dalli::Server::KSocket::TCP < Dalli::Server::KSocket
|
45
|
+
def self.open(host, port, server, options = {})
|
46
|
+
addr = Socket.pack_sockaddr_in(port, host)
|
47
|
+
sock = start(addr)
|
48
|
+
sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
|
49
|
+
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) if options[:keepalive]
|
50
|
+
sock.options = options
|
51
|
+
sock.server = server
|
52
|
+
sock.kgio_wait_writable
|
53
|
+
sock
|
54
|
+
end
|
55
|
+
end
|
53
56
|
|
57
|
+
class Dalli::Server::KSocket::UNIX < Dalli::Server::KSocket
|
58
|
+
def self.open(path, server, options = {})
|
59
|
+
addr = Socket.pack_sockaddr_un(path)
|
60
|
+
sock = start(addr)
|
61
|
+
sock.options = options
|
62
|
+
sock.server = server
|
63
|
+
sock.kgio_wait_writable
|
64
|
+
sock
|
65
|
+
end
|
54
66
|
end
|
55
67
|
|
56
68
|
if ::Kgio.respond_to?(:wait_readable=)
|
@@ -61,48 +73,70 @@ begin
|
|
61
73
|
rescue LoadError
|
62
74
|
|
63
75
|
puts "Using standard socket IO (#{RUBY_DESCRIPTION})" if defined?($TESTING) && $TESTING
|
64
|
-
|
65
|
-
|
76
|
+
module Dalli::Server::KSocket
|
77
|
+
module InstanceMethods
|
78
|
+
def readfull(count)
|
79
|
+
value = ''
|
80
|
+
begin
|
81
|
+
loop do
|
82
|
+
value << read_nonblock(count - value.bytesize)
|
83
|
+
break if value.bytesize == count
|
84
|
+
end
|
85
|
+
rescue Errno::EAGAIN, Errno::EWOULDBLOCK
|
86
|
+
if IO.select([self], nil, nil, options[:socket_timeout])
|
87
|
+
retry
|
88
|
+
else
|
89
|
+
raise Timeout::Error, "IO timeout: #{options.inspect}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
value
|
93
|
+
end
|
94
|
+
|
95
|
+
def read_available
|
96
|
+
value = ''
|
97
|
+
loop do
|
98
|
+
begin
|
99
|
+
value << read_nonblock(8196)
|
100
|
+
rescue Errno::EAGAIN, Errno::EWOULDBLOCK
|
101
|
+
break
|
102
|
+
end
|
103
|
+
end
|
104
|
+
value
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.included(receiver)
|
109
|
+
receiver.send(:attr_accessor, :options, :server)
|
110
|
+
receiver.send(:include, InstanceMethods)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class Dalli::Server::KSocket::TCP < TCPSocket
|
115
|
+
include Dalli::Server::KSocket
|
66
116
|
|
67
117
|
def self.open(host, port, server, options = {})
|
68
118
|
Timeout.timeout(options[:socket_timeout]) do
|
69
119
|
sock = new(host, port)
|
70
120
|
sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
|
71
121
|
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) if options[:keepalive]
|
72
|
-
sock.options = {
|
122
|
+
sock.options = {:host => host, :port => port}.merge(options)
|
73
123
|
sock.server = server
|
74
124
|
sock
|
75
125
|
end
|
76
126
|
end
|
127
|
+
end
|
77
128
|
|
78
|
-
|
79
|
-
|
80
|
-
begin
|
81
|
-
loop do
|
82
|
-
value << read_nonblock(count - value.bytesize)
|
83
|
-
break if value.bytesize == count
|
84
|
-
end
|
85
|
-
rescue Errno::EAGAIN, Errno::EWOULDBLOCK
|
86
|
-
if IO.select([self], nil, nil, options[:socket_timeout])
|
87
|
-
retry
|
88
|
-
else
|
89
|
-
raise Timeout::Error, "IO timeout: #{options.inspect}"
|
90
|
-
end
|
91
|
-
end
|
92
|
-
value
|
93
|
-
end
|
129
|
+
class Dalli::Server::KSocket::UNIX < UNIXSocket
|
130
|
+
include Dalli::Server::KSocket
|
94
131
|
|
95
|
-
def
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
break
|
102
|
-
end
|
132
|
+
def self.open(path, server, options = {})
|
133
|
+
Timeout.timeout(options[:socket_timeout]) do
|
134
|
+
sock = new(path)
|
135
|
+
sock.options = {:path => path}.merge(options)
|
136
|
+
sock.server = server
|
137
|
+
sock
|
103
138
|
end
|
104
|
-
value
|
105
139
|
end
|
106
|
-
|
107
140
|
end
|
141
|
+
|
108
142
|
end
|
data/lib/dalli/version.rb
CHANGED
data/test/benchmark_test.rb
CHANGED
@@ -10,7 +10,8 @@ describe 'performance' do
|
|
10
10
|
@value = []
|
11
11
|
@marshalled = Marshal.dump(@value)
|
12
12
|
|
13
|
-
@
|
13
|
+
@port = 23417
|
14
|
+
@servers = ["127.0.0.1:#{@port}", "localhost:#{@port}"]
|
14
15
|
@key1 = "Short"
|
15
16
|
@key2 = "Sym1-2-3::45"*8
|
16
17
|
@key3 = "Long"*40
|
@@ -22,7 +23,7 @@ describe 'performance' do
|
|
22
23
|
end
|
23
24
|
|
24
25
|
it 'runs benchmarks' do
|
25
|
-
memcached do
|
26
|
+
memcached(@port) do
|
26
27
|
|
27
28
|
Benchmark.bm(37) do |x|
|
28
29
|
|
data/test/helper.rb
CHANGED
@@ -2,12 +2,13 @@ $TESTING = true
|
|
2
2
|
require 'rubygems'
|
3
3
|
# require 'simplecov'
|
4
4
|
# SimpleCov.start
|
5
|
-
require 'minitest/pride'
|
5
|
+
require 'minitest/pride' unless RUBY_ENGINE == 'rbx'
|
6
6
|
require 'minitest/autorun'
|
7
7
|
require 'mocha/setup'
|
8
8
|
require 'memcached_mock'
|
9
9
|
|
10
|
-
ENV['MEMCACHED_SASL_PWDB'] = "#{File.dirname(__FILE__)}/sasldb"
|
10
|
+
ENV['MEMCACHED_SASL_PWDB'] = "#{File.dirname(__FILE__)}/sasl/sasldb"
|
11
|
+
ENV['SASL_CONF_PATH'] = "#{File.dirname(__FILE__)}/sasl/memcached.conf"
|
11
12
|
|
12
13
|
WANT_RAILS_VERSION = ENV['RAILS_VERSION'] || '>= 3.0.0'
|
13
14
|
gem 'rails', WANT_RAILS_VERSION
|
data/test/memcached_mock.rb
CHANGED
@@ -1,18 +1,31 @@
|
|
1
1
|
require "socket"
|
2
|
+
require "tempfile"
|
2
3
|
|
3
4
|
$started = {}
|
4
5
|
|
5
6
|
module MemcachedMock
|
6
|
-
|
7
|
+
UNIX_SOCKET_PATH = (f = Tempfile.new('dalli_test'); f.close; f.path)
|
8
|
+
|
9
|
+
def self.start(port=19123)
|
7
10
|
server = TCPServer.new("localhost", port)
|
8
11
|
session = server.accept
|
9
|
-
|
12
|
+
yield(session)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.start_unix(path=UNIX_SOCKET_PATH)
|
16
|
+
begin
|
17
|
+
File.delete(path)
|
18
|
+
rescue Errno::ENOENT
|
19
|
+
end
|
20
|
+
server = UNIXServer.new(path)
|
21
|
+
session = server.accept
|
22
|
+
yield(session)
|
10
23
|
end
|
11
24
|
|
12
|
-
def self.delayed_start(port=19123, wait=1
|
25
|
+
def self.delayed_start(port=19123, wait=1)
|
13
26
|
server = TCPServer.new("localhost", port)
|
14
27
|
sleep wait
|
15
|
-
|
28
|
+
yield(server)
|
16
29
|
end
|
17
30
|
|
18
31
|
module Helper
|
@@ -23,13 +36,13 @@ module MemcachedMock
|
|
23
36
|
# assert_equal "PONG", Dalli::Client.new('localhost:22122').get('abc')
|
24
37
|
# end
|
25
38
|
#
|
26
|
-
def memcached_mock(proc, meth =
|
39
|
+
def memcached_mock(proc, meth=:start, meth_args=[])
|
27
40
|
return unless supports_fork?
|
28
41
|
begin
|
29
42
|
pid = fork do
|
30
43
|
trap("TERM") { exit }
|
31
44
|
|
32
|
-
MemcachedMock.send(meth) do |*args|
|
45
|
+
MemcachedMock.send(meth, *meth_args) do |*args|
|
33
46
|
proc.call(*args)
|
34
47
|
end
|
35
48
|
end
|
@@ -65,24 +78,89 @@ module MemcachedMock
|
|
65
78
|
raise Errno::ENOENT, "Unable to find memcached 1.4+ locally"
|
66
79
|
end
|
67
80
|
|
68
|
-
def
|
69
|
-
|
70
|
-
yield
|
81
|
+
def memcached_persistent(port=21345)
|
82
|
+
dc = start_and_flush_with_retry(port, '', {})
|
83
|
+
yield dc, port if block_given?
|
71
84
|
end
|
72
85
|
|
73
|
-
def
|
74
|
-
|
86
|
+
def sasl_credentials
|
87
|
+
{ :username => 'testuser', :password => 'testtest' }
|
88
|
+
end
|
89
|
+
|
90
|
+
def sasl_env
|
91
|
+
{
|
92
|
+
'MEMCACHED_SASL_PWDB' => "#{File.dirname(__FILE__)}/sasl/sasldb",
|
93
|
+
'SASL_CONF_PATH' => "#{File.dirname(__FILE__)}/sasl/memcached.conf"
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
def memcached_sasl_persistent(port=21397)
|
98
|
+
dc = start_and_flush_with_retry(port, '-S', sasl_credentials)
|
99
|
+
yield dc, port if block_given?
|
100
|
+
end
|
101
|
+
|
102
|
+
def memcached_cas_persistent(port = 25662)
|
75
103
|
require 'dalli/cas/client'
|
76
|
-
|
104
|
+
dc = start_and_flush_with_retry(port)
|
105
|
+
yield dc, port if block_given?
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
def memcached_low_mem_persistent(port = 19128)
|
110
|
+
dc = start_and_flush_with_retry(port, '-m 1 -M')
|
111
|
+
yield dc, port if block_given?
|
112
|
+
end
|
113
|
+
|
114
|
+
def start_and_flush_with_retry(port, args = '', client_options = {})
|
115
|
+
dc = nil
|
116
|
+
retry_count = 0
|
117
|
+
while dc.nil? do
|
118
|
+
begin
|
119
|
+
dc = start_and_flush(port, args, client_options, (retry_count == 0))
|
120
|
+
rescue StandardError => e
|
121
|
+
$started[port] = nil
|
122
|
+
retry_count += 1
|
123
|
+
raise e if retry_count >= 3
|
124
|
+
end
|
125
|
+
end
|
126
|
+
dc
|
77
127
|
end
|
78
128
|
|
79
|
-
def
|
129
|
+
def start_and_flush(port, args = '', client_options = {}, flush = true)
|
130
|
+
memcached_server(port, args)
|
131
|
+
if "#{port}" =~ /\A\//
|
132
|
+
# unix socket
|
133
|
+
dc = Dalli::Client.new(port, client_options)
|
134
|
+
else
|
135
|
+
dc = Dalli::Client.new(["localhost:#{port}", "127.0.0.1:#{port}"], client_options)
|
136
|
+
end
|
137
|
+
dc.flush_all if flush
|
138
|
+
dc
|
139
|
+
end
|
140
|
+
|
141
|
+
def memcached(port, args='', client_options={})
|
142
|
+
dc = start_and_flush_with_retry(port, args, client_options)
|
143
|
+
yield dc, port if block_given?
|
144
|
+
memcached_kill(port)
|
145
|
+
end
|
146
|
+
|
147
|
+
def memcached_server(port, args='')
|
80
148
|
Memcached.path ||= find_memcached
|
149
|
+
if "#{port}" =~ /\A\//
|
150
|
+
# unix socket
|
151
|
+
port_socket_arg = '-s'
|
152
|
+
begin
|
153
|
+
File.delete(port)
|
154
|
+
rescue Errno::ENOENT
|
155
|
+
end
|
156
|
+
else
|
157
|
+
port_socket_arg = '-p'
|
158
|
+
port = port.to_i
|
159
|
+
end
|
81
160
|
|
82
|
-
cmd = "#{Memcached.path}memcached #{args}
|
161
|
+
cmd = "#{Memcached.path}memcached #{args} #{port_socket_arg} #{port}"
|
83
162
|
|
84
163
|
$started[port] ||= begin
|
85
|
-
#puts "Starting: #{cmd}..."
|
86
164
|
pid = IO.popen(cmd).pid
|
87
165
|
at_exit do
|
88
166
|
begin
|
@@ -91,7 +169,8 @@ module MemcachedMock
|
|
91
169
|
rescue Errno::ECHILD, Errno::ESRCH
|
92
170
|
end
|
93
171
|
end
|
94
|
-
|
172
|
+
wait_time = (args && args =~ /\-S/) ? 0.1 : 0.1
|
173
|
+
sleep wait_time
|
95
174
|
pid
|
96
175
|
end
|
97
176
|
end
|
@@ -106,7 +185,8 @@ module MemcachedMock
|
|
106
185
|
begin
|
107
186
|
Process.kill("TERM", pid)
|
108
187
|
Process.wait(pid)
|
109
|
-
rescue Errno::ECHILD, Errno::ESRCH
|
188
|
+
rescue Errno::ECHILD, Errno::ESRCH => e
|
189
|
+
puts e.inspect
|
110
190
|
end
|
111
191
|
end
|
112
192
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
mech_list: plain
|
data/test/{sasldb → sasl/sasldb}
RENAMED
File without changes
|
data/test/test_active_support.rb
CHANGED
@@ -9,40 +9,48 @@ class MockUser
|
|
9
9
|
end
|
10
10
|
|
11
11
|
describe 'ActiveSupport' do
|
12
|
+
before do
|
13
|
+
@port = 19987
|
14
|
+
end
|
15
|
+
|
12
16
|
describe 'active_support caching' do
|
13
17
|
|
14
18
|
it 'has accessible options' do
|
15
|
-
@dalli = ActiveSupport::Cache.lookup_store(:dalli_store,
|
19
|
+
@dalli = ActiveSupport::Cache.lookup_store(:dalli_store, "localhost:#{@port}", :expires_in => 5.minutes, :frob => 'baz')
|
16
20
|
assert_equal 'baz', @dalli.options[:frob]
|
17
21
|
end
|
18
22
|
|
19
23
|
it 'allow mute and silence' do
|
20
|
-
@
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
+
memcached_persistent(@port) do
|
25
|
+
@dalli = ActiveSupport::Cache.lookup_store(:dalli_store, "localhost:#{@port}")
|
26
|
+
@dalli.mute do
|
27
|
+
assert op_addset_succeeds(@dalli.write('foo', 'bar', nil))
|
28
|
+
assert_equal 'bar', @dalli.read('foo', nil)
|
29
|
+
end
|
30
|
+
refute @dalli.silence?
|
31
|
+
@dalli.silence!
|
32
|
+
assert_equal true, @dalli.silence?
|
24
33
|
end
|
25
|
-
refute @dalli.silence?
|
26
|
-
@dalli.silence!
|
27
|
-
assert_equal true, @dalli.silence?
|
28
34
|
end
|
29
35
|
|
30
36
|
it 'handle nil options' do
|
31
|
-
@
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
memcached_persistent(@port) do
|
38
|
+
@dalli = ActiveSupport::Cache.lookup_store(:dalli_store, "localhost:#{@port}")
|
39
|
+
assert op_addset_succeeds(@dalli.write('foo', 'bar', nil))
|
40
|
+
assert_equal 'bar', @dalli.read('foo', nil)
|
41
|
+
assert_equal 18, @dalli.fetch('lkjsadlfk', nil) { 18 }
|
42
|
+
assert_equal 18, @dalli.fetch('lkjsadlfk', nil) { 18 }
|
43
|
+
assert_equal 1, @dalli.increment('lkjsa', 1, nil)
|
44
|
+
assert_equal 2, @dalli.increment('lkjsa', 1, nil)
|
45
|
+
assert_equal 1, @dalli.decrement('lkjsa', 1, nil)
|
46
|
+
assert_equal true, @dalli.delete('lkjsa')
|
47
|
+
end
|
40
48
|
end
|
41
49
|
|
42
50
|
it 'support fetch' do
|
43
51
|
with_activesupport do
|
44
|
-
|
45
|
-
connect
|
52
|
+
memcached_persistent(@port) do
|
53
|
+
connect(@port)
|
46
54
|
dvalue = @dalli.fetch('someotherkeywithoutspaces', :expires_in => 1.second) { 123 }
|
47
55
|
assert_equal 123, dvalue
|
48
56
|
|
@@ -68,8 +76,8 @@ describe 'ActiveSupport' do
|
|
68
76
|
|
69
77
|
it 'support keys with spaces on Rails3' do
|
70
78
|
with_activesupport do
|
71
|
-
|
72
|
-
connect
|
79
|
+
memcached_persistent(@port) do
|
80
|
+
connect(@port)
|
73
81
|
dvalue = @dalli.fetch('some key with spaces', :expires_in => 1.second) { 123 }
|
74
82
|
assert_equal 123, dvalue
|
75
83
|
end
|
@@ -78,8 +86,8 @@ describe 'ActiveSupport' do
|
|
78
86
|
|
79
87
|
it 'support read_multi' do
|
80
88
|
with_activesupport do
|
81
|
-
|
82
|
-
connect
|
89
|
+
memcached_persistent(@port) do
|
90
|
+
connect(@port)
|
83
91
|
x = rand_key
|
84
92
|
y = rand_key
|
85
93
|
assert_equal({}, @dalli.read_multi(x, y))
|
@@ -92,8 +100,8 @@ describe 'ActiveSupport' do
|
|
92
100
|
|
93
101
|
it 'support read_multi with an array' do
|
94
102
|
with_activesupport do
|
95
|
-
|
96
|
-
connect
|
103
|
+
memcached_persistent(@port) do
|
104
|
+
connect(@port)
|
97
105
|
x = rand_key
|
98
106
|
y = rand_key
|
99
107
|
assert_equal({}, @dalli.read_multi([x, y]))
|
@@ -106,10 +114,19 @@ describe 'ActiveSupport' do
|
|
106
114
|
end
|
107
115
|
end
|
108
116
|
|
117
|
+
it 'support read_multi with an empty array' do
|
118
|
+
with_activesupport do
|
119
|
+
memcached_persistent(@port) do
|
120
|
+
connect(@port)
|
121
|
+
assert_equal({}, @dalli.read_multi([]))
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
109
126
|
it 'support raw read_multi' do
|
110
127
|
with_activesupport do
|
111
|
-
|
112
|
-
connect
|
128
|
+
memcached_persistent(@port) do
|
129
|
+
connect(@port)
|
113
130
|
@dalli.write("abc", 5, :raw => true)
|
114
131
|
@dalli.write("cba", 5, :raw => true)
|
115
132
|
assert_equal({'abc' => '5', 'cba' => '5' }, @dalli.read_multi("abc", "cba"))
|
@@ -119,8 +136,8 @@ describe 'ActiveSupport' do
|
|
119
136
|
|
120
137
|
it 'support read_multi with LocalCache' do
|
121
138
|
with_activesupport do
|
122
|
-
|
123
|
-
connect
|
139
|
+
memcached_persistent(@port) do
|
140
|
+
connect(@port)
|
124
141
|
x = rand_key
|
125
142
|
y = rand_key
|
126
143
|
assert_equal({}, @dalli.read_multi(x, y))
|
@@ -150,8 +167,8 @@ describe 'ActiveSupport' do
|
|
150
167
|
|
151
168
|
it 'supports fetch_multi' do
|
152
169
|
with_activesupport do
|
153
|
-
|
154
|
-
connect
|
170
|
+
memcached_persistent(@port) do
|
171
|
+
connect(@port)
|
155
172
|
|
156
173
|
x = rand_key.to_s
|
157
174
|
y = rand_key
|
@@ -170,8 +187,8 @@ describe 'ActiveSupport' do
|
|
170
187
|
|
171
188
|
it 'support read, write and delete' do
|
172
189
|
with_activesupport do
|
173
|
-
|
174
|
-
connect
|
190
|
+
memcached_persistent(@port) do
|
191
|
+
connect(@port)
|
175
192
|
y = rand_key
|
176
193
|
assert_nil @dalli.read(y)
|
177
194
|
dres = @dalli.write(y, 123)
|
@@ -193,6 +210,11 @@ describe 'ActiveSupport' do
|
|
193
210
|
dres = @dalli.delete(user)
|
194
211
|
assert_equal true, dres
|
195
212
|
|
213
|
+
dres = @dalli.write(:false_value, false)
|
214
|
+
assert op_addset_succeeds(dres)
|
215
|
+
dres = @dalli.read(:false_value)
|
216
|
+
assert_equal false, dres
|
217
|
+
|
196
218
|
bigkey = '123456789012345678901234567890'
|
197
219
|
@dalli.write(bigkey, 'double width')
|
198
220
|
assert_equal 'double width', @dalli.read(bigkey)
|
@@ -201,10 +223,55 @@ describe 'ActiveSupport' do
|
|
201
223
|
end
|
202
224
|
end
|
203
225
|
|
226
|
+
it 'support read, write and delete with local namespace' do
|
227
|
+
with_activesupport do
|
228
|
+
memcached_persistent(@port) do
|
229
|
+
connect(@port, '')
|
230
|
+
key = 'key_with_namespace'
|
231
|
+
namespace_value = @dalli.fetch(key, :namespace => 'namespace') { 123 }
|
232
|
+
assert_equal 123, namespace_value
|
233
|
+
|
234
|
+
res = @dalli.read(key, :namespace => 'namespace')
|
235
|
+
assert_equal 123, res
|
236
|
+
|
237
|
+
res = @dalli.delete(key, :namespace => 'namespace')
|
238
|
+
assert_equal true, res
|
239
|
+
|
240
|
+
res = @dalli.write(key, "foo", :namespace => 'namespace')
|
241
|
+
assert op_addset_succeeds(res)
|
242
|
+
|
243
|
+
res = @dalli.read(key, :namespace => 'namespace')
|
244
|
+
assert_equal "foo", res
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
it 'support multi_read and multi_fetch with local namespace' do
|
250
|
+
with_activesupport do
|
251
|
+
memcached_persistent(@port) do
|
252
|
+
connect(@port, '')
|
253
|
+
x = rand_key.to_s
|
254
|
+
y = rand_key
|
255
|
+
namespace = 'namespace'
|
256
|
+
hash = { x => 'ABC', y => 'DEF' }
|
257
|
+
|
258
|
+
results = @dalli.fetch_multi(x, y, :namespace => namespace) { |key| hash[key] }
|
259
|
+
|
260
|
+
assert_equal({ x => 'ABC', y => 'DEF' }, results)
|
261
|
+
assert_equal('ABC', @dalli.read(x, :namespace => namespace))
|
262
|
+
assert_equal('DEF', @dalli.read(y, :namespace => namespace))
|
263
|
+
|
264
|
+
@dalli.write("abc", 5, :namespace => 'namespace')
|
265
|
+
@dalli.write("cba", 5, :namespace => 'namespace')
|
266
|
+
assert_equal({'abc' => 5, 'cba' => 5 }, @dalli.read_multi("abc", "cba", :namespace => 'namespace'))
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
204
271
|
it 'support read, write and delete with LocalCache' do
|
205
272
|
with_activesupport do
|
206
|
-
|
207
|
-
connect
|
273
|
+
memcached_persistent(@port) do
|
274
|
+
connect(@port)
|
208
275
|
y = rand_key.to_s
|
209
276
|
@dalli.with_local_cache do
|
210
277
|
Dalli::Client.any_instance.expects(:get).with(y, {}).once.returns(123)
|
@@ -231,8 +298,8 @@ describe 'ActiveSupport' do
|
|
231
298
|
|
232
299
|
it 'support unless_exist with LocalCache' do
|
233
300
|
with_activesupport do
|
234
|
-
|
235
|
-
connect
|
301
|
+
memcached_persistent(@port) do
|
302
|
+
connect(@port)
|
236
303
|
y = rand_key.to_s
|
237
304
|
@dalli.with_local_cache do
|
238
305
|
Dalli::Client.any_instance.expects(:add).with(y, 123, nil, {:unless_exist => true}).once.returns(true)
|
@@ -255,8 +322,8 @@ describe 'ActiveSupport' do
|
|
255
322
|
|
256
323
|
it 'support increment/decrement commands' do
|
257
324
|
with_activesupport do
|
258
|
-
|
259
|
-
connect
|
325
|
+
memcached_persistent(@port) do
|
326
|
+
connect(@port)
|
260
327
|
assert op_addset_succeeds(@dalli.write('counter', 0, :raw => true))
|
261
328
|
assert_equal 1, @dalli.increment('counter')
|
262
329
|
assert_equal 2, @dalli.increment('counter')
|
@@ -293,8 +360,8 @@ describe 'ActiveSupport' do
|
|
293
360
|
|
294
361
|
it 'support exist command' do
|
295
362
|
with_activesupport do
|
296
|
-
|
297
|
-
connect
|
363
|
+
memcached_persistent(@port) do
|
364
|
+
connect(@port)
|
298
365
|
@dalli.write(:foo, 'a')
|
299
366
|
@dalli.write(:false_value, false)
|
300
367
|
|
@@ -312,8 +379,8 @@ describe 'ActiveSupport' do
|
|
312
379
|
|
313
380
|
it 'support other esoteric commands' do
|
314
381
|
with_activesupport do
|
315
|
-
|
316
|
-
connect
|
382
|
+
memcached_persistent(@port) do
|
383
|
+
connect(@port)
|
317
384
|
ds = @dalli.stats
|
318
385
|
assert_equal 1, ds.keys.size
|
319
386
|
assert ds[ds.keys.first].keys.size > 0
|
@@ -325,16 +392,17 @@ describe 'ActiveSupport' do
|
|
325
392
|
|
326
393
|
it 'respect "raise_errors" option' do
|
327
394
|
with_activesupport do
|
328
|
-
|
329
|
-
|
395
|
+
new_port = 29333
|
396
|
+
memcached_persistent(new_port) do
|
397
|
+
@dalli = ActiveSupport::Cache.lookup_store(:dalli_store, "localhost:#{new_port}")
|
330
398
|
@dalli.write 'foo', 'bar'
|
331
399
|
assert_equal @dalli.read('foo'), 'bar'
|
332
400
|
|
333
|
-
memcached_kill(
|
401
|
+
memcached_kill(new_port)
|
334
402
|
|
335
403
|
assert_equal @dalli.read('foo'), nil
|
336
404
|
|
337
|
-
@dalli = ActiveSupport::Cache.lookup_store(:dalli_store,
|
405
|
+
@dalli = ActiveSupport::Cache.lookup_store(:dalli_store, "localhost:#{new_port}", :raise_errors => true)
|
338
406
|
|
339
407
|
exception = [Dalli::RingError, { :message => "No server available" }]
|
340
408
|
|
@@ -351,12 +419,46 @@ describe 'ActiveSupport' do
|
|
351
419
|
end
|
352
420
|
end
|
353
421
|
end
|
422
|
+
|
423
|
+
describe 'instruments' do
|
424
|
+
it 'payload hits' do
|
425
|
+
with_activesupport do
|
426
|
+
memcached_persistent(@port) do
|
427
|
+
connect(@port)
|
428
|
+
|
429
|
+
payload = {}
|
430
|
+
dres = @dalli.write('false', false)
|
431
|
+
assert op_addset_succeeds(dres)
|
432
|
+
foo = @dalli.fetch('burrito') do 'tacos' end
|
433
|
+
assert 'tacos', foo
|
434
|
+
|
435
|
+
# NOTE: mocha stubbing for yields
|
436
|
+
# makes the result of the block nil in all cases
|
437
|
+
# there was a ticket about this:
|
438
|
+
# http://floehopper.lighthouseapp.com/projects/22289/tickets/14-8687-blocks-return-value-is-dropped-on-stubbed-yielding-methods
|
439
|
+
@dalli.stubs(:instrument).yields payload
|
440
|
+
|
441
|
+
@dalli.read('false')
|
442
|
+
assert_equal true, payload.delete(:hit)
|
443
|
+
|
444
|
+
|
445
|
+
foo = @dalli.fetch('unset_key') do 'tacos' end
|
446
|
+
assert_equal false, payload.delete(:hit)
|
447
|
+
|
448
|
+
@dalli.fetch('burrito') do 'tacos' end
|
449
|
+
assert_equal true, payload.delete(:hit)
|
450
|
+
|
451
|
+
@dalli.unstub(:instrument)
|
452
|
+
end
|
453
|
+
end
|
454
|
+
end
|
455
|
+
end
|
354
456
|
end
|
355
457
|
|
356
458
|
it 'handle crazy characters from far-away lands' do
|
357
459
|
with_activesupport do
|
358
|
-
|
359
|
-
connect
|
460
|
+
memcached_persistent(@port) do
|
461
|
+
connect(@port)
|
360
462
|
key = "fooƒ"
|
361
463
|
value = 'bafƒ'
|
362
464
|
assert op_addset_succeeds(@dalli.write(key, value))
|
@@ -367,18 +469,18 @@ describe 'ActiveSupport' do
|
|
367
469
|
|
368
470
|
it 'normalize options as expected' do
|
369
471
|
with_activesupport do
|
370
|
-
|
371
|
-
@dalli = ActiveSupport::Cache::DalliStore.new(
|
472
|
+
memcached_persistent(@port) do
|
473
|
+
@dalli = ActiveSupport::Cache::DalliStore.new("localhost:#{@port}", :expires_in => 1, :namespace => 'foo', :compress => true)
|
372
474
|
assert_equal 1, @dalli.instance_variable_get(:@data).instance_variable_get(:@options)[:expires_in]
|
373
475
|
assert_equal 'foo', @dalli.instance_variable_get(:@data).instance_variable_get(:@options)[:namespace]
|
374
|
-
assert_equal ["localhost
|
476
|
+
assert_equal ["localhost:#{@port}"], @dalli.instance_variable_get(:@data).instance_variable_get(:@servers)
|
375
477
|
end
|
376
478
|
end
|
377
479
|
end
|
378
480
|
|
379
481
|
it 'handles nil server with additional options' do
|
380
482
|
with_activesupport do
|
381
|
-
|
483
|
+
memcached_persistent(@port) do
|
382
484
|
@dalli = ActiveSupport::Cache::DalliStore.new(nil, :expires_in => 1, :namespace => 'foo', :compress => true)
|
383
485
|
assert_equal 1, @dalli.instance_variable_get(:@data).instance_variable_get(:@options)[:expires_in]
|
384
486
|
assert_equal 'foo', @dalli.instance_variable_get(:@data).instance_variable_get(:@options)[:namespace]
|
@@ -389,8 +491,8 @@ describe 'ActiveSupport' do
|
|
389
491
|
|
390
492
|
it 'supports connection pooling' do
|
391
493
|
with_activesupport do
|
392
|
-
|
393
|
-
@dalli = ActiveSupport::Cache::DalliStore.new(
|
494
|
+
memcached_persistent(@port) do
|
495
|
+
@dalli = ActiveSupport::Cache::DalliStore.new("localhost:#{@port}", :expires_in => 1, :namespace => 'foo', :compress => true, :pool_size => 3)
|
394
496
|
assert_equal nil, @dalli.read('foo')
|
395
497
|
assert @dalli.write('foo', 1)
|
396
498
|
assert_equal 1, @dalli.fetch('foo') { raise 'boom' }
|
@@ -406,8 +508,8 @@ describe 'ActiveSupport' do
|
|
406
508
|
|
407
509
|
it 'allow keys to be frozen' do
|
408
510
|
with_activesupport do
|
409
|
-
|
410
|
-
connect
|
511
|
+
memcached_persistent(@port) do
|
512
|
+
connect(@port)
|
411
513
|
key = "foo"
|
412
514
|
key.freeze
|
413
515
|
assert op_addset_succeeds(@dalli.write(key, "value"))
|
@@ -417,8 +519,8 @@ describe 'ActiveSupport' do
|
|
417
519
|
|
418
520
|
it 'allow keys from a hash' do
|
419
521
|
with_activesupport do
|
420
|
-
|
421
|
-
connect
|
522
|
+
memcached_persistent(@port) do
|
523
|
+
connect(@port)
|
422
524
|
map = { "one" => "one", "two" => "two" }
|
423
525
|
map.each_pair do |k, v|
|
424
526
|
assert op_addset_succeeds(@dalli.write(k, v))
|
@@ -428,8 +530,8 @@ describe 'ActiveSupport' do
|
|
428
530
|
end
|
429
531
|
end
|
430
532
|
|
431
|
-
def connect
|
432
|
-
@dalli = ActiveSupport::Cache.lookup_store(:dalli_store,
|
533
|
+
def connect(port = 19122, namespace = lambda{33.to_s(36)})
|
534
|
+
@dalli = ActiveSupport::Cache.lookup_store(:dalli_store, "localhost:#{port}", :expires_in => 10.seconds, :namespace => namespace)
|
433
535
|
@dalli.clear
|
434
536
|
end
|
435
537
|
|