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

FI-2791: Revert resume test route refactor #497

Merged
merged 2 commits into from
Jun 5, 2024
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
79 changes: 59 additions & 20 deletions lib/inferno/dsl/resume_test_route.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,102 @@
require_relative 'suite_endpoint'
require 'hanami/controller'

module Inferno
module DSL
# A base class for creating routes to resume test execution upon receiving
# an incoming request.
# @private
# @see Inferno::DSL::Runnable#resume_test_route
class ResumeTestRoute < SuiteEndpoint
# The incoming request
#
# @return [Inferno::Entities::Request]
def request
@request ||= Inferno::Entities::Request.from_hanami_request(req)
class ResumeTestRoute < Hanami::Action
include Import[
requests_repo: 'inferno.repositories.requests',
results_repo: 'inferno.repositories.results',
test_runs_repo: 'inferno.repositories.test_runs',
tests_repo: 'inferno.repositories.tests'
]

def self.call(...)
new.call(...)
end

# @private
def test_run_identifier
@test_run_identifier ||= instance_exec(request, &test_run_identifier_block)
def test_run_identifier_block
self.class.singleton_class.instance_variable_get(:@test_run_identifier_block)
end

# @private
def tags
self.class.singleton_class.instance_variable_get(:@tags) || []
self.class.singleton_class.instance_variable_get(:@tags)
end

# @private
def update_result
results_repo.update_result(result.id, new_result)
def result
self.class.singleton_class.instance_variable_get(:@result)
end

# @private
def make_response
res.redirect_to redirect_route(test_run, test)
def find_test_run(test_run_identifier)
test_runs_repo.find_latest_waiting_by_identifier(test_run_identifier)
end

# @private
def name
test.config.request_name(test.incoming_request_name)
def find_waiting_result(test_run)
results_repo.find_waiting_result(test_run_id: test_run.id)
end

# @private
def test_run_identifier_block
self.class.singleton_class.instance_variable_get(:@test_run_identifier_block)
def update_result(waiting_result)
results_repo.update_result(waiting_result.id, result)
end

# @private
def new_result
self.class.singleton_class.instance_variable_get(:@new_result)
def persist_request(request, test_run, waiting_result, test)
requests_repo.create(
request.to_hash.merge(
test_session_id: test_run.test_session_id,
result_id: waiting_result.id,
name: test.config.request_name(test.incoming_request_name),
tags:
)
)
end

# @private
def redirect_route(test_run, test)
"#{Application['base_url']}/test_sessions/#{test_run.test_session_id}##{resume_ui_at_id(test_run, test)}"
end

# @private
def find_test(waiting_result)
tests_repo.find(waiting_result.test_id)
end

# @private
def resume_ui_at_id(test_run, test)
test_run.test_suite_id || test_run.test_group_id || test.parent.id
end

# @private
def handle(req, res)
request = Inferno::Entities::Request.from_hanami_request(req)

test_run_identifier = instance_exec(request, &test_run_identifier_block)

test_run = find_test_run(test_run_identifier)

halt 500, "Unable to find test run with identifier '#{test_run_identifier}'." if test_run.nil?

test_runs_repo.mark_as_no_longer_waiting(test_run.id)

waiting_result = find_waiting_result(test_run)
test = find_test(waiting_result)

update_result(waiting_result)
persist_request(request, test_run, waiting_result, test)

Jobs.perform(Jobs::ResumeTestRun, test_run.id)

res.redirect_to redirect_route(test_run, test)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/inferno/dsl/runnable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def resume_test_route(method, path, tags: [], result: 'pass', &block)
route_class = Class.new(ResumeTestRoute) do |klass|
klass.singleton_class.instance_variable_set(:@test_run_identifier_block, block)
klass.singleton_class.instance_variable_set(:@tags, tags)
klass.singleton_class.instance_variable_set(:@new_result, result)
klass.singleton_class.instance_variable_set(:@result, result)
end

route(method, path, route_class)
Expand Down
2 changes: 1 addition & 1 deletion lib/inferno/dsl/suite_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def persist_request

# @private
def resume_test_run?
find_result&.result != 'waiting'
find_result&.result != 'wait'
end

# @private
Expand Down
Loading