Skip to content

Commit

Permalink
DEV: Try testing different AR versions
Browse files Browse the repository at this point in the history
  • Loading branch information
CvX committed Apr 5, 2023
1 parent b6d2bff commit 66e27bc
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 229 deletions.
64 changes: 44 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@ on:
- main

jobs:
build:
lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
bundler-cache: true

- name: Rubocop
run: bundle exec rubocop

redis:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
ruby: ['2.7', '3.0', '3.1', '3.2']
build_type: ['redis', 'active_record']
include:
- ruby: '3.2'
build_type: 'lint'

steps:
- uses: actions/checkout@v3
Expand All @@ -26,39 +37,52 @@ jobs:
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}

- name: Setup gems
run: bundle install

- name: Rubocop
if: matrix.build_type == 'lint'
run: bundle exec rubocop
bundler-cache: true

- name: Setup redis
if: matrix.build_type == 'redis'
run: sudo apt-get install redis-server

- name: Redis specs
if: matrix.build_type == 'redis'
run: bin/rspec redis

- name: Setup test app gems
if: matrix.build_type == 'active_record'
run: cd spec/support/dummy_app && bundle install
activerecord:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
ruby: ['2.7', '3.0', '3.1', '3.2']
rails: ['7-0']
include:
- ruby: '3.2'
rails: '6-0'
- ruby: '3.2'
rails: '6-1'

steps:
- uses: actions/checkout@v3

- name: Setup ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}

- name: Setup gems
run: bundle install

- name: Setup postgres
if: matrix.build_type == 'active_record'
run: |
make setup_pg
make start_pg
- name: ActiveRecord specs
if: matrix.build_type == 'active_record'
env:
GEMFILE: 'gemfiles/rails-${{ matrix.rails }}.gemfile'
run: bin/rspec active_record

publish:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: build
needs: [lint, redis, activerecord]
runs-on: ubuntu-latest

steps:
Expand Down
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
AllCops:
Exclude:
- spec/support/dummy_app/**/*
- vendor/**/*

inherit_gem:
rubocop-discourse: default.yml
Expand Down
10 changes: 6 additions & 4 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ all: redis
active_record: teardown_dummy_rails_server setup_dummy_rails_server test_active_record

test_active_record:
@ACTIVE_RECORD=1 bundle exec rspec --tag type:active_record ${RSPEC_PATH}
@ACTIVE_RECORD=1 $(BUNDLER_BIN) exec rspec --tag type:active_record ${RSPEC_PATH}

setup_dummy_rails_server:
@cd spec/support/dummy_app && bundle install --quiet && yarn install && RAILS_ENV=production $(BUNDLER_BIN) exec rails db:create db:migrate db:seed
@cd spec/support/dummy_app && BUNDLE_GEMFILE=$(GEMFILE) bundle install --quiet && yarn install && BUNDLE_GEMFILE=$(GEMFILE) RAILS_ENV=production $(BUNDLER_BIN) exec rails db:create db:migrate db:seed

start_dummy_rails_server:
@cd spec/support/dummy_app && BUNDLE_GEMFILE=Gemfile UNICORN_WORKERS=5 SECRET_KEY_BASE=somekey bundle exec unicorn -c config/unicorn.conf.rb -D -E production
@echo "start_dummy_rails_server"
@cd spec/support/dummy_app && BUNDLE_GEMFILE=$(GEMFILE) SECRET_KEY_BASE=somekey $(BUNDLER_BIN) exec unicorn -c config/unicorn.conf.rb -D -E production || cat log/unicorn.stderr.log

stop_dummy_rails_server:
@echo "stop_dummy_rails_server"
@kill -TERM $(shell cat spec/support/dummy_app/tmp/pids/unicorn.pid)

teardown_dummy_rails_server:
@cd spec/support/dummy_app && (! (bundle check > /dev/null 2>&1) || DISABLE_DATABASE_ENVIRONMENT_CHECK=1 RAILS_ENV=production $(BUNDLER_BIN) exec rails db:drop)
@cd spec/support/dummy_app && (! (BUNDLE_GEMFILE=$(GEMFILE) bundle check > /dev/null 2>&1) || BUNDLE_GEMFILE=$(GEMFILE) DISABLE_DATABASE_ENVIRONMENT_CHECK=1 RAILS_ENV=production $(BUNDLER_BIN) exec rails db:drop)
10 changes: 5 additions & 5 deletions postgresql.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ init_replica:
@chmod 0700 $(PG_REPLICA_DATA_DIR)

start_pg_primary:
@$(PG_BIN_DIR)/pg_ctl --silent --log /dev/null -w -D $(PG_PRIMARY_DATA_DIR) -o "-p $(PG_PRIMARY_PORT)" -o "-k $(PG_PRIMARY_RUN_DIR)" start
@$(PG_BIN_DIR)/pg_ctl --log /dev/stdout -w -D $(PG_PRIMARY_DATA_DIR) -o "-p $(PG_PRIMARY_PORT)" -o "-k $(PG_PRIMARY_RUN_DIR)" start || echo "nope"

