workbench: Improve integration test Rails launch.
authorBrett Smith <brett@curoverse.com>
Tue, 25 Mar 2014 18:36:48 +0000 (14:36 -0400)
committerBrett Smith <brett@curoverse.com>
Tue, 25 Mar 2014 18:36:48 +0000 (14:36 -0400)
The previous version was susceptible to a race condition where it
would read the file after Rails created it, but before it actually
wrote a pid to it.  This would cause the rake test task to kill itself
later by running Process.kill('TERM', 0).

apps/workbench/test/integration_helper.rb

index 8e317ba4e238ae179e3d10c3ee82ca42ffcf311d..d5de3319fbe4c0f40f1398715bc1bd5bfba81d03 100644 (file)
@@ -51,11 +51,15 @@ class IntegrationTestRunner < MiniTest::Unit
       _system('bundle', 'exec', 'rake', 'db:test:load')
       _system('bundle', 'exec', 'rake', 'db:fixtures:load')
       _system('bundle', 'exec', 'rails', 'server', '-d')
-      timeout = Time.now.tv_sec + 5
-      while (not File.exists? SERVER_PID_PATH) and (Time.now.tv_sec < timeout)
-        sleep 0.2
-      end
-      IO.read(SERVER_PID_PATH).to_i
+      begin
+        begin
+          server_pid = IO.read(SERVER_PID_PATH).to_i
+        rescue Errno::ENOENT
+          sleep 0.2
+        end
+      end until (not server_pid.nil?) and (server_pid > 0) and
+        (Process.kill(0, server_pid) rescue false)
+      server_pid
     end
     begin
       super(args)