spec/lib/gush/worker_spec.rb in gush-0.0.1 vs spec/lib/gush/worker_spec.rb in gush-0.1

- old
+ new

@@ -1,96 +1,67 @@ require 'spec_helper' describe Gush::Worker do - let(:workflow_id) { '1234' } - let(:workflow) { TestWorkflow.new(workflow_id) } + subject { described_class.new } + + let!(:workflow) { TestWorkflow.create } let(:job) { workflow.find_job("Prepare") } - let(:config) { client.configuration.to_json } + let(:config) { Gush.configuration.to_json } + let!(:client) { double("client") } before :each do - allow(client).to receive(:find_workflow).with(workflow_id).and_return(workflow) - allow(Gush::Client).to receive(:new).and_return(client) + allow(subject).to receive(:client).and_return(client) + allow(subject).to receive(:enqueue_outgoing_jobs) + + allow(client).to receive(:find_workflow).with(workflow.id).and_return(workflow) + expect(client).to receive(:persist_job).at_least(1).times + expect(client).to receive(:worker_report).with(hash_including(status: :started)).ordered end describe "#perform" do context "when job fails" do - before :each do - expect(job).to receive(:work).and_raise(StandardError) - job.enqueue! - job.start! - end - it "should mark it as failed" do - allow(client).to receive(:persist_job) - Gush::Worker.new.perform(workflow_id, "Prepare", config) + allow(job).to receive(:work).and_raise(StandardError) + expect(client).to receive(:worker_report).with(hash_including(status: :failed)).ordered - expect(client).to have_received(:persist_job).with(workflow_id, job).at_least(1).times do |_, job| - expect(job).to be_failed - end - + subject.perform(workflow.id, "Prepare", config) + expect(workflow.find_job("Prepare")).to be_failed end it "reports that job failed" do - allow(client).to receive(:worker_report) - Gush::Worker.new.perform(workflow_id, "Prepare", config) - expect(client).to have_received(:worker_report).with(hash_including(status: :failed)) - end + allow(job).to receive(:work).and_raise(StandardError) + expect(client).to receive(:worker_report).with(hash_including(status: :failed)).ordered - it "logs the exception" do - logger = TestLogger.new(1234, 'Prepare') - expect(logger).to receive(:<<).with(instance_of(String)).at_least(1).times - expect(workflow).to receive(:build_logger_for_job).and_return(logger) - - Gush::Worker.new.perform(workflow_id, "Prepare", config) + subject.perform(workflow.id, "Prepare", config) end end context "when job completes successfully" do it "should mark it as succedeed" do - allow(client).to receive(:persist_job) + expect(subject).to receive(:mark_as_finished) + expect(client).to receive(:worker_report).with(hash_including(status: :finished)).ordered - Gush::Worker.new.perform(workflow_id, "Prepare", config) - - expect(client).to have_received(:persist_job).at_least(1).times.with(workflow_id, job) do |_, job| - expect(job).to be_succeeded - end + subject.perform(workflow.id, "Prepare", config) end it "reports that job succedeed" do - allow(client).to receive(:worker_report) - Gush::Worker.new.perform(workflow_id, "Prepare", config) + expect(client).to receive(:worker_report).with(hash_including(status: :finished)).ordered - expect(client).to have_received(:worker_report).with(hash_including(status: :finished)) + subject.perform(workflow.id, "Prepare", config) end end - [:before_work, :work, :after_work].each do |method| - it "calls job.#{method} hook" do - expect(job).to receive(method) - Gush::Worker.new.perform(workflow_id, "Prepare", config) - end - end + it "calls job.work method" do + expect(job).to receive(:work) + expect(client).to receive(:worker_report).with(hash_including(status: :finished)).ordered - it "sets up a logger for the job" do - Gush::Worker.new.perform(workflow_id, "Prepare", config) - job.start! - expect(job.logger).to be_a TestLogger + subject.perform(workflow.id, "Prepare", config) end - it "sets a job id" do - job_id = 1234 - worker = Gush::Worker.new - - allow(worker).to receive(:jid).and_return(job_id) - - worker.perform(workflow_id, "Prepare", config) - job.enqueue! - expect(job.jid).to eq job_id - end - it "reports when the job is started" do allow(client).to receive(:worker_report) - Gush::Worker.new.perform(workflow_id, "Prepare", config) - expect(client).to have_received(:worker_report).with(hash_including(status: :started)) + expect(client).to receive(:worker_report).with(hash_including(status: :finished)).ordered + + subject.perform(workflow.id, "Prepare", config) end end end