1require_relative '../../spec_helper'
2require_relative 'fixtures/common'
3
4describe "Process.kill" do
5  ProcessSpecs.use_system_ruby(self)
6
7  before :each do
8    @pid = Process.pid
9  end
10
11  it "raises an ArgumentError for unknown signals" do
12    lambda { Process.kill("FOO", @pid) }.should raise_error(ArgumentError)
13  end
14
15  it "raises an ArgumentError if passed a lowercase signal name" do
16    lambda { Process.kill("term", @pid) }.should raise_error(ArgumentError)
17  end
18
19  it "raises an ArgumentError if signal is not a Fixnum or String" do
20    signal = mock("process kill signal")
21    signal.should_not_receive(:to_int)
22
23    lambda { Process.kill(signal, @pid) }.should raise_error(ArgumentError)
24  end
25
26  it "raises Errno::ESRCH if the process does not exist" do
27    pid = Process.spawn(*ruby_exe, "-e", "sleep 10")
28    Process.kill("SIGKILL", pid)
29    Process.wait(pid)
30    lambda {
31      Process.kill("SIGKILL", pid)
32    }.should raise_error(Errno::ESRCH)
33  end
34end
35
36platform_is_not :windows do
37  describe "Process.kill" do
38    ProcessSpecs.use_system_ruby(self)
39
40    before :each do
41      @sp = ProcessSpecs::Signalizer.new
42    end
43
44    after :each do
45      @sp.cleanup
46    end
47
48    it "accepts a Symbol as a signal name" do
49      Process.kill(:SIGTERM, @sp.pid)
50      @sp.result.should == "signaled"
51    end
52
53    it "accepts a String as signal name" do
54      Process.kill("SIGTERM", @sp.pid)
55      @sp.result.should == "signaled"
56    end
57
58    it "accepts a signal name without the 'SIG' prefix" do
59      Process.kill("TERM", @sp.pid)
60      @sp.result.should == "signaled"
61    end
62
63    it "accepts a signal name with the 'SIG' prefix" do
64      Process.kill("SIGTERM", @sp.pid)
65      @sp.result.should == "signaled"
66    end
67
68    it "acceps an Integer as a signal value" do
69      Process.kill(15, @sp.pid)
70      @sp.result.should == "signaled"
71    end
72
73    it "calls #to_int to coerce the pid to an Integer" do
74      Process.kill("SIGTERM", mock_int(@sp.pid))
75      @sp.result.should == "signaled"
76    end
77  end
78
79  describe "Process.kill" do
80    ProcessSpecs.use_system_ruby(self)
81
82    before :each do
83      @sp1 = ProcessSpecs::Signalizer.new
84      @sp2 = ProcessSpecs::Signalizer.new
85    end
86
87    after :each do
88      @sp1.cleanup
89      @sp2.cleanup
90    end
91
92    it "signals multiple processes" do
93      Process.kill("SIGTERM", @sp1.pid, @sp2.pid)
94      @sp1.result.should == "signaled"
95      @sp2.result.should == "signaled"
96    end
97
98    it "returns the number of processes signaled" do
99      Process.kill("SIGTERM", @sp1.pid, @sp2.pid).should == 2
100    end
101  end
102
103  describe "Process.kill" do
104    after :each do
105      @sp.cleanup if @sp
106    end
107
108    it "signals the process group if the PID is zero" do
109      @sp = ProcessSpecs::Signalizer.new "self"
110      @sp.result.should == "signaled"
111    end
112
113    it "signals the process group if the signal number is negative" do
114      @sp = ProcessSpecs::Signalizer.new "group_numeric"
115      @sp.result.should == "signaled"
116    end
117
118    it "signals the process group if the short signal name starts with a minus sign" do
119      @sp = ProcessSpecs::Signalizer.new "group_short_string"
120      @sp.result.should == "signaled"
121    end
122
123    it "signals the process group if the full signal name starts with a minus sign" do
124      @sp = ProcessSpecs::Signalizer.new "group_full_string"
125      @sp.result.should == "signaled"
126    end
127  end
128end
129