|
| 1 | +require 'spec_helper' |
| 2 | +require 'concurrent/actress' |
| 3 | + |
| 4 | +EXECUTOR = Concurrent::ThreadPoolExecutor.new( |
| 5 | + min_threads: [2, Concurrent.processor_count].max, |
| 6 | + max_threads: [20, Concurrent.processor_count * 15].max, |
| 7 | + idletime: 2 * 60, # 2 minutes |
| 8 | + max_queue: 0, # unlimited |
| 9 | + overflow_policy: :abort # raise an exception |
| 10 | +) |
| 11 | + |
| 12 | +describe Concurrent::Actress do |
| 13 | + |
| 14 | + class Ping |
| 15 | + include Concurrent::Actress::Context |
| 16 | + |
| 17 | + def initialize(queue) |
| 18 | + @queue = queue |
| 19 | + end |
| 20 | + |
| 21 | + def on_message(message) |
| 22 | + case message |
| 23 | + when :terminate |
| 24 | + terminate! |
| 25 | + when :child |
| 26 | + Concurrent::Actress::AdHoc.spawn(name: :pong, executor: EXECUTOR) { -> m { @queue << m } } |
| 27 | + else |
| 28 | + @queue << message |
| 29 | + message |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + # def trace! |
| 35 | + # set_trace_func proc { |event, file, line, id, binding, classname| |
| 36 | + # # thread = eval('Thread.current', binding).object_id.to_s(16) |
| 37 | + # printf "%8s %20s %20s %s %s:%-2d\n", event, id, classname, nil, file, line |
| 38 | + # } |
| 39 | + # yield |
| 40 | + # ensure |
| 41 | + # set_trace_func nil |
| 42 | + # end |
| 43 | + |
| 44 | + def assert condition |
| 45 | + unless condition |
| 46 | + # require 'pry' |
| 47 | + # binding.pry |
| 48 | + raise |
| 49 | + puts "--- \n#{caller.join("\n")}" |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + |
| 54 | + # FIXME increasing this does not work in Rspec environment |
| 55 | + 1.times do |i| |
| 56 | + it format('run %3d', i) do |
| 57 | + # puts format('run %3d', i) |
| 58 | + Array.new(10).map do |
| 59 | + Thread.new do |
| 60 | + 10.times do |
| 61 | + # trace! do |
| 62 | + queue = Queue.new |
| 63 | + actor = Ping.spawn name: :ping, executor: EXECUTOR, args: [queue] |
| 64 | + |
| 65 | + # when spawn returns children are set |
| 66 | + assert Concurrent::Actress::ROOT.send(:core).instance_variable_get(:@children).include?(actor) |
| 67 | + |
| 68 | + actor << 'a' << 1 |
| 69 | + assert queue.pop == 'a' |
| 70 | + assert actor.ask(2).value == 2 |
| 71 | + |
| 72 | + assert actor.parent == Concurrent::Actress::ROOT |
| 73 | + assert Concurrent::Actress::ROOT.path == '/' |
| 74 | + assert actor.path == '/ping' |
| 75 | + child = actor.ask(:child).value |
| 76 | + assert child.path == '/ping/pong' |
| 77 | + queue.clear |
| 78 | + child.ask(3) |
| 79 | + assert queue.pop == 3 |
| 80 | + |
| 81 | + actor << :terminate |
| 82 | + assert actor.ask(:blow_up).wait.rejected? |
| 83 | + end |
| 84 | + end |
| 85 | + end.each(&:join) |
| 86 | + end |
| 87 | + end |
| 88 | +end |
0 commit comments