From 96d1f0b49c3bf2654af731243473d733108d0295 Mon Sep 17 00:00:00 2001 From: Ryuichi KAWAMATA Date: Tue, 4 Aug 2020 16:35:46 +0900 Subject: [PATCH] Create integration_async_test for to be enable test with rainbows --- Gemfile | 3 ++- test/integration/app.rb | 4 ++++ test/integration/rainbows.conf | 3 +++ test/integration/rainbows.rb | 20 ++++++++++++++++ test/integration_async_helper.rb | 14 +++++++++++ test/integration_async_test.rb | 40 ++++++++++++++++++++++++++++++++ test/integration_test.rb | 40 ++------------------------------ 7 files changed, 85 insertions(+), 39 deletions(-) create mode 100644 test/integration/rainbows.conf create mode 100644 test/integration/rainbows.rb create mode 100644 test/integration_async_helper.rb create mode 100644 test/integration_async_test.rb diff --git a/Gemfile b/Gemfile index 98123f722d..e5ecd9b8c5 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/test/integration/app.rb b/test/integration/app.rb index 3e87a00aaf..d877743c79 100644 --- a/test/integration/app.rb +++ b/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 diff --git a/test/integration/rainbows.conf b/test/integration/rainbows.conf new file mode 100644 index 0000000000..31742e961b --- /dev/null +++ b/test/integration/rainbows.conf @@ -0,0 +1,3 @@ +Rainbows! do + use :EventMachine +end diff --git a/test/integration/rainbows.rb b/test/integration/rainbows.rb new file mode 100644 index 0000000000..7a93275d78 --- /dev/null +++ b/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 diff --git a/test/integration_async_helper.rb b/test/integration_async_helper.rb new file mode 100644 index 0000000000..155a627c11 --- /dev/null +++ b/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 diff --git a/test/integration_async_test.rb b/test/integration_async_test.rb new file mode 100644 index 0000000000..79397baab3 --- /dev/null +++ b/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 diff --git a/test/integration_test.rb b/test/integration_test.rb index befe6960b7..9c6177c69f 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -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 @@ -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 @@ -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