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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/switchman/r_spec_helper.rb', line 20
def self.included(klass)
parent_group = klass.parent_groups[1]
return if parent_group && included_in?(parent_group)
root_group = klass.parent_groups.last
root_group.prepend_before(:all) do |group|
next if @@shard1
next if @@sharding_failed
groups = group.class.descendant_filtered_examples.map(&:example_group).uniq
next unless groups.any? { |descendant_group| RSpecHelper.included_in?(descendant_group) }
puts "Setting up sharding for all specs..."
Shard.delete_all
Switchman.cache.delete("default_shard")
@@shard1, @@shard2 = TestHelper.recreate_persistent_test_shards
@@default_shard = Shard.default
if @@shard1.is_a?(Shard)
@@keep_the_shards = true
@@shard3 = nil
else begin
@@shard1 = @@shard1.create_new_shard
@@shard2 = @@shard2.create_new_shard
rescue => e
warn "Sharding setup FAILED!:"
while e
warn "\n#{e}\n"
warn e.backtrace
e = e.respond_to?(:cause) ? e.cause : nil
end
@@sharding_failed = true
@@shard1&.drop_database rescue nil
@@shard2&.drop_database rescue nil
@@shard1 = @@shard2 = nil
Shard.delete_all
Shard.default(reload: true)
next
end
end
Shard.delete_all
Switchman.cache.delete("default_shard")
Shard.default(reload: true)
puts "Done!"
main_pid = Process.pid
at_exit do
next unless main_pid == Process.pid
status = $!.is_a?(::SystemExit) ? $!.status : nil
puts "Tearing down sharding for all specs"
@@shard1.database_server.destroy unless @@shard1.database_server == Shard.default.database_server
unless @@keep_the_shards
@@shard1.drop_database
@@shard1.destroy
@@shard2.drop_database
@@shard2.destroy
end
@@shard2.database_server.destroy
exit status if status
end
end
klass.before(:all) do
next if @@sharding_failed
dup = @@default_shard.dup
dup.id = @@default_shard.id
dup.save!
Switchman.cache.delete("default_shard")
Shard.default(reload: true)
dup = @@shard1.dup
dup.id = @@shard1.id
dup.save!
dup = @@shard2.dup
dup.id = @@shard2.id
dup.save!
@shard1, @shard2 = @@shard1, @@shard2
end
klass.before do
raise "Sharding did not set up correctly" if @@sharding_failed
Shard.clear_cache
if use_transactional_tests
Shard.default(reload: true)
@shard1 = Shard.find(@shard1.id)
@shard2 = Shard.find(@shard2.id)
end
end
klass.after do
next if @@sharding_failed
DatabaseServer.each do |ds|
if ds.fake? && ds != @shard2.database_server
ds.shards.delete_all unless use_transactional_tests
ds.destroy
end
end
end
klass.after(:all) do
Shard.delete_all
Switchman.cache.delete("default_shard")
Shard.default(reload: true)
end
end
|