forked from mloughran/em-hiredis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conflicts: README.md
- Loading branch information
Showing
5 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'spec_helper' | ||
require 'support/inprocess_redis_mock' | ||
|
||
def connect_mock(timeout = 10, url = "redis://localhost:6381", server = nil, &blk) | ||
em(timeout) do | ||
IRedisMock.start | ||
redis = EventMachine::Hiredis.connect(url) | ||
blk.call(redis) | ||
IRedisMock.stop | ||
end | ||
end | ||
|
||
describe EM::Hiredis::BaseClient do | ||
it "should ping after activity timeout reached" do | ||
connect_mock do |redis| | ||
redis.configure_inactivity_check(2, 1) | ||
EM.add_timer(4) { | ||
IRedisMock.received.should include("ping") | ||
done | ||
} | ||
end | ||
end | ||
|
||
it "should not ping before activity timeout reached" do | ||
connect_mock do |redis| | ||
redis.configure_inactivity_check(3, 1) | ||
EM.add_timer(2) { | ||
IRedisMock.received.should_not include("ping") | ||
done | ||
} | ||
end | ||
end | ||
|
||
it "should ping after timeout reached even though command has been sent (no response)" do | ||
connect_mock do |redis| | ||
redis.configure_inactivity_check(2, 1) | ||
IRedisMock.pause # no responses from now on | ||
|
||
EM.add_timer(1.5) { | ||
redis.get "test" | ||
} | ||
|
||
EM.add_timer(4) { | ||
IRedisMock.received.should include("ping") | ||
done | ||
} | ||
end | ||
end | ||
|
||
it "should trigger a reconnect when there's no response to ping" do | ||
connect_mock do |redis| | ||
redis.configure_inactivity_check(2, 1) | ||
IRedisMock.pause # no responses from now on | ||
|
||
EM.add_timer(1.5) { | ||
redis.get "test" | ||
} | ||
|
||
EM.add_timer(5) { | ||
IRedisMock.received.should include("disconnect") | ||
done | ||
} | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
module IRedisMock | ||
def self.start(replies = {}) | ||
@sig = EventMachine::start_server("127.0.0.1", 6381, Connection) | ||
@received = [] | ||
@replies = replies | ||
@paused = false | ||
end | ||
|
||
def self.stop | ||
EventMachine::stop_server(@sig) | ||
end | ||
|
||
def self.received | ||
@received ||= [] | ||
end | ||
|
||
def self.pause | ||
@paused = true | ||
end | ||
def self.unpause | ||
@paused = false | ||
end | ||
|
||
def self.paused | ||
@paused | ||
end | ||
|
||
def self.replies | ||
@replies | ||
end | ||
|
||
class Connection < EventMachine::Connection | ||
def initialize | ||
@data = "" | ||
@parts = [] | ||
end | ||
|
||
def unbind | ||
IRedisMock.received << 'disconnect' | ||
end | ||
|
||
def receive_data(data) | ||
@data << data | ||
|
||
while (idx = @data.index("\r\n")) | ||
@parts << @data[0..idx-1] | ||
@data = @data[idx+2..-1] | ||
end | ||
|
||
while @parts.length > 0 | ||
throw "commands out of sync" unless @parts[0][0] == '*' | ||
|
||
num_parts = @parts[0][1..-1].to_i * 2 + 1 | ||
return if @parts.length < num_parts | ||
|
||
command_parts = @parts[0..num_parts] | ||
@parts = @parts[num_parts..-1] | ||
|
||
# Discard length declarations | ||
command_line = | ||
command_parts | ||
.reject { |p| p[0] == '*' || p[0] == '$' } | ||
.join ' ' | ||
|
||
if IRedisMock.replies.member?(command_line) | ||
reply = IRedisMock.replies[command_line] | ||
else | ||
reply = "+OK" | ||
end | ||
|
||
# p "[#{command_line}] => [#{reply}]" | ||
|
||
IRedisMock.received << command_line | ||
|
||
if IRedisMock.paused | ||
# puts "Paused, therefore not sending [#{reply}]" | ||
else | ||
send_data "#{reply}\r\n" | ||
end | ||
end | ||
end | ||
end | ||
end |