Skip to content

Commit b3045b1

Browse files
committed
Refactor Tracing
1 parent b61f21d commit b3045b1

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

lib/ferrum/page/tracing.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def initialize(page)
2222
def record(path: nil, encoding: :binary, timeout: nil, trace_config: nil, screenshots: false)
2323
@path = path
2424
@encoding = encoding
25-
@result = Concurrent::Promises.resolvable_future
25+
@pending = Concurrent::IVar.new
2626
trace_config ||= DEFAULT_TRACE_CONFIG.dup
2727

2828
if screenshots
@@ -36,7 +36,7 @@ def record(path: nil, encoding: :binary, timeout: nil, trace_config: nil, screen
3636
yield
3737
stop
3838

39-
@result.value!(timeout)
39+
@pending.value!(timeout || @page.timeout)
4040
end
4141

4242
private
@@ -55,9 +55,9 @@ def subscribe_tracing_complete
5555
@page.on("Tracing.tracingComplete") do |event, index|
5656
next if index.to_i != 0
5757

58-
@result.fulfill(stream_handle(event["stream"]))
58+
@pending.set(stream_handle(event["stream"]))
5959
rescue StandardError => e
60-
@result.reject(e)
60+
@pending.fail(e)
6161
end
6262

6363
@subscribed_tracing_complete = true

spec/page/tracing_spec.rb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ module Ferrum
88
let(:content) { JSON.parse(File.read(file_path)) }
99
let(:trace_config) { JSON.parse(content["metadata"]["trace-config"]) }
1010

11+
after do
12+
FileUtils.rm_f(file_path)
13+
FileUtils.rm_f(file_path2)
14+
FileUtils.rm_f(file_path3)
15+
end
16+
1117
it "outputs a trace" do
1218
page.tracing.record(path: file_path) { page.go_to }
1319

1420
expect(File.exist?(file_path)).to be(true)
15-
ensure
16-
FileUtils.rm_f(file_path)
1721
end
1822

1923
it "runs with custom options" do
@@ -29,8 +33,6 @@ module Ferrum
2933
expect(trace_config["excluded_categories"]).to eq(["*"])
3034
expect(trace_config["included_categories"]).to eq(["disabled-by-default-devtools.timeline"])
3135
expect(content["traceEvents"].any? { |o| o["cat"] == "toplevel" }).to eq(false)
32-
ensure
33-
FileUtils.rm_f(file_path)
3436
end
3537

3638
it "runs with default categories" do
@@ -44,11 +46,9 @@ module Ferrum
4446
blink.user_timing latencyInfo disabled-by-default-devtools.timeline.stack
4547
disabled-by-default-v8.cpu_profiler disabled-by-default-v8.cpu_profiler.hires])
4648
expect(content["traceEvents"].any? { |o| o["cat"] == "toplevel" }).to eq(true)
47-
ensure
48-
FileUtils.rm_f(file_path)
4949
end
5050

51-
it "throws an exception if tracing is on two pages" do
51+
it "raises an error when tracing is on two pages" do
5252
page.tracing.record(path: file_path) do
5353
page.go_to
5454

@@ -62,6 +62,15 @@ module Ferrum
6262
expect(File.exist?(file_path)).to be(true)
6363
end
6464

65+
it "raises an error when cannot handle stream" do
66+
allow(page.tracing).to receive(:stream_handle).and_raise("boom")
67+
expect do
68+
page.tracing.record(path: file_path) { page.go_to }
69+
end.to raise_error(RuntimeError, "boom")
70+
71+
expect(File.exist?(file_path)).to be(false)
72+
end
73+
6574
it "handles tracing complete event once" do
6675
expect(page.tracing).to receive(:stream_handle).exactly(3).times.and_call_original
6776

@@ -73,10 +82,6 @@ module Ferrum
7382

7483
page.tracing.record(path: file_path3) { page.go_to }
7584
expect(File.exist?(file_path3)).to be(true)
76-
ensure
77-
FileUtils.rm_f(file_path)
78-
FileUtils.rm_f(file_path2)
79-
FileUtils.rm_f(file_path3)
8085
end
8186

8287
it "returns base64 encoded string" do
@@ -101,8 +106,6 @@ module Ferrum
101106
expect(File.exist?(file_path)).to be(true)
102107
expect(trace_config["included_categories"]).to include("disabled-by-default-devtools.screenshot")
103108
expect(content["traceEvents"].any? { |o| o["name"] == "Screenshot" }).to eq(true)
104-
ensure
105-
FileUtils.rm_f(file_path)
106109
end
107110

108111
it "returns a buffer with screenshot data" do

0 commit comments

Comments
 (0)