Skip to content

Commit

Permalink
fix: only call launch_finished once | bump: 3.0.2.beta.1
Browse files Browse the repository at this point in the history
  • Loading branch information
alexo committed Jan 29, 2024
1 parent 2a6a3f2 commit 62e5d30
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 45 deletions.
5 changes: 5 additions & 0 deletions lib/parallel_report_portal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def self.configure(&block)
if ParallelReportPortal.parallel?
if ParallelTests.first_process?
ParallelTests.wait_for_other_processes_to_finish

launch_id = File.read(launch_id_file)
response = http_repeater { req_launch_finished(launch_id, clock) }
response.success? ? parse_report_link_from_response(response) : force_stop(launch_id, clock)

delete_file(launch_id_file)
delete_file(hierarchy_file)
end
Expand Down
31 changes: 10 additions & 21 deletions lib/parallel_report_portal/cucumber/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ class Report
unknown: 'UNKNOWN'
}


# Create a new instance of the report
def initialize(ast_lookup = nil)
@feature = nil
@tree = Tree::TreeNode.new( 'root' )
@tree = Tree::TreeNode.new('root')
@ast_lookup = ast_lookup
check_faraday_compatibility
end
Expand All @@ -38,7 +37,7 @@ def check_faraday_compatibility
end
end

# Issued to start a launch. It is possilbe that this method could be called
# Issued to start a launch. It is possible that this method could be called
# from multiple processes for the same launch if this is being run with
# parallel tests enabled. A temporary launch file will be created (using
# exclusive locking). The first time this method is called it will write the
Expand All @@ -48,13 +47,13 @@ def check_faraday_compatibility
# @param start_time [Integer] the millis from the epoch
# @return [String] the UUID of this launch
def launch_started(start_time)
ParallelReportPortal.file_open_exlock_and_block(ParallelReportPortal.launch_id_file, 'a+' ) do |file|
ParallelReportPortal.file_open_exlock_and_block(ParallelReportPortal.launch_id_file, 'a+') do |file|
if file.size == 0
@launch_id = ParallelReportPortal.req_launch_started(start_time)
file.write(@launch_id)
file.flush
else
@launch_id = file.readline
@launch_id = file.readline
end
@launch_id
end
Expand All @@ -65,12 +64,8 @@ def launch_started(start_time)
# @param clock [Integer] the millis from the epoch
def launch_finished(clock)
@tree.postordered_each do |node|
response = ParallelReportPortal.req_feature_finished(node.content, clock) unless node.is_root?
parse_report_link_from_response(response)
ParallelReportPortal.http_repeater { ParallelReportPortal.req_feature_finished(node.content, clock) } unless node.is_root?
end
response = ParallelReportPortal.req_launch_finished(launch_id, clock)
parse_report_link_from_response(response)
ParallelReportPortal.launch_finished_block.call if ParallelReportPortal.launch_finished_block
end

# Called to indicate that a feature has started.
Expand Down Expand Up @@ -114,7 +109,7 @@ def test_step_started(event, clock)
if (using_cucumber_messages? ? test_step : step_source).multiline_arg.doc_string?
detail << %(\n"""\n#{(using_cucumber_messages? ? test_step : step_source).multiline_arg.content}\n""")
elsif (using_cucumber_messages? ? test_step : step_source).multiline_arg.data_table?
detail << (using_cucumber_messages? ? test_step : step_source).multiline_arg.raw.reduce("\n") {|acc, row| acc << "| #{row.join(' | ')} |\n"}
detail << (using_cucumber_messages? ? test_step : step_source).multiline_arg.raw.reduce("\n") { |acc, row| acc << "| #{row.join(' | ')} |\n" }
end

ParallelReportPortal.req_log(@test_case_id, detail, status_to_level(:trace), clock)
Expand Down Expand Up @@ -155,13 +150,14 @@ def hierarchy(feature, clock)
else
feature.location.file.split(File::SEPARATOR)
end
ParallelReportPortal.file_open_exlock_and_block(ParallelReportPortal.hierarchy_file, 'a+b' ) do |file|

ParallelReportPortal.file_open_exlock_and_block(ParallelReportPortal.hierarchy_file, 'a+b') do |file|
@tree = Marshal.load(File.read(file)) if file.size > 0
node = @tree.root
path_components[0..-2].each do |component|
next_node = node[component]
unless next_node
id = ParallelReportPortal.req_hierarchy(launch_id, "Folder: #{component}", node.content, 'SUITE', [], nil, clock )
id = ParallelReportPortal.req_hierarchy(launch_id, "Folder: #{component}", node.content, 'SUITE', [], nil, clock)
next_node = Tree::TreeNode.new(component, id)
node << next_node
node = next_node
Expand Down Expand Up @@ -219,7 +215,7 @@ def hook?(test_step)
if using_cucumber_messages?
test_step.hook?
else
! test_step.source.last.respond_to?(:keyword)
!test_step.source.last.respond_to?(:keyword)
end
end

