Skip to content

Commit

Permalink
update concurrent processing test
Browse files Browse the repository at this point in the history
Use `Thread#stop`/`#run` instead of `#sleep` to avoid OS-sensitive race condition.
  • Loading branch information
wjordan committed Mar 3, 2018
1 parent 69ead85 commit 05b27b6
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions test/test_manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -702,27 +702,31 @@ def teardown
end
end

# Sleep duration to context switch between concurrent threads.
CONTEXT_SWITCH_DURATION = 0.001

# Record Processor sequence with a delay to test concurrency.
# Record Processor sequence with thread context-switching to test concurrency.
class SlowProcessor
attr_reader :seq
attr_reader :seq, :threads, :total

def initialize
def initialize(total)
@total = total
@seq = []
@threads = []
end

def call(_)
seq << '0'
sleep CONTEXT_SWITCH_DURATION
threads << Thread.current
if threads.length == total
threads.map(&:run)
else
Thread.stop
end
seq << '1'
nil
end
end

test 'concurrent processing' do
processor = SlowProcessor.new
processor = SlowProcessor.new(2)
@env.register_postprocessor 'image/png', processor
Sprockets::Manifest.new(@env, @dir).compile('logo.png', 'troll.png')
assert_equal %w(0 0 1 1), processor.seq
Expand Down

0 comments on commit 05b27b6

Please sign in to comment.