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
1d2287c0
Commit
1d2287c0
authored
Oct 28, 2020
by
Rémy Coutable
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reduce the duration of migrate_uploads_worker_spec.rb from 2m11s to 21s
Signed-off-by:
Rémy Coutable
<
remy@rymai.me
>
parent
4fbf5d4c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
131 deletions
+103
-131
spec/support/shared_examples/uploaders/workers/object_storage/migrate_uploads_shared_examples.rb
...workers/object_storage/migrate_uploads_shared_examples.rb
+0
-120
spec/uploaders/workers/object_storage/migrate_uploads_worker_spec.rb
...ers/workers/object_storage/migrate_uploads_worker_spec.rb
+103
-11
No files found.
spec/support/shared_examples/uploaders/workers/object_storage/migrate_uploads_shared_examples.rb
deleted
100644 → 0
View file @
4fbf5d4c
# frozen_string_literal: true
# Expects the calling spec to define:
# - model_class
# - mounted_as
# - to_store
RSpec
.
shared_examples
'uploads migration worker'
do
def
perform
(
uploads
,
store
=
nil
)
described_class
.
new
.
perform
(
uploads
.
ids
,
model_class
.
to_s
,
mounted_as
,
store
||
to_store
)
rescue
ObjectStorage
::
MigrateUploadsWorker
::
Report
::
MigrationFailures
# swallow
end
describe
'.enqueue!'
do
def
enqueue!
described_class
.
enqueue!
(
uploads
,
model_class
,
mounted_as
,
to_store
)
end
it
'is guarded by .sanity_check!'
do
expect
(
described_class
).
to
receive
(
:perform_async
)
expect
(
described_class
).
to
receive
(
:sanity_check!
)
enqueue!
end
context
'sanity_check! fails'
do
include_context
'sanity_check! fails'
it
'does not enqueue a job'
do
expect
(
described_class
).
not_to
receive
(
:perform_async
)
expect
{
enqueue!
}.
to
raise_error
(
described_class
::
SanityCheckError
)
end
end
end
describe
'.sanity_check!'
do
shared_examples
'raises a SanityCheckError'
do
|
expected_message
|
let
(
:mount_point
)
{
nil
}
it
do
expect
{
described_class
.
sanity_check!
(
uploads
,
model_class
,
mount_point
)
}
.
to
raise_error
(
described_class
::
SanityCheckError
).
with_message
(
expected_message
)
end
end
context
'uploader types mismatch'
do
let!
(
:outlier
)
{
create
(
:upload
,
uploader:
'GitlabUploader'
)
}
include_examples
'raises a SanityCheckError'
,
/Multiple uploaders found/
end
context
'mount point not found'
do
include_examples
'raises a SanityCheckError'
,
/Mount point [a-z:]+ not found in/
do
let
(
:mount_point
)
{
:potato
}
end
end
end
describe
'#perform'
do
shared_examples
'outputs correctly'
do
|
success:
0
,
failures:
0
|
total
=
success
+
failures
if
success
>
0
it
'outputs the reports'
do
expect
(
Gitlab
::
AppLogger
).
to
receive
(
:info
).
with
(
%r{Migrated
#{
success
}
/
#{
total
}
files}
)
perform
(
uploads
)
end
end
if
failures
>
0
it
'outputs upload failures'
do
expect
(
Gitlab
::
AppLogger
).
to
receive
(
:warn
).
with
(
/Error .* I am a teapot/
)
perform
(
uploads
)
end
end
end
it_behaves_like
'outputs correctly'
,
success:
10
it
'migrates files to remote storage'
do
perform
(
uploads
)
expect
(
Upload
.
where
(
store:
ObjectStorage
::
Store
::
LOCAL
).
count
).
to
eq
(
0
)
end
context
'reversed'
do
let
(
:to_store
)
{
ObjectStorage
::
Store
::
LOCAL
}
before
do
perform
(
uploads
,
ObjectStorage
::
Store
::
REMOTE
)
end
it
'migrates files to local storage'
do
expect
(
Upload
.
where
(
store:
ObjectStorage
::
Store
::
REMOTE
).
count
).
to
eq
(
10
)
perform
(
uploads
)
expect
(
Upload
.
where
(
store:
ObjectStorage
::
Store
::
LOCAL
).
count
).
to
eq
(
10
)
end
end
context
'migration is unsuccessful'
do
before
do
allow_any_instance_of
(
ObjectStorage
::
Concern
)
.
to
receive
(
:migrate!
).
and_raise
(
CarrierWave
::
UploadError
,
'I am a teapot.'
)
end
it_behaves_like
'outputs correctly'
,
failures:
10
end
end
end
RSpec
.
shared_context
'sanity_check! fails'
do
before
do
expect
(
described_class
).
to
receive
(
:sanity_check!
).
and_raise
(
described_class
::
SanityCheckError
)
end
end
spec/uploaders/workers/object_storage/migrate_uploads_worker_spec.rb
View file @
1d2287c0
...
@@ -13,8 +13,103 @@ RSpec.describe ObjectStorage::MigrateUploadsWorker do
...
@@ -13,8 +13,103 @@ RSpec.describe ObjectStorage::MigrateUploadsWorker do
# swallow
# swallow
end
end
# Expects the calling spec to define:
# - model_class
# - mounted_as
# - to_store
RSpec
.
shared_examples
'uploads migration worker'
do
describe
'.enqueue!'
do
def
enqueue!
described_class
.
enqueue!
(
uploads
,
model_class
,
mounted_as
,
to_store
)
end
it
'is guarded by .sanity_check!'
do
expect
(
described_class
).
to
receive
(
:perform_async
)
expect
(
described_class
).
to
receive
(
:sanity_check!
)
enqueue!
end
context
'sanity_check! fails'
do
before
do
expect
(
described_class
).
to
receive
(
:sanity_check!
).
and_raise
(
described_class
::
SanityCheckError
)
end
it
'does not enqueue a job'
do
expect
(
described_class
).
not_to
receive
(
:perform_async
)
expect
{
enqueue!
}.
to
raise_error
(
described_class
::
SanityCheckError
)
end
end
end
describe
'.sanity_check!'
do
shared_examples
'raises a SanityCheckError'
do
|
expected_message
|
let
(
:mount_point
)
{
nil
}
it
do
expect
{
described_class
.
sanity_check!
(
uploads
,
model_class
,
mount_point
)
}
.
to
raise_error
(
described_class
::
SanityCheckError
).
with_message
(
expected_message
)
end
end
context
'uploader types mismatch'
do
let!
(
:outlier
)
{
create
(
:upload
,
uploader:
'GitlabUploader'
)
}
include_examples
'raises a SanityCheckError'
,
/Multiple uploaders found/
end
context
'mount point not found'
do
include_examples
'raises a SanityCheckError'
,
/Mount point [a-z:]+ not found in/
do
let
(
:mount_point
)
{
:potato
}
end
end
end
describe
'#perform'
do
it
'migrates files to remote storage'
do
expect
(
Gitlab
::
AppLogger
).
to
receive
(
:info
).
with
(
%r{Migrated 1/1 files}
)
perform
(
uploads
)
expect
(
Upload
.
where
(
store:
ObjectStorage
::
Store
::
LOCAL
).
count
).
to
eq
(
0
)
end
context
'reversed'
do
let
(
:to_store
)
{
ObjectStorage
::
Store
::
LOCAL
}
before
do
perform
(
uploads
,
ObjectStorage
::
Store
::
REMOTE
)
end
it
'migrates files to local storage'
do
expect
(
Upload
.
where
(
store:
ObjectStorage
::
Store
::
REMOTE
).
count
).
to
eq
(
1
)
perform
(
uploads
)
expect
(
Upload
.
where
(
store:
ObjectStorage
::
Store
::
LOCAL
).
count
).
to
eq
(
1
)
end
end
context
'migration is unsuccessful'
do
before
do
allow_any_instance_of
(
ObjectStorage
::
Concern
)
.
to
receive
(
:migrate!
).
and_raise
(
CarrierWave
::
UploadError
,
'I am a teapot.'
)
end
it
'does not migrate files to remote storage'
do
expect
(
Gitlab
::
AppLogger
).
to
receive
(
:warn
).
with
(
/Error .* I am a teapot/
)
perform
(
uploads
)
expect
(
Upload
.
where
(
store:
ObjectStorage
::
Store
::
LOCAL
).
count
).
to
eq
(
1
)
end
end
end
end
context
"for AvatarUploader"
do
context
"for AvatarUploader"
do
let!
(
:project
s
)
{
create_list
(
:project
,
10
,
:with_avatar
)
}
let!
(
:project
_with_avatar
)
{
create
(
:project
,
:with_avatar
)
}
let
(
:mounted_as
)
{
:avatar
}
let
(
:mounted_as
)
{
:avatar
}
before
do
before
do
...
@@ -27,16 +122,15 @@ RSpec.describe ObjectStorage::MigrateUploadsWorker do
...
@@ -27,16 +122,15 @@ RSpec.describe ObjectStorage::MigrateUploadsWorker do
it
"to N*5"
do
it
"to N*5"
do
query_count
=
ActiveRecord
::
QueryRecorder
.
new
{
perform
(
uploads
)
}
query_count
=
ActiveRecord
::
QueryRecorder
.
new
{
perform
(
uploads
)
}
more_projects
=
create_list
(
:project
,
3
,
:with_avatar
)
create
(
:project
,
:with_avatar
)
expected_queries_per_migration
=
5
*
more_projects
.
count
expect
{
perform
(
Upload
.
all
)
}.
not_to
exceed_query_limit
(
query_count
).
with_threshold
(
5
)
expect
{
perform
(
Upload
.
all
)
}.
not_to
exceed_query_limit
(
query_count
).
with_threshold
(
expected_queries_per_migration
)
end
end
end
end
end
end
context
"for FileUploader"
do
context
"for FileUploader"
do
let!
(
:project
s
)
{
create_list
(
:project
,
10
)
}
let!
(
:project
_with_file
)
{
create
(
:project
)
}
let
(
:secret
)
{
SecureRandom
.
hex
}
let
(
:secret
)
{
SecureRandom
.
hex
}
let
(
:mounted_as
)
{
nil
}
let
(
:mounted_as
)
{
nil
}
...
@@ -48,7 +142,7 @@ RSpec.describe ObjectStorage::MigrateUploadsWorker do
...
@@ -48,7 +142,7 @@ RSpec.describe ObjectStorage::MigrateUploadsWorker do
before
do
before
do
stub_uploads_object_storage
(
FileUploader
)
stub_uploads_object_storage
(
FileUploader
)
projects
.
map
(
&
method
(
:upload_file
)
)
upload_file
(
project_with_file
)
end
end
it_behaves_like
"uploads migration worker"
it_behaves_like
"uploads migration worker"
...
@@ -57,18 +151,16 @@ RSpec.describe ObjectStorage::MigrateUploadsWorker do
...
@@ -57,18 +151,16 @@ RSpec.describe ObjectStorage::MigrateUploadsWorker do
it
"to N*5"
do
it
"to N*5"
do
query_count
=
ActiveRecord
::
QueryRecorder
.
new
{
perform
(
uploads
)
}
query_count
=
ActiveRecord
::
QueryRecorder
.
new
{
perform
(
uploads
)
}
more_projects
=
create_list
(
:project
,
3
)
upload_file
(
create
(
:project
))
more_projects
.
map
(
&
method
(
:upload_file
))
expected_queries_per_migration
=
5
*
more_projects
.
count
expect
{
perform
(
Upload
.
all
)
}.
not_to
exceed_query_limit
(
query_count
).
with_threshold
(
5
)
expect
{
perform
(
Upload
.
all
)
}.
not_to
exceed_query_limit
(
query_count
).
with_threshold
(
expected_queries_per_migration
)
end
end
end
end
end
end
context
'for DesignManagement::DesignV432x230Uploader'
do
context
'for DesignManagement::DesignV432x230Uploader'
do
let
(
:model_class
)
{
DesignManagement
::
Action
}
let
(
:model_class
)
{
DesignManagement
::
Action
}
let!
(
:design_action
s
)
{
create_list
(
:design_action
,
10
,
:with_image_v432x230
)
}
let!
(
:design_action
)
{
create
(
:design_action
,
:with_image_v432x230
)
}
let
(
:mounted_as
)
{
:image_v432x230
}
let
(
:mounted_as
)
{
:image_v432x230
}
before
do
before
do
...
...
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