File tree Expand file tree Collapse file tree 2 files changed +12
-9
lines changed Expand file tree Collapse file tree 2 files changed +12
-9
lines changed Original file line number Diff line number Diff line change 1
- require 'thread '
1
+ require 'concurrent/synchronization '
2
2
3
3
module Concurrent
4
4
5
5
# A simple utility class that executes a callable and returns and array of three elements:
6
6
# success - indicating if the callable has been executed without errors
7
7
# value - filled by the callable result if it has been executed without errors, nil otherwise
8
8
# reason - the error risen by the callable if it has been executed with errors, nil otherwise
9
- class SafeTaskExecutor
9
+ class SafeTaskExecutor < Synchronization :: Object
10
10
11
11
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 )
15
13
end
16
14
17
15
# @return [Array]
18
16
def execute ( *args )
19
- @mutex . synchronize do
17
+ synchronize do
20
18
success = false
21
19
value = reason = nil
22
20
@@ -31,5 +29,12 @@ def execute(*args)
31
29
[ success , value , reason ]
32
30
end
33
31
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
34
39
end
35
40
end
Original file line number Diff line number Diff line change @@ -32,9 +32,7 @@ module Concurrent
32
32
end
33
33
34
34
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 )
38
36
subject . execute
39
37
end
40
38
end
You can’t perform that action at this time.
0 commit comments