Skip to content

Commit

Permalink
Fix flaky daemonize specs. (#434)
Browse files Browse the repository at this point in the history
The specs were leaking state, in other words, the server from the previous test might still be running, clobbering then pid file, etc.

* Delete mongrel parsing spec - random data tests don't always fail.
  • Loading branch information
ioquatix authored Jan 29, 2025
1 parent df145c4 commit dac0260
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 50 deletions.
45 changes: 34 additions & 11 deletions spec/daemonizing_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'spec_helper'

require 'timeout'
require "tmpdir"

class TestServer
include Logging
Expand All @@ -15,17 +16,24 @@ def name
end

describe 'Daemonizing' do
let(:path) {File.expand_path("tmp", __dir__)}
let(:log_file) {File.expand_path("test_server.log", path)}
let(:pid_file) {File.expand_path("test.pid", path)}
let(:log_file) {File.join(@root, "test_server.log")}
let(:pid_file) {File.join(@root, "test.pid")}

before do
FileUtils.rm_rf path
FileUtils.mkpath path
FileUtils.touch log_file
around do |example|
Dir.mktmpdir do |root|
@root = root

# Ensure the log file exists with the correct permissions:
File.open(log_file, "w+") {}

example.run
@root = nil
end
end

subject(:server) do
raise "No root directory" unless @root

TestServer.new.tap do |server|
server.log_file = log_file
server.pid_file = pid_file
Expand Down Expand Up @@ -81,7 +89,7 @@ def name
it 'should kill process in pid file' do
expect(File.exist?(subject.pid_file)).to be_falsey

fork do
pid = fork do
subject.daemonize
sleep
end
Expand All @@ -94,7 +102,7 @@ def name
subject.kill(1)
end

sleep(1)
Process.wait(pid)
expect(File.exist?(subject.pid_file)).to be_falsey
end

Expand Down Expand Up @@ -184,8 +192,23 @@ def name
private

def wait_for_server_to_start
until File.exist?(subject.pid_file)
sleep(0.1)
count = 1

while true
break if File.exist?(subject.pid_file)

sleep(count * 0.1)

if count > 10
$stderr.puts "Dumping log file #{subject.log_file}:"
File.foreach(subject.log_file) do |line|
$stderr.puts line
end

raise "Server did not start"
end

count += 1
end
end
end
39 changes: 0 additions & 39 deletions spec/request/mongrel_spec.rb

This file was deleted.

0 comments on commit dac0260

Please sign in to comment.