Skip to content

Commit 971cb10

Browse files
authored
Merge pull request #138 from ahorek/concurrency_fix
thread safe function helpers
2 parents b20c8b0 + 31ffcde commit 971cb10

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

lib/sassc/functions_handler.rb

+5-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ def setup(native_options)
1212

1313
list = Native.make_function_list(Script.custom_functions.count)
1414

15-
functions = FunctionWrapper.extend(Script::Functions)
15+
# use an anonymous class wrapper to avoid mutations in a threaded environment
16+
functions = Class.new do
17+
attr_accessor :options
18+
include Script::Functions
19+
end.new
1620
functions.options = @options
1721

1822
Script.custom_functions.each_with_index do |custom_function, i|
@@ -65,11 +69,5 @@ def error(message)
6569
$stderr.puts "[SassC::FunctionsHandler] #{message}"
6670
Native.make_error(message)
6771
end
68-
69-
class FunctionWrapper
70-
class << self
71-
attr_accessor :options
72-
end
73-
end
7472
end
7573
end

test/functions_test.rb

+20
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ def test_function_that_takes_a_sass_list
185185
CSS
186186
end
187187

188+
def test_concurrency
189+
10.times do
190+
threads = []
191+
10.times do |i|
192+
threads << Thread.new(i) do |id|
193+
out = Engine.new("div { url: inspect_options(); }", {test_key1: 'test_value', test_key2: id}).render
194+
assert_match /test_key1/, out
195+
assert_match /test_key2/, out
196+
assert_match /test_value/, out
197+
assert_match /#{id}/, out
198+
end
199+
end
200+
threads.each(&:join)
201+
end
202+
end
203+
188204
private
189205

190206
def assert_sass(sass, expected_css)
@@ -279,6 +295,10 @@ def inspect_list(argument)
279295
return argument
280296
end
281297

298+
def inspect_options
299+
SassC::Script::Value::String.new(self.options.inspect, :string)
300+
end
301+
282302
def returns_sass_value
283303
return SassC::Script::Value::Color.new(red: 0, green: 0, blue: 0)
284304
end

0 commit comments

Comments
 (0)