minitest-parallel-db 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: af5e71ed5246d3fcbcab66122854e2ded8e86f14
4
+ data.tar.gz: 267b812995e22c39c77e0a81cb647ab1c329eb0e
5
+ SHA512:
6
+ metadata.gz: 29b59408553caf12225d3ec6c58b5092d1112a02d113a35ea7853acccb276371636a4ac11bd14ffc7a393d731089f37c3f42091aff84552761b33560a32d0f41
7
+ data.tar.gz: 61840c54cc06ab3067dbbec7da569b5481d117aa4beb2a92a3c51fc7bd8f87ce8690acbd7e4904582270a8ada6b9cb56f3de0413e9250cc481719771a9a432fb
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ InstalledFiles
7
+ _yardoc
8
+ coverage
9
+ doc/
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
17
+ log
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p353
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
5
+ script:
6
+ - 'ruby spec/active_record/postgres/spec.rb'
7
+ - 'ruby spec/sequel/postgres/spec.rb'
8
+ before_script:
9
+ - psql -c 'create database minitest_parallel_db;' -U postgres
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in minitest-parallel-db.gemspec
4
+ gemspec
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ minitest-parallel-db (0.0.4)
5
+ minitest (>= 4.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.1.1)
11
+ activesupport (= 4.1.1)
12
+ builder (~> 3.1)
13
+ activerecord (4.1.1)
14
+ activemodel (= 4.1.1)
15
+ activesupport (= 4.1.1)
16
+ arel (~> 5.0.0)
17
+ activesupport (4.1.1)
18
+ i18n (~> 0.6, >= 0.6.9)
19
+ json (~> 1.7, >= 1.7.7)
20
+ minitest (~> 5.1)
21
+ thread_safe (~> 0.1)
22
+ tzinfo (~> 1.1)
23
+ arel (5.0.1.20140414130214)
24
+ builder (3.2.2)
25
+ i18n (0.6.9)
26
+ json (1.8.1)
27
+ minitest (5.3.4)
28
+ pg (0.17.0)
29
+ rake (10.1.0)
30
+ sequel (3.47.0)
31
+ thread_safe (0.3.3)
32
+ tzinfo (1.1.0)
33
+ thread_safe (~> 0.1)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ activerecord
40
+ bundler (~> 1.3)
41
+ minitest-parallel-db!
42
+ pg
43
+ rake
44
+ sequel
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jared Ning
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,91 @@
1
+ # Minitest::Parallel::Db
2
+
3
+ [![Build Status](https://travis-ci.org/ordinaryzelig/minitest-parallel-db.png?branch=master)](https://travis-ci.org/ordinaryzelig/minitest-parallel-db)
4
+
5
+ Run Minitest in parallel with a single database.
6
+
7
+ ## Rationale
8
+
9
+ We should be able to run tests in parallel even if there is a database involved.
10
+ We can leverage database transactions to keep our tests isolated from each other.
11
+
12
+ There are solutions out there already, but some require creating multiple databases
13
+ or running multiple processes.
14
+ None of that sounds appealing to me.
15
+
16
+ ## The solution
17
+
18
+ Tests in parallel must be run in isolation.
19
+ If the tests are sharing the same database and table, we have a problem.
20
+ But if we can use a database transaction for each test,
21
+ they will be run in isolation from each other
22
+ (although write locks are still in effect).
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ require 'minitest/parallel/db'
28
+ Minitest::Test.send(:include, Minitest::Parallel::DB::ActiveRecord)
29
+ # Set number of threads you want. Best to match your pool size.
30
+ # Minitest defaults this to 2.
31
+ Minitest::Parallel::Db.concurrency = 10
32
+
33
+ describe 'your parallel tests' do
34
+
35
+ it 'saves a record' do
36
+ model = Model.create!
37
+ Model.last.id.must_equal model.id
38
+ end
39
+
40
+ it 'edits a record' do
41
+ Model.create!
42
+ Model.last.update_attributes(name: 'changed')
43
+ Model.last.name.must_equal 'changed'
44
+ end
45
+
46
+ end
47
+ ```
48
+
49
+ ## Requirements
50
+
51
+ * Postgres (should work with databases that support transactions, but I haven't tried any)
52
+ * Minitest >= 4.2 (where `parallelize_me!` exists)
53
+ * Supported ORM (ActiveRecord, Sequel)
54
+ * Get rid of DatabaseCleaner if you're using it.
55
+
56
+ ### Minitest versions
57
+
58
+ At the time this gem was created, Minitest was at v4.7.5.
59
+ Since then, the way to set the number of concurrent tests to be run has changed.
60
+ Please let me know if the version of Minitest you are using doesn't work with this gem so I can try to patch it.
61
+
62
+ ### Rails
63
+
64
+ If you are using a recent enough version of Rails, you don't need this gem.
65
+ See the Rails section of my [blog post] (http://redningja.com/dev/parallel-tests-with-single-database/) about this.
66
+
67
+ ## Tips
68
+
69
+ ### Use sequences with factories to avoid write blocks on unique fields
70
+
71
+ If you have a field that is unique (i.e. the database defines it as unique,
72
+ not just unique by a model validation), use sequences in your factories.
73
+ Unique indexes will be enforced by the database (even if the writes are
74
+ in their own transactions), and it will block writes, slowing you down a bit.
75
+ To avoid that, use sequences where you can to easily avoid duplicates.
76
+
77
+ ```
78
+ factory :users do
79
+ sequence(:username) { |idx| "user #{idx}" }
80
+ end
81
+ ```
82
+
83
+ ## Running tests for this gem
84
+
85
+ The safe way: run each of the scripts listed in `.travis.yml`.
86
+ The quick way: `rake test`.
87
+
88
+ ## Contributing
89
+
90
+ I have only had the need to implement this with Postgres, ActiveRecord, and Sequel.
91
+ I'm sure there is need for more databases like MySQL, and ORMs like Datamapper.
@@ -0,0 +1,18 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ puts '*** You should see groups of tests run very quickly (~1s per group).'
8
+ puts '*** If you want to be really safe, run each of the suites individually (listed in .travis.yml).'
9
+ t.pattern = 'spec/**/*spec.rb'
10
+ end
11
+
12
+ namespace :db do
13
+
14
+ task :create do
15
+ sh 'createdb minitest_parallel_db'
16
+ end
17
+
18
+ end
@@ -0,0 +1,44 @@
1
+ require 'minitest'
2
+ require 'minitest/parallel/db/version'
3
+
4
+ module Minitest
5
+ module Parallel
6
+ module Db
7
+
8
+ autoload :ActiveRecord, 'minitest/parallel/db/orms/active_record'
9
+ autoload :Sequel, 'minitest/parallel/db/orms/sequel'
10
+
11
+ def self.included(suite)
12
+ suite.parallelize_me!
13
+ suite.send :prepend, InstanceMethods
14
+ end
15
+
16
+ class << self
17
+
18
+ attr_reader :concurrency
19
+
20
+ def concurrency=(num)
21
+ @concurrency = num
22
+ if Minitest.const_defined?(:Parallel)
23
+ Minitest.parallel_executor = Minitest::Parallel::Executor.new(num)
24
+ else
25
+ ENV['N'] = num
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ module InstanceMethods
32
+
33
+ def run
34
+ adapter_run do
35
+ super
36
+ end
37
+ self
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,32 @@
1
+ require 'pg'
2
+
3
+ module Minitest
4
+ module Parallel::Db
5
+ module ActiveRecord
6
+
7
+ def self.included(suite)
8
+ suite.send(:include, Parallel::Db)
9
+ suite.send(:include, TestInstanceMethods)
10
+ end
11
+
12
+ def adapter_run
13
+ model.transaction do
14
+ yield
15
+ raise ::ActiveRecord::Rollback
16
+ end
17
+ model.connection.close
18
+ end
19
+
20
+ module TestInstanceMethods
21
+
22
+ # If you want to be specific with a certain model,
23
+ # define your own `model` method (or `let` statement).
24
+ def model
25
+ ::ActiveRecord::Base
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ require 'pg'
2
+
3
+ module Minitest
4
+ module Parallel::Db
5
+ module Sequel
6
+
7
+ def self.included(suite)
8
+ suite.send(:include, Parallel::Db)
9
+ end
10
+
11
+ def adapter_run
12
+ DB.transaction(rollback: :always) do
13
+ yield
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ module Minitest
2
+ module Parallel
3
+ module Db
4
+ VERSION = '0.0.4'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'minitest/parallel/db/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "minitest-parallel-db"
8
+ spec.version = Minitest::Parallel::Db::VERSION
9
+ spec.authors = ["Jared Ning"]
10
+ spec.email = ["jared@redningja.com"]
11
+ spec.description = %q{Run tests in parallel with a single database}
12
+ spec.summary = %q{Run tests in parallel with a single database}
13
+ spec.homepage = "https://github.com/ordinaryzelig/minitest-parallel-db"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'minitest', '>= 4.2'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+
26
+ spec.add_development_dependency "pg"
27
+ spec.add_development_dependency "sequel"
28
+ spec.add_development_dependency "activerecord"
29
+ end
@@ -0,0 +1,39 @@
1
+ require_relative '../../helper'
2
+
3
+ require 'active_record'
4
+
5
+ # Model
6
+ class PostgresActiveRecordModel < ActiveRecord::Base
7
+ validates :name, uniqueness: true
8
+ end
9
+ PARM = PostgresActiveRecordModel
10
+
11
+ db_config = DB_CONFIG[:postgres].merge(
12
+ adapter: 'postgresql',
13
+ )
14
+
15
+ # Create database.
16
+ begin
17
+ ActiveRecord::Base.establish_connection(db_config)
18
+ ActiveRecord::Base.connection
19
+ rescue
20
+ ActiveRecord::Base.establish_connection(
21
+ adapter: db_config[:adapter],
22
+ database: 'postgres',
23
+ schema_search_path: 'public',
24
+ )
25
+ ActiveRecord::Base.connection.create_database(db_config[:database])
26
+ end
27
+
28
+ ActiveRecord::Base.establish_connection(db_config)
29
+
30
+ # Migration
31
+ ActiveRecord::Base.connection.tap do |conn|
32
+ conn.drop_table PARM.table_name if PARM.table_exists?
33
+ conn.create_table PARM.table_name do |t|
34
+ t.string :name, null: false
35
+ end
36
+ end
37
+
38
+ Minitest::Parallel::Db.concurrency = ActiveRecord::Base.connection_config[:pool]
39
+ ActiveRecord::Base.connection.close
@@ -0,0 +1,20 @@
1
+ require_relative 'setup'
2
+
3
+ describe 'ActiveRecord + Postgres' do
4
+
5
+ include Minitest::Parallel::Db::ActiveRecord
6
+
7
+ ActiveRecord::Base.connection_config[:pool].times do |idx|
8
+ it "tests in parallel (#{idx + 1})" do
9
+ PARM.create! name: "name #{idx}"
10
+
11
+ # Give time for other parallel tests to catch up.
12
+ sleep 1
13
+
14
+ # If this is run in transaction,
15
+ # we should only see the model we just created.
16
+ PARM.count.must_equal 1
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,10 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+
4
+ require 'minitest/autorun'
5
+ require 'minitest/pride'
6
+
7
+ require 'minitest/parallel/db'
8
+
9
+ require_relative 'support/db_config'
10
+ require_relative 'support/timer_plugin'
@@ -0,0 +1,31 @@
1
+ require_relative '../../helper'
2
+
3
+ require 'sequel'
4
+
5
+ db_config = {
6
+ adapter: 'postgres',
7
+ host: 'localhost',
8
+ database: DB_CONFIG[:postgres][:database],
9
+ max_connections: DB_CONFIG[:postgres][:pool],
10
+ }
11
+
12
+ DB = Sequel.postgres(db_config)
13
+
14
+ # Migration
15
+ DB.create_table! :postgres_sequel_models do
16
+ primary_key :id
17
+ String :name, null: false
18
+ end
19
+
20
+ # Model
21
+ Sequel::Model.plugin :validation_helpers
22
+ class PostgresSequelModel < Sequel::Model
23
+ def validate
24
+ super
25
+ validates_presence [:name]
26
+ validates_unique :name
27
+ end
28
+ end
29
+ PSM = PostgresSequelModel
30
+
31
+ Minitest::Parallel::Db.concurrency = DB.pool.max_size
@@ -0,0 +1,20 @@
1
+ require_relative 'setup'
2
+
3
+ describe 'Sequel + Postgres' do
4
+
5
+ include Minitest::Parallel::Db::Sequel
6
+
7
+ DB.pool.max_size.times do |idx|
8
+ it "tests in parallel (#{idx + 1})" do
9
+ PSM.new(name: 'name').save
10
+
11
+ # Give time for other parallel tests to catch up.
12
+ sleep 1
13
+
14
+ # If this is run in transaction,
15
+ # we should only see the model we just created.
16
+ PSM.count.must_equal 1
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,7 @@
1
+ DB_CONFIG = {
2
+ postgres: {
3
+ database: 'minitest_parallel_db',
4
+ pool: 10,
5
+ host: 'localhost',
6
+ },
7
+ }
@@ -0,0 +1,39 @@
1
+ module Minitest
2
+
3
+ def self.plugin_timer_init(options)
4
+ self.reporter << TimerReporter.new(options)
5
+ end
6
+
7
+ class TimerReporter < AbstractReporter
8
+
9
+ # Only top-level describes
10
+ def self.top_level_test_suites
11
+ @top_level_test_suites ||=
12
+ Minitest::Runnable.runnables.select do |runnable|
13
+ [Minitest::Spec, Minitest::Unit::TestCase].include? runnable.superclass
14
+ end
15
+ end
16
+
17
+ def initialize(options)
18
+ options.fetch(:io).puts "Num test suites to time at 2s each: #{self.class.top_level_test_suites.size}."
19
+ @max_time_allowed = 2 * self.class.top_level_test_suites.size
20
+ end
21
+
22
+ def start
23
+ @start_time = Time.now
24
+ end
25
+
26
+ def report
27
+ @total_time = Time.now - @start_time
28
+ raise "Tests took too long (#{@total_time}s)" unless passed?
29
+ end
30
+
31
+ def passed?
32
+ @total_time <= @max_time_allowed
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
39
+ Minitest.extensions << :timer
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minitest-parallel-db
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Jared Ning
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sequel
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Run tests in parallel with a single database
98
+ email:
99
+ - jared@redningja.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .ruby-version
106
+ - .travis.yml
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - lib/minitest/parallel/db.rb
113
+ - lib/minitest/parallel/db/orms/active_record.rb
114
+ - lib/minitest/parallel/db/orms/sequel.rb
115
+ - lib/minitest/parallel/db/version.rb
116
+ - minitest-parallel-db.gemspec
117
+ - spec/active_record/postgres/setup.rb
118
+ - spec/active_record/postgres/spec.rb
119
+ - spec/helper.rb
120
+ - spec/sequel/postgres/setup.rb
121
+ - spec/sequel/postgres/spec.rb
122
+ - spec/support/db_config.rb
123
+ - spec/support/timer_plugin.rb
124
+ homepage: https://github.com/ordinaryzelig/minitest-parallel-db
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.0.14
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Run tests in parallel with a single database
148
+ test_files:
149
+ - spec/active_record/postgres/setup.rb
150
+ - spec/active_record/postgres/spec.rb
151
+ - spec/helper.rb
152
+ - spec/sequel/postgres/setup.rb
153
+ - spec/sequel/postgres/spec.rb
154
+ - spec/support/db_config.rb
155
+ - spec/support/timer_plugin.rb