From fa03cd736a310c1006a026c42ba828aa849f7fb3 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Fri, 17 Apr 2015 21:23:10 +0000 Subject: [PATCH] Implement Runner#worker --- lib/uh/wm/runner.rb | 18 ++++++++++++++++++ spec/uh/wm/runner_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/uh/wm/runner.rb b/lib/uh/wm/runner.rb index 2a36744..8bee78b 100644 --- a/lib/uh/wm/runner.rb +++ b/lib/uh/wm/runner.rb @@ -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 diff --git a/spec/uh/wm/runner_spec.rb b/spec/uh/wm/runner_spec.rb index a2f82aa..863abba 100644 --- a/spec/uh/wm/runner_spec.rb +++ b/spec/uh/wm/runner_spec.rb @@ -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 { }