Skip to content

Commit

Permalink
More reliable tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 26, 2024
1 parent 070de42 commit 446183c
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
11 changes: 3 additions & 8 deletions lib/async/container/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,9 @@

module Async
module Container
module Atomic
def atomic(&block)
::Thread.handle_interrupt(Exception => :never, &block)
end
end

# Manages the life-cycle of one or more containers in order to support a persistent system.
# e.g. a web server, job server or some other long running system.
class Controller
include Atomic

SIGHUP = Signal.list["HUP"]
SIGINT = Signal.list["INT"]
SIGTERM = Signal.list["TERM"]
Expand Down Expand Up @@ -194,14 +186,17 @@ def run
# I thought this was the default... but it doesn't always raise an exception unless you do this explicitly.
# We use `Thread.current.raise(...)` so that exceptions are filtered through `Thread.handle_interrupt` correctly.
interrupt_action = Signal.trap(:INT) do
# $stderr.puts "Received INT signal, terminating...", caller
::Thread.current.raise(Interrupt)
end

terminate_action = Signal.trap(:TERM) do
# $stderr.puts "Received TERM signal, terminating...", caller
::Thread.current.raise(Terminate)
end

hangup_action = Signal.trap(:HUP) do
# $stderr.puts "Received HUP signal, restarting...", caller
::Thread.current.raise(Hangup)
end

Expand Down
4 changes: 4 additions & 0 deletions lib/async/container/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def initialize
@queue = nil
end

def inspect
"#<#{self.class} running=#{@running.size}>"
end

# @attribute [Hash(IO, Fiber)] the running tasks, indexed by IO.
attr :running

Expand Down
2 changes: 1 addition & 1 deletion lib/async/container/notify/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Notify
# Implements a general process readiness protocol with output to the local console.
class Console < Client
# Open a notification client attached to the current console.
def self.open!(logger = ::Console.logger)
def self.open!(logger = ::Console)
self.new(logger)
end

Expand Down
4 changes: 2 additions & 2 deletions test/async/container/.bad.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

require_relative '../../../lib/async/container/controller'

$stdout.sync = true

class Bad < Async::Container::Controller
def setup(container)
container.run(name: "bad", count: 1, restart: true) do |instance|
# Deliberately missing call to `instance.ready!`:
# instance.ready!

$stdout.puts "Ready..."
$stdout.flush

sleep
ensure
$stdout.puts "Exiting..."
$stdout.flush
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions test/async/container/.dots.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def setup(container)
container.run(name: "dots", count: 1, restart: true) do |instance|
instance.ready!

# This is to avoid race conditions in the controller in test conditions.
sleep 0.1

$stdout.write "."

sleep
Expand Down
4 changes: 4 additions & 0 deletions test/async/container/.graceful.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class Graceful < Async::Container::Controller
def setup(container)
container.run(name: "graceful", count: 1, restart: true) do |instance|
instance.ready!

# This is to avoid race conditions in the controller in test conditions.
sleep 0.1

clock = Async::Clock.start

original_action = Signal.trap(:INT) do
Expand Down

0 comments on commit 446183c

Please sign in to comment.