Implement Runner#worker

This commit is contained in:
Thibault Jouan 2015-04-17 21:23:10 +00:00
parent 887c66a6f7
commit fa03cd736a
2 changed files with 39 additions and 0 deletions

View File

@ -54,6 +54,24 @@ module Uh
end
end
def worker
@worker ||= Workers.build(*(@env.worker)).tap do |w|
w.on_read do
@env.log_debug 'Processing pending events'
@manager.handle_pending_events
end
w.on_read_next do
@env.log_debug 'Processing next event'
@manager.handle_next_event
end
w.on_timeout do |*args|
@env.log_debug "Worker timeout: #{args.inspect}"
@env.log_debug 'Flushing X output buffer'
@manager.flush
end
end
end
def run_until &block
manager.handle_pending_events until block.call
end

View File

@ -122,6 +122,27 @@ module Uh
end
end
describe '#worker' do
it 'returns a worker' do
expect(runner.worker).to respond_to :work_events
end
it 'setups the read callback to tell manager to handle pending events' do
expect(runner.manager).to receive :handle_pending_events
runner.worker.on_read.call
end
it 'setups the read_next callback to tell manager to handle next event' do
expect(runner.manager).to receive :handle_next_event
runner.worker.on_read_next.call
end
it 'setups the timeout callback to tell manager to flush the output' do
expect(runner.manager).to receive :flush
runner.worker.on_timeout.call
end
end
describe '#run_until' do
it 'tells the manager to handle events until given block is true' do
block = proc { }