-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from bitjourney/timeout_and_errors
add timeout and rescue options
- Loading branch information
Showing
8 changed files
with
170 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mato | ||
class Rescue | ||
attr_reader :filter | ||
attr_reader :on_error | ||
|
||
def initialize(filter, on_error:) | ||
@filter = filter | ||
@on_error = on_error | ||
end | ||
|
||
def call(content) | ||
filter.call(content) | ||
rescue => e | ||
on_error.call(e) | ||
content | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'timeout' | ||
|
||
module Mato | ||
class Timeout | ||
attr_reader :filter | ||
attr_reader :duration_sec | ||
attr_reader :on_timeout | ||
|
||
def initialize(filter, timeout:, on_timeout:) | ||
@filter = filter | ||
@duration_sec = timeout | ||
@on_timeout = on_timeout | ||
|
||
unless on_timeout | ||
raise ArgumentError, "Missing on_timeout callback" | ||
end | ||
end | ||
|
||
def call(content) | ||
::Timeout.timeout(duration_sec) do | ||
filter.call(content) | ||
end | ||
rescue ::Timeout::Error => e | ||
on_timeout.call(e) | ||
content | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative './test_helper' | ||
|
||
class MatoRescueTest < MyTest | ||
def test_text_filters | ||
error = nil | ||
mato = Mato.define do |config| | ||
config.append_text_filter(->(text) { | ||
raise 'Hello!' | ||
}, on_error: ->(e) { error = e }) | ||
end | ||
|
||
doc = mato.process('Hello, world!') | ||
|
||
assert do | ||
doc.render_html == "<p>Hello, world!</p>\n" | ||
end | ||
|
||
assert do | ||
error.message == 'Hello!' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative './test_helper' | ||
|
||
class MatoTimeoutTest < MyTest | ||
def test_text_filters | ||
timeout = nil | ||
mato = Mato.define do |config| | ||
config.append_text_filter(->(text) { | ||
sleep | ||
}, timeout: 0.1, on_timeout: ->(e) { timeout = e }) | ||
end | ||
|
||
doc = mato.process('Hello, world!') | ||
|
||
assert do | ||
doc.render_html == "<p>Hello, world!</p>\n" | ||
end | ||
|
||
assert do | ||
timeout != nil | ||
end | ||
end | ||
|
||
def test_text_filters_with_on_error | ||
timeout = nil | ||
mato = Mato.define do |config| | ||
config.append_text_filter(->(text) { | ||
sleep | ||
}, timeout: 0.1, on_error: ->(e) { timeout = e }) | ||
end | ||
|
||
doc = mato.process('Hello, world!') | ||
|
||
assert do | ||
doc.render_html == "<p>Hello, world!</p>\n" | ||
end | ||
|
||
assert do | ||
timeout != nil | ||
end | ||
end | ||
end |