Skip to content

Instantly share code, notes, and snippets.

@benjaminoakes
Created January 3, 2020 17:21
Show Gist options
  • Save benjaminoakes/ada5f96706fb6231eadd7b478a3953ea to your computer and use it in GitHub Desktop.
Save benjaminoakes/ada5f96706fb6231eadd7b478a3953ea to your computer and use it in GitHub Desktop.
# Simulates a race condition for testing. The first try always fails, the second try succeeds.
#
# Abstract example:
#
# RaceConditionCollection.new(active_record_model.associated_collection)
#
# Concrete example:
#
# RaceConditionCollection.new(blog_post.comments)
class RaceConditionCollection
def initialize(collection)
@collection = collection
@tried = false
end
def method_missing(method, args)
if @tried
@collection.send(method, args)
else
@tried = true
raise ActiveRecord::RecordNotUnique
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment