Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
6a092c05
Commit
6a092c05
authored
Dec 05, 2021
by
Kev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix RSpec/TimecopTravel offenses (Part 2/2)
Changelog: changed
parent
35f1dd93
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
33 additions
and
50 deletions
+33
-50
.rubocop_todo/rspec/timecop_travel.yml
.rubocop_todo/rspec/timecop_travel.yml
+1
-11
spec/features/users/terms_spec.rb
spec/features/users/terms_spec.rb
+1
-1
spec/lib/feature_spec.rb
spec/lib/feature_spec.rb
+2
-2
spec/lib/gitlab/analytics/cycle_analytics/median_spec.rb
spec/lib/gitlab/analytics/cycle_analytics/median_spec.rb
+2
-2
spec/models/broadcast_message_spec.rb
spec/models/broadcast_message_spec.rb
+3
-3
spec/models/concerns/issuable_spec.rb
spec/models/concerns/issuable_spec.rb
+2
-2
spec/requests/api/ci/runner/jobs_trace_spec.rb
spec/requests/api/ci/runner/jobs_trace_spec.rb
+4
-4
spec/requests/api/issues/put_projects_issues_spec.rb
spec/requests/api/issues/put_projects_issues_spec.rb
+8
-8
spec/support/shared_contexts/cache_allowed_users_in_namespace_shared_context.rb
...ntexts/cache_allowed_users_in_namespace_shared_context.rb
+2
-2
spec/support/shared_examples/requests/api/time_tracking_shared_examples.rb
...ed_examples/requests/api/time_tracking_shared_examples.rb
+4
-4
spec/support/shared_examples/workers/concerns/reenqueuer_shared_examples.rb
...d_examples/workers/concerns/reenqueuer_shared_examples.rb
+4
-11
No files found.
.rubocop_todo/rspec/timecop_travel.yml
View file @
6a092c05
...
...
@@ -7,15 +7,5 @@ RSpec/TimecopTravel:
-
ee/spec/lib/gitlab/geo/log_cursor/daemon_spec.rb
-
ee/spec/models/broadcast_message_spec.rb
-
ee/spec/models/burndown_spec.rb
-
qa/spec/support/repeater_spec.rb
-
spec/features/users/terms_spec.rb
-
spec/lib/feature_spec.rb
-
spec/models/broadcast_message_spec.rb
-
spec/models/concerns/issuable_spec.rb
-
spec/requests/api/ci/runner/jobs_trace_spec.rb
-
spec/requests/api/issues/put_projects_issues_spec.rb
-
spec/support/shared_contexts/cache_allowed_users_in_namespace_shared_context.rb
-
spec/support/shared_examples/requests/api/time_tracking_shared_examples.rb
-
spec/support/shared_examples/workers/concerns/reenqueuer_shared_examples.rb
-
spec/workers/concerns/reenqueuer_spec.rb
-
spec/lib/gitlab/analytics/cycle_analytics/median
_spec.rb
-
qa/spec/support/repeater
_spec.rb
spec/features/users/terms_spec.rb
View file @
6a092c05
...
...
@@ -99,7 +99,7 @@ RSpec.describe 'Users > Terms', :js do
enforce_terms
# Application settings are cached for a minute
Timecop
.
travel
2
.
minutes
do
travel_to
2
.
minutes
.
from_now
do
within
(
'.nav-sidebar'
)
do
click_link
'Issues'
end
...
...
spec/lib/feature_spec.rb
View file @
6a092c05
...
...
@@ -257,7 +257,7 @@ RSpec.describe Feature, stub_feature_flags: false do
end
it
'caches the status in L2 cache after 2 minutes'
do
Timecop
.
travel
2
.
minutes
do
travel_to
2
.
minutes
.
from_now
do
expect
do
expect
(
described_class
.
send
(
:l1_cache_backend
)).
to
receive
(
:fetch
).
once
.
and_call_original
expect
(
described_class
.
send
(
:l2_cache_backend
)).
to
receive
(
:fetch
).
once
.
and_call_original
...
...
@@ -267,7 +267,7 @@ RSpec.describe Feature, stub_feature_flags: false do
end
it
'fetches the status after an hour'
do
Timecop
.
travel
61
.
minutes
do
travel_to
61
.
minutes
.
from_now
do
expect
do
expect
(
described_class
.
send
(
:l1_cache_backend
)).
to
receive
(
:fetch
).
once
.
and_call_original
expect
(
described_class
.
send
(
:l2_cache_backend
)).
to
receive
(
:fetch
).
once
.
and_call_original
...
...
spec/lib/gitlab/analytics/cycle_analytics/median_spec.rb
View file @
6a092c05
...
...
@@ -30,11 +30,11 @@ RSpec.describe Gitlab::Analytics::CycleAnalytics::Median do
merge_request1
=
create
(
:merge_request
,
source_branch:
'1'
,
target_project:
project
,
source_project:
project
)
merge_request2
=
create
(
:merge_request
,
source_branch:
'2'
,
target_project:
project
,
source_project:
project
)
Timecop
.
travel
(
5
.
minutes
.
from_now
)
do
travel_to
(
5
.
minutes
.
from_now
)
do
merge_request1
.
metrics
.
update!
(
merged_at:
Time
.
zone
.
now
)
end
Timecop
.
travel
(
10
.
minutes
.
from_now
)
do
travel_to
(
10
.
minutes
.
from_now
)
do
merge_request2
.
metrics
.
update!
(
merged_at:
Time
.
zone
.
now
)
end
...
...
spec/models/broadcast_message_spec.rb
View file @
6a092c05
...
...
@@ -62,7 +62,7 @@ RSpec.describe BroadcastMessage do
subject
.
call
Timecop
.
travel
(
3
.
weeks
)
do
travel_to
(
3
.
weeks
.
from_now
)
do
subject
.
call
end
end
...
...
@@ -73,7 +73,7 @@ RSpec.describe BroadcastMessage do
expect
(
subject
.
call
).
to
match_array
([
message
])
expect
(
described_class
.
cache
).
to
receive
(
:expire
).
and_call_original
Timecop
.
travel
(
1
.
week
)
do
travel_to
(
1
.
week
.
from_now
)
do
2
.
times
{
expect
(
subject
.
call
).
to
be_empty
}
end
end
...
...
@@ -96,7 +96,7 @@ RSpec.describe BroadcastMessage do
expect
(
subject
.
call
.
length
).
to
eq
(
1
)
Timecop
.
travel
(
future
.
starts_at
)
do
travel_to
(
future
.
starts_at
+
1
.
second
)
do
expect
(
subject
.
call
.
length
).
to
eq
(
2
)
end
end
...
...
spec/models/concerns/issuable_spec.rb
View file @
6a092c05
...
...
@@ -798,7 +798,7 @@ RSpec.describe Issuable do
it
'updates issues updated_at'
do
issue
Timecop
.
travel
(
1
.
minute
.
from_now
)
do
travel_to
(
2
.
minutes
.
from_now
)
do
expect
{
spend_time
(
1800
)
}.
to
change
{
issue
.
updated_at
}
end
end
...
...
@@ -823,7 +823,7 @@ RSpec.describe Issuable do
context
'when time to subtract exceeds the total time spent'
do
it
'raise a validation error'
do
Timecop
.
travel
(
1
.
minute
.
from_now
)
do
travel_to
(
1
.
minute
.
from_now
)
do
expect
do
expect
do
spend_time
(
-
3600
)
...
...
spec/requests/api/ci/runner/jobs_trace_spec.rb
View file @
6a092c05
...
...
@@ -35,7 +35,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_trace_chunks do
let
(
:headers
)
{
{
API
::
Ci
::
Helpers
::
Runner
::
JOB_TOKEN_HEADER
=>
job
.
token
,
'Content-Type'
=>
'text/plain'
}
}
let
(
:headers_with_range
)
{
headers
.
merge
({
'Content-Range'
=>
'11-20'
})
}
let
(
:update_interval
)
{
10
.
seconds
.
to_i
}
let
(
:update_interval
)
{
10
.
seconds
}
before
do
initial_patch_the_trace
...
...
@@ -81,7 +81,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_trace_chunks do
end
context
'when job was not updated recently'
do
let
(
:update_interval
)
{
1
5
.
minutes
.
to_i
}
let
(
:update_interval
)
{
1
6
.
minutes
}
it
{
expect
{
patch_the_trace
}.
to
change
{
job
.
updated_at
}
}
...
...
@@ -293,10 +293,10 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_trace_chunks do
end
end
Timecop
.
travel
(
job
.
updated_at
+
update_interval
)
do
travel_to
(
job
.
updated_at
+
update_interval
)
do
patch
api
(
"/jobs/
#{
job_id
}
/trace"
),
params:
content
,
headers:
request_headers
job
.
reload
end
job
.
reload
end
def
initial_patch_the_trace
...
...
spec/requests/api/issues/put_projects_issues_spec.rb
View file @
6a092c05
...
...
@@ -323,44 +323,44 @@ RSpec.describe API::Issues do
end
it
'removes all labels and touches the record'
do
Timecop
.
travel
(
1
.
minute
.
from_now
)
do
travel_to
(
2
.
minutes
.
from_now
)
do
put
api_for_user
,
params:
{
labels:
''
}
end
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
[
'labels'
]).
to
eq
([])
expect
(
json_response
[
'updated_at'
]).
to
be
>
Time
.
now
expect
(
json_response
[
'updated_at'
]).
to
be
>
Time
.
current
end
it
'removes all labels and touches the record with labels param as array'
do
Timecop
.
travel
(
1
.
minute
.
from_now
)
do
travel_to
(
2
.
minutes
.
from_now
)
do
put
api_for_user
,
params:
{
labels:
[
''
]
}
end
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
[
'labels'
]).
to
eq
([])
expect
(
json_response
[
'updated_at'
]).
to
be
>
Time
.
now
expect
(
json_response
[
'updated_at'
]).
to
be
>
Time
.
current
end
it
'updates labels and touches the record'
do
Timecop
.
travel
(
1
.
minute
.
from_now
)
do
travel_to
(
2
.
minutes
.
from_now
)
do
put
api_for_user
,
params:
{
labels:
'foo,bar'
}
end
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
[
'labels'
]).
to
contain_exactly
(
'foo'
,
'bar'
)
expect
(
json_response
[
'updated_at'
]).
to
be
>
Time
.
now
expect
(
json_response
[
'updated_at'
]).
to
be
>
Time
.
current
end
it
'updates labels and touches the record with labels param as array'
do
Timecop
.
travel
(
1
.
minute
.
from_now
)
do
travel_to
(
2
.
minutes
.
from_now
)
do
put
api_for_user
,
params:
{
labels:
%w(foo bar)
}
end
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
[
'labels'
]).
to
include
'foo'
expect
(
json_response
[
'labels'
]).
to
include
'bar'
expect
(
json_response
[
'updated_at'
]).
to
be
>
Time
.
now
expect
(
json_response
[
'updated_at'
]).
to
be
>
Time
.
current
end
it
'allows special label names'
do
...
...
spec/support/shared_contexts/cache_allowed_users_in_namespace_shared_context.rb
View file @
6a092c05
...
...
@@ -10,7 +10,7 @@ RSpec.shared_examples 'allowed user IDs are cached' do
end
it
'caches the allowed user IDs in L1 cache for 1 minute'
,
:use_clean_rails_memory_store_caching
do
Timecop
.
travel
2
.
minutes
do
travel_to
2
.
minutes
.
from_now
do
expect
do
expect
(
described_class
.
l1_cache_backend
).
to
receive
(
:fetch
).
and_call_original
expect
(
described_class
.
l2_cache_backend
).
to
receive
(
:fetch
).
and_call_original
...
...
@@ -20,7 +20,7 @@ RSpec.shared_examples 'allowed user IDs are cached' do
end
it
'caches the allowed user IDs in L2 cache for 5 minutes'
,
:use_clean_rails_memory_store_caching
do
Timecop
.
travel
6
.
minutes
do
travel_to
6
.
minutes
.
from_now
do
expect
do
expect
(
described_class
.
l1_cache_backend
).
to
receive
(
:fetch
).
and_call_original
expect
(
described_class
.
l2_cache_backend
).
to
receive
(
:fetch
).
and_call_original
...
...
spec/support/shared_examples/requests/api/time_tracking_shared_examples.rb
View file @
6a092c05
...
...
@@ -86,7 +86,7 @@ RSpec.shared_examples 'time tracking endpoints' do |issuable_name|
end
it
"add spent time for
#{
issuable_name
}
"
do
Timecop
.
travel
(
1
.
minute
.
from_now
)
do
travel_to
(
2
.
minutes
.
from_now
)
do
expect
do
post
api
(
"/projects/
#{
project
.
id
}
/
#{
issuable_collection_name
}
/
#{
issuable
.
iid
}
/add_spent_time"
,
user
),
params:
{
duration:
'2h'
}
end
.
to
change
{
issuable
.
reload
.
updated_at
}
...
...
@@ -98,7 +98,7 @@ RSpec.shared_examples 'time tracking endpoints' do |issuable_name|
context
'when subtracting time'
do
it
'subtracts time of the total spent time'
do
Timecop
.
travel
(
1
.
minute
.
from_now
)
do
travel_to
(
2
.
minutes
.
from_now
)
do
expect
do
issuable
.
update!
(
spend_time:
{
duration:
7200
,
user_id:
user
.
id
})
end
.
to
change
{
issuable
.
reload
.
updated_at
}
...
...
@@ -115,7 +115,7 @@ RSpec.shared_examples 'time tracking endpoints' do |issuable_name|
it
'does not modify the total time spent'
do
issuable
.
update!
(
spend_time:
{
duration:
7200
,
user_id:
user
.
id
})
Timecop
.
travel
(
1
.
minute
.
from_now
)
do
travel_to
(
2
.
minutes
.
from_now
)
do
expect
do
post
api
(
"/projects/
#{
project
.
id
}
/
#{
issuable_collection_name
}
/
#{
issuable
.
iid
}
/add_spent_time"
,
user
),
params:
{
duration:
'-1w'
}
end
.
not_to
change
{
issuable
.
reload
.
updated_at
}
...
...
@@ -160,7 +160,7 @@ RSpec.shared_examples 'time tracking endpoints' do |issuable_name|
end
it
"resets spent time for
#{
issuable_name
}
"
do
Timecop
.
travel
(
1
.
minute
.
from_now
)
do
travel_to
(
2
.
minutes
.
from_now
)
do
expect
do
post
api
(
"/projects/
#{
project
.
id
}
/
#{
issuable_collection_name
}
/
#{
issuable
.
iid
}
/reset_spent_time"
,
user
)
end
.
to
change
{
issuable
.
reload
.
updated_at
}
...
...
spec/support/shared_examples/workers/concerns/reenqueuer_shared_examples.rb
View file @
6a092c05
...
...
@@ -30,18 +30,11 @@ end
# `job_args` to be arguments to #perform if it takes arguments
RSpec
.
shared_examples
'#perform is rate limited to 1 call per'
do
|
minimum_duration
|
before
do
# Allow Timecop freeze and travel without the block form
Timecop
.
safe_mode
=
false
Timecop
.
freeze
freeze_time
time_travel_during_perform
(
actual_duration
)
end
after
do
Timecop
.
return
Timecop
.
safe_mode
=
true
end
let
(
:subject_perform
)
{
defined?
(
job_args
)
?
subject
.
perform
(
job_args
)
:
subject
.
perform
}
context
'when the work finishes in 0 seconds'
do
...
...
@@ -58,7 +51,7 @@ RSpec.shared_examples '#perform is rate limited to 1 call per' do |minimum_durat
let
(
:actual_duration
)
{
0.1
*
minimum_duration
}
it
'sleeps 90% of minimum duration'
do
expect
(
subject
).
to
receive
(
:sleep
).
with
(
a_value_within
(
0.0
1
).
of
(
0.9
*
minimum_duration
))
expect
(
subject
).
to
receive
(
:sleep
).
with
(
a_value_within
(
1
).
of
(
0.9
*
minimum_duration
))
subject_perform
end
...
...
@@ -68,7 +61,7 @@ RSpec.shared_examples '#perform is rate limited to 1 call per' do |minimum_durat
let
(
:actual_duration
)
{
0.9
*
minimum_duration
}
it
'sleeps 10% of minimum duration'
do
expect
(
subject
).
to
receive
(
:sleep
).
with
(
a_value_within
(
0.0
1
).
of
(
0.1
*
minimum_duration
))
expect
(
subject
).
to
receive
(
:sleep
).
with
(
a_value_within
(
1
).
of
(
0.1
*
minimum_duration
))
subject_perform
end
...
...
@@ -111,7 +104,7 @@ RSpec.shared_examples '#perform is rate limited to 1 call per' do |minimum_durat
allow
(
subject
).
to
receive
(
:ensure_minimum_duration
)
do
|
minimum_duration
,
&
block
|
original_ensure_minimum_duration
.
call
(
minimum_duration
)
do
# Time travel inside the block inside ensure_minimum_duration
Timecop
.
travel
(
actual_duration
)
if
actual_duration
&&
actual_duration
>
0
travel_to
(
actual_duration
.
from_now
)
if
actual_duration
&&
actual_duration
>
0
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment