Skip to content

Commit

Permalink
Implement wait_for_db
Browse files Browse the repository at this point in the history
  • Loading branch information
mirakui committed Oct 20, 2024
1 parent 08941a3 commit add26f4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@ jobs:
- uses: actions/checkout@v4
- name: docker compose up
run: docker compose -f compose-ci.yaml up -d
- name: wait for db # FIXME
run: sleep 10
- name: Run integration test
run: docker compose -f compose-ci.yaml exec ruby bundle exec appraisal ${{ matrix.appraisal }} rspec spec/integration/*_spec.rb
8 changes: 6 additions & 2 deletions spec/integration/mysql2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

context "MySQL (AR#{ar_version})" do
before(:all) do
host = ENV.fetch('MYSQL_HOST', '127.0.0.1')
port = ENV.fetch('MYSQL_PORT', '23306').to_i
wait_for_db(host, port)

ActiveRecord::Base.establish_connection(
adapter: 'mysql2',
host: ENV.fetch('MYSQL_HOST', '127.0.0.1'),
port: ENV.fetch('MYSQL_PORT', '23306').to_i,
host: host,
port: port,
database: 'arproxy_test',
username: 'arproxy',
password: ENV.fetch('ARPROXY_DB_PASSWORD')
Expand Down
8 changes: 6 additions & 2 deletions spec/integration/postgresql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

context "PostgreSQL (AR#{ar_version})" do
before(:all) do
host = ENV.fetch('POSTGRES_HOST', '127.0.0.1')
port = ENV.fetch('POSTGRES_PORT', '25432').to_i
wait_for_db(host, port)

ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: ENV.fetch('POSTGRES_HOST', '127.0.0.1'),
port: ENV.fetch('POSTGRES_PORT', '25432').to_i,
host: host,
port: port,
database: 'arproxy_test',
username: 'arproxy',
password: ENV.fetch('ARPROXY_DB_PASSWORD')
Expand Down
9 changes: 7 additions & 2 deletions spec/integration/sqlserver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
'active_record/connection_adapters/sqlserver_adapter'
)
end

host = ENV.fetch('MSSQL_HOST', '127.0.0.1')
port = ENV.fetch('MSSQL_PORT', '21433').to_i
wait_for_db(host, port)

ActiveRecord::Base.establish_connection(
adapter: 'sqlserver',
host: ENV.fetch('MSSQL_HOST', '127.0.0.1'),
port: ENV.fetch('MSSQL_PORT', '21433').to_i,
host: host,
port: port,
database: 'arproxy_test',
username: 'arproxy',
password: ENV.fetch('ARPROXY_DB_PASSWORD')
Expand Down
8 changes: 6 additions & 2 deletions spec/integration/trilogy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

context "Trilogy (AR#{ar_version})", if: ActiveRecord.version >= '7.1' do
before(:all) do
host = ENV.fetch('MYSQL_HOST', '127.0.0.1')
port = ENV.fetch('MYSQL_PORT', '23306').to_i
wait_for_db(host, port)

mysql_data_dir = File.expand_path('../../db/mysql/data', __dir__)
ActiveRecord::Base.establish_connection(
adapter: 'trilogy',
host: ENV.fetch('MYSQL_HOST', '127.0.0.1'),
port: ENV.fetch('MYSQL_PORT', '23306').to_i,
host: host,
port: port,
ssl: true,
ssl_mode: Trilogy::SSL_VERIFY_CA,
tls_min_version: Trilogy::TLS_VERSION_12,
Expand Down
16 changes: 16 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,19 @@ def cleanup_activerecord
ActiveRecord::Base.descendants.each(&:reset_column_information)
ActiveRecord::Base.connection.schema_cache.clear!
end

def wait_for_db(host, port, interval = 0.2, timeout = 10)
print "\nWaiting for DB on #{host}:#{port}..." if ENV['DEBUG']
Timeout.timeout(timeout) do
loop do
TCPSocket.new(host, port).close
puts 'ok' if ENV['DEBUG']
break
rescue Errno::ECONNREFUSED
print '.' if ENV['DEBUG']
sleep interval
end
end
rescue Timeout::Error
raise "Timeout waiting for DB on #{host}:#{port}"
end

0 comments on commit add26f4

Please sign in to comment.