Class: InteractiveServer

Inherits:
Object
  • Object
show all
Defined in:
app/drivers/chrome/pipe.rb

Overview

Interactive pipe for testing. Creates a server that routes $stdin into if_dispatch and $stdout to int_dispatch.

Instance Method Summary collapse

Constructor Details

#initialize(app_js_path) ⇒ InteractiveServer

Returns a new instance of InteractiveServer.



11
12
13
14
# File 'app/drivers/chrome/pipe.rb', line 11

def initialize app_js_path
  @app_js = File.read app_js_path
  inject_dispatch_shims
end

Instance Method Details

#_begin_pipeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/drivers/chrome/pipe.rb', line 23

def _begin_pipe
  catch(:is_restarting) do
    tmp = Tempfile.new(SecureRandom.hex)
    path = tmp.path
    tmp.close!
    File.write path, @app_js

    p = Open3.popen3 "boojs #{path}" do |inp, out, err, t|
      pid = t[:pid]
      begin
        loop do
          results = select([out, err, STDIN], [])
          if results[0].include? err
            err_msg = err.readline
            $stderr.puts err_msg
            #exit 1 unless err_msg =~ /[debug]/
          end

          if results[0].include? STDIN
            begin
              q = gets.strip

              if q == "HARD_RESTART"
                throw :is_restarting
              elsif q == "RESTART"
                inp.puts "$__RESTART__"

                begin
                  #Wait for restart to respond 'ok'
                  Timeout::timeout(10) do
                    restart_res = out.readline
                    raise "Restart of phantomjs did not return '__RESTART_OK__' like expected, returned: #{restart_res.inspect}" unless restart_res == "$__RESTART_OK__\n"

                    puts "RESTART OK"
                  end
                rescue Timeout::Error
                  raise "Restart of boojs did not happen within 5 seconds"
                rescue EOFError
                  #Sleep for a second to let the error pipe fill up from boojs, this error
                  #will then be displayed and then we will crash
                  sleep 3
                  $stderr.puts err.read
                  raise "boojs encountered an error"
                end

              else
                inp.puts "if_dispatch(JSON.parse('#{q}'))"
              end
            rescue Errno::EIO
              #Can't say anything here, we don't have a pipe
              exit 1
            rescue NoMethodError
              exit 1
            end
          end

          if results[0].include? out
            res = out.readline
            $stdout.puts res
            $stdout.flush
          end
        end
      ensure
        begin
          Process.kill :INT, pid
        rescue Errno::ESRCH
        end
      end
    end
  end

  puts "RESTART OK"
  _begin_pipe
end

#begin_pipeObject

Will take over any remaining IO



17
18
19
20
21
# File 'app/drivers/chrome/pipe.rb', line 17

def begin_pipe
  puts "LOADED"

  _begin_pipe
end

#inject_dispatch_shimsObject

JS Functions##################################################### Make calls to if_dispatch go to $stdout, make $stdin call int_dispatch



100
101
102
103
104
105
106
# File 'app/drivers/chrome/pipe.rb', line 100

def inject_dispatch_shims
  @app_js << %{
    function int_dispatch(q) {
      system.stdout.writeLine(JSON.stringify(q));
    }
  }
end