Skip to content

Instantly share code, notes, and snippets.

@subelsky
Last active March 4, 2016 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subelsky/8dcc5c4bb10071f5abf0 to your computer and use it in GitHub Desktop.
Save subelsky/8dcc5c4bb10071f5abf0 to your computer and use it in GitHub Desktop.
# This is how I'd use threads in rspec to test whether a shared state primitive works as expected
#(pessimistic database lock,advisory database lock, mutex, etc.)
around do |example|
die = false
lock_taker = Thread.new do
code_that_takes a lock do
loop do
break if die
sleep 0.1 # not strictly necessary but slows CPU churn
end
end
example.run
die = true
locker_taker.join
end
it "can't get an adivsory lock when alreaedy locked" do
code_that_also_takes_a_lock_but_fails
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment