# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2004, 2005 Uttk team. All rights reserved. # License:: LGPL # $Id: /w/fey/uttk/trunk/lib/uttk/strategies/KillAll.rb 22102 2006-02-21T23:03:39.538964Z pouillar $ module Uttk module Strategies # I kill all the process that match my _regexp_ attribute by sending my # _signal_ attribute. I'm unfortunately not compatible with all OS since I # depend on the _ps_ program. class KillAll < Strategy include Concrete def prologue super @pids = [] end def run_impl IO.popen('ps a') do |ps| ps.readline ps.each do |line| if line =~ /^\s*(\d+)(?:\s+\S+){3}\s+(.*)$/ pid, name = $1.to_i, $2 if name =~ @regexp @pids << pid begin Process.kill(@signal, pid) rescue raise RuntimeError, "Cannot kill #{name}:#{pid}" end end else raise RuntimeError, 'bad ps output' end end end end def assertion pass if @pids.empty? fail "I killed some process #{@pids.inspect}" end def epilogue unless @pids.empty? @log.process = @pids sleep 0.2 end end attribute :signal, 'Signal to send to selected processes', 'KILL' attribute :regexp, 'A regexp to match process names', :mandatory end # class KillAll end # module Strategies end # module Uttk