Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread Safety #171

Merged
merged 2 commits into from
Dec 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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