Skip to content

Commit

Permalink
Merge pull request #171 from tmm1/thread_safe
Browse files Browse the repository at this point in the history
Thread Safety
  • Loading branch information
gfx authored Dec 24, 2016
2 parents 46c74dd + fb1a6a9 commit a9d2641
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
29 changes: 27 additions & 2 deletions lib/pygments.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
require File.join(File.dirname(__FILE__), 'pygments/popen')

require 'forwardable'

module Pygments
extend Pygments::Popen

autoload :Lexer, 'pygments/lexer'

class << self
extend Forwardable

def engine
Thread.current.thread_variable_get(:pygments_engine) ||
Thread.current.thread_variable_set(:pygments_engine, Pygments::Popen.new)
end

def_delegators :engine,
# public
:formatters,
:lexers,
:lexers!,
:filters,
:styles,
:css,
:lexer_name_for,
:highlight,
# not public but documented
:start,
# only for testing
:size_check,
:get_fixed_bits_from_header,
:add_ids
end
end
3 changes: 1 addition & 2 deletions lib/pygments/popen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class MentosError < IOError
# Pygments provides access to the Pygments library via a pipe and a long-running
# Python process.
module Pygments
module Popen
extend self
class Popen

def popen4(cmd)
stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
Expand Down
11 changes: 11 additions & 0 deletions test/test_pygments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ def test_highlight_still_works_with_invalid_code
code = P.highlight("importr python; wat?", :lexer => 'py')
assert_match ">importr</span>", code
end

def test_highlight_on_multi_threads
10.times.map do
Thread.new do
test_full_html_highlight
end
end.each do |thread|
thread.join
end
end

end

# Philosophically, I'm not the biggest fan of testing private
Expand Down

0 comments on commit a9d2641

Please sign in to comment.