diff --git a/Gemfile.lock b/Gemfile.lock index ab1e2a5..130879c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - lamby (5.2.0) + lamby (5.2.1) lambda-console-ruby rack @@ -183,6 +183,7 @@ GEM PLATFORMS aarch64-linux + arm64-darwin-21 arm64-darwin-22 DEPENDENCIES diff --git a/lib/lamby.rb b/lib/lamby.rb index c3e706b..ac56373 100644 --- a/lib/lamby.rb +++ b/lib/lamby.rb @@ -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 \ No newline at end of file diff --git a/lib/lamby/version.rb b/lib/lamby/version.rb index 6680a26..b67f254 100644 --- a/lib/lamby/version.rb +++ b/lib/lamby/version.rb @@ -1,3 +1,3 @@ module Lamby - VERSION = '5.2.0' + VERSION = '5.2.1' end diff --git a/test/lamby_core_test.rb b/test/lamby_core_test.rb index 9ee4459..02dc125 100644 --- a/test/lamby_core_test.rb +++ b/test/lamby_core_test.rb @@ -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 \ No newline at end of file