Skip to content

Commit

Permalink
Add some basic end-to-end tests
Browse files Browse the repository at this point in the history
Part of #1
  • Loading branch information
agis committed Jul 17, 2020
1 parent b69b8e1 commit 7bf72b9
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.gem
dump.rdb
Gemfile.lock
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "rake/testtask"

Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/test*.rb']
t.verbose = true
end

task default: :test
4 changes: 3 additions & 1 deletion rspecq.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Gem::Specification.new do |s|
s.add_dependency "rspec-core"
s.add_dependency "redis"

s.add_development_dependency "minitest", "~> 5.14"
s.add_development_dependency "rake"
s.add_development_dependency "pry-byebug"
s.add_development_dependency "minitest"
s.add_development_dependency "rspec"
end
45 changes: 45 additions & 0 deletions test/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "securerandom"

module Helpers
REDIS_HOST = "127.0.0.1".freeze

def rand_id
SecureRandom.hex(4)
end

def exec_build(path, args="")
worker_id = rand_id
build_id = rand_id

Dir.chdir(File.join("test", "sample_suites", path)) do
out = `bundle exec rspecq --worker #{worker_id} --build #{build_id} #{args}`
puts out if ENV["RSPECQ_DEBUG"]
end

assert_equal 0, $?.exitstatus

queue = RSpecQ::Queue.new(build_id, worker_id, REDIS_HOST)
assert_queue_well_formed(queue)

return queue
end

def assert_queue_well_formed(queue, msg=nil)
redis = queue.redis
heartbeats = redis.zrange(
queue.send(:key_worker_heartbeats), 0, -1, withscores: true)

assert queue.published?
assert queue.exhausted?
assert_operator heartbeats.size, :>=, 0
assert heartbeats.all? { |hb| Time.at(hb.last) <= Time.now }
end

def assert_build_not_flakey(queue)
assert_empty queue.requeued_jobs
end

def assert_processed_jobs(exp, queue)
assert_equal exp.sort, queue.processed_jobs.sort
end
end
1 change: 1 addition & 0 deletions test/sample_suites/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sample RSpec suites, used as fixtures in the tests.
5 changes: 5 additions & 0 deletions test/sample_suites/different_spec_path/mytests/foo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RSpec.describe do
it "I should not be executed!" do
expect(1).to eq 2
end
end
13 changes: 13 additions & 0 deletions test/sample_suites/different_spec_path/mytests/qwe_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
RSpec.describe do
context "foo" do
describe "abc" do
it { expect(false).to be false }
end
end

context "bar" do
describe "dfg" do
it { expect(true).to be true }
end
end
end
4 changes: 4 additions & 0 deletions test/sample_suites/failing_suite/spec/bar_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RSpec.describe do
it { expect(false).to be false }
it { expect(1).to be 2 }
end
3 changes: 3 additions & 0 deletions test/sample_suites/failing_suite/spec/foo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RSpec.describe do
it { expect(true).to be true }
end
8 changes: 8 additions & 0 deletions test/sample_suites/flakey_suite/spec/foo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
RSpec.describe do
it do
$tries ||= 0
$tries += 1

expect($tries).to eq 3
end
end
3 changes: 3 additions & 0 deletions test/sample_suites/passing_suite/spec/foo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RSpec.describe do
it { expect(true).to be true }
end
57 changes: 57 additions & 0 deletions test/test_e2e.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require "minitest/autorun"
require "rspecq"
require "helpers"

class TestEndToEnd < Minitest::Test
include Helpers

def setup
Redis.new(host: REDIS_HOST).flushdb
end

def test_suite_with_legit_failures
queue = exec_build("failing_suite")

refute queue.build_successful?

assert_processed_jobs [
"./spec/foo_spec.rb",
"./spec/bar_spec.rb",
"./spec/bar_spec.rb[1:2]",
], queue

assert_equal 3+RSpecQ::MAX_REQUEUES, queue.example_count

assert_equal({ "./spec/bar_spec.rb[1:2]" => "3" }, queue.requeued_jobs)
end

def test_passing_suite
queue = exec_build("passing_suite")

assert queue.build_successful?
assert_build_not_flakey(queue)
assert_equal 1, queue.example_count
assert_equal ["./spec/foo_spec.rb"], queue.processed_jobs
end

def test_flakey_suite
queue = exec_build("flakey_suite")

assert queue.build_successful?
assert_processed_jobs [
"./spec/foo_spec.rb",
"./spec/foo_spec.rb[1:1]",
], queue

assert_equal({ "./spec/foo_spec.rb[1:1]" => "2" }, queue.requeued_jobs)
end

def test_scheduling_by_file_and_custom_spec_path
queue = exec_build("different_spec_path", "mytests/qwe_spec.rb")

assert queue.build_successful?
assert_build_not_flakey(queue)
assert_equal 2, queue.example_count
assert_processed_jobs ["./mytests/qwe_spec.rb"], queue
end
end

0 comments on commit 7bf72b9

Please sign in to comment.