Skip to content

Instantly share code, notes, and snippets.

@ollieh-m
Created March 17, 2022 16:56
Show Gist options
  • Save ollieh-m/8ee884f69798b112d4815e4c9d4b5adb to your computer and use it in GitHub Desktop.
Save ollieh-m/8ee884f69798b112d4815e4c9d4b5adb to your computer and use it in GitHub Desktop.
# Set up `use_transactional_tests: false` as a tag that turns off transactional tests for the spec.
# Wrapping each spec in a transaction prevents concurrent transactions within the spec.
# See https://guides.rubyonrails.org/testing.html#testing-parallel-transactions
RSpec.configure do |config|
config.around(:each, use_transactional_tests: false) do |example|
self.use_transactional_tests = false
example.run
self.use_transactional_tests = true
end
end
it "causes deadlock?", use_transactional_tests: false do
device = create(:device, push_notification_subscriptions: %w(cooksnap_reminder mentioned_in_comment moderation_message))
threads = []
threads << Thread.new do
puts "START THREAD 1"
DeviceCreationService.run(
device.user, {
access_token: device.access_token,# the service finds (then updates) an existing device with the given access_token
token: "new token", # the device `token` attribute needs to be updated
platform: device.platform,
push_notification_subscriptions: ["tip_likers", "comment"] # the device subscriptions also need updating
}
)
end
threads << Thread.new do
puts "START THREAD 2"
sleep(0.07)
DeviceCreationService.run(
device.user, {
access_token: device.access_token,
token: "new token",
platform: device.platform,
push_notification_subscriptions: ["tip_likers", "comment"]
}
)
end
threads.each(&:join)
ensure
# Need to perform manual clean up since transactional tests are disabled
User.destroy_all
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment