boojs 0.0.26 → 0.0.27

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: db230cbf699816c58332b402c9323cc8a2247dbd
4
- data.tar.gz: 832c8f6043bfc99893297a9d321d1d8ff27337f9
3
+ metadata.gz: 43c6680ba1e3ee1e6c2009cfd4ca7b2a5b8e9f93
4
+ data.tar.gz: d550cec2bb6aa2f7c32196720bcbeb1dc4aff732
5
5
  SHA512:
6
- metadata.gz: ad5ca9aad30b0632ea9aed44e8ed5dfb09bbde16b570af4f08c0adecfe3389ea9b5355ace6a19aeb87061806f838b6a5149d647f81a44bfb2555c2e9ac9d1f48
7
- data.tar.gz: ffcedb596fc53da6bf9d3a96e3a685226dd715d08cea17e93912efef2c89517705124328680a245c7e136f545019ceb83c898460628f28053a6314caa5ff69fa
6
+ metadata.gz: 1db9a67b86ac314f34fcf87968160bcc19a9a655b885149aec1e497b398201e0b44a8d07a54bb623b593d80f5d1b7be9125b3fb55079c2c8519388a30f8f5cf6
7
+ data.tar.gz: ef361b2ad6a036e7a8ac26f414628244be9feef46bb5dfb3fdebe0c998e76d66bc13512d7326980246db448fb8b84f6a38ade463c1a6c768f1ad8f3804dcea44
data/bin/boojs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- require 'boojs'
2
+ require File.join(File.dirname(__FILE__), "../lib/boojs")
3
3
  require 'optparse'
4
4
  require 'phantomjs'
5
5
  require 'timeout'
@@ -23,6 +23,13 @@ parser = OptionParser.new do |opts|
23
23
  opts.on "-t TIMEOUT" do |t|
24
24
  @t = t.to_i
25
25
  end
26
+
27
+ #The first line of stdout will be the PID of the PhantomJS process
28
+ #This is for a spec that tests to make sure the PhanomJS process is
29
+ #correctly terminated when it is spawned
30
+ opts.on "-p" do
31
+ @output_phantomjs_pid = true
32
+ end
26
33
  end
27
34
  parser.parse!
28
35
  file = ARGV.pop
@@ -32,9 +39,9 @@ file = ARGV.pop
32
39
  #Pipe with with a optional file and optional execute command
33
40
  pipe_proc = Proc.new do
34
41
  if file
35
- BooJS.pipe File.read(file), @c, !@t.nil?
42
+ BooJS.pipe File.read(file), @c, !@t.nil?, @output_phantomjs_pid
36
43
  else
37
- BooJS.pipe nil, @c, !@t.nil?
44
+ BooJS.pipe nil, @c, !@t.nil?, @output_phantomjs_pid
38
45
  end
39
46
  end
40
47
 
data/lib/boojs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module BooJS
2
- VERSION = '0.0.26'
2
+ VERSION = '0.0.27'
3
3
  end
data/lib/boojs.rb CHANGED
@@ -1,3 +1,4 @@
1
+ $stdout.sync = true
1
2
  require 'tempfile'
2
3
  require 'securerandom'
3
4
  require 'greenletters'
@@ -69,7 +70,7 @@ module BooJS
69
70
 
70
71
  #Optionally, accept code to inject and a command to run
71
72
  #If the command is nil, this is not executed as a oneshot
