Class: Moto::Runner::ThreadContext

Inherits:
Object
  • Object
show all
Defined in:
lib/runner/thread_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test, test_reporter) ⇒ ThreadContext

Returns a new instance of ThreadContext.



13
14
15
16
17
18
19
20
21
22
# File 'lib/runner/thread_context.rb', line 13

def initialize(test, test_reporter)
  @test = test
  @clients = {}
  @test.context = self
  @test_reporter = test_reporter

  # TODO: temporary fix
  Thread.current['context'] = self
  Thread.current['test_environment'] = @test.env
end

Instance Attribute Details

#loggerObject (readonly)

all resources specific for single thread will be initialized here. E.g. browser session



10
11
12
# File 'lib/runner/thread_context.rb', line 10

def logger
  @logger
end

#testObject (readonly)

Returns the value of attribute test.



11
12
13
# File 'lib/runner/thread_context.rb', line 11

def test
  @test
end

Instance Method Details

#client(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/runner/thread_context.rb', line 24

def client(name)
  return @clients[name] if @clients.key? name

  name_app = 'MotoApp::Lib::Clients::' + name
  name_moto = 'Moto::Clients::' + name

  c = try_client(name_app, "#{MotoApp::DIR}/")
  unless c.nil?
    @clients[name] = c
    return c
  end

  c = try_client(name_moto, "#{Moto::DIR}/lib")
  unless c.nil?
    @clients[name] = c
    return c
  end
  raise "Could not find client class for name #{name}"
end

#runObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/runner/thread_context.rb', line 63

def run
  max_attempts = config[:test_attempt_max]   || 1
  sleep_time   = config[:test_attempt_sleep] || 0

  log_directory = File.dirname(@test.log_path)
  if !File.directory?(log_directory)
    FileUtils.mkdir_p(log_directory)
  end

  @logger = Logger.new(File.open(@test.log_path, File::WRONLY | File::TRUNC | File::CREAT))
  @logger.level = config[:test_log_level] || Logger::DEBUG

  # Reporting: start_test
  @test_reporter.report_start_test(@test.status)

  (1..max_attempts).each do |attempt|

    @clients.each_value { |c| c.start_test(@test) }
    @test.before
    @logger.info "Start: #{@test.name} attempt #{attempt}/#{max_attempts}"

    begin
      @test.run_test
    rescue Exceptions::TestForcedPassed, Exceptions::TestForcedFailure, Exceptions::TestSkipped => e
      @logger.info(e.message)
    rescue Exception => e
      @logger.error("#{e.class.name}: #{e.message}")
      @logger.error(e.backtrace.join("\n"))
      @clients.each_value { |c| c.handle_test_exception(@test, e) }
    end

    @test.after
    @clients.each_value { |c| c.end_test(@test) }

    @logger.info("Result: #{@test.status.results.last.code}")

    # test should have another attempt only in case of an error
    # pass, skip and fail statuses end attempts
    if @test.status.results.last.code != Moto::Test::Result::ERROR
      break
    end

    # don't go to sleep in the last attempt
    if attempt < max_attempts
      sleep sleep_time
    end

  end # Make another attempt

  # Close and flush stream to file
  @logger.close

  # Reporting: end_test
  @test_reporter.report_end_test(@test.status)

  @clients.each_value { |c| c.end_run }

end

#try_client(name, dir) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/runner/thread_context.rb', line 44

def try_client(name, dir)
  begin
    a = name.underscore.split('/')
    client_path = a[1..-1].join('/')
    require "#{dir}/#{client_path}"
    client_const = name.constantize
    instance = client_const.new(self)
    instance.init
    instance.start_run
    instance.start_test(@test)
    return instance
  rescue Exception => e
    # puts e
    # puts e.backtrace
    return nil
  end
end