From efda95bfea294fffd913821675268e8f33c22f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=BCnnewig?= Date: Mon, 9 May 2016 15:01:57 +0200 Subject: [PATCH 01/11] Prevent duplicate output for commands. Closes #374 If a users decided to use `run_simple`, a command is stopped twice: 1. After the configured wait time 2. On the end of test suite via terminate_all_commands This makes the output appear twice. This is fixed by this PR. It also make a method call fail: 1. Command already stopped, `#stop` is called again 2. Comannd has not been started, but ist `#stop`ped --- lib/aruba/command.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/aruba/command.rb b/lib/aruba/command.rb index 8073b95f2..e3b681b7d 100644 --- a/lib/aruba/command.rb +++ b/lib/aruba/command.rb @@ -48,6 +48,8 @@ def initialize(command, opts = {}) # Stop command def stop(*) + fail Aruba::UserError, 'Please start a command first, before stopping it' if __getobj__.stopped? + __getobj__.stop event_bus.notify Events::CommandStopped.new(self) @@ -56,6 +58,8 @@ def stop(*) # Terminate command def terminate(*) + return if __getobj__.stopped? + __getobj__.terminate event_bus.notify Events::CommandStopped.new(self) @@ -64,6 +68,8 @@ def terminate(*) # Start command def start + fail Aruba::UserError, 'Please stop a command first, before starting it' if __getobj__.started? + __getobj__.start event_bus.notify Events::CommandStarted.new(self) From 5d6d8dd3b65cff56cba31fe1119d3f975bc13598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=BCnnewig?= Date: Fri, 17 Jun 2016 22:31:14 +0200 Subject: [PATCH 02/11] Add spec for command --- spec/aruba/command_spec.rb | 66 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 spec/aruba/command_spec.rb diff --git a/spec/aruba/command_spec.rb b/spec/aruba/command_spec.rb new file mode 100644 index 000000000..402f44e3a --- /dev/null +++ b/spec/aruba/command_spec.rb @@ -0,0 +1,66 @@ +require 'spec_helper' + +RSpec.describe Aruba::Command do + subject do + described_class.new( + 'true', + event_bus: event_bus, + exit_timeout: exit_timeout, + io_wait_timeout: io_wait_timeout, + working_directory: working_directory, + environment: environment, + main_class: main_class, + stop_signal: stop_signal, + startup_wait_time: startup_wait_time + ) + end + + let(:event_bus) { instance_double('Aruba::EventBus') } + let(:exit_timeout) { 1 } + let(:io_wait_timeout) { 1 } + let(:working_directory) { expand_path('.') } + let(:environment) { ENV.to_hash } + let(:main_class) { nil } + let(:stop_signal) { nil } + let(:startup_wait_time) { 1 } + + context '#started' do + before :each do + allow(event_bus).to receive(:notify) { |a| a.is_a?(Events::CommandStarted) } + end + + before :each do + subject.start + end + + it { is_expected.to be_started } + end + + context '#stopped' do + before :each do + allow(event_bus).to receive(:notify) { |a| a.is_a?(Events::CommandStarted) } + allow(event_bus).to receive(:notify) { |a| a.is_a?(Events::CommandStopped) } + end + + before :each do + subject.start + subject.stop + end + + it { is_expected.to be_stopped } + end + + context '#terminate' do + before :each do + allow(event_bus).to receive(:notify) { |a| a.is_a?(Events::CommandStarted) } + allow(event_bus).to receive(:notify) { |a| a.is_a?(Events::CommandStopped) } + end + + before :each do + subject.start + subject.terminate + end + + it { is_expected.to be_stopped } + end +end From 78267ee56bf4f1819472bcb9a7f4d88560e892c5 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Wed, 22 Nov 2017 13:33:35 +0100 Subject: [PATCH 03/11] Allow start and stop even when not needed --- lib/aruba/command.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/aruba/command.rb b/lib/aruba/command.rb index e3b681b7d..5448d0db9 100644 --- a/lib/aruba/command.rb +++ b/lib/aruba/command.rb @@ -48,8 +48,6 @@ def initialize(command, opts = {}) # Stop command def stop(*) - fail Aruba::UserError, 'Please start a command first, before stopping it' if __getobj__.stopped? - __getobj__.stop event_bus.notify Events::CommandStopped.new(self) @@ -68,8 +66,6 @@ def terminate(*) # Start command def start - fail Aruba::UserError, 'Please stop a command first, before starting it' if __getobj__.started? - __getobj__.start event_bus.notify Events::CommandStarted.new(self) From 5772103c5235937134b112a2aca07f0ed533b0e7 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Tue, 21 Nov 2017 09:52:40 +0100 Subject: [PATCH 04/11] Make announcements on failure work again --- lib/aruba/api/commands.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/aruba/api/commands.rb b/lib/aruba/api/commands.rb index 6a7754650..2d6e98d83 100644 --- a/lib/aruba/api/commands.rb +++ b/lib/aruba/api/commands.rb @@ -209,6 +209,7 @@ def run_command_and_stop(cmd, opts = {}) expect(command).to be_successfully_executed rescue ::RSpec::Expectations::ExpectationNotMetError => e aruba.announcer.activate(aruba.config.activate_announcer_on_command_failure) + aruba.event_bus.notify Events::CommandStopped.new(command) raise e end end From acd846a315c90d76855eabe680f3731652d1b2f6 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Wed, 22 Nov 2017 14:18:40 +0100 Subject: [PATCH 05/11] Do not announce stop if already stopped --- lib/aruba/command.rb | 2 ++ spec/aruba/command_spec.rb | 54 ++++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/lib/aruba/command.rb b/lib/aruba/command.rb index 5448d0db9..f754ac853 100644 --- a/lib/aruba/command.rb +++ b/lib/aruba/command.rb @@ -48,6 +48,8 @@ def initialize(command, opts = {}) # Stop command def stop(*) + return if __getobj__.stopped? + __getobj__.stop event_bus.notify Events::CommandStopped.new(self) diff --git a/spec/aruba/command_spec.rb b/spec/aruba/command_spec.rb index 402f44e3a..38aebe8d1 100644 --- a/spec/aruba/command_spec.rb +++ b/spec/aruba/command_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' RSpec.describe Aruba::Command do - subject do + let(:command) do described_class.new( 'true', event_bus: event_bus, @@ -26,41 +26,67 @@ context '#started' do before :each do - allow(event_bus).to receive(:notify) { |a| a.is_a?(Events::CommandStarted) } + allow(event_bus).to receive(:notify).with(Events::CommandStarted) end before :each do - subject.start + command.start end - it { is_expected.to be_started } + it 'leaves the command in the started state' do + expect(command).to be_started + end end context '#stopped' do before :each do - allow(event_bus).to receive(:notify) { |a| a.is_a?(Events::CommandStarted) } - allow(event_bus).to receive(:notify) { |a| a.is_a?(Events::CommandStopped) } + allow(event_bus).to receive(:notify).with(Events::CommandStarted) + allow(event_bus).to receive(:notify).with(Events::CommandStopped) end before :each do - subject.start - subject.stop + command.start + command.stop + end + + it 'leaves the command in the stopped state' do + expect(command).to be_stopped end - it { is_expected.to be_stopped } + it 'notifies the event bus only once per run' do + command.stop + expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once + end + + it 'prevents #terminate from notifying the event bus' do + command.terminate + expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once + end end context '#terminate' do before :each do - allow(event_bus).to receive(:notify) { |a| a.is_a?(Events::CommandStarted) } - allow(event_bus).to receive(:notify) { |a| a.is_a?(Events::CommandStopped) } + allow(event_bus).to receive(:notify).with(Events::CommandStarted) + allow(event_bus).to receive(:notify).with(Events::CommandStopped) end before :each do - subject.start - subject.terminate + command.start + command.terminate end - it { is_expected.to be_stopped } + it 'leaves the command in the stopped state' do + expect(command).to be_stopped + end + + it 'notifies the event bus only once per run' do + command.terminate + expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once + end + + it 'prevents #stop from notifying the event bus' do + command.stop + expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once + end end end From 60fe2b08e06c46866e81c015c86dfeadc6a54eef Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Wed, 22 Nov 2017 15:36:12 +0100 Subject: [PATCH 06/11] Mark command started in DebugProcess#start Without this, the command is never stopped and the stop notification is never broadcast. --- lib/aruba/processes/debug_process.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/aruba/processes/debug_process.rb b/lib/aruba/processes/debug_process.rb index f283444ac..c546fc089 100644 --- a/lib/aruba/processes/debug_process.rb +++ b/lib/aruba/processes/debug_process.rb @@ -18,6 +18,7 @@ def self.match?(mode) end def start + @started = true Dir.chdir @working_directory do Aruba.platform.with_environment(environment) do @exit_status = system(command, *arguments) ? 0 : 1 From 83cdedce5ac9ff009f63e7e6304113a20245e7be Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Wed, 22 Nov 2017 20:41:15 +0100 Subject: [PATCH 07/11] Improve specs - Merge before blocks and remove :each - Move calls that are part of the specs' Act steps into the it blocks - Replace context blocks with describe and name the correct method names --- spec/aruba/command_spec.rb | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/spec/aruba/command_spec.rb b/spec/aruba/command_spec.rb index 38aebe8d1..47b199135 100644 --- a/spec/aruba/command_spec.rb +++ b/spec/aruba/command_spec.rb @@ -24,67 +24,62 @@ let(:stop_signal) { nil } let(:startup_wait_time) { 1 } - context '#started' do - before :each do + describe '#start' do + before do allow(event_bus).to receive(:notify).with(Events::CommandStarted) end - before :each do - command.start - end - it 'leaves the command in the started state' do + command.start expect(command).to be_started end end - context '#stopped' do - before :each do + describe '#stop' do + before do allow(event_bus).to receive(:notify).with(Events::CommandStarted) allow(event_bus).to receive(:notify).with(Events::CommandStopped) - end - - before :each do command.start - command.stop end it 'leaves the command in the stopped state' do + command.stop expect(command).to be_stopped end it 'notifies the event bus only once per run' do + command.stop command.stop expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once end it 'prevents #terminate from notifying the event bus' do + command.stop command.terminate expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once end end - context '#terminate' do - before :each do + describe '#terminate' do + before do allow(event_bus).to receive(:notify).with(Events::CommandStarted) allow(event_bus).to receive(:notify).with(Events::CommandStopped) - end - - before :each do command.start - command.terminate end it 'leaves the command in the stopped state' do + command.terminate expect(command).to be_stopped end it 'notifies the event bus only once per run' do + command.terminate command.terminate expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once end it 'prevents #stop from notifying the event bus' do + command.terminate command.stop expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once end From a5e36835032de295042fbe1e9c5575d7779bbc29 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Fri, 2 Feb 2018 11:57:54 +0100 Subject: [PATCH 08/11] Restore some of the original spec style --- spec/aruba/command_spec.rb | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/spec/aruba/command_spec.rb b/spec/aruba/command_spec.rb index 47b199135..79022bdef 100644 --- a/spec/aruba/command_spec.rb +++ b/spec/aruba/command_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' RSpec.describe Aruba::Command do - let(:command) do + subject(:command) do described_class.new( 'true', event_bus: event_bus, @@ -27,10 +27,10 @@ describe '#start' do before do allow(event_bus).to receive(:notify).with(Events::CommandStarted) + command.start end it 'leaves the command in the started state' do - command.start expect(command).to be_started end end @@ -40,21 +40,17 @@ allow(event_bus).to receive(:notify).with(Events::CommandStarted) allow(event_bus).to receive(:notify).with(Events::CommandStopped) command.start - end - - it 'leaves the command in the stopped state' do command.stop - expect(command).to be_stopped end + it { is_expected.to be_stopped } + it 'notifies the event bus only once per run' do - command.stop command.stop expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once end it 'prevents #terminate from notifying the event bus' do - command.stop command.terminate expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once end @@ -65,21 +61,17 @@ allow(event_bus).to receive(:notify).with(Events::CommandStarted) allow(event_bus).to receive(:notify).with(Events::CommandStopped) command.start - end - - it 'leaves the command in the stopped state' do command.terminate - expect(command).to be_stopped end + it { is_expected.to be_stopped } + it 'notifies the event bus only once per run' do - command.terminate command.terminate expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once end it 'prevents #stop from notifying the event bus' do - command.terminate command.stop expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once end From cbc08297b1162c7f436ae7629ea8c292cef11c5f Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sat, 3 Feb 2018 20:23:13 +0100 Subject: [PATCH 09/11] Bring InProcess class under test --- spec/aruba/processes/in_process_spec.rb | 91 +++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 spec/aruba/processes/in_process_spec.rb diff --git a/spec/aruba/processes/in_process_spec.rb b/spec/aruba/processes/in_process_spec.rb new file mode 100644 index 000000000..edaa6b253 --- /dev/null +++ b/spec/aruba/processes/in_process_spec.rb @@ -0,0 +1,91 @@ +require 'spec_helper' + +RSpec.describe Aruba::Processes::InProcess do + class Runner + def initialize(argv, stdin, stdout, stderr, kernel) + @stdout = stdout + @stderr = stderr + end + + def execute! + end + end + + class StdoutRunner < Runner + def execute! + @stdout.puts 'yo' + end + end + + class StderrRunner < Runner + def execute! + @stderr.puts 'yo' + end + end + + class FailedRunner < Runner + def execute! + raise 'Oops' + end + end + + subject(:process) do + described_class.new(command, exit_timeout, io_wait, working_directory, + environment, main_class) + end + + let(:command) { 'foo' } + let(:exit_timeout) { 1 } + let(:io_wait) { 1 } + let(:working_directory) { Dir.getwd } + let(:environment) { ENV.to_hash.dup } + let(:main_class) { Runner } + + describe "#stdout" do + let(:main_class) { StdoutRunner } + before(:each) { process.start } + before(:each) { process.stop } + + context 'when invoked once' do + it { expect(process.stdout).to eq "yo\n" } + end + + context 'when invoked twice' do + it { 2.times { expect(process.stdout).to eq "yo\n" } } + end + end + + describe "#stderr" do + let(:main_class) { StderrRunner } + before(:each) { process.start } + before(:each) { process.stop } + + context 'when invoked once' do + it { expect(process.stderr).to eq "yo\n" } + end + + context 'when invoked twice' do + it { 2.times { expect(process.stderr).to eq "yo\n" } } + end + end + + describe "#stop" do + before(:each) { process.start } + + context 'when stopped successfully' do + it { process.stop } + end + end + + describe "#start" do + context "when process run succeeds" do + it { expect { process.start }.not_to raise_error } + end + + context "when process run fails" do + let(:main_class) { FailedRunner } + + it { expect { process.start }.to raise_error RuntimeError, 'Oops' } + end + end +end From af37ecdb5920d354e4347926aac75c3bddaac40a Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sat, 3 Feb 2018 20:35:18 +0100 Subject: [PATCH 10/11] Ensure #stopped? and #started? work for InProcess --- lib/aruba/processes/in_process.rb | 2 ++ spec/aruba/processes/in_process_spec.rb | 12 +++++++++++- spec/aruba/processes/spawn_process_spec.rb | 12 +++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/aruba/processes/in_process.rb b/lib/aruba/processes/in_process.rb index 6ec70fff4..bcfb13a6c 100644 --- a/lib/aruba/processes/in_process.rb +++ b/lib/aruba/processes/in_process.rb @@ -55,6 +55,8 @@ def initialize(cmd, exit_timeout, io_wait_timeout, working_directory, environmen def start fail "You need to call aruba.config.main_class = YourMainClass" unless main_class + @started = true + Dir.chdir @working_directory do before_run diff --git a/spec/aruba/processes/in_process_spec.rb b/spec/aruba/processes/in_process_spec.rb index edaa6b253..49d1b4174 100644 --- a/spec/aruba/processes/in_process_spec.rb +++ b/spec/aruba/processes/in_process_spec.rb @@ -73,13 +73,23 @@ def execute! before(:each) { process.start } context 'when stopped successfully' do - it { process.stop } + it { expect { process.stop }.not_to raise_error } + + it 'makes the process stopped' do + process.stop + expect(process).to be_stopped + end end end describe "#start" do context "when process run succeeds" do it { expect { process.start }.not_to raise_error } + + it 'makes the process started' do + process.start + expect(process).to be_started + end end context "when process run fails" do diff --git a/spec/aruba/processes/spawn_process_spec.rb b/spec/aruba/processes/spawn_process_spec.rb index 67ac713b9..664d82ac1 100644 --- a/spec/aruba/processes/spawn_process_spec.rb +++ b/spec/aruba/processes/spawn_process_spec.rb @@ -42,13 +42,23 @@ before(:each) { process.start } context 'when stopped successfully' do - it { process.stop } + it { expect { process.stop }.not_to raise_error } + + it 'makes the process stopped' do + process.stop + expect(process).to be_stopped + end end end describe "#start" do context "when process run succeeds" do it { expect { process.start }.not_to raise_error } + + it 'makes the process started' do + process.start + expect(process).to be_started + end end context "when process run fails" do From 76b8c4724a308e62451c5b573624ab7d9ca5c49d Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sun, 4 Feb 2018 17:55:28 +0100 Subject: [PATCH 11/11] Improve before blocks --- spec/aruba/processes/in_process_spec.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/spec/aruba/processes/in_process_spec.rb b/spec/aruba/processes/in_process_spec.rb index 49d1b4174..66aaf0781 100644 --- a/spec/aruba/processes/in_process_spec.rb +++ b/spec/aruba/processes/in_process_spec.rb @@ -43,8 +43,11 @@ def execute! describe "#stdout" do let(:main_class) { StdoutRunner } - before(:each) { process.start } - before(:each) { process.stop } + + before do + process.start + process.stop + end context 'when invoked once' do it { expect(process.stdout).to eq "yo\n" } @@ -57,8 +60,11 @@ def execute! describe "#stderr" do let(:main_class) { StderrRunner } - before(:each) { process.start } - before(:each) { process.stop } + + before do + process.start + process.stop + end context 'when invoked once' do it { expect(process.stderr).to eq "yo\n" } @@ -70,7 +76,7 @@ def execute! end describe "#stop" do - before(:each) { process.start } + before { process.start } context 'when stopped successfully' do it { expect { process.stop }.not_to raise_error }