Skip to content

Commit e3feb88

Browse files
committed
Clean up container specs
- Extract with_timeout helper - Remove complex on_output stub, add the sleep to existing handler
1 parent f3f08fc commit e3feb88

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

spec/cc/analyzer/container_spec.rb

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,7 @@ module CC::Analyzer
129129
end
130130

131131
it "times out slow containers" do
132-
old_timeout = ENV["CONTAINER_TIMEOUT_SECONDS"]
133-
ENV["CONTAINER_TIMEOUT_SECONDS"] = "1"
134-
135-
begin
132+
with_timeout(1) do
136133
listener = TestContainerListener.new
137134
listener.expects(:finished).never
138135
container = Container.new(
@@ -153,8 +150,6 @@ module CC::Analyzer
153150
@container_result.exit_status.wont_equal nil
154151
@container_result.duration.must_be :>=, 0
155152
@container_result.duration.must_be :<, 2_000
156-
ensure
157-
ENV["CONTAINER_TIMEOUT_SECONDS"] = old_timeout
158153
end
159154
end
160155

@@ -171,24 +166,14 @@ module CC::Analyzer
171166
listener: listener,
172167
)
173168
container.on_output do |str|
169+
sleep 0.5
174170
stdout_lines << str
175171
end
176172

177-
slow_read_stdout = Proc.new do |out|
178-
Thread.new do
179-
out.each_line(container.instance_variable_get(:@output_delimeter)) do |chunk|
180-
sleep 0.5
181-
output = chunk.chomp(container.instance_variable_get(:@output_delimeter))
182-
container.instance_variable_get(:@on_output).call(output)
183-
end
184-
end
185-
end
186-
container.stub :read_stdout, slow_read_stdout do
187-
run_container(container)
173+
run_container(container)
188174

189-
assert_container_stopped
190-
@container_result.timed_out?.must_equal false
191-
end
175+
assert_container_stopped
176+
@container_result.timed_out?.must_equal false
192177
end
193178

194179
it "stops containers that emit more than the configured maximum output bytes" do
@@ -277,5 +262,13 @@ def expect_spawn(args, status: nil, out: StringIO.new, err: StringIO.new)
277262

278263
return [pid, out, err]
279264
end
265+
266+
def with_timeout(timeout)
267+
old_timeout = ENV["CONTAINER_TIMEOUT_SECONDS"]
268+
ENV["CONTAINER_TIMEOUT_SECONDS"] = timeout.to_s
269+
yield
270+
ensure
271+
ENV["CONTAINER_TIMEOUT_SECONDS"] = old_timeout
272+
end
280273
end
281274
end

0 commit comments

Comments
 (0)