Skip to content

Commit

Permalink
feat: parallel rspec tests in CI
Browse files Browse the repository at this point in the history
Adds parallel_tests gem to make tests parallel in CI
  • Loading branch information
elasticspoon committed Apr 21, 2024
1 parent c652ecc commit c8855fd
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ on:
paths-ignore:
- "doc/**"
- "**/*.md"
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
docker:
Expand Down
53 changes: 38 additions & 15 deletions .github/workflows/rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ on:

jobs:
rspec:
name: RSpec Groups ${{ matrix.ci_job_index }}
runs-on: ubuntu-latest
env:
RAILS_ENV: test

BUNDLE_WITHOUT: "development"
CI_TOTAL_JOBS: ${{ matrix.ci_total_jobs }}
CI_JOB_INDEX: ${{ matrix.ci_job_index }}
GROUPS_COMMA: ${{ join(fromJSON(matrix.ci_job_index), ',') }}
GROUPS_UNDERSCORE: ${{ join(fromJSON(matrix.ci_job_index), '_') }}
services:
db:
image: postgres:14.8
Expand All @@ -29,6 +34,20 @@ jobs:
--health-interval 10s
--health-timeout 5s
--health-retries 5
strategy:
fail-fast: false
matrix:
# Set N number of parallel jobs you want to run.
# Normally equal to the number of CPU cores but in this case relates to the total number of test groups to be run across all runners.
ci_total_jobs: [12]
# Remember to update ci_node_index below to 0..N-1
# When you run 2 parallel jobs then first job will have index 0, the second job will have index 1 etc. For larger runners runners with more CPU adjust accordingly.
ci_job_index:
[
"[0, 1, 2, 3]",
"[4, 5, 6, 7]",
"[8, 9, 10, 11]",
]

steps:
- uses: actions/checkout@v4
Expand All @@ -42,42 +61,46 @@ jobs:
run: |
sudo apt-get -yqq install libpq-dev
- name: Build App
- name: Setup Parallel Database
env:
RAILS_ENV: test
POSTGRES_HOST: localhost
DATABASE_HOST: localhost
POSTGRES_USER: postgres
CASA_DATABASE_PASSWORD: password
POSTGRES_PASSWORD: password
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_PORT: 5432
run: |
echo "setting up database"
bundle exec rake parallel:create
bundle exec rake parallel:rake[db:schema:load]
echo "done"
- name: Build App
run: |
yarn
bundle exec rake db:create
bundle exec rake db:schema:load
bundle exec rails css:build
bundle exec rails javascript:build
- name: Run rspec and upload code coverage
- name: Run rspec group ${{ matrix.ci_job_index }}
env:
RAILS_ENV: test
POSTGRES_HOST: localhost
DATABASE_HOST: localhost
POSTGRES_USER: postgres
CASA_DATABASE_PASSWORD: password
POSTGRES_PASSWORD: password
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_PORT: 5432
RUN_SIMPLECOV: true
CC_TEST_REPORTER_ID: 31464536e34ab26588cb951d0fa6b5898abdf401dbe912fd47274df298e432ac
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
RUBYOPT='-W:no-deprecated -W:no-experimental' bundle exec rspec
./cc-test-reporter after-build --exit-code $?
bundle exec parallel_rspec -n "${CI_TOTAL_JOBS}" --only-group "${CI_JOB_INDEX}" ./spec
- name: Archive selenium screenshots
- name: Upload Selenium Screenshots
if: ${{ failure() }}
uses: actions/upload-artifact@v4
with:
name: selenium-screenshots
path: |
${{ github.workspace }}/tmp/capybara/*.png
${{ github.workspace }}/tmp/capybara/*.html
name: screenshots_${{ env.GROUPS_UNDERSCORE }}
path: ${{ github.workspace }}/tmp/screenshots${{ env.GROUPS_UNDERSCORE }}/
4 changes: 4 additions & 0 deletions .rspec_parallel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--format progress
--format ParallelTests::RSpec::RuntimeLogger --out tmp/parallel_runtime_rspec.log
--format ParallelTests::RSpec::SummaryLogger --out tmp/spec_summary.log
--format ParallelTests::RSpec::FailuresLogger --out tmp/failing_specs.log
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ group :development, :test do
gem "rswag-specs"
gem "shoulda-matchers"
gem "standard", "~> 1.31.0"
gem "parallel_tests"
end

group :development do
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ GEM
bigdecimal (>= 3.0)
orm_adapter (0.5.0)
parallel (1.23.0)
parallel_tests (4.6.1)
parallel
paranoia (2.6.3)
activerecord (>= 5.1, < 7.2)
parser (3.2.2.3)
Expand Down Expand Up @@ -606,6 +608,7 @@ DEPENDENCIES
net-smtp
noticed
oj
parallel_tests
paranoia
pdf-forms
pg
Expand Down
2 changes: 1 addition & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ development:

test:
<<: *default
database: casa_test
database: casa_test<%= ENV["TEST_ENV_NUMBER"] %>
host: <%= ENV.fetch("DATABASE_HOST") { } %>

production:
Expand Down
2 changes: 1 addition & 1 deletion config/storage.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
root: <%= Rails.root.join("tmp/storage#{ENV['TEST_ENV_NUMBER']}") %>

local:
service: Disk
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/tasks/emancipation_checklist_reminder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "rake"
Rake.application.rake_require "tasks/emancipation_checklist_reminder"

RSpec.describe "lib/tasks/emancipation_checklist_reminder.rake" do
RSpec.describe "lib/tasks/emancipation_checklist_reminder.rake", ci_only: true do
let(:rake_task) { Rake::Task["emancipation_checklist_reminder"].invoke }

before do
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/tasks/volunteer_birthday_reminder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "rake"
Rake.application.rake_require "tasks/volunteer_birthday_reminder"

RSpec.describe "lib/tasks/volunteer_birthday_reminder.rake" do
RSpec.describe "lib/tasks/volunteer_birthday_reminder.rake", ci_only: true do
let(:rake_task) { Rake::Task["volunteer_birthday_reminder"].invoke }

before do
Expand Down
9 changes: 9 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,13 @@
def pre_transition_aged_youth_age
Date.current - CasaCase::TRANSITION_AGE.years
end

config.around do |example|
Capybara.server_port = 7654 + ENV["TEST_ENV_NUMBER"].to_i
example.run
end

unless ENV["GITHUB_ACTIONS"]
config.filter_run_excluding :ci_only
end
end
10 changes: 10 additions & 0 deletions spec/support/capybara.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
# disable CSS transitions and js animations
Capybara.disable_animation = true

Capybara::Screenshot.autosave_on_failure = true

module Capybara
module Screenshot
def self.capybara_tmp_path
Rails.root.join("tmp", "screenshots#{ENV["GROUPS_UNDERSCORE"]}")
end
end
end

options = Selenium::WebDriver::Chrome::Options.new
options.add_argument("--disable-gpu")
options.add_argument("--ignore-certificate-errors")
Expand Down
2 changes: 1 addition & 1 deletion spec/support/download_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module DownloadHelpers
TIMEOUT = 10
PATH = Rails.root.join("tmp/downloads")
PATH = Rails.root.join("tmp/downloads#{ENV["TEST_ENV_NUMBER"]}")

def downloads
Dir[PATH.join("*")]
Expand Down

0 comments on commit c8855fd

Please sign in to comment.