Sha256: c5a2dd2a4686d58d12a924bc0c925dd2064ab061aa67f0c0af1677a2ae5de27b

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

# Author::    Nicolas Pouillard  <ertai@lrde.epita.fr>.
# 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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
uttk-0.3.6.1 lib/uttk/strategies/KillAll.rb
uttk-0.3.1.2 lib/uttk/strategies/KillAll.rb
uttk-0.3.5.0 lib/uttk/strategies/KillAll.rb
uttk-0.4.6.1 lib/uttk/strategies/KillAll.rb
uttk-0.4.6.2 lib/uttk/strategies/KillAll.rb
uttk-0.4.5.0 lib/uttk/strategies/KillAll.rb