Skip to content

Commit

Permalink
worker: 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 16, 2020
1 parent deee40b commit bd57d73
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.gem
dump.rdb
Gemfile.lock
spec/examples.txt
1 change: 1 addition & 0 deletions rspecq.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ Gem::Specification.new do |s|
s.add_dependency "redis"

s.add_development_dependency "rake"
s.add_development_dependency "pry-byebug"
s.add_development_dependency "rspec", "~> 3.9"
end
1 change: 1 addition & 0 deletions sample_suites/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO
4 changes: 4 additions & 0 deletions 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 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 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 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
52 changes: 52 additions & 0 deletions spec/e2e_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# TODO check process exit code
# TODO test empty no spec files scenario
# TODO test providing a specific file
# TODO test providing a specific example (foo[2:1]) notation
# TODO test providing a different spec folder
# TODO test worker liveness
# TODO reporter
# TODO concurrent workers

RSpec.describe RSpecQ do
context "suite with legit failures" do
it "retries failed examples and build fails" do
queue = exec_build("failing_suite")

expect(queue).to be_well_formed
expect(queue.build_successful?).to be false
expect(queue.example_count).to eq 6
expect(queue.requeued_jobs).to eq({ "./spec/bar_spec.rb[1:2]" => "3" })
expect(queue.processed_jobs).to match_array([
"./spec/foo_spec.rb",
"./spec/bar_spec.rb",
"./spec/bar_spec.rb[1:2]",
])
end
end

context "passing suite" do
it do
queue = exec_build("passing_suite")

expect(queue).to be_well_formed
expect(queue.build_successful?).to be true
expect(queue.example_count).to eq 1
expect(queue.requeued_jobs).to be_empty
expect(queue.processed_jobs).to match_array(["./spec/foo_spec.rb"])
end
end

context "suite with flakey failures" do
it "passes" do
queue = exec_build("flakey_suite")

expect(queue).to be_well_formed
expect(queue.build_successful?).to be true
expect(queue.processed_jobs).to match_array([
"./spec/foo_spec.rb",
"./spec/foo_spec.rb[1:1]",
])
expect(queue.requeued_jobs).to eq({ "./spec/foo_spec.rb[1:1]" => "2" })
end
end
end
39 changes: 30 additions & 9 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
require "rspecq"
require "support/helpers"
require "pry-byebug"

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.include Helpers

config.before(:example) do |t|
Redis.new(host: Helpers::REDIS_HOST).flushdb
end

config.define_derived_metadata do |meta|
meta[:aggregate_failures] = true
end

# Everything from that point on is the default configuration
#
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
Expand Down Expand Up @@ -81,7 +87,7 @@
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
#config.profile_examples = 10

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
Expand All @@ -95,3 +101,18 @@
# as the one that triggered the failure.
Kernel.srand config.seed
end

# Performs some basic sanity checks regarding the queue's Redis state
RSpec::Matchers.define :be_well_formed do |expected|
match do |queue|
redis = queue.redis
heartbeats = redis.zrange(
queue.send(:key_worker_heartbeats), 0, -1, withscores: true)

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

20 changes: 20 additions & 0 deletions spec/support/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "securerandom"

module Helpers
REDIS_HOST = "127.0.0.1".freeze

def rand_id
SecureRandom.hex(4)
end

def exec_build(path)
worker_id = rand_id
build_id = rand_id

Dir.chdir(File.join("sample_suites", path)) do
`bundle exec rspecq --worker #{worker_id} --build #{build_id}`
end

return RSpecQ::Queue.new(build_id, worker_id, REDIS_HOST)
end
end

0 comments on commit bd57d73

Please sign in to comment.