Skip to content

Commit 58a2f53

Browse files
committed
SafeTaskExecutor is now a Synchronization::Object
1 parent 7444122 commit 58a2f53

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

lib/concurrent/executor/safe_task_executor.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
require 'thread'
1+
require 'concurrent/synchronization'
22

33
module Concurrent
44

55
# A simple utility class that executes a callable and returns and array of three elements:
66
# success - indicating if the callable has been executed without errors
77
# value - filled by the callable result if it has been executed without errors, nil otherwise
88
# reason - the error risen by the callable if it has been executed with errors, nil otherwise
9-
class SafeTaskExecutor
9+
class SafeTaskExecutor < Synchronization::Object
1010

1111
def initialize(task, opts = {})
12-
@task = task
13-
@mutex = Mutex.new
14-
@exception_class = opts.fetch(:rescue_exception, false) ? Exception : StandardError
12+
super(task, opts)
1513
end
1614

1715
# @return [Array]
1816
def execute(*args)
19-
@mutex.synchronize do
17+
synchronize do
2018
success = false
2119
value = reason = nil
2220

@@ -31,5 +29,12 @@ def execute(*args)
3129
[success, value, reason]
3230
end
3331
end
32+
33+
protected
34+
35+
def ns_initialize(task, opts)
36+
@task = task
37+
@exception_class = opts.fetch(:rescue_exception, false) ? Exception : StandardError
38+
end
3439
end
3540
end

spec/concurrent/executor/safe_task_executor_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ module Concurrent
3232
end
3333

3434
it 'protectes #execute with a mutex' do
35-
mutex = double(:mutex)
36-
expect(Mutex).to receive(:new).with(no_args).and_return(mutex)
37-
expect(mutex).to receive(:synchronize).with(no_args)
35+
expect(subject).to receive(:synchronize).with(no_args)
3836
subject.execute
3937
end
4038
end

0 commit comments

Comments
 (0)