72
- def self.pipe(str=nil, cmd=nil, will_timeout=false)
73
+ def self.pipe(str=nil, cmd=nil, will_timeout=false, output_phantomjs_pid=false)
73
74
  js = %{
74
75
  var system = require('system');
75
76
  function __spec_ping(str) {
@@ -120,26 +121,41 @@ module BooJS
120
121
  @input_sender_r, @input_sender_w = IO.pipe
121
122
 
122
123
  #Phantom JS Process
123
- p = IO.popen("#{$phantomjs_path} #{tmp.path}")
124
- loop do
125
- rr, _ = select([p, STDIN]); e = rr[0]
126
- #PhantomJS has written something
127
- if e == p
128
- res = e.readline
129
-
130
- if res =~ /STDIN_PORT/
131
- port = res.split(" ")[1].to_i
132
- start_server(port, @input_sender_r)
133
- else
134
- puts res
135
- $stdout.flush
136
- end
124
+ begin
125
+ p = IO.popen("#{$phantomjs_path} #{tmp.path}")
126
+
127
+ #We are going to do a loop back (ready to rcv) to wait for the JS to respond before outputing the spec PID
128
+ #So that in the specs, we have a reliable way of testing the process execution as ending this process
129
+ #before the child process ends, will termiante the spawned process (PhantomJS) by default but not if it's
130
+ #kept alive for a while (which we handle via sending it an INT in the ensure)
131
+ if output_phantomjs_pid
132
+ @input_sender_w.puts "booPing();"
133
+ res = p.readline
134
+ puts p.pid
137
135
  end
138
136
 
139
- #User has written to this program
140
- if e == STDIN
141
- @input_sender_w.puts e.readline
137
+ loop do
138
+ rr, _ = select([p, STDIN]); e = rr[0]
139
+ #PhantomJS has written something
140
+ if e == p
141
+ res = e.readline
142
+
143
+ if res =~ /STDIN_PORT/
144
+ port = res.split(" ")[1].to_i
145
+ start_server(port, @input_sender_r)
146
+ else
147
+ puts res
148
+ $stdout.flush
149
+ end
150
+ end
151
+
152
+ #User has written to this program
153
+ if e == STDIN
154
+ @input_sender_w.puts e.readline
155
+ end
142
156
  end
157
+ ensure
158
+ Process.kill :INT, p.pid
143
159
  end
144
160
 
145
161
  tmp.unlink
@@ -0,0 +1,86 @@
1
+ #Verify correct termination (full cleanup on any exit)
2
+ require 'open3'
3
+ require 'tempfile'
4
+ require 'securerandom'
5
+
6
+ BOOJS = File.join(File.dirname(__FILE__), "../bin/boojs")
7
+
8
+ describe "CLI" do
9
+ before(:each) do
10
+ #Laziness
11
+ end
12
+
13
+ it "Will output a pid for the first stdout when given the -p flag via the command line" do
14
+ begin
15
+ p = IO.popen("#{BOOJS} -p", "r+")
16
+
17
+ Timeout.timeout(6) do
18
+ @line = p.readline
19
+ end
20
+
21
+ expect(@line).to match(/\d*/)
22
+ ensure
23
+ Process.kill(:INT, p.pid)
24
+ end
25
+ end
26
+
27
+ it "Will terminate the process it spawns after the main program is sent a SIGINT" do
28
+ begin
29
+ p = IO.popen("#{BOOJS} -p", "r+")
30
+
31
+ Timeout.timeout(6) do
32
+ @spawned_pid = p.readline.to_i
33
+ end
34
+
35
+ #Kill boojs process
36
+ Process.kill :INT, p.pid
37
+
38
+ sleep 1
39
+
40
+ #Make sure phantomJS process is not running
41
+ begin
42
+ res = Process.kill 0, @spawned_pid
43
+ rescue Errno::ESRCH
44
+ @phantomjs_did_not_exist = true
45
+ end
46
+
47
+ expect(@phantomjs_did_not_exist).to eq(true)
48
+ ensure
49
+ begin
50
+ #Process.kill(:INT, p.pid)
51
+ #Process.kill(:INT, @spawned_pid)
52
+ rescue Errno::ESRCH
53
+ end
54
+ end
55
+ end
56
+
57
+ it "Will terminate the process it spawns after the main program is sent a SIGTERM" do
58
+ begin
59
+ p = IO.popen("#{BOOJS} -p", "r+")
60
+
61
+ Timeout.timeout(6) do
62
+ @spawned_pid = p.readline.to_i
63
+ end
64
+
65
+ #Kill boojs process
66
+ Process.kill :TERM, p.pid
67
+
68
+ sleep 1
69
+
70
+ #Make sure phantomJS process is not running
71
+ begin
72
+ res = Process.kill 0, @spawned_pid
73
+ rescue Errno::ESRCH
74
+ @phantomjs_did_not_exist = true
75
+ end
76
+
77
+ expect(@phantomjs_did_not_exist).to eq(true)
78
+ ensure
79
+ begin
80
+ Process.kill(:INT, p.pid)
81
+ Process.kill(:INT, @spawned_pid)
82
+ rescue Errno::ESRCH
83
+ end
84
+ end
85
+ end
86
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boojs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.26
4
+ version: 0.0.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - seo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-25 00:00:00.000000000 Z
11
+ date: 2015-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -150,6 +150,7 @@ files:
150
150
  - spec/samples/set_a_stdin.js
151
151
  - spec/samples/syntax_problem.js
152
152
  - spec/speed_spec.rb
153
+ - spec/termination.rb
153
154
  - spec/verify_spec.rb
154
155
  - usage.gif
155
156
  homepage: https://github.com/sotownsend/BooJS
@@ -186,4 +187,5 @@ test_files:
186
187
  - spec/samples/set_a_stdin.js
187
188
  - spec/samples/syntax_problem.js
188
189
  - spec/speed_spec.rb
190
+ - spec/termination.rb
189
191
  - spec/verify_spec.rb