Skip to content

Commit

Permalink
exit gracefully when SIGTERM error happens (#178)
Browse files Browse the repository at this point in the history
* exit gracfully when SIGTERM error happens

* Update version.rb

* Add tests for lamby core hander, cmd and config move sig trap in lamby_core_test
  • Loading branch information
jeremiahlukus authored Jun 12, 2024
1 parent 99dc164 commit 5f8e5ce
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
lamby (5.2.0)
lamby (5.2.1)
lambda-console-ruby
rack

Expand Down Expand Up @@ -183,6 +183,7 @@ GEM

PLATFORMS
aarch64-linux
arm64-darwin-21
arm64-darwin-22

DEPENDENCIES
Expand Down
11 changes: 11 additions & 0 deletions lib/lamby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,14 @@ def config
autoload :ProxyServer, 'lamby/proxy_server'

end

# Add signal traps for clean exit
Signal.trap("TERM") do
puts "Received SIGTERM, exiting gracefully..."
exit!(0) # exit! ensures no exception is raised
end

Signal.trap("INT") do
puts "Received SIGINT, exiting gracefully..."
exit!(0) # exit! ensures no exception is raised
end
2 changes: 1 addition & 1 deletion lib/lamby/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Lamby
VERSION = '5.2.0'
VERSION = '5.2.1'
end
48 changes: 46 additions & 2 deletions test/lamby_core_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,53 @@
require 'test_helper'

class LambyCoreSpec < LambySpec

def setup
@event = { 'httpMethod' => 'GET', 'path' => '/' }
@context = TestHelpers::LambdaContext.new
end

it 'has a version number' do
expect(Lamby::VERSION).wont_be_nil
end

it 'catches SIGTERM signal' do
assert_raises(SystemExit) do
Process.kill('TERM', Process.pid)
sleep 0.1 # Give time for the signal to be processed
end
end

it 'catches SIGINT signal' do
assert_raises(SystemExit) do
Process.kill('INT', Process.pid)
sleep 0.1 # Give time for the signal to be processed
end
end

it 'executes cmd method' do
Lamby.config.stubs(:handled_proc).returns(->(_, _) {})
result = Lamby.cmd(event: @event, context: @context)

assert result.is_a?(Hash), "Expected result to be a Hash, but got #{result.class}"
assert_equal 200, result[:statusCode], "Expected statusCode to be 200, but got #{result[:statusCode]}"
assert_includes result[:body], "Hello Lamby", "Expected body to contain 'Hello Lamby'"
end

it 'executes handler method' do
app = Rack::Builder.new do
run lambda { |env| [200, { 'Content-Type' => 'text/plain' }, ['OK']] }
end.to_app

event = {'httpMethod' => 'GET'}
result = Lamby.handler(app, event, @context)

assert result.is_a?(Hash), "Expected result to be a Hash, but got #{result.class}"
assert_equal 200, result[:statusCode], "Expected statusCode to be 200, but got #{result[:statusCode]}"
assert_equal 'OK', result[:body], "Expected body to be 'OK', but got #{result[:body]}"
end

end
it 'returns the configuration' do
config = Lamby.config
assert config.is_a?(Lamby::Configuration), "Expected config to be an instance of Lamby::Config, but got #{config.class}"
end
end

0 comments on commit 5f8e5ce

Please sign in to comment.