Class: Testjour::CLI::Run

Inherits:
BaseCommand show all
Defined in:
lib/testjour/commands/run.rb

Instance Attribute Summary

Attributes inherited from BaseCommand

#non_options, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

detailed_help, help, options

Constructor Details

#initialize(*args) ⇒ Run

Returns a new instance of Run.



15
16
17
18
19
20
21
22
23
# File 'lib/testjour/commands/run.rb', line 15

def initialize(*args)
  Testjour.logger.debug "Runner command #{self.class}..."
  Testjour.load_cucumber
  
  super
  @found_server = 0
  require "testjour/cucumber_extensions/queueing_executor"
  require "testjour/colorer"
end

Class Method Details

.commandObject



11
12
13
# File 'lib/testjour/commands/run.rb', line 11

def self.command
  "run"
end

Instance Method Details

#available_serversObject



72
73
74
# File 'lib/testjour/commands/run.rb', line 72

def available_servers
  @available_servers ||= Testjour::Bonjour.list
end

#command_for_local_runObject



119
120
121
# File 'lib/testjour/commands/run.rb', line 119

def command_for_local_run
  "#{testjour_bin_path} local:run #{testjour_uri} -- #{@non_options.join(' ')}".strip
end

#disable_cucumber_requireObject



40
41
42
43
44
45
46
# File 'lib/testjour/commands/run.rb', line 40

def disable_cucumber_require
  Cucumber::CLI.class_eval do
    def require_files
      ARGV.clear # Shut up RSpec
    end
  end
end

#no_remote?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/testjour/commands/run.rb', line 141

def no_remote?
  @options[:no_remote]
end

#option_parserObject



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/testjour/commands/run.rb', line 149

def option_parser
  OptionParser.new do |opts|
    opts.on("--on SERVER", "Specify a pattern to exclude servers to. Disabled local runners") do |server|
      @options[:server] ||= []
      @options[:server] << server
    end
    
    opts.on("--no-remote", "Only use local runners") do |server|
      @options[:no_remote] = true
    end
  end
end


56
57
58
59
60
61
62
63
# File 'lib/testjour/commands/run.rb', line 56

def print_results
  puts
  puts "Requesting build from #{@found_server} processes..."
  puts
  
  Cucumber::CLI.executor.wait_for_results
  Testjour.logger.debug "DONE"
end

#queue_features(queue) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/testjour/commands/run.rb', line 48

def queue_features(queue)
  Testjour.logger.debug "Queueing features..."
  disable_cucumber_require
  ARGV.replace(@non_options.clone)
  Cucumber::CLI.executor = Testjour::QueueingExecutor.new(queue, Cucumber::CLI.step_mother)
  Cucumber::CLI.execute
end

#request_build_from(server) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/testjour/commands/run.rb', line 76

def request_build_from(server)
  slave_server = DRbObject.new(nil, server.uri)
  result = slave_server.run(testjour_uri, @non_options)
  
  if result
    Testjour.logger.info "Requesting buld from available server: #{server.uri}. Accepted."
    @found_server += 1
  else
    Testjour.logger.info "Requesting buld from available server: #{server.uri}. Rejected."
  end
end

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/testjour/commands/run.rb', line 25

def run
  Testjour::QueueServer.with_server do |queue|
    start_local_runners unless servers_specified?
    start_slave_runners unless no_remote?
    
    if @found_server.zero?
      puts "No processes to build on. Aborting."
      exit
    end
    
    queue_features(queue)
    print_results
  end
end

#servers_specified?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/testjour/commands/run.rb', line 145

def servers_specified?
  @options[:server] && @options[:server].any?
end

#slave_servers_to_useObject



65
66
67
68
69
70
# File 'lib/testjour/commands/run.rb', line 65

def slave_servers_to_use
  # require "rubygems"; require "ruby-debug"; Debugger.start; debugger
  @slave_servers_to_use ||= available_servers.select do |potential_server|
    !servers_specified? || specified_servers_include?(potential_server)
  end
end

#specified_servers_include?(potential_server) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
138
139
# File 'lib/testjour/commands/run.rb', line 135

def specified_servers_include?(potential_server)
  @options[:server].any? do |specified_server|
    potential_server.host.include?(specified_server)
  end
end

#start_local_runnerObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/testjour/commands/run.rb', line 100

def start_local_runner
  pid_queue = Queue.new

  Thread.new do
    Thread.current.abort_on_exception = true
    cmd = command_for_local_run
    Testjour.logger.debug "Starting local:run with command: #{cmd}"
    status, stdout, stderr = systemu(cmd) { |pid| pid_queue << pid }
    Testjour.logger.warn stderr if stderr.strip.size > 0
  end

  pid = pid_queue.pop
  
  @found_server += 1
  Testjour.logger.info "Started local:run on PID #{pid}"
  
  pid
end

#start_local_runnersObject



88
89
90
91
92
# File 'lib/testjour/commands/run.rb', line 88

def start_local_runners
  2.times do 
    start_local_runner
  end
end

#start_slave_runnersObject



94
95
96
97
98
# File 'lib/testjour/commands/run.rb', line 94

def start_slave_runners
  slave_servers_to_use.each do |server|
    request_build_from(server)
  end
end

#testjour_bin_pathObject



123
124
125
# File 'lib/testjour/commands/run.rb', line 123

def testjour_bin_path
  File.expand_path(File.dirname(__FILE__) + "/../../../bin/testjour")
end

#testjour_uriObject



127
128
129
130
131
132
133
# File 'lib/testjour/commands/run.rb', line 127

def testjour_uri
  uri = URI.parse(DRb.uri)
  uri.path = File.expand_path(".")
  uri.scheme = "testjour"
  uri.user = `whoami`.strip
  uri.to_s
end