start_pg_replica:
@$(PG_BIN_DIR)/pg_ctl --silent --log /dev/null -w -D $(PG_REPLICA_DATA_DIR) -o "-p $(PG_REPLICA_PORT)" -o "-k $(PG_REPLICA_RUN_DIR)" start
@$(PG_BIN_DIR)/pg_ctl --log /dev/stdout -w -D $(PG_REPLICA_DATA_DIR) -o "-p $(PG_REPLICA_PORT)" -o "-k $(PG_REPLICA_RUN_DIR)" start

restart_pg_primary:
@$(PG_BIN_DIR)/pg_ctl --silent --log /dev/null -w -D $(PG_PRIMARY_DATA_DIR) -o "-p $(PG_PRIMARY_PORT)" -o "-k $(PG_PRIMARY_RUN_DIR)" restart
@$(PG_BIN_DIR)/pg_ctl --log /dev/stdout -w -D $(PG_PRIMARY_DATA_DIR) -o "-p $(PG_PRIMARY_PORT)" -o "-k $(PG_PRIMARY_RUN_DIR)" restart

stop_pg_primary:
@$(PG_BIN_DIR)/pg_ctl --silent --log /dev/null -w -D $(PG_PRIMARY_DATA_DIR) -o "-p $(PG_PRIMARY_PORT)" -o "-k $(PG_PRIMARY_RUN_DIR)" stop
@$(PG_BIN_DIR)/pg_ctl --log /dev/stdout -w -D $(PG_PRIMARY_DATA_DIR) -o "-p $(PG_PRIMARY_PORT)" -o "-k $(PG_PRIMARY_RUN_DIR)" stop

stop_pg_replica:
@$(PG_BIN_DIR)/pg_ctl --silent --log /dev/null -w -D $(PG_REPLICA_DATA_DIR) -o "-p $(PG_REPLICA_PORT)" -o "-k $(PG_REPLICA_RUN_DIR)" stop
@$(PG_BIN_DIR)/pg_ctl --log /dev/stdout -w -D $(PG_REPLICA_DATA_DIR) -o "-p $(PG_REPLICA_PORT)" -o "-k $(PG_REPLICA_RUN_DIR)" stop

cleanup_pg:
@rm -rf $(PG_PRIMARY_DIR) $(PG_REPLICA_DIR)
1 change: 0 additions & 1 deletion rails_failover.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "byebug"
spec.add_development_dependency "redis", "~> 4.1"
spec.add_development_dependency "pg", "~> 1.2"
spec.add_development_dependency "activerecord", "~> 6.0"
spec.add_development_dependency "rack"
end
26 changes: 24 additions & 2 deletions spec/integration/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
EXPECTED_POSTS_COUNT = "100"

def start_dummy_rails_server
system("make start_dummy_rails_server")
if !system("make start_dummy_rails_server")
raise "Could not start the dummy rails server"
end
end

def stop_dummy_rails_server
Expand All @@ -15,16 +17,21 @@ def stop_dummy_rails_server

# rubocop:disable RSpec/BeforeAfterAll
before(:all) do
puts "before :all"
start_dummy_rails_server
puts "before :all 2"
end

# rubocop:disable RSpec/BeforeAfterAll
after(:all) do
puts "after :all"
stop_dummy_rails_server
puts "after :all2"
end

it "should failover to reading connection handler when PG primary " \
"is down and fallback to writing connection handler when PG primary is back up" do
puts "spec1"

response = get("/posts")

Expand All @@ -43,12 +50,17 @@ def stop_dummy_rails_server
end
ensure
system("make restart_pg_primary")
puts "spec1 end"
end

it "should be able to start with the PG primary being down" do
puts "spec2"

stop_dummy_rails_server
system("make stop_pg_primary")
start_dummy_rails_server
puts "XYZ"
puts start_dummy_rails_server
puts "ZZZ"

flood_get("/posts", times: 100) do |response|
expect(response.code.to_i).to eq(200)
Expand All @@ -63,11 +75,16 @@ def stop_dummy_rails_server
expect(response.code.to_i).to eq(200)
expect(response.body).to include("writing")
end
puts "spec2 before ensure"
ensure
puts "spec2 ensure"
system("make restart_pg_primary")
puts "spec2 end"
end

it 'supports multiple databases automatically' do
puts "spec3"

response = get("/posts?role=two_writing")

expect(response.code.to_i).to eq(200)
Expand All @@ -83,9 +100,11 @@ def stop_dummy_rails_server
end
ensure
system("make start_pg_primary")
puts "spec3 end"
end

it 'should not failover on PG server errors' do
puts "spec4"
response = get("/trigger-pg-server-error")

expect(response.code.to_i).to eq(500)
Expand All @@ -94,9 +113,11 @@ def stop_dummy_rails_server

expect(response.code.to_i).to eq(200)
expect(response.body).to include("writing")
puts "spec4 end"
end

it 'should failover if PG exception is raised before ActionDispatch::DebugExceptions' do
puts "spec5"
flood_get("/trigger-middleware-pg-exception", times: 10) do |response|
expect(response.code.to_i).to eq(500)
end
Expand All @@ -105,5 +126,6 @@ def stop_dummy_rails_server

expect(response.code.to_i).to eq(200)
expect(response.body).to include("reading")
puts "spec5 end"
end
end
15 changes: 0 additions & 15 deletions spec/support/dummy_app/Gemfile

This file was deleted.

Loading

0 comments on commit 66e27bc

Please sign in to comment.