Skip to content

Instantly share code, notes, and snippets.

@halorgium
Created June 20, 2013 23:02
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 halorgium/b9533251e1ce5b81fcc9 to your computer and use it in GitHub Desktop.
Save halorgium/b9533251e1ce5b81fcc9 to your computer and use it in GitHub Desktop.
This should work always
# jruby 1.7.5.dev (1.9.3p392) 2013-06-20 b13d153 on Java HotSpot(TM) 64-Bit Server VM 1.6.0_45-b06-451-11M4406 [darwin-x86_64]
ruby shutdown.rb
I, [2013-06-20T16:01:33.988000 #64291] INFO -- : thread start
I, [2013-06-20T16:01:33.996000 #64291] INFO -- : popping
# this should block because we are joined on the thread which is blocks on the pop
# PASS
ruby shutdown.rb push
I, [2013-06-20T16:01:48.191000 #64308] INFO -- : thread start
I, [2013-06-20T16:01:48.199000 #64308] INFO -- : popping
I, [2013-06-20T16:01:48.394000 #64308] INFO -- : got value: nil
I, [2013-06-20T16:01:48.394000 #64308] INFO -- : thread finish
# this should complete because we have pushed a value into the queue on which the thread is blocking
# PASS
ruby shutdown.rb kill
I, [2013-06-20T16:02:04.305000 #64327] INFO -- : thread start
I, [2013-06-20T16:02:04.314000 #64327] INFO -- : popping
# this should complete because we are joined on the thread which we have just killed
# FAIL
ruby shutdown.rb kill push
I, [2013-06-20T16:02:30.654000 #64348] INFO -- : thread start
I, [2013-06-20T16:02:30.661000 #64348] INFO -- : popping
I, [2013-06-20T16:02:30.855000 #64348] INFO -- : got value: nil
# this should complete because we have both killed the thread and pushed a value into the queue
# PASS
require 'logger'
require 'pry'
r, w = IO.pipe
l = Logger.new($stderr)
trap("INFO") {
w.write(".")
}
Thread.new {
while r.read(1)
Thread.list.each do |t|
puts t.inspect
puts t.backtrace
puts "-" * 80
end
end
}
q = Queue.new
t = Thread.new {
l.info "thread start"
Fiber.new {
l.info "popping"
v = q.pop
l.info "got value: #{v.inspect}"
}.resume
l.info "thread finish"
}
kill = ARGV.include?("kill")
push = ARGV.include?("push")
sleep 0.1
t.kill if kill
sleep 0.1
q.push(nil) if push
t.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment