Added specs for started_at and finished_at

This commit is contained in:
Kamil Trzcinski 2016-08-12 11:53:27 +02:00
parent e1f05b932d
commit ad3e1edcfc
2 changed files with 46 additions and 15 deletions

View file

@ -22,10 +22,11 @@ module Ci
state_machine :status, initial: :created do
event :queue do
transition :created => :pending
transition any - [:created, :pending] => :running
end
event :run do
transition [:pending, :success, :failed, :canceled, :skipped] => :running
transition any => :running
end
event :skip do
@ -44,15 +45,15 @@ module Ci
transition any => :canceled
end
after_transition [:created, :pending] => :running do |pipeline|
pipeline.update(started_at: Time.now)
before_transition [:created, :pending] => :running do |pipeline|
pipeline.started_at = Time.now
end
after_transition any => [:success, :failed, :canceled] do |pipeline|
pipeline.update(finished_at: Time.now)
before_transition any => [:success, :failed, :canceled] do |pipeline|
pipeline.finished_at = Time.now
end
after_transition do |pipeline|
before_transition do |pipeline|
pipeline.update_duration
end
end
@ -245,7 +246,7 @@ module Ci
end
def update_duration
update(duration: statuses.latest.duration)
self.duration = statuses.latest.duration
end
private

View file

@ -120,18 +120,48 @@ describe Ci::Pipeline, models: true do
end
end
describe '#duration' do
describe 'state machine' do
let(:current) { Time.now.change(usec: 0) }
let!(:build) { create :ci_build, name: 'build1', pipeline: pipeline, started_at: current - 60, finished_at: current }
let!(:build2) { create :ci_build, name: 'build2', pipeline: pipeline, started_at: current - 60, finished_at: current }
let(:build) { create :ci_build, name: 'build1', pipeline: pipeline, started_at: current - 60, finished_at: current }
let(:build2) { create :ci_build, name: 'build2', pipeline: pipeline, started_at: current - 60, finished_at: current }
before do
build.skip
build2.skip
describe '#duration' do
before do
build.skip
build2.skip
end
it 'matches sum of builds duration' do
expect(pipeline.reload.duration).to eq(build.duration + build2.duration)
end
end
it 'matches sum of builds duration' do
expect(pipeline.reload.duration).to eq(build.duration + build2.duration)
describe '#started_at' do
it 'updates on transitioning to running' do
build.run
expect(pipeline.reload.started_at).not_to be_nil
end
it 'do not update on transitioning to success' do
build.success
expect(pipeline.reload.started_at).to be_nil
end
end
describe '#finished_at' do
it 'updates on transitioning to success' do
build.success
expect(pipeline.reload.finished_at).not_to be_nil
end
it 'do not update on transitioning to running' do
build.run
expect(pipeline.reload.finished_at).to be_nil
end
end
end