Class: Spork::RunStrategy::Magazine

Inherits:
Spork::RunStrategy show all
Defined in:
lib/spork/run_strategy/magazine.rb

Constant Summary collapse

Slave_Id_Range =

Ringserver uses id: 0. Slave use: 1..MAX_SLAVES

1..2

Instance Attribute Summary

Attributes inherited from Spork::RunStrategy

#test_framework

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Spork::RunStrategy

#cleanup

Constructor Details

#initialize(test_framework) ⇒ Magazine

Returns a new instance of Magazine.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spork/run_strategy/magazine.rb', line 28

def initialize(test_framework)
  @test_framework = test_framework
  this_path = File.expand_path(File.dirname(__FILE__))
  @path = File.join(this_path, 'magazine')
  @pids = []

  @pids << start_Rinda_ringserver
  sleep 1

  fill_slave_pool
rescue RuntimeError => e
  kill_all_processes
  raise e
end

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/spork/run_strategy/magazine.rb', line 82

def self.available?
  true
end

Instance Method Details

#abortObject



140
141
142
# File 'lib/spork/run_strategy/magazine.rb', line 140

def abort
  kill_all_processes
end

#assert_ready!Object



153
154
# File 'lib/spork/run_strategy/magazine.rb', line 153

def assert_ready!
end

#fill_slave_poolObject



48
49
50
51
52
53
54
55
56
# File 'lib/spork/run_strategy/magazine.rb', line 48

def fill_slave_pool
  Slave_Id_Range.each do |id|
    start_slave(id)
  end
  puts "  -- Starting to fill pool..."
  puts "     Wait until at least one slave is provided before running tests..."
  puts "  ** CTRL+BREAK to stop Spork and kill all ruby slave processes **"
  $stdout.flush
end

#kill_all_processesObject



125
126
127
128
129
130
131
# File 'lib/spork/run_strategy/magazine.rb', line 125

def kill_all_processes

  @pids.each {|pid| 
    kill_slave(pid)
  }
  puts "\nKilling processes."; $stdout.flush
end

#kill_slave(pid) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/spork/run_strategy/magazine.rb', line 117

def kill_slave(pid)
  if windows?
    system("taskkill /f /t /pid #{pid} > nul")
  else
    Process.kill(9, pid)
  end
end

#preloadObject



144
145
146
147
# File 'lib/spork/run_strategy/magazine.rb', line 144

def preload
  true
  #    @test_framework.preload
end

#restart_slave(id) ⇒ Object



107
108
109
110
111
# File 'lib/spork/run_strategy/magazine.rb', line 107

def restart_slave(id)
  pid   = @pids[id]
  kill_slave(pid)
  start_slave(id)
end

#run(argv, stderr, stdout) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/spork/run_strategy/magazine.rb', line 86

def run(argv, stderr, stdout)
      DRb.start_service
      ts = Rinda::RingFinger.primary
      if ts.read_all([:name, :MagazineSlave, nil, nil]).size > 0
        print '  <-- take tuple'; stdout.flush
        tuple = ts.take([:name, :MagazineSlave, nil, nil])
        slave = tuple[2]
        id = tuple[3]

        puts "(#{slave.id_num}); slave.run..."; $stdout.flush
        begin
          slave.run(argv,stderr,stdout)
          puts "   -- (#{slave.id_num});run done"; $stdout.flush
        ensure
          restart_slave(id)
        end
      else
        puts '- NO tuple'; $stdout.flush
      end
end

#running?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/spork/run_strategy/magazine.rb', line 149

def running?
  @running
end

#slave_countObject



133
134
135
136
137
# File 'lib/spork/run_strategy/magazine.rb', line 133

def slave_count
  DRb.start_service
  ts = Rinda::RingFinger.primary
  ts.read_all([:name, :MagazineSlave, nil, nil]).size
end

#slave_maxObject



24
25
26
# File 'lib/spork/run_strategy/magazine.rb', line 24

def slave_max
  Slave_Id_Range.to_a.size
end

#spawn_process(app) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/spork/run_strategy/magazine.rb', line 64

def spawn_process(app)

  if RUBY_PLATFORM =~ /java/
    # jruby 1.8 has no easy way to just spawn, so use a thread
    Dir.chdir(@path) do
      io = IO.popen app
      Thread.new { puts io.read }        
      return io.pid
    end
  end
  
  if RUBY_VERSION < '1.9.1'
    Process.create( :app_name => app, :cwd => @path ).process_id
  else
    Process.spawn( app, :chdir => @path )
  end
end

#start_Rinda_ringserverObject



43
44
45
46
# File 'lib/spork/run_strategy/magazine.rb', line 43

def start_Rinda_ringserver
  app_name = "#{Gem.ruby} ring_server.rb"
  spawn_process(app_name)
end

#start_slave(id) ⇒ Object



58
59
60
61
62
# File 'lib/spork/run_strategy/magazine.rb', line 58

def start_slave(id)
  app_pwd = Dir.pwd  # path running app in
  app = "#{Gem.ruby} magazine_slave_provider.rb #{id} '#{app_pwd}' #{@test_framework.short_name}"
  @pids[id] = spawn_process(app)
end

#windows?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/spork/run_strategy/magazine.rb', line 113

def windows?
  ENV['OS'] == 'Windows_NT'
end