zerg_support 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.0.4. Polished implementation and tests for process spawning.
2
+
1
3
  v0.0.3. Preliminary implementation of process spawning.
2
4
 
3
5
  v0.0.2. Process management.
data/Rakefile CHANGED
@@ -12,7 +12,9 @@ Echoe.new('zerg_support') do |p|
12
12
 
13
13
  p.need_tar_gz = true
14
14
  p.need_zip = true
15
- p.rdoc_pattern = /^(lib|bin|tasks|ext)|^BUILD|^README|^CHANGELOG|^TODO|^LICENSE|^COPYING$/
15
+ p.rdoc_pattern = /^(lib|bin|tasks|ext)|^BUILD|^README|^CHANGELOG|^TODO|^LICENSE|^COPYING$/
16
+
17
+ p.development_dependencies = ["flexmock >=0.8.3"]
16
18
  end
17
19
 
18
20
  if $0 == __FILE__
@@ -128,8 +128,11 @@ rescue Exception
128
128
  priority, user_priority].map { |s| s.to_i })
129
129
  @start = Time.parse start
130
130
  @ix_rss, @id_rss, @is_rss = text_size.to_f, rssize.to_f, vsz.to_f
131
- @pctcpu, @pctmem = *([pctcpu, pctmem].map { |s| s.to_f })
131
+ @pctcpu, @pctmem = *([pctcpu, pctmem].map { |s| s.to_f })
132
132
  @cmdline = cmdline
133
+
134
+ # TODO(victor): translate UNIX strings into something else
135
+ @state = state
133
136
  end
134
137
  attr_reader :pid, :ppid, :ruid, :rgid, :uid, :gid, :nice, :priority,
135
138
  :user_priority, :start, :ix_rss, :id_rss, :is_rss, :pctcpu,
@@ -137,9 +140,9 @@ rescue Exception
137
140
  end
138
141
 
139
142
  @@ps_root_cmdline = 'ps -o pid,ppid,ruid,rgid,uid,gid' +
140
- ',lstart=STARTED+++++++++++++++++++++++++++++++++' +
141
- ',nice,rss,rssize,tsiz,vsz,utime,cputime,pcpu=%CPU+,' +
142
- ',pmem=%MEM+,pri,usrpri,state,command'
143
+ ',lstart="STARTED_________________________________"' +
144
+ ',nice,rss,rssize,tsiz,vsz,utime,cputime,pcpu="PCPU_"' +
145
+ ',pmem="PMEM_",pri,usrpri,stat,command'
143
146
 
144
147
  @@ps_all_cmdline = @@ps_root_cmdline + ' -A'
145
148
  @@ps_some_cmdline = @@ps_root_cmdline + ' -p '
data/test/test_spawn.rb CHANGED
@@ -3,6 +3,9 @@ require 'test/unit'
3
3
 
4
4
  require 'zerg_support'
5
5
 
6
+ require 'rubygems'
7
+ require 'flexmock/test_unit'
8
+
6
9
  class TestSpawn < Test::Unit::TestCase
7
10
  def temp_file
8
11
  now_cookie = "zerg_test_#{Time.now.to_f}"
@@ -11,6 +14,7 @@ class TestSpawn < Test::Unit::TestCase
11
14
  end
12
15
 
13
16
  def setup
17
+ super
14
18
  @in_file = temp_file
15
19
  @out_file = temp_file
16
20
  end
@@ -18,6 +22,7 @@ class TestSpawn < Test::Unit::TestCase
18
22
  def teardown
19
23
  File.delete @in_file rescue nil
20
24
  File.delete @out_file rescue nil
25
+ super
21
26
  end
22
27
 
23
28
  def test_stdout_redirect
@@ -28,6 +33,22 @@ class TestSpawn < Test::Unit::TestCase
28
33
  assert_equal "1234\n", File.read(@out_file)
29
34
  end
30
35
 
36
+ def test_stdin_redirect
37
+ File.open(@in_file, 'w') { |f| f.write "1234\n" }
38
+
39
+ pid = Zerg::Support::Process.spawn 'ruby', ['-e', "print gets"],
40
+ STDIN => @in_file, STDOUT => @out_file
41
+ Process.wait pid
42
+ assert_equal "1234\n", File.read(@out_file)
43
+ end
44
+
45
+ def test_stderr_redirect
46
+ pid = Zerg::Support::Process.spawn 'ruby', ['-e', "raise 'zerg_stderr'"],
47
+ STDOUT => @out_file, STDERR => STDOUT
48
+ Process.wait pid
49
+ assert_equal "-e:1: zerg_stderr (RuntimeError)\n", File.read(@out_file)
50
+ end
51
+
31
52
  def test_async
32
53
  t0 = Time.now
33
54
  pid = Zerg::Support::Process.spawn 'ruby', ['-e', 'sleep 0.5']
@@ -38,4 +59,79 @@ class TestSpawn < Test::Unit::TestCase
38
59
  assert t1 - t0 < 0.2, 'Spawning is not asynchronous'
39
60
  assert t2 - t0 > 0.5, 'The spawned program did not sleep for 1 second'
40
61
  end
