Method: Threads#assert

Defined in:
lib/threads.rb

#assert(reps = @total) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/threads.rb', line 43

def assert(reps = @total)
  raise "Repetition counter #{reps} can't be smaller than #{@total}" if reps < @total

  done = Concurrent::AtomicFixnum.new
  rep = Concurrent::AtomicFixnum.new
  pool = Concurrent::FixedThreadPool.new(@total)
  latch = Concurrent::CountDownLatch.new(1)
  @total.times do |t|
    pool.post do
      Thread.current.name = "assert-thread-#{t}"
      latch.wait(10)
      loop do
        r = rep.increment
        break if r > reps

        begin
          yield(t, r - 1)
        rescue StandardError => e
          print(Backtrace.new(e))
          raise e
        end
      end
      done.increment
    end
  end
  latch.count_down
  pool.shutdown
  raise "Can't stop the pool" unless pool.wait_for_termination(30)
  return if done.value == @total

  raise "Only #{done.value} out of #{@total} threads completed successfully"
end