Expand All @@ -235,13 +231,6 @@ def status_to_level(status)
LOG_LEVELS.fetch(status, LOG_LEVELS[:info])
end
end

def parse_report_link_from_response(response)
if response
json = JSON.parse(response.body)
ParallelReportPortal.report_url = json['link'] if json['link']
end
end
end
end
end
50 changes: 48 additions & 2 deletions lib/parallel_report_portal/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module ParallelReportPortal
# REST interface.
module HTTP

class HTTPBadGatewayError < StandardError; end

# Creating class level logger and setting log level
@@logger = Logger.new(STDOUT)
@@logger.level = Logger::ERROR
Expand Down Expand Up @@ -93,9 +95,30 @@ def req_launch_started(time)
# Send a request to Report Portal to finish a launch.
# It will bubble up any Faraday connection exceptions.
def req_launch_finished(launch_id, time)
ParallelReportPortal.http_connection.put("launch/#{launch_id}/finish") do |req|
@@logger.debug { "Launch finish with ID: '#{launch_id}'" }

response = ParallelReportPortal.http_connection.put("launch/#{launch_id}/finish") do |req|
req.body = { end_time: time }.to_json
end

@@logger.error { "Launch finish failed with response code #{response.status} -- message #{response.body}" } unless response.success?
response
end

def force_stop(launch_id, time)
resp = ParallelReportPortal.http_connection.put("launch/#{launch_id}/stop") do |req|
req.body = { end_time: time, status: :stopped }.to_json
end
@@logger.warn { "Failed to force stop: response code #{resp.status} -- message #{resp.body}" } unless resp.success?

resp
end

def parse_report_link_from_response(response)
if response
json = JSON.parse(response.body)
ParallelReportPortal.report_url = json['link'] if json['link']
end
end

# Send a request to ReportPortal to start a feature.
Expand Down Expand Up @@ -145,9 +168,14 @@ def req_hierarchy(launch_id, name, parent, type, tags, description, time )

# Send a request to Report Portal that a feature has completed.
def req_feature_finished(feature_id, time)
ParallelReportPortal.http_connection.put("item/#{feature_id}") do |req|
response = ParallelReportPortal.http_connection.put("item/#{feature_id}") do |req|
req.body = { end_time: time }.to_json
end
@@logger.debug { "Feature finish with ID: '#{feature_id}'" }


@@logger.error { "Feature finish failed with response code #{response.status} -- message #{response.body}" } unless response.success?
response
end

# Send a request to ReportPortal to start a test case.
Expand Down Expand Up @@ -236,5 +264,23 @@ def send_file(
end
end
end



def http_repeater(&block)
tries = 0
begin
tries += 1
response = block.call
raise HTTPBadGatewayError if response && !response.success? && response.status == 502
rescue HTTPBadGatewayError => _e
if tries <= 3
sleep(1)
@@logger.warn { 'HTTP call failed, retrying...' }
retry
end
end
response
end
end
end
2 changes: 1 addition & 1 deletion lib/parallel_report_portal/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ParallelReportPortal
VERSION = "3.0.1"
VERSION = "3.0.2.beta.1"
end
2 changes: 1 addition & 1 deletion parallel_report_portal.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'faraday-net_http_persistent', '~> 2.1'
spec.add_runtime_dependency 'faraday-multipart', '~> 1.0', '>= 1.0.4'
spec.add_runtime_dependency 'parallel_tests', '>= 2.29.1'
spec.add_runtime_dependency 'rubytree', '~> 1.0'
spec.add_runtime_dependency 'rubytree', '~> 2.0'
spec.add_runtime_dependency 'net-http-persistent', '~> 4.0'
end
20 changes: 0 additions & 20 deletions spec/parallel_report_portal/cucumber/report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,13 @@
context 'handling launch finished events' do
let(:uuid) { 'b56818b2-391e-4844-8a6e-144afaf504ed' }

it 'terminates the launch' do
report.instance_variable_set(:@launch_id, uuid)
expect(ParallelReportPortal).to receive(:req_launch_finished)
.with(uuid, 0).once

report.launch_finished(0)
end

it 'issues executes a post launch hook' do
report.instance_variable_set(:@launch_id, uuid)
expect(ParallelReportPortal).to receive(:req_launch_finished)

called = false
ParallelReportPortal.after_launch { called = true }
report.launch_finished(0)
expect(called).to eq true
end

it 'terminates any existing child items before terminating the launch' do
tree = Tree::TreeNode.new( 'root' )
tree << Tree::TreeNode.new('child', 'child_node_uuid')

report.instance_variable_set(:@launch_id, uuid)
report.instance_variable_set(:@tree, tree)

expect(ParallelReportPortal).to receive(:req_launch_finished)
.with(uuid, 0).once
expect(ParallelReportPortal).to receive(:req_feature_finished)
.with('child_node_uuid', 0).once

Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
require 'tree'
require 'yaml'

require 'pry'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
Expand Down

0 comments on commit 62e5d30

Please sign in to comment.