Skip to content

Commit 9e82c58

Browse files
Add test for Controller#start idempotency. (#69)
1 parent 818bfd3 commit 9e82c58

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

lib/async/container/controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,18 @@ def setup(container)
120120
end
121121

122122
# Start the container unless it's already running.
123+
# @returns [Boolean] Whether a new container was started.
123124
def start
124125
unless @container
125126
Console.info(self, "Controller starting...")
126127
self.restart
128+
129+
Console.info(self, "Controller started.")
130+
131+
return true
127132
end
128133

129-
Console.info(self, "Controller started.")
134+
return false
130135
end
131136

132137
# Stop the container if it's running.

test/async/container/controller.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,21 @@ def controller.setup(container)
139139
expect(controller.container).to be_nil
140140
end
141141

142+
it "doesn't restart a container if it's already running" do
143+
expect(controller).to receive(:setup)
144+
145+
expect(controller.start).to be == true
146+
expect(controller).to be(:running?)
147+
148+
container = controller.container
149+
150+
# Starting again is idempotent: it returns false and the container is not replaced.
151+
expect(controller.start).to be == false
152+
expect(controller.container).to be_equal(container)
153+
ensure
154+
controller.stop
155+
end
156+
142157
it "can spawn a reactor" do
143158
def controller.setup(container)
144159
container.async do |task|

0 commit comments

Comments
 (0)