do you like evented i/o? i like evented i/o.
This commit is contained in:
parent
91027aee61
commit
4ee3660077
@ -16,19 +16,24 @@ Flowerbox =
|
|||||||
message = [ url, data ]
|
message = [ url, data ]
|
||||||
Flowerbox.messageQueue.push(message)
|
Flowerbox.messageQueue.push(message)
|
||||||
|
|
||||||
if Flowerbox.socket
|
Flowerbox.queuePuller() if Flowerbox.socket
|
||||||
while Flowerbox.messageQueue.length > 0
|
|
||||||
message = Flowerbox.messageQueue.shift()
|
|
||||||
Flowerbox.socket.send(JSON.stringify(message))
|
|
||||||
|
|
||||||
if url == 'results'
|
|
||||||
if __$instrument?
|
|
||||||
Flowerbox.socket.send(JSON.stringify(['instrument', __$instrument]))
|
|
||||||
|
|
||||||
Flowerbox.done = true
|
|
||||||
|
|
||||||
started: false
|
started: false
|
||||||
done: false
|
done: false
|
||||||
|
|
||||||
|
queuePuller: ->
|
||||||
|
if Flowerbox.messageQueue.length > 0
|
||||||
|
message = Flowerbox.messageQueue.shift()
|
||||||
|
|
||||||
|
Flowerbox.socket.send JSON.stringify(message), {}, ->
|
||||||
|
if message[0] == 'results'
|
||||||
|
if __$instrument?
|
||||||
|
Flowerbox.socket.send JSON.stringify(['instrument', __$instrument]) {}, ->
|
||||||
|
Flowerbox.done = true
|
||||||
|
else
|
||||||
|
Flowerbox.done = true
|
||||||
|
|
||||||
|
Flowerbox.queuePuller()
|
||||||
|
|
||||||
fail: ->
|
fail: ->
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ module Flowerbox
|
|||||||
def_delegators :data, :[]
|
def_delegators :data, :[]
|
||||||
|
|
||||||
def type
|
def type
|
||||||
self.class.name.split("::").last.downcase.to_sym
|
@type ||= self.class.name.split("::").last.downcase.to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(data)
|
def initialize(data)
|
||||||
@ -41,13 +41,7 @@ module Flowerbox
|
|||||||
end
|
end
|
||||||
|
|
||||||
def <=>(other)
|
def <=>(other)
|
||||||
result = self.name.length <=> other.name.length
|
self.name.length <=> other.name.length
|
||||||
|
|
||||||
if result == 0
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
result
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def runners
|
def runners
|
||||||
|
@ -35,16 +35,11 @@ module Flowerbox::Result
|
|||||||
end
|
end
|
||||||
|
|
||||||
def filtered_stack
|
def filtered_stack
|
||||||
filtered_stack = stack.reject { |line|
|
filtered_stack = stack.reject { |line| Flowerbox.backtrace_filter.any? { |filter| line[filter] } }
|
||||||
Flowerbox.backtrace_filter.any? { |filter| line[filter] }
|
|
||||||
}.collect { |line|
|
|
||||||
line.gsub(%r{\.coffee:(\d+)}) do |_|
|
|
||||||
".coffee:~#{($1.to_i * 0.67 + 1).to_i}"
|
|
||||||
end
|
|
||||||
}
|
|
||||||
|
|
||||||
filtered_stack.shift if exception?
|
filtered_stack.shift if exception?
|
||||||
filtered_stack
|
|
||||||
|
filter_coffeescript_lines(filtered_stack)
|
||||||
end
|
end
|
||||||
|
|
||||||
def first_local_stack
|
def first_local_stack
|
||||||
|
@ -25,7 +25,7 @@ module Flowerbox::Result::FileInfo
|
|||||||
return @line_number if @line_number
|
return @line_number if @line_number
|
||||||
|
|
||||||
@line_number = file.to_s.split(":")[1]
|
@line_number = file.to_s.split(":")[1]
|
||||||
@line_number = "~#{(@line_number.to_i * 0.67).to_i}" if file_translated?
|
@line_number = "~#{coffeescript_line_to_js_line(@line_number)}" if file_translated?
|
||||||
@line_number ||= "0"
|
@line_number ||= "0"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -38,5 +38,15 @@ module Flowerbox::Result::FileInfo
|
|||||||
def system_files
|
def system_files
|
||||||
Flowerbox.test_environment.system_files
|
Flowerbox.test_environment.system_files
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def filter_coffeescript_lines(lines)
|
||||||
|
[ lines ].flatten.collect { |line|
|
||||||
|
line.gsub(%r{\.coffee:(\d+)}) { |_| ".coffee:~#{coffeescript_line_to_js_line($1)}" }
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def coffeescript_line_to_js_line(number)
|
||||||
|
(number.to_i * 0.67 + 1).to_i
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -38,6 +38,15 @@ module Flowerbox
|
|||||||
File.open(file, 'wb') { |fh| fh.print template }
|
File.open(file, 'wb') { |fh| fh.print template }
|
||||||
|
|
||||||
system %{node #{file}}
|
system %{node #{file}}
|
||||||
|
|
||||||
|
if $?.exitstatus == 0
|
||||||
|
count = 20
|
||||||
|
while !finished? && count > 0
|
||||||
|
sleep 0.1
|
||||||
|
|
||||||
|
count -= 1
|
||||||
|
end
|
||||||
|
end
|
||||||
ensure
|
ensure
|
||||||
File.unlink(file) if file
|
File.unlink(file) if file
|
||||||
end
|
end
|
||||||
@ -66,6 +75,8 @@ jsdom.env(
|
|||||||
|
|
||||||
var gotFlowerbox = false;
|
var gotFlowerbox = false;
|
||||||
|
|
||||||
|
console._log = console.log;
|
||||||
|
|
||||||
var socket = new ws('ws://localhost:#{server.port + 1}/');
|
var socket = new ws('ws://localhost:#{server.port + 1}/');
|
||||||
socket.onopen = function() {
|
socket.onopen = function() {
|
||||||
var files = #{sprockets.files.to_json};
|
var files = #{sprockets.files.to_json};
|
||||||
|
@ -40,16 +40,20 @@ module Flowerbox
|
|||||||
puts msg
|
puts msg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def page_title
|
||||||
|
"Flowerbox - #{Flowerbox.test_environment.name} Runner"
|
||||||
|
end
|
||||||
|
|
||||||
def template
|
def template
|
||||||
env = start_test_environment
|
env = start_test_environment
|
||||||
|
|
||||||
<<-HTML
|
<<-HTML
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Flowerbox - #{Flowerbox.test_environment.name} Runner</title>
|
<title>#{page_title}</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Flowerbox - #{Flowerbox.test_environment.name} Runner</h1>
|
<h1>#{page_title}</h1>
|
||||||
<pre id="queue"></pre>
|
<pre id="queue"></pre>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
console._log = console.log;
|
console._log = console.log;
|
||||||
|
@ -5,16 +5,20 @@ require 'rack/builder'
|
|||||||
require 'thin'
|
require 'thin'
|
||||||
require 'em-websocket'
|
require 'em-websocket'
|
||||||
require 'json'
|
require 'json'
|
||||||
|
require 'forwardable'
|
||||||
|
|
||||||
module Flowerbox
|
module Flowerbox
|
||||||
class Server
|
class Server
|
||||||
attr_reader :options
|
attr_reader :options
|
||||||
|
|
||||||
attr_accessor :runner
|
attr_accessor :runner
|
||||||
|
|
||||||
class MissingRackApp < StandardError ; end
|
class MissingRackApp < StandardError ; end
|
||||||
class ServerDiedError < StandardError ; end
|
class ServerDiedError < StandardError ; end
|
||||||
|
|
||||||
|
extend Forwardable
|
||||||
|
|
||||||
|
def_delegators :@server_thread, :alive?
|
||||||
|
|
||||||
def initialize(runner, options = {})
|
def initialize(runner, options = {})
|
||||||
@runner = runner
|
@runner = runner
|
||||||
@options = { :logging => false }.merge(options || {})
|
@options = { :logging => false }.merge(options || {})
|
||||||
@ -52,9 +56,9 @@ module Flowerbox
|
|||||||
def start
|
def start
|
||||||
@server_thread = Thread.new do
|
@server_thread = Thread.new do
|
||||||
begin
|
begin
|
||||||
EventMachine.run do
|
server_options = { :Port => port, :Host => interface }
|
||||||
server_options = { :Port => port, :Host => interface }
|
|
||||||
|
|
||||||
|
EventMachine.run do
|
||||||
EventMachine::WebSocket.start(:host => interface, :port => port + 1, &method(:websocket_app))
|
EventMachine::WebSocket.start(:host => interface, :port => port + 1, &method(:websocket_app))
|
||||||
|
|
||||||
::Rack::Handler::Thin.run(rack_app, server_options) do |server|
|
::Rack::Handler::Thin.run(rack_app, server_options) do |server|
|
||||||
@ -65,18 +69,19 @@ module Flowerbox
|
|||||||
end
|
end
|
||||||
rescue => e
|
rescue => e
|
||||||
@server_thread[:exception] = e
|
@server_thread[:exception] = e
|
||||||
|
|
||||||
raise e
|
raise e
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
while !@server_thread[:server] && @server_thread.alive?
|
while !@server_thread[:server] && alive?
|
||||||
sleep 0.1
|
sleep 0.1
|
||||||
end
|
end
|
||||||
|
|
||||||
if @server_thread[:exception]
|
if @server_thread[:exception]
|
||||||
raise @server_thread[:exception]
|
raise @server_thread[:exception]
|
||||||
else
|
else
|
||||||
raise ServerDiedError.new if !@server_thread.alive?
|
raise ServerDiedError.new if !alive?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -98,24 +103,16 @@ module Flowerbox
|
|||||||
return @port if @port
|
return @port if @port
|
||||||
|
|
||||||
if options[:port]
|
if options[:port]
|
||||||
@port = options[:port]
|
return @port = options[:port]
|
||||||
return @port
|
|
||||||
end
|
end
|
||||||
|
|
||||||
attempts = 20
|
attempts = 20
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
try_server_to_something(nil, Proc.new { |port| @port = port }, (random_port / 2).floor * 2)
|
||||||
|
|
||||||
attempts -= 1
|
attempts -= 1
|
||||||
|
end while !@port and attempts != 0
|
||||||
current_port = (random_port / 2).floor * 2
|
|
||||||
|
|
||||||
begin
|
|
||||||
socket = TCPSocket.new(interface, current_port)
|
|
||||||
socket.close
|
|
||||||
rescue Errno::ECONNREFUSED => e
|
|
||||||
@port = current_port
|
|
||||||
end
|
|
||||||
end while !@port and attempts > 0
|
|
||||||
|
|
||||||
raise StandardError.new("can't start server") if attempts == 0
|
raise StandardError.new("can't start server") if attempts == 0
|
||||||
|
|
||||||
@ -123,43 +120,44 @@ module Flowerbox
|
|||||||
end
|
end
|
||||||
|
|
||||||
def address
|
def address
|
||||||
"http://#{interface}:#{port}/"
|
@address ||= "http://#{interface}:#{port}/"
|
||||||
end
|
|
||||||
|
|
||||||
def alive?
|
|
||||||
@server_thread.alive?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def wait_for_server_to_start
|
def wait_for_server_to_start
|
||||||
while true do
|
started = false
|
||||||
begin
|
|
||||||
connect_interface = '127.0.0.1' if interface == '0.0.0.0'
|
|
||||||
|
|
||||||
TCPSocket.new(connect_interface, port)
|
while !started do
|
||||||
break
|
try_server_to_something(Proc.new { started = true })
|
||||||
rescue Errno::ECONNREFUSED => e
|
|
||||||
end
|
|
||||||
|
|
||||||
sleep 0.1
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def wait_for_server_to_stop
|
def wait_for_server_to_stop
|
||||||
while alive? do
|
while alive? do
|
||||||
begin
|
try_server_to_something(nil, Proc.new { return })
|
||||||
connect_interface = '127.0.0.1' if interface == '0.0.0.0'
|
|
||||||
|
|
||||||
socket = TCPSocket.new(connect_interface, port)
|
|
||||||
socket.close
|
|
||||||
rescue Errno::ECONNREFUSED => e
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
sleep 0.1
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def try_server_to_something(success, failure = nil, current_port = port)
|
||||||
|
begin
|
||||||
|
connect_interface = '127.0.0.1' if interface == '0.0.0.0'
|
||||||
|
|
||||||
|
socket = TCPSocket.new(connect_interface, current_port)
|
||||||
|
socket.close
|
||||||
|
|
||||||
|
success.call(current_port) if success
|
||||||
|
rescue => e
|
||||||
|
case e
|
||||||
|
when Errno::ECONNREFUSED, Errno::ECONNRESET
|
||||||
|
failure.call(current_port) if failure
|
||||||
|
else
|
||||||
|
raise e
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
sleep 0.1
|
||||||
|
end
|
||||||
|
|
||||||
def random_port
|
def random_port
|
||||||
25000 + Kernel.rand(1000)
|
25000 + Kernel.rand(1000)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user