# encoding: UTF-8 # # Author: Stefano Harding # License: Apache License, Version 2.0 # Copyright: (C) 2014-2015 Stefano Harding # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # require_relative 'event' require_relative 'executor' module Garcon # An executor service which runs all operations on the current thread, # blocking as necessary. Operations are performed in the order they are # received and no two operations can be performed simultaneously. # # This executor service exists mainly for testing an debugging. When used # it immediately runs every `#post` operation on the current thread, blocking # that thread until the operation is complete. This can be very beneficial # during testing because it makes all operations deterministic. # # @note Intended for use primarily in testing and debugging. class ImmediateExecutor include SerialExecutor # Creates a new executor def initialize @stopped = Garcon::Event.new end # @!macro executor_method_post def post(*args, &task) raise ArgumentError, 'no block given' unless block_given? return false unless running? task.call(*args) true end # @!macro executor_method_left_shift def <<(task) post(&task) self end # @!macro executor_method_running_question def running? ! shutdown? end # @!macro executor_method_shuttingdown_question def shuttingdown? false end # @!macro executor_method_shutdown_question def shutdown? @stopped.set? end # @!macro executor_method_shutdown def shutdown @stopped.set true end alias_method :kill, :shutdown # @!macro executor_method_wait_for_termination def wait_for_termination(timeout = nil) @stopped.wait(timeout) end end end