Commit f3e8b888 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Use travel_to instead of dependency injection, feedback:

https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/5734#note_14056642
parent ec4549e4
...@@ -48,11 +48,11 @@ module Ci ...@@ -48,11 +48,11 @@ module Ci
end end
before_transition [:created, :pending] => :running do |pipeline| before_transition [:created, :pending] => :running do |pipeline|
pipeline.started_at = Gitlab::Utils.now pipeline.started_at = Time.now
end end
before_transition any => [:success, :failed, :canceled] do |pipeline| before_transition any => [:success, :failed, :canceled] do |pipeline|
pipeline.finished_at = Gitlab::Utils.now pipeline.finished_at = Time.now
end end
before_transition do |pipeline| before_transition do |pipeline|
......
...@@ -87,7 +87,7 @@ module Statuseable ...@@ -87,7 +87,7 @@ module Statuseable
if started_at && finished_at if started_at && finished_at
finished_at - started_at finished_at - started_at
elsif started_at elsif started_at
Gitlab::Utils.now - started_at Time.now - started_at
end end
end end
end end
...@@ -13,10 +13,5 @@ module Gitlab ...@@ -13,10 +13,5 @@ module Gitlab
def force_utf8(str) def force_utf8(str)
str.force_encoding(Encoding::UTF_8) str.force_encoding(Encoding::UTF_8)
end end
# The same as Time.now but using this would make it easier to test
def now
Time.now
end
end end
end end
...@@ -128,12 +128,14 @@ describe Ci::Pipeline, models: true do ...@@ -128,12 +128,14 @@ describe Ci::Pipeline, models: true do
describe '#duration' do describe '#duration' do
before do before do
allow(Gitlab::Utils).to receive(:now). travel_to(current - 120) do
and_return(current - 120, current)
pipeline.run pipeline.run
end
travel_to(current) do
pipeline.succeed pipeline.succeed
end end
end
it 'matches sum of builds duration' do it 'matches sum of builds duration' do
expect(pipeline.reload.duration).to eq(120) expect(pipeline.reload.duration).to eq(120)
......
...@@ -33,6 +33,7 @@ RSpec.configure do |config| ...@@ -33,6 +33,7 @@ RSpec.configure do |config|
config.include EmailHelpers config.include EmailHelpers
config.include TestEnv config.include TestEnv
config.include ActiveJob::TestHelper config.include ActiveJob::TestHelper
config.include ActiveSupport::Testing::TimeHelpers
config.include StubGitlabCalls config.include StubGitlabCalls
config.include StubGitlabData config.include StubGitlabData
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment