Skip to content

Commit

Permalink
Respect container count configuration. (#234)
Browse files Browse the repository at this point in the history
* Insert `count` into default `container_options`. Fixes #233.
  • Loading branch information
ioquatix committed Apr 3, 2024
1 parent e17d919 commit 896baed
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
6 changes: 0 additions & 6 deletions lib/falcon/environment/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ def endpoint
authority: authority
)
end

# Number of instances to start.
# @returns [Integer | nil]
def count
nil
end
end
end
end
2 changes: 1 addition & 1 deletion lib/falcon/environment/rackup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/falcon/environment/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ def authority
self.name
end

# Number of instances to start. By default (when nil), uses `Etc.nprocessors`.
# @returns [Integer | nil]
def count
nil
end

# Options to use when creating the container.
def container_options
{restart: true}
{restart: true, count: self.count}.compact
end

# The host that this server will receive connections for.
Expand Down
6 changes: 6 additions & 0 deletions test/falcon/service/.server/hello/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

app = proc do |env|
[200, {}, ["Hello World"]]
end

run app
66 changes: 66 additions & 0 deletions test/falcon/service/server.rb
Original file line number Diff line number Diff line change
@@ -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 == Etc.nprocessors)

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

0 comments on commit 896baed

Please sign in to comment.