waterpig 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d5d52f37188f943be46dc7b4dc9dc77029dd61a
4
- data.tar.gz: 74284911ef5ddc9597f020e9226ed2141a0e40e5
3
+ metadata.gz: 318ff8fa9afe73198a7e40f033a80df4a7f6e756
4
+ data.tar.gz: e0497a4ce25ea9f435bfcdcd51c368e1c49af4be
5
5
  SHA512:
6
- metadata.gz: abb67dfdcf7fe87469332d09dc5f20df18d5b03583674005f43302e846d93f14087dc46ce5d418988169a58ab8f5e2acae83564e81d2d7926566f1347a5083e0
7
- data.tar.gz: 09688c753841167d77842c3ff7558a9526d826fd56f46adafe64b5f07ea72cd8f8968ef6d692fe8ad5d5e325861c629fbff77ca411ca1602ab121f1e274c40e8
6
+ metadata.gz: ce3aa8b7b4369f071f1a70d91fdcfe07f844c5788940072ad68fdf51891795e213bcfd1e5e162e302588c420748d5cce813d7bb3fa051eeb600be3ff6385697b
7
+ data.tar.gz: 275f405d9b585bbae00e209a0cece55684435d6c243d78220525efdee52d058d615c577c7cc2c660cbe27b2a9501d817b78d1fdcb428db979c19476fb5898ef6
@@ -0,0 +1,70 @@
1
+ require 'thread'
2
+
3
+ module Waterpig
4
+
5
+ # This Rack middleware is designed to resolve common timing problems with
6
+ # end-to-end tests. Without it, specs will often finish and :after hooks will
7
+ # get executed while a request is still "in flight". The result can be, for
8
+ # example, specs failing because the database has been wiped before a request
9
+ # truly finishes.
10
+ #
11
+ # This middleware counts requests and allows other processes (e.g. testing
12
+ # processes) to block via RequestWaitMiddle.wait_for_idle() so that they do
13
+ # not proceed until all requests have completed.
14
+ class RequestWaitMiddleware
15
+ @@idle_mutex = Mutex.new
16
+
17
+ @@waiting_requests = {}
18
+ @@block_requests = false
19
+
20
+ # This is the only method one would normally call: wrap calls that e.g. drop
21
+ # the test database in wait_for_idle{ <code> } in order to be sure that
22
+ # outstanding requests are complete
23
+ def self.wait_for_idle
24
+ @@block_requests = true
25
+
26
+ @@idle_mutex.synchronize do
27
+ yield
28
+ end
29
+
30
+ @@block_requests = false
31
+ end
32
+
33
+ def block_requests?
34
+ @@block_requests
35
+ end
36
+
37
+ def initialize(app)
38
+ @app = app
39
+ end
40
+
41
+ def call(env)
42
+ increment_active_requests(env)
43
+ if block_requests?
44
+ block_request
45
+ else
46
+ @app.call(env)
47
+ end
48
+ ensure
49
+ decrement_active_requests(env)
50
+ end
51
+
52
+ BLOCKED = [503, {}, ["Test is over - server no longer available"]].freeze
53
+
54
+ def block_request
55
+ BLOCKED
56
+ end
57
+
58
+ def increment_active_requests(env)
59
+ @@idle_mutex.lock unless @@idle_mutex.owned?
60
+ @@waiting_requests[env.object_id] = true
61
+ end
62
+
63
+ def decrement_active_requests(env)
64
+ @@waiting_requests.delete(env.object_id)
65
+ if @@waiting_requests.empty?
66
+ @@idle_mutex.unlock
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,122 @@
1
+ module Waterpig
2
+
3
+
4
+ # Tool to handle migrations and rebuilds on a test template database. For
5
+ # explanation of what the test templated DB is and how it's used, see the
6
+ # README.
7
+ #
8
+ # This should auto detect any needed migrations in that database. However, it
9
+ # does not detect changes to db/seeds.rb, so if you have changed seeds
10
+ # without adding a DB migration, you will need to drop and rebuild the
11
+ # test_template.
12
+ module TemplateRefresh
13
+ Base = ActiveRecord::Base
14
+ Migrator = ActiveRecord::Migrator
15
+ DatabaseTasks = ActiveRecord::Tasks::DatabaseTasks
16
+
17
+ extend self
18
+
19
+ def load_config
20
+ Base.configurations = DatabaseTasks.database_configuration || {}
21
+ Migrator.migrations_paths = DatabaseTasks.migrations_paths
22
+ end
23
+
24
+ def purge_env(env)
25
+ DatabaseTasks.purge(config_for(env))
26
+ end
27
+
28
+ def with_temporary_connection(env)
29
+ begin
30
+ should_reconnect = Base.connection_pool.active_connection?
31
+
32
+ yield
33
+ ensure
34
+ if should_reconnect
35
+ Base.establish_connection(config_for(DatabaseTasks.env))
36
+ end
37
+ end
38
+ end
39
+
40
+ #Assumes schema_format == ruby
41
+ def load_schema(env)
42
+ ActiveRecord::Schema.verbose = false
43
+ DatabaseTasks.load_schema_for config_for(env), :ruby, ENV['SCHEMA']
44
+ end
45
+
46
+ def load_seed
47
+ DatabaseTasks.load_seed
48
+ # load('spec/test_seeds.rb')
49
+ end
50
+
51
+
52
+ def config_for(env)
53
+ Base.configurations.fetch(env.to_s)
54
+ end
55
+
56
+ def connection_for(env)
57
+ Base.establish_connection(env).connection
58
+ end
59
+
60
+ def if_needs_migration(env)
61
+ if Migrator.needs_migration?(connection_for(env))
62
+ begin
63
+ current_config = Base.connection_config
64
+ Base.clear_all_connections!
65
+
66
+ yield
67
+
68
+ ensure
69
+ Base.establish_connection(current_config)
70
+
71
+ ActiveRecord::Migration.check_pending!
72
+ end
73
+ end
74
+ end
75
+
76
+ def ensure_created(env)
77
+ Base.establish_connection(env).connection
78
+ rescue ActiveRecord::NoDatabaseError
79
+ DatabaseTasks.create(config_for(env))
80
+ end
81
+
82
+
83
+ def load_if_pending!(env)
84
+ ensure_created(env)
85
+ if_needs_migration(env) do
86
+ puts "Refreshing unmigrated test env: #{env}"
87
+ purge_env(env)
88
+ with_temporary_connection(env) do
89
+ load_schema(env)
90
+ load_seed
91
+ end
92
+ end
93
+ end
94
+
95
+ def commandeer_database(env)
96
+ config = config_for(env)
97
+ connection_for(env).select_all(
98
+ "select pid, pg_terminate_backend(pid) " +
99
+ "from pg_stat_activity where datname='#{config['database']}' AND state='idle';")
100
+ end
101
+
102
+ def refresh_database(env)
103
+ Rails.logger.fatal("Resetting test DB...")
104
+ config = config_for(env)
105
+
106
+ tasks = ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(config)
107
+
108
+ start = Time.now
109
+ Rails.logger.fatal("Dropping")
110
+ begin
111
+ tasks.drop
112
+ end
113
+ Rails.logger.fatal("Creating")
114
+ begin
115
+ tasks.create
116
+ end
117
+ message = "Test database recopied in #{Time.now - start}s"
118
+ #puts message
119
+ Rails.logger.fatal(message)
120
+ end
121
+ end
122
+ end
data/spec/embarrassing.rb CHANGED
@@ -3,6 +3,28 @@ Sequel.sqlite
3
3
 
