Skip to content

Commit

Permalink
Migrate to megatest
Browse files Browse the repository at this point in the history
And add support for test parallelization, which speeds up
the test suite quite significantly.
  • Loading branch information
byroot committed Aug 23, 2024
1 parent a6a920c commit 5012e04
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 83 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
/hiredis-client/pkg/
/spec/reports/
/tmp/
/log/

/hiredis-client/tmp/
/bin/toxiproxy-server
*.so
Expand Down
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Lint/UnderscorePrefixedVariableName:
Lint/EmptyBlock:
Enabled: false

Lint/ShadowedException:
Enabled: false

Lint/DuplicateBranch:
Enabled: false

Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source "https://rubygems.org"
# Specify your gem's dependencies in redis-client.gemspec
gemspec name: "redis-client"

gem "minitest"
gem "megatest", github: "byroot/megatest"
gem "rake", "~> 13.2"
gem "rake-compiler"
gem "rubocop"
Expand Down
9 changes: 7 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
GIT
remote: https://github.com/byroot/megatest.git
revision: 3f9c2688571ccc451086924f314c3c8dba3b02dc
specs:
megatest (0.1.1)

PATH
remote: .
specs:
Expand All @@ -15,7 +21,6 @@ GEM
hiredis (0.6.3-java)
json (2.7.1)
json (2.7.1-java)
minitest (5.25.1)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
Expand Down Expand Up @@ -58,7 +63,7 @@ DEPENDENCIES
benchmark-ips
byebug
hiredis
minitest
megatest!
rake (~> 13.2)
rake-compiler
redis (~> 4.6)
Expand Down
36 changes: 18 additions & 18 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "rake/extensiontask"
require "rake/testtask"
require 'rubocop/rake_task'

RuboCop::RakeTask.new
Expand All @@ -21,29 +20,30 @@ Rake::ExtensionTask.new do |ext|
CLEAN.add("#{ext.ext_dir}/vendor/*.{a,o}")
end

require "megatest/test_task"

