From 8d2dae564222491a7365ff4f4f57f0dd8d63ae2c Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 4 Apr 2024 09:30:27 +1300 Subject: [PATCH 1/3] Insert `count` into default `container_options`. Fixes #233. --- lib/falcon/environment/application.rb | 6 ------ lib/falcon/environment/server.rb | 8 +++++++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/falcon/environment/application.rb b/lib/falcon/environment/application.rb index f07e73e7..1f21d219 100644 --- a/lib/falcon/environment/application.rb +++ b/lib/falcon/environment/application.rb @@ -49,12 +49,6 @@ def endpoint authority: authority ) end - - # Number of instances to start. - # @returns [Integer | nil] - def count - nil - end end end end diff --git a/lib/falcon/environment/server.rb b/lib/falcon/environment/server.rb index 71be8693..9e58c120 100644 --- a/lib/falcon/environment/server.rb +++ b/lib/falcon/environment/server.rb @@ -25,9 +25,15 @@ def authority self.name end + # Number of instances to start. + # @returns [Integer | nil] + def count + nil + end + # Options to use when creating the container. def container_options - {restart: true} + {restart: true, count: self.count} end # The host that this server will receive connections for. From 8717fdaa324cc58dc4bb4b4692f5b65b476a4151 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 4 Apr 2024 10:23:44 +1300 Subject: [PATCH 2/3] Add tests. --- lib/falcon/environment/rackup.rb | 2 +- lib/falcon/environment/server.rb | 4 +- test/falcon/service/.server/hello/config.ru | 6 ++ test/falcon/service/server.rb | 66 +++++++++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 test/falcon/service/.server/hello/config.ru create mode 100644 test/falcon/service/server.rb diff --git a/lib/falcon/environment/rackup.rb b/lib/falcon/environment/rackup.rb index 40491978..8336778b 100644 --- a/lib/falcon/environment/rackup.rb +++ b/lib/falcon/environment/rackup.rb @@ -11,7 +11,7 @@ module Environment # Provides an environment for hosting loading a Rackup `config.ru` file. module Rackup def rackup_path - 'config.ru' + File.expand_path('config.ru', root) end def rack_app diff --git a/lib/falcon/environment/server.rb b/lib/falcon/environment/server.rb index 9e58c120..ebdc9fbb 100644 --- a/lib/falcon/environment/server.rb +++ b/lib/falcon/environment/server.rb @@ -28,12 +28,12 @@ def authority # Number of instances to start. # @returns [Integer | nil] def count - nil + Etc.nprocessors end # Options to use when creating the container. def container_options - {restart: true, count: self.count} + {restart: true, count: self.count}.compact end # The host that this server will receive connections for. diff --git a/test/falcon/service/.server/hello/config.ru b/test/falcon/service/.server/hello/config.ru new file mode 100644 index 00000000..d158dc91 --- /dev/null +++ b/test/falcon/service/.server/hello/config.ru @@ -0,0 +1,6 @@ + +app = proc do |env| + [200, {}, ["Hello World"]] +end + +run app diff --git a/test/falcon/service/server.rb b/test/falcon/service/server.rb new file mode 100644 index 00000000..0841fabe --- /dev/null +++ b/test/falcon/service/server.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2024, by Samuel Williams. + +require 'falcon/service/server' +require 'falcon/configuration' +require 'falcon/environment/server' +require 'falcon/environment/rackup' + +describe Falcon::Service::Server do + let(:environment) do + Async::Service::Environment.new(Falcon::Environment::Server).with( + Falcon::Environment::Rackup, + name: 'hello', + root: File.expand_path('.server/hello', __dir__), + url: "http://localhost:0", + ) + end + + let(:server) do + subject.new(environment) + end + + it "can create a server" do + expect(server).to be_a subject + end + + it "can start and stop server" do + container = Async::Container.new + + server.start + server.setup(container) + container.wait_until_ready + + expect(container.group.running).to have_attributes(size: be > 0) + + server.stop + container.stop + end + + with 'a limited count' do + let(:environment) do + Async::Service::Environment.new(Falcon::Environment::Server).with( + Falcon::Environment::Rackup, + name: 'hello', + root: File.expand_path('.server/hello', __dir__), + url: "http://localhost:0", + count: 1, + ) + end + + it "can start and stop server" do + container = Async::Container.new + + server.start + server.setup(container) + container.wait_until_ready + + expect(container.group.running).to have_attributes(size: be == 1) + + server.stop + container.stop + end + end +end From bc920be077b935d125ab4c98e3054334109e8b86 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 4 Apr 2024 10:43:02 +1300 Subject: [PATCH 3/3] More precise tests. --- lib/falcon/environment/server.rb | 4 ++-- test/falcon/service/server.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/falcon/environment/server.rb b/lib/falcon/environment/server.rb index ebdc9fbb..5d45716f 100644 --- a/lib/falcon/environment/server.rb +++ b/lib/falcon/environment/server.rb @@ -25,10 +25,10 @@ def authority self.name end - # Number of instances to start. + # Number of instances to start. By default (when nil), uses `Etc.nprocessors`. # @returns [Integer | nil] def count - Etc.nprocessors + nil end # Options to use when creating the container. diff --git a/test/falcon/service/server.rb b/test/falcon/service/server.rb index 0841fabe..21b06afc 100644 --- a/test/falcon/service/server.rb +++ b/test/falcon/service/server.rb @@ -33,7 +33,7 @@ server.setup(container) container.wait_until_ready - expect(container.group.running).to have_attributes(size: be > 0) + expect(container.group.running).to have_attributes(size: be == Etc.nprocessors) server.stop container.stop