11require 'spec_helper'
22
33require 'timeout'
4+ require "tmpdir"
45
56class TestServer
67 include Logging
@@ -15,17 +16,24 @@ def name
1516end
1617
1718describe 'Daemonizing' do
18- let ( :path ) { File . expand_path ( "tmp" , __dir__ ) }
19- let ( :log_file ) { File . expand_path ( "test_server.log" , path ) }
20- let ( :pid_file ) { File . expand_path ( "test.pid" , path ) }
19+ let ( :log_file ) { File . join ( @root , "test_server.log" ) }
20+ let ( :pid_file ) { File . join ( @root , "test.pid" ) }
2121
22- before do
23- FileUtils . rm_rf path
24- FileUtils . mkpath path
25- FileUtils . touch log_file
22+ around do |example |
23+ Dir . mktmpdir do |root |
24+ @root = root
25+
26+ # Ensure the log file exists with the correct permissions:
27+ File . open ( log_file , "w+" ) { }
28+
29+ example . run
30+ @root = nil
31+ end
2632 end
2733
2834 subject ( :server ) do
35+ raise "No root directory" unless @root
36+
2937 TestServer . new . tap do |server |
3038 server . log_file = log_file
3139 server . pid_file = pid_file
@@ -81,7 +89,7 @@ def name
8189 it 'should kill process in pid file' do
8290 expect ( File . exist? ( subject . pid_file ) ) . to be_falsey
8391
84- fork do
92+ pid = fork do
8593 subject . daemonize
8694 sleep
8795 end
@@ -94,7 +102,7 @@ def name
94102 subject . kill ( 1 )
95103 end
96104
97- sleep ( 1 )
105+ Process . wait ( pid )
98106 expect ( File . exist? ( subject . pid_file ) ) . to be_falsey
99107 end
100108
@@ -184,8 +192,23 @@ def name
184192 private
185193
186194 def wait_for_server_to_start
187- until File . exist? ( subject . pid_file )
188- sleep ( 0.1 )
195+ count = 1
196+
197+ while true
198+ break if File . exist? ( subject . pid_file )
199+
200+ sleep ( count * 0.1 )
201+
202+ if count > 10
203+ $stderr. puts "Dumping log file #{ subject . log_file } :"
204+ File . foreach ( subject . log_file ) do |line |
205+ $stderr. puts line
206+ end
207+
208+ raise "Server did not start"
209+ end
210+
211+ count += 1
189212 end
190213 end
191214end
0 commit comments