Skip to content

Commit

Permalink
Test full matrix of supported adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
segiddins committed Jul 21, 2024
1 parent 53d6769 commit 5959e35
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 8 deletions.
65 changes: 64 additions & 1 deletion .github/workflows/gemstash-ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Tests

on: [push, pull_request]
on:
push:
branches:
- main
pull_request:

jobs:
gemstash_tests:
Expand All @@ -9,6 +13,65 @@ jobs:
fail-fast: false
matrix:
ruby: ["3.1", "3.2", "3.3", "jruby-9.4"]
db_adapter: ["sqlite3"]
cache_type: ["memory"]
include:
- ruby: "3.3"
db_adapter: "postgres"
cache_type: "memory"
- ruby: "3.3"
db_adapter: "mysql2"
cache_type: "memory"
- ruby: "3.3"
db_adapter: "sqlite3"
cache_type: "memcached"
- ruby: "3.3"
db_adapter: "sqlite3"
cache_type: "redis"
- ruby: "jruby-9.4"
db_adapter: "postgres"
cache_type: "memory"
- ruby: "jruby-9.4"
db_adapter: "mysql2"
cache_type: "memory"
- ruby: "jruby-9.4"
db_adapter: "sqlite3"
cache_type: "memcached"
- ruby: "jruby-9.4"
db_adapter: "sqlite3"
cache_type: "redis"
services:
postgres:
image: postgres:13
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: gemstash_test
ports:
- 5432:5432
mysql:
image: mysql:8
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: gemstash_test
MYSQL_USER: root
MYSQL_PASSWORD: root
ports:
- 3306:3306
memcached:
image: memcached:1.6
ports:
- 11211:11211
redis:
image: redis:7
ports:
- 6379:6379
env:
GEMSTASH_SPEC_CACHE_TYPE: ${{ matrix.cache_type }}
GEMSTASH_SPEC_DB_ADAPTER: ${{ matrix.db_adapter }}
GEMSTASH_SPEC_DB_URL: ${{ (matrix.db_adapter == 'postgres' && 'postgres://postgres:postgres@localhost/gemstash_test') || (matrix.db_adapter == 'mysql2' && 'mysql2://root:[email protected]:3306/gemstash_test') }}
GEMSTASH_SPEC_REDIS_SERVERS: redis://localhost:6379
GEMSTASH_SPEC_MEMCACHED_SERVERS: localhost:11211
steps:
- uses: actions/checkout@v4
- name: Setup ruby
Expand Down
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ gem "rspec", "~> 3.3"
gem "webrick", "~> 1.6"

platform :jruby do
gem "jdbc-mysql"
gem "jdbc-postgres"
gem "jdbc-sqlite3"
gem "psych"
end

platform :ruby do
gem "mysql2"
gem "pg"
gem "sqlite3"
end

group :linting do
gem "rubocop", "~> 1.44"
gem "rubocop-performance", "~> 1.5"
Expand Down
3 changes: 2 additions & 1 deletion lib/gemstash/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ def get(key)
end

def get_multi(keys)
@cache.mget(*keys).each do |k, v|
@cache.mget(*keys).each_with_index do |v, idx|
next if v.nil?

k = keys[idx]
yield(k, YAML.load(v))
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/gemstash/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ def db
Sequel.connect("sqlite://#{CGI.escape(db_path)}", config.database_connection_config)
end
when "postgres", "mysql", "mysql2"
db = Sequel.connect(config[:db_url], config.database_connection_config)
db_url = config[:db_url]
raise "Missing DB URL" unless db_url

db = Sequel.connect(db_url, config.database_connection_config)
else
raise "Unsupported DB adapter: '#{config[:db_adapter]}'"
end
Expand Down
37 changes: 32 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,32 @@
Pathname.new(TEST_BASE_PATH).children.each(&:rmtree)
TEST_LOG_FILE = File.join(TEST_BASE_PATH, "server.log")
TEST_CONFIG = Gemstash::Configuration.new(config: {
:base_path => TEST_BASE_PATH
base_path: TEST_BASE_PATH,
cache_type: ENV.fetch("GEMSTASH_SPEC_CACHE_TYPE", "memory"),
db_adapter: ENV.fetch("GEMSTASH_SPEC_DB_ADAPTER", "sqlite3"),
db_url: ENV.fetch("GEMSTASH_SPEC_DB_URL", nil).then do |db_url|
if RUBY_ENGINE == "jruby" && db_url
db_url = "jdbc" + db_url
db_url.sub(":mysql2:", ":mysql:")
else
db_url
end
end,
redis_servers: ENV.fetch("GEMSTASH_SPEC_REDIS_SERVERS", nil),
memcached_servers: ENV.fetch("GEMSTASH_SPEC_MEMCACHED_SERVERS", nil)
})
Gemstash::Env.current = Gemstash::Env.new(TEST_CONFIG)
Thread.current[:test_gemstash_env_set] = true
TEST_DB = Gemstash::Env.current.db
db_connect_attempts = 0
TEST_DB = begin
db_connect_attempts += 1
Gemstash::Env.current.db
rescue Sequel::DatabaseConnectionError
raise if db_connect_attempts > 5

sleep(2**(db_connect_attempts - 1))
retry
end
Sequel::Model.db = TEST_DB

RSpec.configure do |config|
Expand All @@ -45,13 +66,19 @@
end
end

TEST_DB.disconnect

# If a spec has no transaction, delete the DB, and force recreate/migrate to ensure it is clean
if example.metadata[:db_transaction] == false
File.delete(File.join(TEST_BASE_PATH, "gemstash.db"))
if TEST_CONFIG[:db_adapter] == "sqlite3"
File.delete(File.join(TEST_BASE_PATH, "gemstash.db"))
else
# Drop all tables
TEST_DB.drop_table(*TEST_DB.tables)
end
TEST_DB.disconnect
Gemstash::Env.migrate(TEST_DB)
end

TEST_DB.disconnect
end

config.before(:each) do
Expand Down

0 comments on commit 5959e35

Please sign in to comment.