Created
January 3, 2020 17:21
-
-
Save benjaminoakes/ada5f96706fb6231eadd7b478a3953ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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