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 ]
|
||||
Flowerbox.messageQueue.push(message)
|
||||
|
||||
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
|
||||
Flowerbox.queuePuller() if Flowerbox.socket
|
||||
|
||||
started: 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: ->
|
||||
|
||||
|
@ -13,7 +13,7 @@ module Flowerbox
|
||||
def_delegators :data, :[]
|
||||
|
||||
def type
|
||||
self.class.name.split("::").last.downcase.to_sym
|
||||
@type ||= self.class.name.split("::").last.downcase.to_sym
|
||||
end
|
||||
|
||||
def initialize(data)
|
||||
@ -41,13 +41,7 @@ module Flowerbox
|
||||
end
|
||||
|
||||
def <=>(other)
|
||||
result = self.name.length <=> other.name.length
|
||||
|
||||
if result == 0
|
||||
|
||||
end
|
||||
|
||||
result
|
||||
self.name.length <=> other.name.length
|
||||
end
|
||||
|
||||
def runners
|
||||
|
@ -35,16 +35,11 @@ module Flowerbox::Result
|
||||
end
|
||||
|
||||
def filtered_stack
|
||||
filtered_stack = stack.reject { |line|
|
||||
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 = stack.reject { |line| Flowerbox.backtrace_filter.any? { |filter| line[filter] } }
|
||||
|
||||
filtered_stack.shift if exception?
|
||||
filtered_stack
|
||||
|
||||
filter_coffeescript_lines(filtered_stack)
|
||||
end
|
||||
|
||||
def first_local_stack
|
||||
|
@ -25,7 +25,7 @@ module Flowerbox::Result::FileInfo
|
||||
return @line_number if @line_number
|
||||
|
||||
@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"
|
||||
end
|
||||
|
||||
@ -38,5 +38,15 @@ module Flowerbox::Result::FileInfo
|
||||
def system_files
|
||||
Flowerbox.test_environment.system_files
|
||||
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
|
||||
|
||||
|
@ -38,6 +38,15 @@ module Flowerbox
|
||||
File.open(file, 'wb') { |fh| fh.print template }
|
||||
|
||||
system %{node #{file}}
|
||||
|
||||
if $?.exitstatus == 0
|
||||
count = 20
|
||||
while !finished? && count > 0
|
||||
sleep 0.1
|
||||
|
||||
count -= 1
|
||||
end
|
||||
end
|
||||
ensure
|
||||
File.unlink(file) if file
|
||||
end
|
||||
@ -66,6 +75,8 @@ jsdom.env(
|
||||
|
||||
var gotFlowerbox = false;
|
||||
|
||||
console._log = console.log;
|
||||
|
||||
var socket = new ws('ws://localhost:#{server.port + 1}/');
|
||||
socket.onopen = function() {
|
||||
var files = #{sprockets.files.to_json};
|
||||
|
@ -40,16 +40,20 @@ module Flowerbox
|
||||
puts msg
|
||||
end
|
||||
|
||||
def page_title
|
||||
"Flowerbox - #{Flowerbox.test_environment.name} Runner"
|
||||
end
|
||||
|
||||
def template
|
||||
env = start_test_environment
|
||||
|
||||
<<-HTML
|
||||
<html>
|
||||
<head>
|
||||
<title>Flowerbox - #{Flowerbox.test_environment.name} Runner</title>
|
||||
<title>#{page_title}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Flowerbox - #{Flowerbox.test_environment.name} Runner</h1>
|
||||
<h1>#{page_title}</h1>
|
||||
<pre id="queue"></pre>
|
||||
<script type="text/javascript">
|
||||
console._log = console.log;
|
||||
|
@ -5,16 +5,20 @@ require 'rack/builder'
|
||||
require 'thin'
|
||||
require 'em-websocket'
|
||||
require 'json'
|
||||
require 'forwardable'
|
||||
|
||||
module Flowerbox
|
||||
class Server
|
||||
attr_reader :options
|
||||
|
||||
attr_accessor :runner
|
||||
|
||||
class MissingRackApp < StandardError ; end
|
||||
class ServerDiedError < StandardError ; end
|
||||
|
||||
extend Forwardable
|
||||
|
||||
def_delegators :@server_thread, :alive?
|
||||
|
||||
def initialize(runner, options = {})
|
||||
@runner = runner
|
||||
@options = { :logging => false }.merge(options || {})
|
||||
@ -52,9 +56,9 @@ module Flowerbox
|
||||
def start
|
||||
@server_thread = Thread.new do
|
||||
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))
|
||||
|
||||
::Rack::Handler::Thin.run(rack_app, server_options) do |server|
|
||||
@ -65,18 +69,19 @@ module Flowerbox
|
||||
end
|
||||
rescue => e
|
||||
@server_thread[:exception] = e
|
||||
|
||||
raise e
|
||||
end
|
||||
end
|
||||
|
||||
while !@server_thread[:server] && @server_thread.alive?
|
||||
while !@server_thread[:server] && alive?
|
||||
sleep 0.1
|
||||
end
|
||||
|
||||
if @server_thread[:exception]
|
||||
raise @server_thread[:exception]
|
||||
else
|
||||
raise ServerDiedError.new if !@server_thread.alive?
|
||||
raise ServerDiedError.new if !alive?
|
||||
end
|
||||
end
|
||||
|
||||
@ -98,24 +103,16 @@ module Flowerbox
|
||||
return @port if @port
|
||||
|
||||
if options[:port]
|
||||
@port = options[:port]
|
||||
return @port
|
||||
return @port = options[:port]
|
||||
end
|
||||
|
||||
attempts = 20
|
||||
|
||||
begin
|
||||
try_server_to_something(nil, Proc.new { |port| @port = port }, (random_port / 2).floor * 2)
|
||||
|
||||
attempts -= 1
|
||||
|
||||
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
|
||||
end while !@port and attempts != 0
|
||||
|
||||
raise StandardError.new("can't start server") if attempts == 0
|
||||
|
||||
@ -123,43 +120,44 @@ module Flowerbox
|
||||
end
|
||||
|
||||
def address
|
||||
"http://#{interface}:#{port}/"
|
||||
end
|
||||
|
||||
def alive?
|
||||
@server_thread.alive?
|
||||
@address ||= "http://#{interface}:#{port}/"
|
||||
end
|
||||
|
||||
private
|
||||
def wait_for_server_to_start
|
||||
while true do
|
||||
begin
|
||||
connect_interface = '127.0.0.1' if interface == '0.0.0.0'
|
||||
started = false
|
||||
|
||||
TCPSocket.new(connect_interface, port)
|
||||
break
|
||||
rescue Errno::ECONNREFUSED => e
|
||||
end
|
||||
|
||||
sleep 0.1
|
||||
while !started do
|
||||
try_server_to_something(Proc.new { started = true })
|
||||
end
|
||||
end
|
||||
|
||||
def wait_for_server_to_stop
|
||||
while alive? do
|
||||
begin
|
||||
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
|
||||
try_server_to_something(nil, Proc.new { return })
|
||||
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
|
||||
25000 + Kernel.rand(1000)
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user