Permalink
Browse files

Revert to black-box tests without Mocha

  • Loading branch information...
1 parent 6e3f954 commit 462d6bd7a9427704a1c53d723249c2f0a85123cf @pietern pietern committed Mar 29, 2011
View
4 Rakefile
@@ -87,9 +87,7 @@ namespace :test do
threaded_tests = [
'./test/thread_safety_test.rb',
- './test/error_replies_test.rb',
- './test/distributed_internals_test.rb',
- './test/internals_test.rb'
+ './test/error_replies_test.rb'
]
Cutest.run(Dir['./test/**/*_test.rb'] - threaded_tests)
View
2 redis.gemspec
@@ -21,6 +21,4 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.rubygems_version = %q{1.5.0}
-
- s.add_development_dependency "mocha", "~> 0.9"
end
View
13 test/commands_on_value_types_test.rb
@@ -1,6 +1,9 @@
# encoding: UTF-8
require File.expand_path("./helper", File.dirname(__FILE__))
+require File.expand_path("./redis_mock", File.dirname(__FILE__))
+
+include RedisMock::Helper
setup do
init Redis.new(OPTIONS)
@@ -75,9 +78,11 @@
assert 0 == r.dbsize
end
-test_with_mocha "FLUSHALL" do |r|
- r.client.connection.expects(:write).with [:flushall]
- r.client.connection.expects(:read).returns "FLUSHALL"
- assert "FLUSHALL" == r.flushall
+test "FLUSHALL" do
+ redis_mock(:flushall => lambda { "+FLUSHALL" }) do
+ redis = Redis.new(OPTIONS.merge(:port => 6380))
+
+ assert "FLUSHALL" == redis.flushall
+ end
end
View
16 test/helper.rb
@@ -1,7 +1,6 @@
$:.unshift File.expand_path('../lib', File.dirname(__FILE__))
require "cutest"
-require "mocha"
require "logger"
require "stringio"
@@ -10,8 +9,6 @@
rescue LoadError
end
-include Mocha::API
-
PORT = 6379
OPTIONS = {:port => PORT, :db => 15, :timeout => 3}
NODES = ["redis://127.0.0.1:6379/15"]
@@ -146,16 +143,3 @@ def assert_nothing_raised(*exceptions)
end
end
-def test_with_mocha(title, &block)
- test title do |*args|
- mocha_setup
-
- begin
- block.call(*args)
- mocha_verify
- ensure
- mocha_teardown
- end
- end
-end
-
View
29 test/internals_test.rb
@@ -35,6 +35,9 @@
# Don't use assert_raise because Timeour::Error in 1.8 inherits
# Exception instead of StandardError (on 1.9).
test "Connection timeout" do
+ # EM immediately raises CONNREFUSED
+ next if driver == :synchrony
+
result = false
begin
@@ -126,13 +129,29 @@
end
end
-test_with_mocha "Bubble EAGAIN without retrying" do |redis,log|
- redis.client.connection.stubs(:read).raises(Errno::EAGAIN).once
- assert_raise(Errno::EAGAIN) { redis.ping }
-end
-
test "Connecting to UNIX domain socket" do
assert_nothing_raised do
Redis.new(OPTIONS.merge(:path => "/tmp/redis.sock")).ping
end
end
+
+# Using a mock server in a thread doesn't work here (possibly because blocking
+# socket ops, raw socket timeouts and Ruby's thread scheduling don't mix).
+test "Bubble EAGAIN without retrying" do
+ cmd = %{(sleep 0.3; echo "+PONG\r\n") | nc -l 6380}
+ IO.popen(cmd) do |_|
+ sleep 0.1 # Give nc a little time to start listening
+ redis = Redis.connect(:port => 6380, :timeout => 0.1)
+
+ begin
+ assert_raise(Errno::EAGAIN) { redis.ping }
+ ensure
+ # Explicitly close connection so nc can quit
+ redis.client.disconnect
+
+ # Make the reactor loop do a tick to really close
+ EM::Synchrony.sleep(0) if driver == :synchrony
+ end
+ end
+end
+
View
21 test/publish_subscribe_test.rb
@@ -137,9 +137,22 @@
end
end
-test_with_mocha "SUBSCRIBE disables timeout" do |r|
- r.client.expects(:without_socket_timeout).yields
- r.subscribe "foo" do |on|
- on.subscribe { r.unsubscribe }
+test "SUBSCRIBE past a timeout" do |r|
+ # For some reason, a thread here doesn't reproduce the issue.
+ sleep = %{sleep #{OPTIONS[:timeout] + 1}}
+ publish = %{echo "publish foo bar\r\n" | nc localhost #{OPTIONS[:port]}}
+ cmd = [sleep, publish].join("; ")
+
+ IO.popen(cmd, "r+") do |pipe|
+ received = false
+
+ r.subscribe "foo" do |on|
+ on.message do |channel, message|
+ received = true
+ r.unsubscribe
+ end
+ end
+
+ assert received
end
end
View
11 test/redis_mock.rb
@@ -30,6 +30,8 @@ def self.start(port = 6380)
# Ignore client closing the connection
end
end
+ ensure
+ server.close
end
module Helper
@@ -45,9 +47,7 @@ module Helper
#
def redis_mock(replies = {})
begin
- pid = fork do
- trap("TERM") { exit }
-
+ thread = Thread.new do
RedisMock.start do |command, *args|
(replies[command.to_sym] || lambda { |*_| "+OK" }).call(*args)
end
@@ -58,10 +58,7 @@ def redis_mock(replies = {})
yield
ensure
- if pid
- Process.kill("TERM", pid)
- Process.wait(pid)
- end
+ thread.raise if thread.alive?
end
end
end
View
16 test/remote_server_control_commands_test.rb
@@ -1,6 +1,9 @@
# encoding: UTF-8
require File.expand_path("./helper", File.dirname(__FILE__))
+require File.expand_path("./redis_mock", File.dirname(__FILE__))
+
+include RedisMock::Helper
setup do
init Redis.new(OPTIONS)
@@ -49,9 +52,12 @@
assert r.debug(:object, "foo").kind_of?(String)
end
-test_with_mocha "SYNC" do |r|
- r.client.connection.expects(:write).with [:sync]
- r.client.connection.expects(:read).returns "OK"
- assert "OK" == r.sync
-end
+test "SYNC" do |r|
+ replies = {:sync => lambda { "+OK" }}
+
+ redis_mock(replies) do
+ redis = Redis.new(OPTIONS.merge(:port => 6380))
+ assert "OK" == redis.sync
+ end
+end

0 comments on commit 462d6bd

Please sign in to comment.