Repository navigation
Expand file tree
/
Copy pathguide_test_helper.rb
More file actions
89 lines (74 loc) · 2.56 KB
/
Copy pathguide_test_helper.rb
File metadata and controls
89 lines (74 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true
require "database_test_helper"
require "active_job"
require "solid_objects/test_helper"
require_relative "../../examples/guides/ordered_jobs/create_ledger_entries"
ActiveJob::Base.queue_adapter = :test
ActiveJob::Base.logger = Logger.new(nil)
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
class ApplicationJob < ActiveJob::Base
end
module GuideSchema
def self.create
connection = ActiveRecord::Base.connection
connection.create_table(:events, if_not_exists: true) do |table|
table.integer :seats_available, null: false, default: 0
end
connection.create_table(:accounts, if_not_exists: true) do |table|
table.integer :balance_cents, null: false, default: 0
table.integer :next_sequence, null: false, default: 1
end
CreateLedgerEntries.new.migrate(:up) unless connection.table_exists?(:ledger_entries)
connection.create_table(:orders, if_not_exists: true) do |table|
table.string :reference, null: false
table.integer :total_cents, null: false
end
connection.create_table(:outbox_messages, if_not_exists: true) do |table|
table.string :name, null: false
table.json :arguments, null: false
table.datetime :delivered_at
end
end
def self.reset
[ "events", "accounts", "ledger_entries", "orders", "outbox_messages" ].each do |table|
ActiveRecord::Base.connection.execute("DELETE FROM #{table}")
end
end
end
GuideSchema.create
module GuideTestSupport
def reminder_due_at(actor_id:, operation:, key:)
SolidObjects::Reminder.find_by(actor_id:, name: "#{operation}:#{key}")&.next_run_at
end
def concurrently(count, prepare: ->(index) { index })
ready = Queue.new
start = Queue.new
results = Queue.new
errors = Queue.new
connection_slots = Queue.new
[ count, ActiveRecord::Base.connection_pool.size - 1 ].min.times { connection_slots << true }
threads = count.times.map do |index|
Thread.new do
prepared = ActiveRecord::Base.connection_pool.with_connection { prepare.call(index) }
ready << true
start.pop
connection_slots.pop
begin
results << ActiveRecord::Base.connection_pool.with_connection { yield(prepared) }
ensure
connection_slots << true
end
rescue => error
ready << true
errors << error
end
end
count.times { ready.pop }
count.times { start << true }
threads.each(&:join)
raise errors.pop unless errors.empty?
Array.new(results.size) { results.pop }
end
end