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

jdantonio / concurrent-ruby / #812

23 May 2014 08:08PM UTC coverage: 39.344% (-57.9%) from 97.237%
#812

push

jdantonio
Merge pull request #96 from jdantonio/refactor/errors

Moved all custom errors into a single file and into the Concurrent module

17 of 18 new or added lines in 10 files covered. (94.44%)

1435 existing lines in 57 files now uncovered.

1187 of 3017 relevant lines covered (39.34%)

0.5 hits per line

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

29.63
/lib/concurrent/runnable.rb
1
require 'thread'
1✔
2

3
require 'concurrent/errors'
1✔
4

5
module Concurrent
1✔
6

7
  module Runnable
1✔
8

9
    class Context
1✔
10
      attr_reader :runner, :thread
1✔
11
      def initialize(runner)
1✔
UNCOV
12
        @runner = runner
×
UNCOV
13
        @thread = Thread.new(runner) do |runner|
×
UNCOV
14
          Thread.abort_on_exception = false
×
UNCOV
15
          runner.run
×
16
        end
UNCOV
17
        @thread.join(0.1) # let the thread start
×
18
      end
19
    end
20

21
    def self.included(base)
1✔
22

23
      class << base
1✔
24

25
        def run!(*args, &block)
1✔
UNCOV
26
          runner = self.new(*args, &block)
×
UNCOV
27
          return Context.new(runner)
×
28
        rescue => ex
UNCOV
29
          return nil
×
30
        end
31
      end
32
    end
33

34
    def run!(abort_on_exception = false)
1✔
UNCOV
35
      raise LifecycleError.new('already running') if @running
×
UNCOV
36
      thread = Thread.new do
×
UNCOV
37
        Thread.current.abort_on_exception = abort_on_exception
×
UNCOV
38
        self.run
×
39
      end
UNCOV
40
      thread.join(0.1) # let the thread start
×
UNCOV
41
      return thread
×
42
    end
43

44
    def run
1✔
UNCOV
45
      mutex.synchronize do
×
UNCOV
46
        raise LifecycleError.new('already running') if @running
×
UNCOV
47
        raise LifecycleError.new('#on_task not implemented') unless self.respond_to?(:on_task, true)
×
UNCOV
48
        on_run if respond_to?(:on_run, true)
×
UNCOV
49
        @running = true
×
50
      end
51

UNCOV
52
      loop do
×
UNCOV
53
        break unless @running
×
UNCOV
54
        on_task
×
UNCOV
55
        break unless @running
×
UNCOV
56
        Thread.pass
×
57
      end
58

UNCOV
59
      after_run if respond_to?(:after_run, true)
×
UNCOV
60
      return true
×
61
    rescue LifecycleError => ex
UNCOV
62
      @running = false
×
UNCOV
63
      raise ex
×
64
    rescue => ex
UNCOV
65
      @running = false
×
UNCOV
66
      return false
×
67
    end
68

69
    def stop
1✔
UNCOV
70
      return true unless @running
×
UNCOV
71
      mutex.synchronize do
×
UNCOV
72
        @running = false
×
UNCOV
73
        on_stop if respond_to?(:on_stop, true)
×
74
      end
UNCOV
75
      return true
×
76
    rescue => ex
UNCOV
77
      return false
×
78
    end
79

80
    def running?
1✔
UNCOV
81
      return @running == true
×
82
    end
83

84
    protected
1✔
85

86
    def mutex
1✔
UNCOV
87
      @mutex ||= Mutex.new
×
88
    end
89
  end
90
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc