Skip to content

Commit

Permalink
DEV: Fix a flaky spec
Browse files Browse the repository at this point in the history
This patch removes the random order on specs and changes how we test for
a PG exception.
  • Loading branch information
Flink committed May 24, 2023
1 parent 137ee85 commit 642ff0a
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
--color
--require spec_helper
--order rand
--format documentation
3 changes: 0 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,3 @@ stop_dummy_rails_server:

teardown_dummy_rails_server:
@cd spec/support/dummy_app && (! (bundle check > /dev/null 2>&1) || BUNDLE_GEMFILE=Gemfile DISABLE_DATABASE_ENVIRONMENT_CHECK=1 RAILS_ENV=production $(BUNDLER_BIN) exec rails db:drop)

db_seed_dummy_rails_server:
@cd spec/support/dummy_app && BUNDLE_GEMFILE=Gemfile RAILS_ENV=production $(BUNDLER_BIN) exec rails db:seed
25 changes: 15 additions & 10 deletions spec/integration/active_record_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "spec_helper"
require "fileutils"

RSpec.describe "ActiveRecord failover", type: :active_record do
EXPECTED_POSTS_COUNT = "100"
Expand All @@ -15,7 +16,6 @@ def stop_dummy_rails_server

def restart_dummy_rails_server
stop_dummy_rails_server
system("make db_seed_dummy_rails_server")
start_dummy_rails_server
end

Expand Down Expand Up @@ -97,17 +97,22 @@ def restart_dummy_rails_server
expect(response.body).to include("writing")
end

it "should failover if PG exception is raised before ActionDispatch::DebugExceptions" do
flood_get("/trigger-middleware-pg-exception", times: 10) do |response|
expect(response.code.to_i).to eq(500)
context "when PG exception is raised before ActionDispatch::DebugExceptions" do
let(:path) do
Pathname.new("#{__dir__}/../support/dummy_app/triggered_from_pg_exception.writing")
end

sleep 0.5
response = get("/posts")
after { FileUtils.rm_f(path) }

expect(response.code.to_i).to eq(200)
expect(response.body).to include("triggered_from_pg_exception:writing")
ensure
restart_dummy_rails_server
it "fails over" do
flood_get("/trigger-middleware-pg-exception", times: 10) do |response|
expect(response.code.to_i).to eq(500)
end

sleep 0.5
response = get("/posts")
expect(response.code.to_i).to eq(200)
expect(path.exist?).to be true
end
end
end
1 change: 0 additions & 1 deletion spec/support/dummy_app/app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class PostsController < ApplicationController
def index
@posts_count = Post.count
@role = request.env["rails_failover.role"]
@last_post_body = Post.last.body
end

def trigger_pg_server_error
Expand Down
1 change: 0 additions & 1 deletion spec/support/dummy_app/app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<%= @role %>
<%= @posts_count %>
<%= @last_post_body %>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(app)
def call(env)
if env["REQUEST_PATH"] == "/trigger-middleware-pg-exception"
RailsFailover::ActiveRecord.on_failover do |role|
Post.create!(body: "triggered_from_pg_exception:#{role}")
FileUtils.touch("#{Rails.root}/triggered_from_pg_exception.#{role}")
end
raise ::PG::UnableToSend
else
Expand Down
1 change: 0 additions & 1 deletion spec/support/dummy_app/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)

Post.delete_all
100.times { |i| Post.create!(body: "a" * i) }

0 comments on commit 642ff0a

Please sign in to comment.