Skip to content

Commit 68dc70c

Browse files
committed
add e2e test
1 parent 9d35065 commit 68dc70c

6 files changed

Lines changed: 77 additions & 4 deletions

File tree

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
source 'https://rubygems.org'
22

33
gemspec
4+
5+
gem 'puma'

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ GEM
1111
climate_control (0.2.0)
1212
diff-lcs (1.5.0)
1313
net_http_unix (0.2.2)
14+
nio4r (2.7.4)
1415
parallel (1.24.0)
16+
puma (7.0.3)
17+
nio4r (~> 2.0)
1518
rspec (3.12.0)
1619
rspec-core (~> 3.12.0)
1720
rspec-expectations (~> 3.12.0)
@@ -32,6 +35,7 @@ PLATFORMS
3235

3336
DEPENDENCIES
3437
climate_control (~> 0.2)
38+
puma
3539
puma-status!
3640
rspec (~> 3.8)
3741
timecop (~> 0.9)

lib/core.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ def get_memory_from_top(raw_memory)
4848
MEM_COLUMN = 5
4949
CPU_COLUMN = 8
5050
OPEN3_STDOUT = 1
51+
OPEN3_STDERR = 2
5152

5253
def get_top_stats(pids)
5354
pids.each_slice(19).inject({}) do |res, pids19|
54-
top_result = Open3.popen3({ 'LC_ALL' => 'C' }, "top -b -n 1 -p #{pids19.map(&:to_i).join(',')}")[OPEN3_STDOUT].read
55+
top = Open3.popen3({ 'LC_ALL' => 'C' }, "top -b -n 1 -p #{pids19.map(&:to_i).join(',')}")
56+
if error = top[OPEN3_STDERR].read.strip and !error.empty?
57+
warn "Error when calling top: #{error}"
58+
next res
59+
end
60+
top_result = top[OPEN3_STDOUT].read
5561
top_result.split("\n").last(pids19.length).map { |row| r = row.split(' '); [r[PID_COLUMN].to_i, get_memory_from_top(r[MEM_COLUMN]), r[CPU_COLUMN].to_f] }
5662
.inject(res) { |hash, row| hash[row[0]] = { mem: row[1], pcpu: row[2] }; hash }
5763
res

lib/puma-status.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
require 'parallel'
44

55
def run
6+
run_argv(ARGV)
7+
end
8+
9+
def run_argv(argv)
610
debug "puma-status"
711

8-
if ARGV.count < 1
12+
if argv.count < 1
913
puts "Call with:"
1014
puts "\tpuma-status path/to/puma.state"
1115
exit -1
1216
end
1317

1418
errors = []
15-
16-
outputs = Parallel.map(ARGV, in_threads: ARGV.count) do |state_file_path|
19+
20+
outputs = Parallel.map(argv, in_threads: argv.count) do |state_file_path|
1721
begin
1822
debug "State file: #{state_file_path}"
1923
format_stats(get_stats(state_file_path))

spec/e2e_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'spec_helper'
2+
3+
require 'puma-status'
4+
5+
describe 'e2e' do
6+
it 'works' do
7+
File.delete('/tmp/test_server.state') if File.exist?('/tmp/test_server.state')
8+
File.delete('/tmp/test_server.sock') if File.exist?('/tmp/test_server.sock')
9+
File.delete('/tmp/test_server_control.sock') if File.exist?('/tmp/test_server_control.sock')
10+
11+
# start process test_server.rb
12+
pid = spawn('ruby spec/test_server.rb')
13+
14+
# wait until /tmp/test_server.state exists
15+
until File.exist?('/tmp/test_server.state')
16+
sleep 0.1
17+
end
18+
19+
expect {
20+
expect {
21+
run_argv(['/tmp/test_server.state'])
22+
}.not_to output.to_stderr
23+
}.to output(/CPU/).to_stdout
24+
25+
Process.kill('TERM', pid)
26+
Process.wait(pid)
27+
end
28+
end

spec/test_server.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'puma'
2+
3+
puma_version = Puma::Const::PUMA_VERSION
4+
ruby_version = RUBY_VERSION
5+
6+
# Rack app
7+
app = proc { |env| [200, { 'Content-Type' => 'text/plain' }, ["Test server: puma: #{puma_version} ruby: #{ruby_version}\n"]] }
8+
9+
Puma::Launcher.new(
10+
Puma::Configuration.new do |user_config|
11+
user_config.bind 'unix:///tmp/test_server.sock'
12+
user_config.workers 1
13+
user_config.threads 1, 1
14+
user_config.app app
15+
user_config.activate_control_app 'unix:///tmp/test_server_control.sock', { auth_token: 'secret_token' }
16+
user_config.state_path '/tmp/test_server.state'
17+
user_config.stdout_redirect '/tmp/test_server.log', '/tmp/test_server.err', true
18+
end,
19+
{
20+
log_writer: Puma::LogWriter.null,
21+
}
22+
).run
23+
24+
# To run this server, execute:
25+
# ruby test_server.rb
26+
# Then, you can test it with:
27+
# curl --unix-socket /tmp/test_server.sock http://localhost/
28+
# You should see the output with Puma and Ruby versions.
29+
# To stop the server, simply interrupt the process (Ctrl+C).

0 commit comments

Comments
 (0)