Class: DatatrueClient::TestRun

Inherits:
Object
  • Object
show all
Defined in:
lib/datatrue_client/test_run.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TestRun

Returns a new instance of TestRun.

Parameters:

  • options (Hash) (defaults to: {})

    options =

    polling_interval: 0.1,
    polling_timeout: 1
    

    options will also be passed to ‘Bridge.new` and `Bridge#create_test_run`, see `Bridge` for other possible option keys



65
66
67
68
69
70
71
72
73
# File 'lib/datatrue_client/test_run.rb', line 65

def initialize(options={})
  @options = options
  @bridge = Bridge.new(options)

  @polling_timeout = options[:polling_timeout] || 60
  @polling_interval = options[:polling_interval] || 2

  create
end

Instance Attribute Details

#job_idObject (readonly)

Returns the value of attribute job_id.



55
56
57
# File 'lib/datatrue_client/test_run.rb', line 55

def job_id
  @job_id
end

#polling_intervalObject

Returns the value of attribute polling_interval.



56
57
58
# File 'lib/datatrue_client/test_run.rb', line 56

def polling_interval
  @polling_interval
end

#polling_timeoutObject

Returns the value of attribute polling_timeout.



56
57
58
# File 'lib/datatrue_client/test_run.rb', line 56

def polling_timeout
  @polling_timeout
end

#progressObject (readonly)

Returns the value of attribute progress.



55
56
57
# File 'lib/datatrue_client/test_run.rb', line 55

def progress
  @progress
end

#titleObject (readonly)

Returns the value of attribute title.



55
56
57
# File 'lib/datatrue_client/test_run.rb', line 55

def title
  @title
end

Class Method Details

.build_progress_message(details) ⇒ String

Parameters:

  • details (Hash)

    The progress details hash. See CI API doc for the shape.

Returns:

  • (String)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/datatrue_client/test_run.rb', line 25

def build_progress_message(details)
  status = get_progress_status(details)
  tests = details.dig(:progress, 'tests') || []
  steps = tests.map { |test| test['steps'] }.compact.flatten
  total_steps = details.dig(:progress, 'steps_total')
  current_step_index = steps.index { |step| step['running'] }

  if current_step_index
    # read step and crawled pages details
    step = steps[current_step_index]
    is_coverage_step = step['actions_total'] > 1
    coverage_step_details = " (#{step['actions_completed']}/#{step['actions_total']} pages)"

    message = %{
      test_run_id=#{details[:options]['test_run_id']}
      step=#{current_step_index + 1}#{is_coverage_step ? coverage_step_details : ''}
      total_steps=#{total_steps}
      result=#{status}
    }
  else
    message = %{
      test_run_id=#{details[:options]['test_run_id']}
      result=#{status}
    }
  end

  message.gsub(/\s+/, ' ').strip
end

.get_progress_status(details) ⇒ String

Parameters:

  • details (Hash)

    The progress details hash. See CI API doc for the shape.

Returns:

  • (String)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/datatrue_client/test_run.rb', line 11

def get_progress_status(details)
  if details[:status] == 'completed'
    if details[:progress]['tests'].all? { |result| ['success', 'validated'].include? result['state'] }
      status = 'passed'
    else
      status = 'failed'
    end
  else
    status = details[:status]
  end
end

Instance Method Details

#poll_progress(print_progress = nil) ⇒ Object

Keeps querying progress until progress status is ‘complete`

Parameters:

  • print_progress (Proc) (defaults to: nil)

    called with current progress percentage and details



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/datatrue_client/test_run.rb', line 83

def poll_progress(print_progress=nil)
  start_time = DateTime.now

  loop do
    res = query_progress
    @progress = progress_percentage(res)
    print_progress.call(@progress, res) if print_progress

    return res if res[:status] == 'completed'

    if elapsed_milliseconds(start_time, DateTime.now) >= @polling_timeout * 1000
      raise TimeoutError.new("Polling progress timed out after #{@polling_timeout} seconds")

      break
    else
      sleep @polling_interval
    end
  end
end

#query_progressHash

Queries test run progress

Returns:

  • (Hash)

    data about progress



77
78
79
# File 'lib/datatrue_client/test_run.rb', line 77

def query_progress
  @bridge.test_run_progress(job_id)
end