lib/boojs.rb in boojs-0.0.26 vs lib/boojs.rb in boojs-0.0.27
- old
+ new
@@ -1,5 +1,6 @@
+$stdout.sync = true
require 'tempfile'
require 'securerandom'
require 'greenletters'
require "net/http"
require "uri"
@@ -67,11 +68,11 @@
return 1
end
#Optionally, accept code to inject and a command to run
#If the command is nil, this is not executed as a oneshot
- def self.pipe(str=nil, cmd=nil, will_timeout=false)
+ def self.pipe(str=nil, cmd=nil, will_timeout=false, output_phantomjs_pid=false)
js = %{
var system = require('system');
function __spec_ping(str) {
system.stdout.writeLine("pong"+str)
}
@@ -118,29 +119,44 @@
tmp.close
@input_sender_r, @input_sender_w = IO.pipe
#Phantom JS Process
- p = IO.popen("#{$phantomjs_path} #{tmp.path}")
- loop do
- rr, _ = select([p, STDIN]); e = rr[0]
- #PhantomJS has written something
- if e == p
- res = e.readline
+ begin
+ p = IO.popen("#{$phantomjs_path} #{tmp.path}")
- if res =~ /STDIN_PORT/
- port = res.split(" ")[1].to_i
- start_server(port, @input_sender_r)
- else
- puts res
- $stdout.flush
- end
+ #We are going to do a loop back (ready to rcv) to wait for the JS to respond before outputing the spec PID
+ #So that in the specs, we have a reliable way of testing the process execution as ending this process
+ #before the child process ends, will termiante the spawned process (PhantomJS) by default but not if it's
+ #kept alive for a while (which we handle via sending it an INT in the ensure)
+ if output_phantomjs_pid
+ @input_sender_w.puts "booPing();"
+ res = p.readline
+ puts p.pid
end
- #User has written to this program
- if e == STDIN
- @input_sender_w.puts e.readline
+ loop do
+ rr, _ = select([p, STDIN]); e = rr[0]
+ #PhantomJS has written something
+ if e == p
+ res = e.readline
+
+ if res =~ /STDIN_PORT/
+ port = res.split(" ")[1].to_i
+ start_server(port, @input_sender_r)
+ else
+ puts res
+ $stdout.flush
+ end
+ end
+
+ #User has written to this program
+ if e == STDIN
+ @input_sender_w.puts e.readline
+ end
end
+ ensure
+ Process.kill :INT, p.pid
end
tmp.unlink
exit ret
rescue EOFError => e