Skip to content

Commit d0a6d68

Browse files
committed
Merge pull request #307 from ruby-concurrency/more-async
Recent changes to Async are now backward compatible.
2 parents 24b55fc + 9c52d3b commit d0a6d68

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

lib/concurrent/async.rb

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,23 @@ def self.validate_argc(obj, method, *args)
128128

129129
# @!visibility private
130130
def self.included(base)
131+
base.singleton_class.send(:alias_method, :original_new, :new)
132+
base.send(:private_class_method, :original_new)
131133
base.extend(ClassMethods)
132-
base.send(:private_class_method, :new)
133-
super(base)
134-
end
135-
136-
# @!visibility private
137-
def self.extended(base)
138-
base.extend(ClassMethods)
139-
base.send(:private_class_method, :new)
140134
super(base)
141135
end
142136

143137
# @!visibility private
144138
module ClassMethods
139+
140+
# @deprecated
141+
def new(*args, &block)
142+
warn '[DEPRECATED] use the `create` method instead'
143+
create(*args, &block)
144+
end
145+
145146
def create(*args, &block)
146-
obj = self.send(:new, *args, &block)
147+
obj = original_new(*args, &block)
147148
obj.send(:init_synchronization)
148149
obj
149150
end
@@ -269,6 +270,22 @@ def executor=(executor)
269270
raise ArgumentError.new('executor has already been set')
270271
end
271272

273+
# Initialize the internal serializer and other stnchronization mechanisms.
274+
#
275+
# @note This method *must* be called immediately upon object construction.
276+
# This is the only way thread-safe initialization can be guaranteed.
277+
#
278+
# @raise [Concurrent::InitializationError] when called more than once
279+
#
280+
# @!visibility private
281+
# @deprecated
282+
def init_mutex
283+
warn '[DEPRECATED] use the `create` method instead'
284+
init_synchronization
285+
rescue InitializationError
286+
# suppress
287+
end
288+
272289
private
273290

274291
# Initialize the internal serializer and other stnchronization mechanisms.

spec/concurrent/async_spec.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module Concurrent
88
Class.new do
99
include Concurrent::Async
1010
attr_accessor :accessor
11+
def initialize(*args)
12+
end
1113
def echo(msg)
1214
msg
1315
end
@@ -34,8 +36,16 @@ def with_block
3436

3537
context 'object creation' do
3638

37-
it 'makes #new private' do
38-
expect{ async_class.new }.to raise_error(NoMethodError)
39+
# Will be added in 1.0 once deprecated methods are removed
40+
#it 'makes #new private' do
41+
# expect{ async_class.new }.to raise_error(NoMethodError)
42+
#end
43+
44+
# Will be removed in 1.0 once deprecated methods are removed
45+
it '#new delegates to #create' do
46+
args = [:foo, 'bar', 42]
47+
expect(async_class).to receive(:create).once.with(*args)
48+
async_class.new(*args)
3949
end
4050

4151
it 'uses #create to instanciate new objects' do
@@ -45,7 +55,7 @@ def with_block
4555

4656
specify '#create initializes synchronization' do
4757
mock = async_class.create
48-
allow(async_class).to receive(:new).and_return(mock)
58+
allow(async_class).to receive(:original_new).and_return(mock)
4959
expect(mock).to receive(:init_synchronization).once.with(no_args)
5060
async_class.create
5161
end

0 commit comments

Comments
 (0)