Skip to content

Commit

Permalink
Create integration_async_test for to be enable test with rainbows
Browse files Browse the repository at this point in the history
  • Loading branch information
rkmathi committed Aug 5, 2020
1 parent c0d4dd7 commit 96d1f0b
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 39 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -41,7 +41,8 @@ if RUBY_ENGINE == "ruby"
gem 'puma'
gem 'yajl-ruby'
gem 'nokogiri'
gem 'puma'
gem 'rainbows'
gem 'eventmachine'
gem 'slim', '~> 2.0'
gem 'coffee-script', '>= 2.0'
gem 'rdoc'
Expand Down
4 changes: 4 additions & 0 deletions test/integration/app.rb
@@ -1,6 +1,10 @@
$stderr.puts "loading"
require 'sinatra'

if RUBY_ENGINE == "ruby"
require_relative './rainbows'
end

configure do
set :foo, :bar
end
Expand Down
3 changes: 3 additions & 0 deletions test/integration/rainbows.conf
@@ -0,0 +1,3 @@
Rainbows! do
use :EventMachine
end
20 changes: 20 additions & 0 deletions test/integration/rainbows.rb
@@ -0,0 +1,20 @@
require 'rainbows'

module Rack
module Handler
class Rainbows
def self.run(app, **options)
rainbows_options = {
listeners: ["#{options[:Host]}:#{options[:Port]}"],
worker_processes: 1,
timeout: 30,
config_file: ::File.expand_path('../rainbows.conf', __FILE__),
}

::Rainbows::HttpServer.new(app, rainbows_options).start.join
end
end

register "rainbows", "Rack::Handler::Rainbows"
end
end
14 changes: 14 additions & 0 deletions test/integration_async_helper.rb
@@ -0,0 +1,14 @@
require File.expand_path('../integration_helper', __FILE__)

module IntegrationAsyncHelper
def it(message, &block)
base_port = 5100 + Process.pid % 100

%w(rainbows).each_with_index do |server_name, index|
server = IntegrationHelper::BaseServer.new(server_name, base_port + index)
next unless server.installed?

super("with #{server.name}: #{message}") { server.run_test(self, &block) }
end
end
end
40 changes: 40 additions & 0 deletions test/integration_async_test.rb
@@ -0,0 +1,40 @@
require File.expand_path('../helper', __FILE__)
require File.expand_path('../integration_async_helper', __FILE__)

# These tests are like integration_test, but they test asynchronous streaming.
class IntegrationAsyncTest < Minitest::Test
extend IntegrationAsyncHelper
attr_accessor :server

it 'streams async' do
Timeout.timeout(3) do
chunks = []
server.get_stream '/async' do |chunk|
next if chunk.empty?
chunks << chunk
case chunk
when "hi!" then server.get "/send?msg=hello"
when "hello" then server.get "/send?close=1"
end
end

assert_equal ['hi!', 'hello'], chunks
end
end

it 'streams async from subclass' do
Timeout.timeout(3) do
chunks = []
server.get_stream '/subclass/async' do |chunk|
next if chunk.empty?
chunks << chunk
case chunk
when "hi!" then server.get "/subclass/send?msg=hello"
when "hello" then server.get "/subclass/send?close=1"
end
end

assert_equal ['hi!', 'hello'], chunks
end
end
end
40 changes: 2 additions & 38 deletions test/integration_test.rb
Expand Up @@ -13,7 +13,7 @@ class IntegrationTest < Minitest::Test
it('only extends main') { assert_equal "true", server.get("/mainonly") }

it 'logs once in development mode' do
next if server.puma? or RUBY_ENGINE == 'jruby'
next if server.puma? or server.rainbows? or RUBY_ENGINE == 'jruby'
random = "%064x" % Kernel.rand(2**256-1)
server.get "/ping?x=#{random}"
count = server.log.scan("GET /ping?x=#{random}").count
Expand All @@ -39,42 +39,6 @@ class IntegrationTest < Minitest::Test
assert times[2] - times[1] > 1
end

it 'streams async' do
next unless server.rainbows?

Timeout.timeout(3) do
chunks = []
server.get_stream '/async' do |chunk|
next if chunk.empty?
chunks << chunk
case chunk
when "hi!" then server.get "/send?msg=hello"
when "hello" then server.get "/send?close=1"
end
end

assert_equal ['hi!', 'hello'], chunks
end
end

it 'streams async from subclass' do
next unless server.rainbows?

Timeout.timeout(3) do
chunks = []
server.get_stream '/subclass/async' do |chunk|
next if chunk.empty?
chunks << chunk
case chunk
when "hi!" then server.get "/subclass/send?msg=hello"
when "hello" then server.get "/subclass/send?close=1"
end
end

assert_equal ['hi!', 'hello'], chunks
end
end

it 'starts the correct server' do
exp = %r{
==\sSinatra\s\(v#{Sinatra::VERSION}\)\s
Expand All @@ -83,7 +47,7 @@ class IntegrationTest < Minitest::Test
}ix

# because Net HTTP Server logs to $stderr by default
assert_match exp, server.log unless server.net_http_server? || server.reel?
assert_match exp, server.log unless server.net_http_server? || server.reel? || server.rainbows?
end

it 'does not generate warnings' do
Expand Down

0 comments on commit 96d1f0b

Please sign in to comment.