summaryrefslogtreecommitdiff
path: root/spec/ruby/core/process/setrlimit_spec.rb
blob: 8bc035a4f1282e95cf30171fe79c909213eecf05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
require_relative '../../spec_helper'

platform_is_not :windows do
  describe "Process.setrlimit" do
    context "when passed an Object" do
      before do
        @resource = Process::RLIMIT_CORE
        @limit, @max = Process.getrlimit @resource
      end

      it "calls #to_int to convert resource to an Integer" do
        Process.setrlimit(mock_int(@resource), @limit, @max).should be_nil
      end

      it "raises a TypeError if #to_int for resource does not return an Integer" do
        obj = mock("process getrlimit integer")
        obj.should_receive(:to_int).and_return(nil)

        -> { Process.setrlimit(obj, @limit, @max) }.should raise_error(TypeError)
      end

      it "calls #to_int to convert the soft limit to an Integer" do
        Process.setrlimit(@resource, mock_int(@limit), @max).should be_nil
      end

      it "raises a TypeError if #to_int for resource does not return an Integer" do
        obj = mock("process getrlimit integer")
        obj.should_receive(:to_int).and_return(nil)

        -> { Process.setrlimit(@resource, obj, @max) }.should raise_error(TypeError)
      end

      it "calls #to_int to convert the hard limit to an Integer" do
        Process.setrlimit(@resource, @limit, mock_int(@max)).should be_nil
      end

      it "raises a TypeError if #to_int for resource does not return an Integer" do
        obj = mock("process getrlimit integer")
        obj.should_receive(:to_int).and_return(nil)

        -> { Process.setrlimit(@resource, @limit, obj) }.should raise_error(TypeError)
      end
    end

    context "when passed a Symbol" do
      platform_is_not :openbsd do
        it "coerces :AS into RLIMIT_AS" do
          Process.setrlimit(:AS, *Process.getrlimit(Process::RLIMIT_AS)).should be_nil
        end
      end

      it "coerces :CORE into RLIMIT_CORE" do
        Process.setrlimit(:CORE, *Process.getrlimit(Process::RLIMIT_CORE)).should be_nil
      end

      it "coerces :CPU into RLIMIT_CPU" do
        Process.setrlimit(:CPU, *Process.getrlimit(Process::RLIMIT_CPU)).should be_nil
      end

      it "coerces :DATA into RLIMIT_DATA" do
        Process.setrlimit(:DATA, *Process.getrlimit(Process::RLIMIT_DATA)).should be_nil
      end

      it "coerces :FSIZE into RLIMIT_FSIZE" do
        Process.setrlimit(:FSIZE, *Process.getrlimit(Process::RLIMIT_FSIZE)).should be_nil
      end

      it "coerces :NOFILE into RLIMIT_NOFILE" do
        Process.setrlimit(:NOFILE, *Process.getrlimit(Process::RLIMIT_NOFILE)).should be_nil
      end

      it "coerces :STACK into RLIMIT_STACK" do
        Process.setrlimit(:STACK, *Process.getrlimit(Process::RLIMIT_STACK)).should be_nil
      end

      platform_is_not :solaris, :aix do
        it "coerces :MEMLOCK into RLIMIT_MEMLOCK" do
          Process.setrlimit(:MEMLOCK, *Process.getrlimit(Process::RLIMIT_MEMLOCK)).should be_nil
        end
      end

      platform_is_not :solaris do
        it "coerces :NPROC into RLIMIT_NPROC" do
          Process.setrlimit(:NPROC, *Process.getrlimit(Process::RLIMIT_NPROC)).should be_nil
        end

        it "coerces :RSS into RLIMIT_RSS" do
          Process.setrlimit(:RSS, *Process.getrlimit(Process::RLIMIT_RSS)).should be_nil
        end
      end

      platform_is :netbsd, :freebsd do
        it "coerces :SBSIZE into RLIMIT_SBSIZE" do
          Process.setrlimit(:SBSIZE, *Process.getrlimit(Process::RLIMIT_SBSIZE)).should be_nil
        end
      end

      platform_is :linux do
        it "coerces :RTPRIO into RLIMIT_RTPRIO" do
          Process.setrlimit(:RTPRIO, *Process.getrlimit(Process::RLIMIT_RTPRIO)).should be_nil
        end

        guard -> { defined?(Process::RLIMIT_RTTIME) } do
          it "coerces :RTTIME into RLIMIT_RTTIME" do
            Process.setrlimit(:RTTIME, *Process.getrlimit(Process::RLIMIT_RTTIME)).should be_nil
          end
        end

        it "coerces :SIGPENDING into RLIMIT_SIGPENDING" do
          Process.setrlimit(:SIGPENDING, *Process.getrlimit(Process::RLIMIT_SIGPENDING)).should be_nil
        end

        it "coerces :MSGQUEUE into RLIMIT_MSGQUEUE" do
          Process.setrlimit(:MSGQUEUE, *Process.getrlimit(Process::RLIMIT_MSGQUEUE)).should be_nil
        end

        it "coerces :NICE into RLIMIT_NICE" do
          Process.setrlimit(:NICE, *Process.getrlimit(Process::RLIMIT_NICE)).should be_nil
        end
      end

      it "raises ArgumentError when passed an unknown resource" do
        -> { Process.setrlimit(:FOO, 1, 1) }.should raise_error(ArgumentError)
      end
    end

    context "when passed a String" do
      platform_is_not :openbsd do
        it "coerces 'AS' into RLIMIT_AS" do
          Process.setrlimit("AS", *Process.getrlimit(Process::RLIMIT_AS)).should be_nil
        end
      end

      it "coerces 'CORE' into RLIMIT_CORE" do
        Process.setrlimit("CORE", *Process.getrlimit(Process::RLIMIT_CORE)).should be_nil
      end

      it "coerces 'CPU' into RLIMIT_CPU" do
        Process.setrlimit("CPU", *Process.getrlimit(Process::RLIMIT_CPU)).should be_nil
      end

      it "coerces 'DATA' into RLIMIT_DATA" do
        Process.setrlimit("DATA", *Process.getrlimit(Process::RLIMIT_DATA)).should be_nil
      end

      it "coerces 'FSIZE' into RLIMIT_FSIZE" do
        Process.setrlimit("FSIZE", *Process.getrlimit(Process::RLIMIT_FSIZE)).should be_nil
      end

      it "coerces 'NOFILE' into RLIMIT_NOFILE" do
        Process.setrlimit("NOFILE", *Process.getrlimit(Process::RLIMIT_NOFILE)).should be_nil
      end

      it "coerces 'STACK' into RLIMIT_STACK" do
        Process.setrlimit("STACK", *Process.getrlimit(Process::RLIMIT_STACK)).should be_nil
      end

      platform_is_not :solaris, :aix do
        it "coerces 'MEMLOCK' into RLIMIT_MEMLOCK" do
          Process.setrlimit("MEMLOCK", *Process.getrlimit(Process::RLIMIT_MEMLOCK)).should be_nil
        end
      end

      platform_is_not :solaris do
        it "coerces 'NPROC' into RLIMIT_NPROC" do
          Process.setrlimit("NPROC", *Process.getrlimit(Process::RLIMIT_NPROC)).should be_nil
        end

        it "coerces 'RSS' into RLIMIT_RSS" do
          Process.setrlimit("RSS", *Process.getrlimit(Process::RLIMIT_RSS)).should be_nil
        end
      end

      platform_is :netbsd, :freebsd do
        it "coerces 'SBSIZE' into RLIMIT_SBSIZE" do
          Process.setrlimit("SBSIZE", *Process.getrlimit(Process::RLIMIT_SBSIZE)).should be_nil
        end
      end

      platform_is :linux do
        it "coerces 'RTPRIO' into RLIMIT_RTPRIO" do
          Process.setrlimit("RTPRIO", *Process.getrlimit(Process::RLIMIT_RTPRIO)).should be_nil
        end

        guard -> { defined?(Process::RLIMIT_RTTIME) } do
          it "coerces 'RTTIME' into RLIMIT_RTTIME" do
            Process.setrlimit("RTTIME", *Process.getrlimit(Process::RLIMIT_RTTIME)).should be_nil
          end
        end

        it "coerces 'SIGPENDING' into RLIMIT_SIGPENDING" do
          Process.setrlimit("SIGPENDING", *Process.getrlimit(Process::RLIMIT_SIGPENDING)).should be_nil
        end

        it "coerces 'MSGQUEUE' into RLIMIT_MSGQUEUE" do
          Process.setrlimit("MSGQUEUE", *Process.getrlimit(Process::RLIMIT_MSGQUEUE)).should be_nil
        end

        it "coerces 'NICE' into RLIMIT_NICE" do
          Process.setrlimit("NICE", *Process.getrlimit(Process::RLIMIT_NICE)).should be_nil
        end
      end

      it "raises ArgumentError when passed an unknown resource" do
        -> { Process.setrlimit("FOO", 1, 1) }.should raise_error(ArgumentError)
      end
    end

    context "when passed on Object" do
      before do
        @resource = Process::RLIMIT_CORE
        @limit, @max = Process.getrlimit @resource
      end

      it "calls #to_str to convert to a String" do
        obj = mock("process getrlimit string")
        obj.should_receive(:to_str).and_return("CORE")
        obj.should_not_receive(:to_int)

        Process.setrlimit(obj, @limit, @max).should be_nil
      end

      it "calls #to_int if #to_str does not return a String" do
        obj = mock("process getrlimit string")
        obj.should_receive(:to_str).and_return(nil)
        obj.should_receive(:to_int).and_return(@resource)

        Process.setrlimit(obj, @limit, @max).should be_nil
      end
    end
  end
end