sys-proctable 0.7.4-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ require "sys/proctable"
2
+ module Sys
3
+ class Top
4
+ VERSION = "1.0.1"
5
+ WINDOWS = PLATFORM.match("mswin")
6
+
7
+ def self.top(num=10, field="pctcpu")
8
+ # Sort by pid on Windows by default
9
+ if WINDOWS && field == "pctcpu"
10
+ field = "pid"
11
+ end
12
+
13
+ # Linux does not have a pctcpu field yet
14
+ if RUBY_PLATFORM =~ /linux/ && field == "pctcpu"
15
+ field = "pid"
16
+ end
17
+
18
+ Sys::ProcTable.ps.sort{ |a,b|
19
+ b.send(field) <=> a.send(field)
20
+ }[0..num-1]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,65 @@
1
+ ##################################################
2
+ # tc_all.rb
3
+ #
4
+ # Test suite for methods common to all platforms.
5
+ ##################################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ Dir.chdir '..' if base == 'test'
11
+
12
+ if RUBY_PLATFORM.match('mswin')
13
+ File.rename('lib/sys/windows.rb', 'lib/sys/proctable.rb')
14
+ else
15
+ require 'rbconfig'
16
+ file = 'ext/proctable.' + Config::CONFIG['DLEXT']
17
+ Dir.mkdir('sys') unless File.exists?('sys')
18
+ FileUtils.cp(file, 'sys')
19
+ end
20
+
21
+ $LOAD_PATH.unshift(Dir.pwd)
22
+ $LOAD_PATH.unshift(Dir.pwd + '/lib')
23
+ end
24
+
25
+ require 'sys/proctable'
26
+ require 'test/unit'
27
+ include Sys
28
+
29
+ class TC_ProcTable_All < Test::Unit::TestCase
30
+
31
+ def test_version
32
+ assert_equal('0.7.4', ProcTable::VERSION)
33
+ end
34
+
35
+ def test_fields
36
+ assert_respond_to(ProcTable, :fields)
37
+ assert_nothing_raised{ ProcTable.fields }
38
+ assert_kind_of(Array, ProcTable.fields)
39
+ assert_kind_of(String, ProcTable.fields.first)
40
+ end
41
+
42
+ def test_ps
43
+ assert_respond_to(ProcTable, :ps)
44
+ assert_nothing_raised{ ProcTable.ps }
45
+ assert_nothing_raised{ ProcTable.ps{} }
46
+ end
47
+
48
+ def test_ps_with_args
49
+ assert_nothing_raised{ ProcTable.ps(0) }
50
+ assert_nothing_raised{ ProcTable.ps(nil) }
51
+ assert_raises(TypeError){ ProcTable.ps('vim') }
52
+ assert_raises(ArgumentError){ ProcTable.ps(0,1) }
53
+ end
54
+
55
+ def test_ps_return_values
56
+ assert_kind_of(Array, ProcTable.ps)
57
+ assert_kind_of(Struct::ProcTableStruct, ProcTable.ps(1))
58
+ assert_equal(nil, ProcTable.ps(999999999))
59
+ assert_equal(nil, ProcTable.ps{})
60
+ end
61
+
62
+ alias :set_up :setup
63
+ alias :tear_down :teardown
64
+
65
+ end
@@ -0,0 +1,64 @@
1
+ ############################################
2
+ # tc_freebsd.rb
3
+ #
4
+ # Test suite for sys-proctable for FreeBSD.
5
+ ############################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ require 'rbconfig'
11
+
12
+ Dir.chdir '..' if base == 'test'
13
+ file = 'ext/proctable.' + Config::CONFIG['DLEXT']
14
+ Dir.mkdir('sys') unless File.exists?('sys')
15
+ FileUtils.cp(file, 'sys')
16
+
17
+ $LOAD_PATH.unshift(Dir.pwd)
18
+ end
19
+
20
+ require "sys/proctable"
21
+ require "test/unit"
22
+ require "test/tc_all"
23
+ include Sys
24
+
25
+ class TC_ProcTable_FreeBSD < Test::Unit::TestCase
26
+
27
+ def setup
28
+ @fields = %w/
29
+ comm pid ppid pgid sid tdev_maj tdev_min flags start
30
+ utime stime wchan euid ruid rgid egid groups cmdline
31
+ /
32
+ @pt = ProcTable.ps.first
33
+ end
34
+
35
+ def test_field_members
36
+ msg = "Struct members don't match expected fields"
37
+ assert_equal(@fields.length, @pt.length, "Bad length")
38
+ assert_equal(@fields, ProcTable.fields, "Bad fields")
39
+ assert_equal(@fields, @pt.members, msg)
40
+ end
41
+
42
+ def test_struct_member_types
43
+ @fields.each{ |f|
44
+ if %w/comm cmdline groups wchan flags/.include?(f)
45
+ assert_kind_of(String, @pt.send(f), "Bad #{f} type")
46
+ elsif %w/utime stime/.include?(f)
47
+ assert_kind_of(Float, @pt.send(f), "Bad #{f} type")
48
+ elsif %w/start/.include?(f)
49
+ assert_kind_of(Time, @pt.send(f), "Bad #{f} type")
50
+ else
51
+ assert_kind_of(Fixnum, @pt.send(f), "Bad #{f} type")
52
+ end
53
+ }
54
+ end
55
+
56
+ def teardown
57
+ @fields = nil
58
+ @pt = nil
59
+ end
60
+
61
+ alias :set_up :setup
62
+ alias :tear_down :teardown
63
+
64
+ end
@@ -0,0 +1,67 @@
1
+ ##################################################
2
+ # tc_hpux.rb
3
+ #
4
+ # Test case for sys-proctable for HP-UX.
5
+ ##################################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ require 'rbconfig'
11
+
12
+ Dir.chdir('..') if base == 'test'
13
+ file = 'ext/proctable.' + Config::CONFIG['DLEXT']
14
+ Dir.mkdir('sys') unless File.exists?('sys')
15
+ FileUtils.cp(file, 'sys')
16
+
17
+ $LOAD_PATH.unshift(Dir.pwd)
18
+ end
19
+
20
+ require "sys/proctable"
21
+ require "test/unit"
22
+ require "test/tc_all"
23
+ include Sys
24
+
25
+ class TC_ProcTable_HPUX < Test::Unit::TestCase
26
+
27
+ def setup
28
+ @fields = %w/
29
+ comm uid pid ppid dsize tsize ssize nice ttydev pgrp pri addr
30
+ cpu utime stime start flag stat wchan procnum cmd cmdline time
31
+ cpticks cptickstotal fss pctcpu rssize suid shmsize mmsize usize
32
+ iosize vtsize vdsize vssize vshmsize vmmsize vusize viosize
33
+ minorfaults majorfaults nswap nsignals msgrcv msgsnd maxrss
34
+ sid schedpolicy ticksleft euid egid gid sgid
35
+ /
36
+ @pt = ProcTable.ps.first
37
+ end
38
+
39
+ def test_expected_fields
40
+ msg = "Struct members don't match expected fields"
41
+ assert_equal(@fields.length, @pt.length,"Bad length")
42
+ assert_equal(@fields, ProcTable.fields,"Bad fields")
43
+ assert_equal(@fields, @pt.members,msg)
44
+ end
45
+
46
+ def test_struct_member_types
47
+ @fields.each{ |f|
48
+ if %w/comm stat cmd cmdline ttydev/.include?(f)
49
+ assert_kind_of(String,@pt.send(f),"Bad #{f} type")
50
+ elsif %w/pctcpu/.include?(f)
51
+ assert_kind_of(Float,@pt.send(f),"Bad #{f} type")
52
+ elsif %w/start/.include(f)
53
+ assert_kind_of(Time,@pt.send(f),"Bad #{f} type")
54
+ else
55
+ assert_kind_of(Fixnum,@pt.send(f),"Bad #{f} type")
56
+ end
57
+ }
58
+ end
59
+
60
+ def teardown
61
+ @fields = nil
62
+ @pt = nil
63
+ end
64
+
65
+ alias :set_up :setup
66
+ alias :tear_down :teardown
67
+ end
@@ -0,0 +1,45 @@
1
+ ################################################################
2
+ # tc_kvm_bsd.rb
3
+ #
4
+ # Test suite for the BSD specific version of the kvm interface.
5
+ ################################################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ require 'rbconfig'
11
+
12
+ Dir.chdir '..' if base == 'test'
13
+ file = 'ext/proctable.' + Config::CONFIG['DLEXT']
14
+ Dir.mkdir('sys') unless File.exists?('sys')
15
+ FileUtils.cp(file, 'sys')
16
+
17
+ $LOAD_PATH.unshift(Dir.pwd)
18
+ end
19
+
20
+ require "test/unit"
21
+ require "sys/proctable"
22
+ include Sys
23
+
24
+ class TC_KVM_BSD < Test::Unit::TestCase
25
+ def setup
26
+ @fields = %w/
27
+ pid ppid pgid ruid rgid comm state pct_cpu cpu_num tty_num
28
+ tty_dev wchan time priority usr_priority nice cmdline start_time
29
+ max_rss ix_rss id_rss is_rss minflt maxflt nswap inblock oublock
30
+ msgsnd msgrcv nsignals nvcsw nivcsw user_time system_time
31
+ /
32
+ @pt = ProcTable.ps.first
33
+ end
34
+
35
+ def test_field_members
36
+ msg = "Struct members don't match expected fields"
37
+ assert_equal(@fields.length, @pt.length, "Bad number of fields")
38
+ assert_equal(@fields, ProcTable.fields, "Bad field members")
39
+ assert_equal(@fields, @pt.members, msg)
40
+ end
41
+
42
+ def teardown
43
+ @fields = nil
44
+ end
45
+ end
@@ -0,0 +1,64 @@
1
+ ##########################################
2
+ # tc_linux.rb
3
+ #
4
+ # Test suite for sys-proctable for Linux.
5
+ ##########################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ require 'rbconfig'
11
+
12
+ Dir.chdir '..' if base == 'test'
13
+ file = 'ext/proctable.' + Config::CONFIG['DLEXT']
14
+ Dir.mkdir('sys') unless File.exists?('sys')
15
+ FileUtils.cp(file, 'sys')
16
+
17
+ $LOAD_PATH.unshift(Dir.pwd)
18
+ end
19
+
20
+ require "sys/proctable"
21
+ require "test/unit"
22
+ require "test/tc_all"
23
+ include Sys
24
+
25
+ class TC_ProcTable_Linux < Test::Unit::TestCase
26
+
27
+ def setup
28
+ @fields = %w/
29
+ cmdline cwd exe pid name uid euid gid egid comm state ppid pgrp
30
+ session tty_num tpgid flags minflt cminflt majflt cmajflt utime
31
+ stime cutime cstime priority nice itrealvalue starttime vsize
32
+ rss rlim startcode endcode startstack kstkesp kstkeip signal blocked
33
+ sigignore sigcatch wchan nswap cnswap exit_signal processor environ
34
+ /
35
+ @pt = ProcTable.ps.first
36
+ end
37
+
38
+ def test_field_members
39
+ msg = "Struct members don't match expected fields"
40
+ assert_equal(@fields.length, @pt.length, "Bad length")
41
+ assert_equal(@fields, ProcTable.fields, "Bad fields")
42
+ assert_equal(@fields, @pt.members,msg)
43
+ end
44
+
45
+ def test_struct_member_types
46
+ @fields.each{ |f|
47
+ if %w/cmdline cwd exe name comm state/.include?(f)
48
+ assert_kind_of(String,@pt.send(f),"Bad #{f} type")
49
+ elsif f == "environ"
50
+ assert_kind_of(Hash,@pt.environ,"Bad environ type")
51
+ else
52
+ assert_kind_of(Integer,@pt.send(f),"Bad #{f} type")
53
+ end
54
+ }
55
+ end
56
+
57
+ def teardown
58
+ @pt = nil
59
+ end
60
+
61
+ alias :set_up :setup
62
+ alias :tear_down :teardown
63
+
64
+ end
@@ -0,0 +1,69 @@
1
+ ##################################################
2
+ # tc_sunos.rb
3
+ #
4
+ # Test suite for sys-proctable for SunOS/Solaris.
5
+ ##################################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ require 'rbconfig'
11
+
12
+ Dir.chdir '..' if base == 'test'
13
+ file = 'ext/proctable.' + Config::CONFIG['DLEXT']
14
+ Dir.mkdir('sys') unless File.exists?('sys')
15
+ FileUtils.cp(file, 'sys')
16
+
17
+ $LOAD_PATH.unshift(Dir.pwd)
18
+ end
19
+
20
+ require "sys/proctable"
21
+ require "test/unit"
22
+ require "test/tc_all"
23
+ include Sys
24
+
25
+ class TC_ProcTable_SunOS < Test::Unit::TestCase
26
+ def setup
27
+ @fields = %w/
28
+ flag nlwp pid ppid pgid sid uid euid gid egid priority
29
+ nice ttydev time ctime size rss wchan pctcpu pctmem
30
+ state fname cmdline start processor comm num_args
31
+ cmd_args lwpid count tstamp create term rtime utime
32
+ stime ttime tftime dftime kftime ltime slptime wtime
33
+ stoptime minf majf nswap inblk oublk msnd mrcv sigs
34
+ vctx ictx sysc ioch
35
+ /
36
+ @pt = ProcTable.ps.first
37
+ end
38
+
39
+ def test_field_members
40
+ msg = "Struct members don't match expected fields"
41
+ assert_equal(@fields.length, @pt.length, "Bad number of fields")
42
+ assert_equal(@fields, ProcTable.fields, "Bad field members")
43
+ assert_equal(@fields, @pt.members, msg)
44
+ end
45
+
46
+ def test_struct_member_types
47
+ @fields.each{ |f|
48
+ if %w/comm state fname cmdline/.include?(f)
49
+ assert_kind_of(String,@pt.send(f),"Bad #{f} type")
50
+ elsif %w/pctcpu pctmem/.include?(f)
51
+ assert_kind_of(Float,@pt.send(f),"Bad #{f} type")
52
+ elsif %w/cmd_args/.include?(f)
53
+ assert_kind_of(Array,@pt.send(f),"Bad #{f} type")
54
+ elsif %w/start/.include?(f)
55
+ assert_kind_of(Time,@pt.send(f),"Bad #{f} type")
56
+ else
57
+ assert_kind_of(Fixnum,@pt.send(f),"Bad #{f} type")
58
+ end
59
+ }
60
+ end
61
+
62
+ def teardown
63
+ @pt = nil
64
+ @fields = nil
65
+ end
66
+
67
+ alias :set_up :setup
68
+ alias :tear_down :teardown
69
+ end
@@ -0,0 +1,36 @@
1
+ ##############################################################################
2
+ # tc_top.rb
3
+ #
4
+ # Test suite for the sys-top package that is included with this distribution.
5
+ ##############################################################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ require 'fileutils'
10
+ Dir.chdir('..') if base == 'test'
11
+ Dir.mkdir('sys') unless File.exists?('sys')
12
+ FileUtils.cp('lib/top.rb', 'sys')
13
+ $LOAD_PATH.unshift Dir.pwd
14
+ end
15
+
16
+ require 'sys/top'
17
+ require 'test/unit'
18
+ include Sys
19
+
20
+ class TC_Top < Test::Unit::TestCase
21
+ def test_version
22
+ assert_equal('1.0.1', Top::VERSION)
23
+ end
24
+
25
+ def test_top
26
+ assert_respond_to(Top,:top)
27
+ assert_nothing_raised{ Top.top }
28
+ assert_nothing_raised{ Top.top(5) }
29
+ assert_nothing_raised{ Top.top(5, 'cmdline') }
30
+ assert_raises(ArgumentError){ Top.top(1,'foo',2) }
31
+ assert_kind_of(Array, Top.top)
32
+ assert_equal(10, Top.top.length)
33
+ assert_equal(5, Top.top(5).length)
34
+ assert_kind_of(Struct::ProcTableStruct, Top.top.first)
35
+ end
36
+ end
@@ -0,0 +1,49 @@
1
+ ##################################################
2
+ # tc_windows.rb
3
+ #
4
+ # Test suite for sys-proctable for MS Windows
5
+ ##################################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == 'test' || base =~ /sys-proctable.*/
9
+ Dir.chdir('..') if base == 'test'
10
+ File.rename("lib/sys/windows.rb", "lib/sys/proctable.rb")
11
+ $LOAD_PATH.unshift(Dir.pwd + '/lib')
12
+ end
13
+
14
+ require 'sys/proctable'
15
+ require 'test/unit'
16
+ require 'test/tc_all'
17
+ include Sys
18
+
19
+ class TC_ProcTable_MSWindows < Test::Unit::TestCase
20
+ def setup
21
+ @fields = %w/
22
+ caption cmdline comm creation_class_name creation_date
23
+ cs_creation_class_name cs_name description executable_path
24
+ execution_state handle handle_count install_date kernel_mode_time
25
+ maximum_working_set_size minimum_working_set_size name
26
+ os_creation_class_name os_name other_operation_count
27
+ other_transfer_count page_faults page_file_usage
28
+ peak_virtual_size ppid peak_working_set_size priority
29
+ private_page_count pid quota_non_paged_pool_usage
30
+ quota_paged_pool_usage quota_peak_non_paged_pool_usage
31
+ quota_non_paged_pool_usage read_operation_count read_transfer_count
32
+ session_id termination_date thread_count user_mode_time virtual_size
33
+ windows_version working_set_size write_operation_count write_transfer_count
34
+ /
35
+ @pt = ProcTable.ps.first
36
+ end
37
+
38
+ def test_field_members
39
+ msg = "Struct members don't match expected fields"
40
+ assert_equal(@fields.length, @pt.length, "Bad length")
41
+ assert_equal(@fields, ProcTable.fields, "Bad fields")
42
+ assert_equal(@fields, @pt.members, msg)
43
+ end
44
+
45
+ def teardown
46
+ @pt = nil
47
+ @fields = nil
48
+ end
49
+ end