namespace :test do
Rake::TestTask.new(:ruby) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"].exclude("test/sentinel/*_test.rb")
t.options = '-v' if ENV['CI'] || ENV['VERBOSE']
end
jobs = ENV["JOBS"] || "4"
jobs = jobs && Process.respond_to?(:fork) ? ["-j", jobs] : []
extra_args = ["--max-retries", "1"] + jobs

Rake::TestTask.new(:sentinel) do |t|
t.libs << "test/sentinel"
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/sentinel/*_test.rb"]
t.options = '-v' if ENV['CI'] || ENV['VERBOSE']
Megatest::TestTask.create(:ruby) do |t|
t.tests = FileList["test/**/*_test.rb"].exclude("test/sentinel/*_test.rb")
t.extra_args = extra_args
end

Rake::TestTask.new(:hiredis) do |t|
Megatest::TestTask.create(:hiredis) do |t|
t.libs << "test/hiredis"
t.libs << "test"
t.libs << "hiredis-client/lib"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"].exclude("test/sentinel/*_test.rb")
t.options = '-v' if ENV['CI'] || ENV['VERBOSE']
t.tests = FileList["test/hiredis/test_helper.rb"] + FileList["test/**/*_test.rb"].exclude("test/sentinel/*_test.rb")
t.extra_args = extra_args
t.deps << :compile
end

Megatest::TestTask.create(:sentinel) do |t|
t.libs << "test/sentinel"
t.tests = ["test/sentinel"]
t.extra_args = extra_args
end
end

Expand Down
27 changes: 27 additions & 0 deletions bin/megatest
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'megatest' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("megatest", "megatest")
7 changes: 6 additions & 1 deletion test/hiredis/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
unless RUBY_PLATFORM == "java"
require "redis"
require "hiredis"
Redis.new(host: Servers::HOST, port: Servers::REDIS.port, driver: :hiredis).ping

begin
Redis.new(driver: :hiredis).ping
rescue
nil # does not matter, we just want to load the library
end
end

require "hiredis-client"
Expand Down
2 changes: 2 additions & 0 deletions test/redis_client/ractor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "test_helper"

class RactorTest < RedisClientTestCase
tag isolated: true

def setup
skip("Ractors are not supported on this Ruby version") unless defined?(::Ractor)
skip("Hiredis is not Ractor safe") if RedisClient.default_driver.name == "RedisClient::HiredisConnection"
Expand Down
38 changes: 28 additions & 10 deletions test/sentinel/sentinel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,30 @@ class SentinelTest < RedisClientTestCase
include ClientTestHelper

def setup
@config = new_config
super
@config = new_config
end

def check_server
retried = false
begin
@redis = @config.new_client
@redis.call("FLUSHDB")
rescue
if retried
raise
else
retried = true
Servers::SENTINEL_TESTS.reset
retry
end
end
end

def teardown
Servers::SENTINEL_TESTS.reset
super
end

def test_new_client
Expand Down Expand Up @@ -86,9 +104,9 @@ def test_unknown_master
def test_unresolved_config
client = @config.new_client

@config.stub(:server_url, -> { raise ConnectionError.with_config('this should not be called', @config) }) do
@config.stub(:resolved?, false) do
client.stub(:call, ->(_) { raise ConnectionError.with_config('call error', @config) }) do
stub(@config, :server_url, -> { raise ConnectionError.with_config('this should not be called', @config) }) do
stub(@config, :resolved?, false) do
stub(client, :call, ->(_) { raise ConnectionError.with_config('call error', @config) }) do
error = assert_raises ConnectionError do
client.call('PING')
end
Expand All @@ -108,7 +126,7 @@ def test_master_failover_not_ready
[["SENTINEL", "get-master-addr-by-name", "cache"], [Servers::REDIS_REPLICA.host, Servers::REDIS_REPLICA.port.to_s]],
sentinel_refresh_command_mock,
])
@config.stub(:sentinel_client, ->(_config) { sentinel_client_mock }) do
stub(@config, :sentinel_client, ->(_config) { sentinel_client_mock }) do
client = @config.new_client
assert_raises FailoverError do
client.call("PING")
Expand All @@ -126,7 +144,7 @@ def test_master_failover_ready
replica = RedisClient.new(host: Servers::REDIS_REPLICA.host, port: Servers::REDIS_REPLICA.port)
assert_equal "OK", replica.call("REPLICAOF", "NO", "ONE")

@config.stub(:sentinel_client, ->(_config) { sentinel_client_mock }) do
stub(@config, :sentinel_client, ->(_config) { sentinel_client_mock }) do
client = @config.new_client
assert_equal "PONG", client.call("PING")

Expand All @@ -145,7 +163,7 @@ def test_no_replicas
sentinel_client_mock = SentinelClientMock.new([
[["SENTINEL", "replicas", "cache"], []],
] * tries)
@config.stub(:sentinel_client, ->(_config) { sentinel_client_mock }) do
stub(@config, :sentinel_client, ->(_config) { sentinel_client_mock }) do
assert_raises ConnectionError do
@config.new_client.call("PING")
end
Expand All @@ -159,7 +177,7 @@ def test_replica_failover_not_ready
[["SENTINEL", "replicas", "cache"], [response_hash("ip" => Servers::REDIS.host, "port" => Servers::REDIS.port.to_s, "flags" => "slave")]],
[["SENTINEL", "replicas", "cache"], [response_hash("ip" => Servers::REDIS.host, "port" => Servers::REDIS.port.to_s, "flags" => "slave")]],
])
@config.stub(:sentinel_client, ->(_config) { sentinel_client_mock }) do
stub(@config, :sentinel_client, ->(_config) { sentinel_client_mock }) do
client = @config.new_client
assert_equal "PONG", client.call("PING")

Expand All @@ -180,7 +198,7 @@ def test_replica_failover_ready
[["SENTINEL", "replicas", "cache"], [response_hash("ip" => Servers::REDIS_REPLICA.host, "port" => Servers::REDIS_REPLICA.port.to_s, "flags" => "slave")]],
])

@config.stub(:sentinel_client, ->(_config) { sentinel_client_mock }) do
stub(@config, :sentinel_client, ->(_config) { sentinel_client_mock }) do
client = @config.new_client
assert_equal "PONG", client.call("PING")
end
Expand All @@ -199,7 +217,7 @@ def test_successful_connection_refreshes_sentinels_list
additional_sentinels: [response_hash("ip" => new_sentinel_ip, "port" => new_sentinel_port.to_s)],
),
])
@config.stub(:sentinel_client, ->(_config) { sentinel_client_mock }) do
stub(@config, :sentinel_client, ->(_config) { sentinel_client_mock }) do
client = @config.new_client
assert_equal "PONG", client.call("PING")
end
Expand All @@ -226,7 +244,7 @@ def test_sentinel_refresh_password
),
])

@config.stub(:sentinel_client, ->(_config) { sentinel_client_mock }) do
stub(@config, :sentinel_client, ->(_config) { sentinel_client_mock }) do
client = @config.new_client
assert_equal "PONG", client.call("PING")
end
Expand Down
4 changes: 1 addition & 3 deletions test/sentinel/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@

Servers.build_redis
Servers::SENTINEL_TESTS.prepare

require "minitest/autorun"
Minitest.after_run { Servers::TESTS.shutdown }
Servers.all = Servers::SENTINEL_TESTS
13 changes: 12 additions & 1 deletion test/support/client_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@ def setup
private

def check_server
@redis.call("flushdb", "async")
retried = false
begin
@redis.call("flushdb", "async")
rescue
if retried
raise
else
retried = true
Servers.reset
retry
end
end
end

def travel(seconds)
Expand Down
Loading

0 comments on commit 5012e04

Please sign in to comment.