1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/test/test_worker_gem_independence.rb
Chris LaRose 64c0153cd0
Remove use of json gem (#2479)
* Add basic JSON serializer

For now, it only handles Arrays of Integers, but we'll extend it to
support all of the common types

* Serialize Strings

* Escape quotes in Strings

* Escape backslashes in Strings

* Serialize Hashes with String keys

* Extract method for serializing Strings

* Add test coverage for non-Hash non-Array JSON serialization

* Add test for unexpected key types

* Serialize Hashes with Symbol keys

* Raise on unexpected value types

* Serialize boolean values

* Serialize Floats

* Add module comment to Puma::JSON

* Update integration test to use fully-qualfied JSON module reference

* Remove json gem dependency from /stats status server response

Fixes a bug where requesting `/stats` from the status server would cause
subsequent phased restarts to fail when upgrading/downgrading the json
gem.

* Run gc_stats tests on JRuby

These were disabled at some point on JRuby, but they seem to run fine.
Importantly, this test ensures that a call to `/gc-stats` returns
well-formed JSON on JRuby, where the value of `GC.stat` contains nested
structures.

* Remove json gem dependency from /gc-stats status server response

Fixes a bug where requesting `/gc-stats` from the status server would cause
subsequent phased restarts to fail when upgrading/downgrading the json
gem.

* Remove json gem from /thread-backtraces status server response

Fixes a bug where requesting `/thread-backtraces` from the status server
would cause subsequent phased restarts to fail when
upgrading/downgrading the json gem.

* Remove json gem dependency from Puma.stats

Fixes a bug where accessing `Puma.stats` would cause subsequent phased
restarts to fail when upgrading/downgrading the json gem.

* Fix test name in json test

Co-authored-by: rmacklin <1863540+rmacklin@users.noreply.github.com>

* Add History entry

* Add test for exceptions on values of unexpected types

* Update test name for additional clarity

* Reorder cases to match order in ECMA-404

* Allow all serializable inputs in Puma::JSON::serialize

The pervious implementation was based on and older JSON standard which
defined JSON texts to be either objects or arrays. Modern JSON standands
allow all JSON values to be valid JSON texts.

* Update JSON tests to test value types directly

* Reorder tests to roughly match source order

* Add test for serializing integers as JSON

* Serialize nil as null

* Use block form of gsub instead of hash form

* Escape control characters as required by ECMA-404

* Collapse handling of Symbol and String into one case

* Extract constants used in string serialization

* Remove superflous else case

* Use stringio for incremental JSON construction

* Extract test helper for testing JSON serialization

* Assert that strings generated by Puma::JSON roundtrip when using ::JSON

* Use a recent version of the json gem in tests

`::JSON.parse` doesn't handle JSON texts other than objects and arrays
in old versions

* Handle default expected_roundtrip more explicitly for clarity

Co-authored-by: rmacklin <1863540+rmacklin@users.noreply.github.com>
2020-11-10 10:16:42 -07:00

145 lines
5.7 KiB
Ruby

require_relative "helper"
require_relative "helpers/integration"
class TestWorkerGemIndependence < TestIntegration
def setup
skip NO_FORK_MSG unless HAS_FORK
super
end
def teardown
return if skipped?
FileUtils.rm current_release_symlink, force: true
super
end
def test_changing_nio4r_version_during_phased_restart
change_gem_version_during_phased_restart old_app_dir: 'worker_gem_independence_test/old_nio4r',
old_version: '2.3.0',
new_app_dir: 'worker_gem_independence_test/new_nio4r',
new_version: '2.3.1'
end
def test_changing_json_version_during_phased_restart
change_gem_version_during_phased_restart old_app_dir: 'worker_gem_independence_test/old_json',
old_version: '2.3.1',
new_app_dir: 'worker_gem_independence_test/new_json',
new_version: '2.3.0'
end
def test_changing_json_version_during_phased_restart_after_querying_stats_from_status_server
@control_tcp_port = UniquePort.call
server_opts = "--control-url tcp://#{HOST}:#{@control_tcp_port} --control-token #{TOKEN}"
before_restart = ->() do
cli_pumactl "stats"
end
change_gem_version_during_phased_restart server_opts: server_opts,
before_restart: before_restart,
old_app_dir: 'worker_gem_independence_test/old_json',
old_version: '2.3.1',
new_app_dir: 'worker_gem_independence_test/new_json',
new_version: '2.3.0'
end
def test_changing_json_version_during_phased_restart_after_querying_gc_stats_from_status_server
@control_tcp_port = UniquePort.call
server_opts = "--control-url tcp://#{HOST}:#{@control_tcp_port} --control-token #{TOKEN}"
before_restart = ->() do
cli_pumactl "gc-stats"
end
change_gem_version_during_phased_restart server_opts: server_opts,
before_restart: before_restart,
old_app_dir: 'worker_gem_independence_test/old_json',
old_version: '2.3.1',
new_app_dir: 'worker_gem_independence_test/new_json',
new_version: '2.3.0'
end
def test_changing_json_version_during_phased_restart_after_querying_thread_backtraces_from_status_server
@control_tcp_port = UniquePort.call
server_opts = "--control-url tcp://#{HOST}:#{@control_tcp_port} --control-token #{TOKEN}"
before_restart = ->() do
cli_pumactl "thread-backtraces"
end
change_gem_version_during_phased_restart server_opts: server_opts,
before_restart: before_restart,
old_app_dir: 'worker_gem_independence_test/old_json',
old_version: '2.3.1',
new_app_dir: 'worker_gem_independence_test/new_json',
new_version: '2.3.0'
end
def test_changing_json_version_during_phased_restart_after_accessing_puma_stats_directly
change_gem_version_during_phased_restart old_app_dir: 'worker_gem_independence_test/old_json_with_puma_stats_after_fork',
old_version: '2.3.1',
new_app_dir: 'worker_gem_independence_test/new_json_with_puma_stats_after_fork',
new_version: '2.3.0'
end
private
def change_gem_version_during_phased_restart(old_app_dir:,
new_app_dir:,
old_version:,
new_version:,
server_opts: '',
before_restart: nil)
skip_unless_signal_exist? :USR1
set_release_symlink File.expand_path(old_app_dir, __dir__)
Dir.chdir(current_release_symlink) do
with_unbundled_env do
system("bundle config --local path vendor/bundle", out: File::NULL)
system("bundle install", out: File::NULL)
cli_server "--prune-bundler -w 1 #{server_opts}"
end
end
connection = connect
initial_reply = read_body(connection)
assert_equal old_version, initial_reply
before_restart.call if before_restart
set_release_symlink File.expand_path(new_app_dir, __dir__)
Dir.chdir(current_release_symlink) do
with_unbundled_env do
system("bundle config --local path vendor/bundle", out: File::NULL)
system("bundle install", out: File::NULL)
end
end
start_phased_restart
connection = connect
new_reply = read_body(connection)
assert_equal new_version, new_reply
end
def current_release_symlink
File.expand_path "worker_gem_independence_test/current", __dir__
end
def set_release_symlink(target_dir)
FileUtils.rm current_release_symlink, force: true
FileUtils.symlink target_dir, current_release_symlink, force: true
end
def start_phased_restart
Process.kill :USR1, @pid
true while @server.gets !~ /booted, phase: 1/
end
def with_unbundled_env
bundler_ver = Gem::Version.new(Bundler::VERSION)
if bundler_ver < Gem::Version.new('2.1.0')
Bundler.with_clean_env { yield }
else
Bundler.with_unbundled_env { yield }
end
end
end