Permalink
Browse files

Additional test coverage for miga/daemon

  • Loading branch information...
lmrodriguezr committed Apr 19, 2016
1 parent 995c39c commit 09625aa53e7f65f68a77368d17835c66e1c4f78a
Showing with 51 additions and 17 deletions.
  1. +3 −0 Rakefile
  2. +1 −1 actions/daemon
  3. +13 −11 lib/miga/daemon.rb
  4. +7 −1 lib/miga/project.rb
  5. +2 −2 test/common_test.rb
  6. +25 −2 test/daemon_test.rb
@@ -5,6 +5,9 @@ SOURCES = FileList["lib/**/*.rb"]
desc "Default Task"
task :default => "test:all"

desc "Default tests"
task :test => "test:all"

namespace :test do
desc "All tests"
Rake::TestTask.new(:all) do |t|
@@ -7,7 +7,7 @@ require "miga/daemon"

task = ARGV.shift unless ["-h","--help"].include? ARGV.first
ARGV << "-h" if ARGV.empty?
o = {q:true, update:false, daemon_opts:[]}
o = {q:true, daemon_opts:[]}
OptionParser.new do |opt|
opt.banner = <<BAN
Controls the daemon of a MiGA project.
@@ -55,13 +55,14 @@ def default_options

##
# Set/get #options, where +k+ is the Symbol of the option and +v+ is the value
# (or nil to use as getter). Returns new value.
def runopts(k, v=nil)
# (or nil to use as getter). Skips consistency tests if +force+. Returns new
# value.
def runopts(k, v=nil, force=false)
k = k.to_sym
unless v.nil?
v = v.to_i if [:latency, :maxjobs, :ppn].include? k
raise "Daemon's #{k} cannot be set to zero." if
v.is_a? Integer and v==0
!force and v.is_a? Integer and v==0
@runopts[k] = v
end
@runopts[k]
@@ -80,20 +81,20 @@ def maxjobs() runopts(:maxjobs) ; end
def ppn() runopts(:ppn) ; end

##
# Initializes the daemon.
def start() daemon("start") ; end
# Initializes the daemon with +opts+.
def start(opts=[]) daemon("start", opts) ; end

##
# Stops the daemon.
def stop() daemon("stop") ; end
# Stops the daemon with +opts+.
def stop(opts=[]) daemon("stop", opts) ; end

##
# Restarts the daemon.
def restart() daemon("restart") ; end
# Restarts the daemon with +opts+.
def restart(opts=[]) daemon("restart", opts) ; end

##
# Returns the status of the daemon.
def status() daemon("status") ; end
# Returns the status of the daemon with +opts+.
def status(opts=[]) daemon("status", opts) ; end

##
# Launches the +task+ with options +opts+ (as command-line arguments).
@@ -145,6 +146,7 @@ def check_datasets
# Check if all reference datasets are pre-processed. If yes, check the
# project-level tasks
def check_project
return if project.dataset_names.empty?
if project.done_preprocessing?(false)
to_run = project.next_distances(true)
to_run = project.next_inclade(true) if to_run.nil?
@@ -152,6 +152,12 @@ def name ; metadata[:name] ; end
def datasets
metadata[:datasets].map{ |name| dataset(name) }
end

##
# Returns Array of String (without evaluating dataset objects).
def dataset_names
metadata[:datasets]
end

##
# Returns MiGA::Dataset.
@@ -174,7 +180,7 @@ def each_dataset(&blk)
# Add dataset identified by +name+ and return MiGA::Dataset.
def add_dataset(name)
unless metadata[:datasets].include? name
d = MiGA::Dataset.new(self, name)
MiGA::Dataset.new(self, name)
@metadata[:datasets] << name
save
end
@@ -3,7 +3,7 @@
class CommonTest < Test::Unit::TestCase

def setup
$jruby_tests = !ENV["JRUBY_TESTS"].nil?
#$jruby_tests = !ENV["JRUBY_TESTS"].nil?
end

def test_debug
@@ -28,7 +28,7 @@ def test_debug_trace
assert_respond_to(MiGA::MiGA, :DEBUG)
assert_respond_to(MiGA::MiGA, :DEBUG_ON)
assert_respond_to(MiGA::MiGA, :DEBUG_OFF)
omit_if($jruby_tests, "JRuby doesn't like interceptions.")
#omit_if($jruby_tests, "JRuby doesn't like interceptions.")
MiGA::MiGA.DEBUG_TRACE_ON
err = capture_stderr do
MiGA::MiGA.DEBUG "Dandadi"
@@ -4,11 +4,12 @@
class DaemonTest < Test::Unit::TestCase

def setup
$jruby_tests = !ENV["JRUBY_TESTS"].nil?
$tmp = Dir.mktmpdir
ENV["MIGA_HOME"] = $tmp
FileUtils.touch("#{ENV["MIGA_HOME"]}/.miga_rc")
File.open("#{ENV["MIGA_HOME"]}/.miga_daemon.json", "w") do |fh|
fh.puts '{"maxjobs":1,"ppn":1,"latency":2}'
fh.puts '{"maxjobs":1,"ppn":1,"latency":2,"varsep":" ","var":"%1$s=%2$s"}'
end
$p1 = MiGA::Project.new(File.expand_path("project1", $tmp))
$d1 = MiGA::Daemon.new($p1)
@@ -18,7 +19,29 @@ def teardown
FileUtils.rm_rf $tmp
ENV["MIGA_HOME"] = nil
end


def test_start
$tmp2 = Dir.mktmpdir
p = MiGA::Project.new(File.expand_path("start", $tmp))
d = MiGA::Daemon.new(p)
d.runopts(:latency, 0, true)
assert_equal(0, d.latency)
omit_if($jruby_tests, "JRuby doesn't implement fork.")
$child = fork do
capture_stdout do
d.start
end
end
sleep(1)
assert(File.exist?(File.expand_path("daemon/MiGA:#{p.name}.pid",p.path)))
out = capture_stdout { d.stop }
assert(out.string =~ /MiGA:start: trying to stop process with pid \d+/)
assert(!File.exist?(File.expand_path("daemon/MiGA:#{p.name}.pid",p.path)))
assert(File.exist?(File.expand_path("daemon/MiGA:#{p.name}.output",p.path)))
ensure
Process.kill("KILL", $child) unless $child.nil?
end

def test_last_alive
p = MiGA::Project.new(File.expand_path("last_alive", $tmp))
d = MiGA::Daemon.new(p)

0 comments on commit 09625aa

Please sign in to comment.