@@ -129,6 +129,27 @@ def test_transaction_instrumentation_with_unmaterialized_restart_parent_transact
129
129
ActiveSupport ::Notifications . unsubscribe ( subscriber ) if subscriber
130
130
end
131
131
132
+ def test_transaction_instrumentation_with_materialized_restart_parent_transactions
133
+ topic = topics ( :fifth )
134
+ events = [ ]
135
+ subscriber = ActiveSupport ::Notifications . subscribe ( "transaction.active_record" ) do |event |
136
+ events << event
137
+ end
138
+
139
+ ActiveRecord ::Base . transaction do
140
+ topic . update ( title : "Sinatra" )
141
+ ActiveRecord ::Base . transaction ( requires_new : true ) do
142
+ raise ActiveRecord ::Rollback
143
+ end
144
+ end
145
+
146
+ assert_equal 1 , events . count
147
+ event = events . first
148
+ assert_equal :commit , event . payload [ :outcome ]
149
+ ensure
150
+ ActiveSupport ::Notifications . unsubscribe ( subscriber ) if subscriber
151
+ end
152
+
132
153
def test_transaction_instrumentation_with_restart_savepoint_parent_transactions
133
154
topic = topics ( :fifth )
134
155
@@ -156,6 +177,27 @@ def test_transaction_instrumentation_with_restart_savepoint_parent_transactions
156
177
ActiveSupport ::Notifications . unsubscribe ( subscriber ) if subscriber
157
178
end
158
179
180
+ def test_transaction_instrumentation_with_restart_savepoint_parent_transactions_on_commit
181
+ topic = topics ( :fifth )
182
+
183
+ events = [ ]
184
+ subscriber = ActiveSupport ::Notifications . subscribe ( "transaction.active_record" ) do |event |
185
+ events << event
186
+ end
187
+
188
+ ActiveRecord ::Base . transaction do
189
+ topic . update ( title : "Sinatra" )
190
+ ActiveRecord ::Base . transaction ( requires_new : true ) do
191
+ end
192
+ end
193
+
194
+ assert_equal 1 , events . count
195
+ event = events . first
196
+ assert_equal :commit , event . payload [ :outcome ]
197
+ ensure
198
+ ActiveSupport ::Notifications . unsubscribe ( subscriber ) if subscriber
199
+ end
200
+
159
201
def test_transaction_instrumentation_only_fires_if_materialized
160
202
notified = false
161
203
subscriber = ActiveSupport ::Notifications . subscribe ( "transaction.active_record" ) do |event |
@@ -170,6 +212,36 @@ def test_transaction_instrumentation_only_fires_if_materialized
170
212
ActiveSupport ::Notifications . unsubscribe ( subscriber ) if subscriber
171
213
end
172
214
215
+ def test_transaction_instrumentation_only_fires_on_rollback_if_materialized
216
+ notified = false
217
+ subscriber = ActiveSupport ::Notifications . subscribe ( "transaction.active_record" ) do |event |
218
+ notified = true
219
+ end
220
+
221
+ ActiveRecord ::Base . transaction do
222
+ raise ActiveRecord ::Rollback
223
+ end
224
+
225
+ assert_not notified
226
+ ensure
227
+ ActiveSupport ::Notifications . unsubscribe ( subscriber ) if subscriber
228
+ end
229
+
230
+ def test_reconnecting_after_materialized_transaction_starts_new_event
231
+ events = [ ]
232
+ subscriber = ActiveSupport ::Notifications . subscribe ( "transaction.active_record" ) do |event |
233
+ events << event
234
+ end
235
+ Topic . transaction do
236
+ Topic . connection . materialize_transactions
237
+ Topic . connection . reconnect! ( restore_transactions : true )
238
+ end
239
+
240
+ assert_equal 2 , events . count
241
+ ensure
242
+ ActiveSupport ::Notifications . unsubscribe ( subscriber ) if subscriber
243
+ end
244
+
173
245
def test_transaction_instrumentation_fires_before_after_commit_callbacks
174
246
notified = false
175
247
after_commit_triggered = false
@@ -269,6 +341,26 @@ def test_transaction_instrumentation_on_failed_rollback
269
341
ensure
270
342
ActiveSupport ::Notifications . unsubscribe ( subscriber ) if subscriber
271
343
end
344
+
345
+ def test_transaction_instrumentation_on_failed_rollback_when_unmaterialized
346
+ notified = false
347
+ subscriber = ActiveSupport ::Notifications . subscribe ( "transaction.active_record" ) do |event |
348
+ notified = true
349
+ end
350
+
351
+ error = Class . new ( StandardError )
352
+ assert_raises error do
353
+ # Stubbing this method simulates an error that occurs when the transaction is still unmaterilized.
354
+ Topic . connection . transaction_manager . stub ( :rollback_transaction , -> ( *) { raise error } ) do
355
+ Topic . transaction do
356
+ raise ActiveRecord ::Rollback
357
+ end
358
+ end
359
+ end
360
+ assert_not notified
361
+ ensure
362
+ ActiveSupport ::Notifications . unsubscribe ( subscriber ) if subscriber
363
+ end
272
364
end
273
365
274
366
def test_transaction_instrumentation_on_broken_subscription
0 commit comments