4
4
  require 'waterpig'
5
5
  require 'waterpig/deadbeat-connections'
6
+ module ActiveRecord
7
+ class Base
8
+ end
9
+
10
+ module Tasks
11
+ class DatabaseTasks
12
+ end
13
+ class PostgresDatabaseTasks
14
+ end
15
+ end
16
+
17
+ class Migration
18
+ end
19
+
20
+ class Migrator
21
+ end
22
+
23
+ class Schema
24
+ end
25
+ end
26
+ require 'waterpig/request-wait-middleware'
27
+ require 'waterpig/template-refresh'
6
28
 
7
29
  describe "Nothing" do
8
30
  it "should do more" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: waterpig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Judson Lester
@@ -82,6 +82,8 @@ files:
82
82
  - lib/waterpig/selenium_chrome.rb
83
83
  - lib/waterpig/database-cleaner.rb
84
84
  - lib/waterpig/deadbeat-connections.rb
85
+ - lib/waterpig/template-refresh.rb
86
+ - lib/waterpig/request-wait-middleware.rb
85
87
  - lib/waterpig/tinymce-tools.rb
86
88
  - lib/waterpig/browser-integration.rb
87
89
  - lib/waterpig/browser-console-logger.rb
@@ -103,7 +105,7 @@ rdoc_options:
103
105
  - --main
104
106
  - doc/README
105
107
  - --title
106
- - waterpig-0.8.0 Documentation
108
+ - waterpig-0.8.1 Documentation
107
109
  require_paths:
108
110
  - lib/
109
111
  required_ruby_version: !ruby/object:Gem::Requirement