62
+
63
+ def test_set_environment
64
+ pid = Zerg::Support::Process.spawn 'ruby', ['-e', 'print ENV["XA"]'],
65
+ STDOUT => @out_file, :env => {'XA' => "1234\n"}
66
+ Process.wait pid
67
+ assert_equal "1234\n", File.read(@out_file)
68
+
69
+ pid = Zerg::Support::Process.spawn 'ruby', ['-e', 'print ENV["PATH"]'],
70
+ STDOUT => @in_file, :env => {'XA' => "1234\n"}, :unsetenv_others => true
71
+ Process.wait pid
72
+ # clearing the environment will reset PATH, so ruby might not run at all
73
+ output = File.read(@in_file)
74
+ assert output == 'nil' || output == '', 'The environment was not cleared'
75
+ end
76
+
77
+ def test_close_fd
78
+ in_file = File.open(@in_file, 'w')
79
+ in_file.sync = true
80
+ in_file.write "1234\n"
81
+ in_file.close
82
+
83
+ in_file = File.open(@in_file, 'r')
84
+
85
+ in_fd = in_file.fileno
86
+ read_fd_code = "File.open(#{in_fd}).read"
87
+ assert_equal "1234\n", eval(read_fd_code), 'File reading code is broken'
88
+
89
+ pid = Zerg::Support::Process.spawn 'ruby', ['-e', "print #{read_fd_code}"],
90
+ STDOUT => @out_file, STDERR => STDOUT
91
+ Process.wait pid
92
+ in_file.close
93
+
94
+ assert_equal "-e:1:", File.read(@out_file)[0, 5],
95
+ 'Spawn does not close file descriptors'
96
+ end
97
+
98
+ def test_rlimit_processing
99
+ flexmock(Process).should_receive(:setrlimit).
100
+ with(Process::RLIMIT_CPU, 1, 5).once.and_return(nil)
101
+ flexmock(Process).should_receive(:setrlimit).
102
+ with(Process::RLIMIT_RSS, 64 * 1024 * 1024).once.
103
+ and_return(nil)
104
+
105
+ Zerg::Support::Process::Helpers.set_rlimits :rlimit_cpu => [1, 5],
106
+ :rlimit_rss => 64 * 1024 * 1024
107
+ end
108
+
109
+ def test_process_group_processing
110
+ flexmock(Process).should_receive(:setpgid).with(0, 42).once.and_return(nil)
111
+ flexmock(Process).should_receive(:setsid).with().once.and_return(nil)
112
+
113
+ Zerg::Support::Process::Helpers.set_process_group :pgroup => 42
114
+ Zerg::Support::Process::Helpers.set_process_group :pgroup => true
115
+ end
116
+
117
+ def test_process_group
118
+ pid = Zerg::Support::Process.spawn 'ruby', ['-e', "sleep 0.5"],
119
+ :pgroup => true
120
+ pinfo = Zerg::Support::Process::process_info pid
121
+ Process.wait pid
122
+
123
+ assert pinfo[:state].index('s'), 'Failed to become session leader'
124
+ end
125
+
126
+ def test_rlimits
127
+ t0 = Time.now
128
+ pid = Zerg::Support::Process.spawn 'ruby',
129
+ ['-e', "i = 0; t0 = Time.now; i += 1 while Time.now - t0 < 3.0"],
130
+ :rlimit_cpu => 1
131
+ Process.wait pid
132
+ t1 = Time.now
133
+
134
+ assert t1 - t0 >= 1.0, 'Spawning failed'
135
+ assert t1 - t0 <= 2.0, 'Failed to apply rlimit_cpu'
136
+ end
41
137
  end
data/zerg_support.gemspec CHANGED
@@ -1,6 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  Gem::Specification.new do |s|
2
4
  s.name = %q{zerg_support}
3
- s.version = "0.0.3"
5
+ s.version = "0.0.4"
4
6
 
5
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
6
8
  s.authors = ["Victor Costan"]
@@ -14,7 +16,7 @@ Gem::Specification.new do |s|
14
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Zerg_support", "--main", "README"]
15
17
  s.require_paths = ["lib"]
16
18
  s.rubyforge_project = %q{rails-pwnage}
17
- s.rubygems_version = %q{1.2.0}
19
+ s.rubygems_version = %q{1.3.1}
18
20
  s.summary = %q{Support libraries used by the Zerg system.}
19
21
  s.test_files = ["test/test_gems.rb", "test/test_process.rb", "test/test_spawn.rb"]
20
22
 
@@ -22,12 +24,12 @@ Gem::Specification.new do |s|
22
24
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
25
  s.specification_version = 2
24
26
 
25
- if current_version >= 3 then
26
- s.add_development_dependency(%q<echoe>, [">= 0"])
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_development_dependency(%q<flexmock>, [">= 0.8.3"])
27
29
  else
28
- s.add_dependency(%q<echoe>, [">= 0"])
30
+ s.add_dependency(%q<flexmock>, [">= 0.8.3"])
29
31
  end
30
32
  else
31
- s.add_dependency(%q<echoe>, [">= 0"])
33
+ s.add_dependency(%q<flexmock>, [">= 0.8.3"])
32
34
  end
33
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zerg_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Costan
@@ -13,14 +13,14 @@ date: 2008-11-13 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: echoe
16
+ name: flexmock
17
17
  type: :development
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: "0"
23
+ version: 0.8.3
24
24
  version:
25
25
  description: Support libraries used by the Zerg system.
26
26
  email: victor@zergling.net
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  requirements: []
75
75
 
76
76
  rubyforge_project: rails-pwnage
77
- rubygems_version: 1.2.0
77
+ rubygems_version: 1.3.1
78
78
  signing_key:
79
79
  specification_version: 2
80
80
  summary: Support libraries used by the Zerg system.