diff --git a/.github/workflows/cross-platform.yml b/.github/workflows/cross-platform.yml index 96d63d1..289579f 100644 --- a/.github/workflows/cross-platform.yml +++ b/.github/workflows/cross-platform.yml @@ -153,6 +153,16 @@ jobs: - name: Generate the E2E examples with the WASM run: ruby scripts/tests/generate_examples.rb "${{ matrix.ruby-version }}" sqlc.ci.yaml --generate-gemfile + # the tests TRUNCATE a dozen tables in every setup and need no durability: with fsync on, a flush that stalls on a + # macOS runner freezes the suite (both macOS E2E jobs hung inside a setup's TRUNCATEs and were cancelled after 10 minutes) + - name: Turn off PostgreSQL durability (tests only) + run: | + psql -v ON_ERROR_STOP=1 -h "${POSTGRES_HOST}" -U "${DB_USER}" -d "${DB_NAME}" \ + -c 'ALTER SYSTEM SET fsync = off' \ + -c 'ALTER SYSTEM SET synchronous_commit = off' \ + -c 'ALTER SYSTEM SET full_page_writes = off' \ + -c 'SELECT pg_reload_conf()' + - name: Create the schemas (and allow LOAD DATA LOCAL INFILE for :copyfrom) run: | psql -v ON_ERROR_STOP=1 -h "${POSTGRES_HOST}" -U "${DB_USER}" -d "${DB_NAME}" -f examples/authors/postgresql/schema.sql @@ -184,24 +194,28 @@ jobs: - name: End-to-End tests (SQLite) if: ${{ !cancelled() }} + timeout-minutes: 4 env: EXAMPLES_DRIVER: sqlite3 run: bundle install && bundle exec rake end2end_sqlite3 - name: End-to-End tests (PostgreSQL ${{ matrix.postgres-version }}) if: ${{ !cancelled() }} + timeout-minutes: 4 env: EXAMPLES_DRIVER: pg run: bundle install && bundle exec rake end2end_pg - name: End-to-End tests (MySQL 8.4) if: ${{ !cancelled() }} + timeout-minutes: 5 env: EXAMPLES_DRIVER: mysql2 run: bundle install && bundle exec rake end2end_mysql2 - name: Generator options E2E if: ${{ !cancelled() }} + timeout-minutes: 3 env: BUNDLE_GEMFILE: ${{ github.workspace }}/Gemfile.options.e2e # default gem path (only prebuilt gems): with BUNDLE_PATH, the test's nested `bundle install` on Windows Ruby diff --git a/docs/Testing.md b/docs/Testing.md index 0459f7f..1260d35 100644 --- a/docs/Testing.md +++ b/docs/Testing.md @@ -57,6 +57,12 @@ The servers are configured with `DB_NAME`, `DB_USER`, `DB_PASS`, `POSTGRES_HOST` `*_PORT`). The MySQL tests connect without a password (`DB_PASS` is only used for PostgreSQL), so use a MySQL user that has none. +A test that runs longer than `E2E_TEST_TIMEOUT` seconds (default 120; the slowest real test takes a few seconds) is +reported by `tests/hang_watchdog.rb`: the test, every thread's backtrace and, for PostgreSQL, the server's sessions and +the locks nobody got. The process then exits with status 1, so a stuck suite fails with a diagnosis instead of CI +cancelling the whole job. The cross-platform workflow also gives each E2E step its own `timeout-minutes` and turns off +PostgreSQL's `fsync` / `synchronous_commit` / `full_page_writes` (the tests need no durability). + What each test proves is listed in [Coverage](Coverage.md). ## CI workflows (`.github/workflows/`) diff --git a/tests/end2end_mysql2.rb b/tests/end2end_mysql2.rb index 1c0ec72..50ecf00 100755 --- a/tests/end2end_mysql2.rb +++ b/tests/end2end_mysql2.rb @@ -13,6 +13,7 @@ require_relative 'consts' require_relative 'rbs_types' require_relative 'app_helpers' +require_relative 'hang_watchdog' require 'delegate' require File.join(EXAMPLES_DIR, 'mysql2/query_sql') RbsTypes.install(Object.const_get("#{ENV.fetch('EXAMPLES_MODULE', 'Mysql2Codegen')}::QuerySql"), diff --git a/tests/end2end_pg.rb b/tests/end2end_pg.rb index fb8a8ac..c8b4755 100755 --- a/tests/end2end_pg.rb +++ b/tests/end2end_pg.rb @@ -14,6 +14,7 @@ require_relative 'consts' require_relative 'rbs_types' require_relative 'app_helpers' +require_relative 'hang_watchdog' require 'delegate' require 'json' require 'uri' @@ -21,6 +22,22 @@ RbsTypes.install(Object.const_get("#{ENV.fetch('EXAMPLES_MODULE', 'PgCodegen')}::QuerySql"), File.join(EXAMPLES_DIR, 'pg/query_sql.rbs')) +# a hanging test also reports the server's sessions and the locks nobody got (see hang_watchdog.rb) +HangWatchdog.diagnose do + params = { dbname: ENV.fetch('DB_NAME', nil), host: ENV.fetch('POSTGRES_HOST', nil), port: ENV.fetch('POSTGRES_PORT', nil), + user: ENV.fetch('DB_USER', nil), password: ENV.fetch('DB_PASS', nil), connect_timeout: 5 }.compact + PG.connect(**params) do |admin| + queries = { + 'pg_stat_activity (pid | state | wait | in state | query)' => + "SELECT pid, state, wait_event_type || '/' || wait_event, now() - state_change, left(query, 100) FROM pg_stat_activity " \ + 'WHERE datname = current_database() AND pid <> pg_backend_pid() ORDER BY pid', + 'locks not granted (pid | mode | type | relation)' => + 'SELECT pid, mode, locktype, relation::regclass FROM pg_locks WHERE NOT granted' + } + queries.map { |title, sql| "#{title}:\n" + admin.exec(sql).values.map { |row| " #{row.join(' | ')}" }.join("\n") }.join("\n") + end +end + # End-to-end tests of the code generated for examples/authors/postgresql, against a real PostgreSQL server. class TestPg < Minitest::Test include Object.const_get(ENV.fetch('EXAMPLES_MODULE', 'PgCodegen')) diff --git a/tests/end2end_sqlite3.rb b/tests/end2end_sqlite3.rb index 2f0fb9d..ade0073 100644 --- a/tests/end2end_sqlite3.rb +++ b/tests/end2end_sqlite3.rb @@ -12,6 +12,7 @@ require_relative 'consts' require_relative 'rbs_types' require_relative 'app_helpers' +require_relative 'hang_watchdog' require 'delegate' require 'json' require File.join(EXAMPLES_DIR, 'sqlite3/query_sql') diff --git a/tests/hang_watchdog.rb b/tests/hang_watchdog.rb new file mode 100644 index 0000000..893e146 --- /dev/null +++ b/tests/hang_watchdog.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +require 'minitest' + +# Turns a hanging end-to-end test into a failure that says where it hangs. +# +# Without it a stuck test (a blocked query, a lock, a child process that never exits) shows nothing in the log +# until CI cancels the whole job after its timeout. With it, a test that runs for more than E2E_TEST_TIMEOUT seconds +# (default 120; the slowest real test takes a few seconds) prints the test, every thread's backtrace and what the +# registered diagnostics report (the PostgreSQL suite adds the server's sessions and ungranted locks), then exits +# with status 1. +module HangWatchdog + LIMIT = Integer(ENV.fetch('E2E_TEST_TIMEOUT', '120')) + + @mutex = Mutex.new + @running = nil + @diagnostics = [] + @thread = nil + + class << self + # block: returns a String (or lines) describing the database's state; it runs when a test hangs + def diagnose(&block) + @mutex.synchronize { @diagnostics << block } + end + + def started(test) + watch + @mutex.synchronize { @running = [test, clock] } + end + + def finished + @mutex.synchronize { @running = nil } + end + + private + + def clock = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + + def watch + @thread ||= ::Thread.new do + ::Thread.current.report_on_exception = false + loop do + sleep 1 + test, since = @mutex.synchronize { @running } + report(test, clock - since) if test && clock - since > LIMIT + end + end + end + + def report(test, seconds) + out = ["", "HANG: #{test} has been running for #{seconds.round}s (limit #{LIMIT}s, E2E_TEST_TIMEOUT)"] + ::Thread.list.each do |thread| + out << "#{thread.inspect} (#{thread == ::Thread.main ? 'main' : 'other'})" + out.concat((thread.backtrace || []).first(40).map { |line| " #{line}" }) + end + diagnostics.each do |block| + out << block.call + rescue ::StandardError, ::Timeout::Error => e + out << "diagnostics failed: #{e.class}: #{e.message}" + end + warn out.join("\n") + $stderr.flush + exit!(1) + end + + def diagnostics = @mutex.synchronize { @diagnostics.dup } + end + + # Minitest::Test calls these around every test. + def before_setup + HangWatchdog.started("#{self.class}##{name}") + super + end + + def after_teardown + super + ensure + HangWatchdog.finished + end +end + +Minitest::Test.include(HangWatchdog)