spec/examples/valve_spec.rb in standard-procedure-plumbing-0.3.2 vs spec/examples/valve_spec.rb in standard-procedure-plumbing-0.3.3

- old
+ new

@@ -1,8 +1,35 @@ require "spec_helper" -RSpec.describe "Valve examples" do +RSpec.shared_examples "an example valve" do |runs_in_background| + it "queries an object" do + @person = Employee.start "Alice" + + expect(@person.name).to eq "Alice" + expect(@person.job_title).to eq "Sales assistant" + + @time = Time.now + # `greet_slowly` is a query so will block until a response is received + expect(@person.greet_slowly).to eq "H E L L O" + expect(Time.now - @time).to be > 0.1 + + @time = Time.now + # we're ignoring the result so this will not block (except :inline mode which does not run in the background) + expect(@person.greet_slowly(ignore_result: true)).to be_nil + expect(Time.now - @time).to be < 0.1 if runs_in_background + expect(Time.now - @time).to be > 0.1 if !runs_in_background + end + + it "commands an object" do + @person = Employee.start "Alice" + @person.promote + @job_title = @person.job_title + expect(@job_title).to eq "Sales manager" + end +end + +RSpec.describe "Valve example: " do # standard:disable Lint/ConstantDefinitionInBlock class Employee include Plumbing::Valve query :name, :job_title, :greet_slowly command :promote @@ -24,65 +51,31 @@ "H E L L O" end end # standard:enable Lint/ConstantDefinitionInBlock - context "inline" do - it "queries an object" do - Plumbing.configure mode: :inline do - @person = Employee.start "Alice" - - expect(@person.name).to eq "Alice" - expect(@person.job_title).to eq "Sales assistant" - - @time = Time.now - expect(@person.greet_slowly).to eq "H E L L O" - expect(Time.now - @time).to be > 0.1 - - @time = Time.now - expect(@person.greet_slowly(ignore_result: true)).to be_nil - expect(Time.now - @time).to be > 0.1 - end + context "inline mode" do + around :example do |example| + Plumbing.configure mode: :inline, &example end - it "commands an object" do - Plumbing.configure mode: :inline do - @person = Employee.start "Alice" - - @person.promote - - expect(@person.job_title).to eq "Sales manager" - end - end + it_behaves_like "an example valve", false end - context "async" do + context "async mode" do around :example do |example| Plumbing.configure mode: :async do Kernel::Async(&example) end end - it "queries an object" do - @person = Employee.start "Alice" + it_behaves_like "an example valve", true + end - expect(@person.name).to eq "Alice" - expect(@person.job_title).to eq "Sales assistant" - - @time = Time.now - expect(@person.greet_slowly).to eq "H E L L O" - expect(Time.now - @time).to be > 0.1 - - @time = Time.now - expect(@person.greet_slowly(ignore_result: true)).to be_nil - expect(Time.now - @time).to be < 0.1 + context "threaded mode" do + around :example do |example| + Plumbing.configure mode: :threaded, &example end - it "commands an object" do - @person = Employee.start "Alice" - - @person.promote - - expect(@person.job_title).to eq "Sales manager" - end + it_behaves_like "an example valve", true end end