• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ruby-concurrency / concurrent-ruby / #2743

04 Mar 2015 02:59AM UTC coverage: 45.152% (-50.4%) from 95.548%
#2743

push

jdantonio
Merge pull request #258 from ruby-concurrency/clock_time

Closes #256

23 of 69 new or added lines in 7 files covered. (33.33%)

1563 existing lines in 88 files now uncovered.

1425 of 3156 relevant lines covered (45.15%)

0.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.11
/lib/concurrent/executor/ruby_single_thread_executor.rb
1
require_relative 'executor'
1✔
2

3
module Concurrent
1✔
4

5
  # @!macro single_thread_executor
6
  class RubySingleThreadExecutor
1✔
7
    include RubyExecutor
1✔
8
    include SerialExecutor
1✔
9

10
    # Create a new thread pool.
11
    #
12
    # @option opts [Symbol] :fallback_policy (:discard) the policy for
13
    #   handling new tasks that are received when the queue size has
14
    #   reached `max_queue` or after the executor has shut down
15
    #
16
    # @see http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html
17
    # @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html
18
    # @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
19
    def initialize(opts = {})
1✔
20
      @queue = Queue.new
1✔
21
      @thread = nil
1✔
22
      @fallback_policy = opts.fetch(:fallback_policy, :discard)
1✔
23
      raise ArgumentError.new("#{@fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICIES.include?(@fallback_policy)
1✔
24
      init_executor
1✔
25
    end
26

27
    protected
1✔
28

29
    # @!visibility private
30
    def execute(*args, &task)
1✔
UNCOV
31
      supervise
×
UNCOV
32
      @queue << [args, task]
×
33
    end
34

35
    # @!visibility private
36
    def shutdown_execution
1✔
UNCOV
37
      @queue << :stop
×
UNCOV
38
      stopped_event.set unless alive?
×
39
    end
40

41
    # @!visibility private
42
    def kill_execution
1✔
43
      @queue.clear
1✔
44
      @thread.kill if alive?
1✔
45
    end
46

47
    # @!visibility private
48
    def alive?
1✔
49
      @thread && @thread.alive?
1✔
50
    end
51

52
    # @!visibility private
53
    def supervise
1✔
UNCOV
54
      @thread = new_worker_thread unless alive?
×
55
    end
56

57
    # @!visibility private
58
    def new_worker_thread
1✔
UNCOV
59
      Thread.new do
×
UNCOV
60
        Thread.current.abort_on_exception = false
×
UNCOV
61
        work
×
62
      end
63
    end
64

65
    # @!visibility private
66
    def work
1✔
UNCOV
67
      loop do
×
UNCOV
68
        task = @queue.pop
×
UNCOV
69
        break if task == :stop
×
70
        begin
UNCOV
71
          task.last.call(*task.first)
×
72
        rescue => ex
73
          # let it fail
UNCOV
74
          log DEBUG, ex
×
75
        end
76
      end
UNCOV
77
      stopped_event.set
×
78
    end
79
  end
80
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc