Commit 03cdb94d authored by Mario Celi's avatar Mario Celi

Use rspec-sidekiq gem for better expectations

- Add Cop to enforce have_enqueued_sidekiq_job
- Exclude existing ocurrences of receive(:perform_async)
- Update docs
parent 3276cb9a
......@@ -1354,6 +1354,284 @@ RSpec/AnyInstanceOf:
- 'spec/workers/wait_for_cluster_creation_worker_spec.rb'
- 'ee/spec/workers/security/auto_fix_worker_spec.rb'
# WIP: https://gitlab.com/gitlab-org/gitlab/-/issues/331835
RSpec/HaveEnqueuedSidekiqJob:
Exclude:
- 'ee/spec/lib/gitlab/geo/log_cursor/events/job_artifact_deleted_event_spec.rb'
- 'ee/spec/lib/gitlab/geo/log_cursor/events/repository_deleted_event_spec.rb'
- 'ee/spec/lib/gitlab/geo/log_cursor/events/repository_renamed_event_spec.rb'
- 'ee/spec/lib/gitlab/geo/log_cursor/events/repository_updated_event_spec.rb'
- 'ee/spec/models/ci/pipeline_spec.rb'
- 'ee/spec/models/concerns/elastic/project_wiki_spec.rb'
- 'ee/spec/models/concerns/elastic/projects_search_spec.rb'
- 'ee/spec/models/ee/alert_management/alert_spec.rb'
- 'ee/spec/models/ee/user_spec.rb'
- 'ee/spec/models/elasticsearch_indexed_namespace_spec.rb'
- 'ee/spec/models/elasticsearch_indexed_project_spec.rb'
- 'ee/spec/models/gitlab/seat_link_data_spec.rb'
- 'ee/spec/models/group_wiki_spec.rb'
- 'ee/spec/models/namespace_statistics_spec.rb'
- 'ee/spec/models/project_feature_spec.rb'
- 'ee/spec/models/project_import_state_spec.rb'
- 'ee/spec/models/project_spec.rb'
- 'ee/spec/requests/api/elasticsearch_indexed_namespaces_spec.rb'
- 'ee/spec/requests/api/graphql/mutations/requirements_management/export_requirements_spec.rb'
- 'ee/spec/requests/api/project_mirror_spec.rb'
- 'ee/spec/requests/api/projects_spec.rb'
- 'ee/spec/services/admin/email_service_spec.rb'
- 'ee/spec/services/application_settings/update_service_spec.rb'
- 'ee/spec/services/auto_merge/merge_train_service_spec.rb'
- 'ee/spec/services/ci_cd/github_setup_service_spec.rb'
- 'ee/spec/services/dast_site_validations/create_service_spec.rb'
- 'ee/spec/services/ee/git/branch_push_service_spec.rb'
- 'ee/spec/services/ee/merge_requests/handle_assignees_change_service_spec.rb'
- 'ee/spec/services/ee/merge_requests/update_service_spec.rb'
- 'ee/spec/services/elastic/process_initial_bookkeeping_service_spec.rb'
- 'ee/spec/services/epics/update_dates_service_spec.rb'
- 'ee/spec/services/geo/container_repository_sync_service_spec.rb'
- 'ee/spec/services/geo/design_repository_sync_service_spec.rb'
- 'ee/spec/services/geo/framework_repository_sync_service_spec.rb'
- 'ee/spec/services/geo/hashed_storage_attachments_migration_service_spec.rb'
- 'ee/spec/services/geo/hashed_storage_migration_service_spec.rb'
- 'ee/spec/services/geo/project_housekeeping_service_spec.rb'
- 'ee/spec/services/geo/rename_repository_service_spec.rb'
- 'ee/spec/services/geo/repository_destroy_service_spec.rb'
- 'ee/spec/services/groups/transfer_service_spec.rb'
- 'ee/spec/services/milestones/destroy_service_spec.rb'
- 'ee/spec/services/projects/transfer_service_spec.rb'
- 'ee/spec/services/security/store_report_service_spec.rb'
- 'ee/spec/services/status_page/trigger_publish_service_spec.rb'
- 'ee/spec/services/web_hook_service_spec.rb'
- 'ee/spec/support/shared_examples/models/concerns/verifiable_replicator_shared_examples.rb'
- 'ee/spec/support/shared_examples/quick_actions/issue/status_page_quick_actions_shared_examples.rb'
- 'ee/spec/support/shared_examples/services/base_sync_service_shared_examples.rb'
- 'ee/spec/support/shared_examples/status_page/trigger_publish_shared_examples.rb'
- 'ee/spec/workers/build_finished_worker_spec.rb'
- 'ee/spec/workers/elastic_remove_expired_namespace_subscriptions_from_index_cron_worker_spec.rb'
- 'ee/spec/workers/geo/container_repository_sync_dispatch_worker_spec.rb'
- 'ee/spec/workers/geo/design_repository_shard_sync_worker_spec.rb'
- 'ee/spec/workers/geo/file_download_dispatch_worker_spec.rb'
- 'ee/spec/workers/geo/registry_sync_worker_spec.rb'
- 'ee/spec/workers/geo/repositories_clean_up_worker_spec.rb'
- 'ee/spec/workers/geo/repository_shard_sync_worker_spec.rb'
- 'ee/spec/workers/geo/repository_verification/primary/batch_worker_spec.rb'
- 'ee/spec/workers/geo/repository_verification/primary/shard_worker_spec.rb'
- 'ee/spec/workers/geo/repository_verification/secondary/scheduler_worker_spec.rb'
- 'ee/spec/workers/geo/repository_verification/secondary/shard_worker_spec.rb'
- 'ee/spec/workers/geo/secondary/registry_consistency_worker_spec.rb'
- 'ee/spec/workers/incident_management/oncall_rotations/persist_all_rotations_shifts_job_spec.rb'
- 'ee/spec/workers/post_receive_spec.rb'
- 'ee/spec/workers/store_security_reports_worker_spec.rb'
- 'ee/spec/workers/sync_seat_link_worker_spec.rb'
- 'spec/controllers/admin/clusters_controller_spec.rb'
- 'spec/controllers/admin/services_controller_spec.rb'
- 'spec/controllers/chaos_controller_spec.rb'
- 'spec/controllers/groups/clusters_controller_spec.rb'
- 'spec/controllers/groups_controller_spec.rb'
- 'spec/controllers/projects/clusters_controller_spec.rb'
- 'spec/controllers/projects/issues_controller_spec.rb'
- 'spec/controllers/projects/merge_requests_controller_spec.rb'
- 'spec/controllers/projects/pipeline_schedules_controller_spec.rb'
- 'spec/controllers/projects/registry/repositories_controller_spec.rb'
- 'spec/controllers/projects/settings/ci_cd_controller_spec.rb'
- 'spec/controllers/repositories/git_http_controller_spec.rb'
- 'spec/features/admin/admin_uses_repository_checks_spec.rb'
- 'spec/features/clusters/installing_applications_shared_examples.rb'
- 'spec/features/issues/csv_spec.rb'
- 'spec/graphql/mutations/container_repositories/destroy_spec.rb'
- 'spec/graphql/mutations/merge_requests/accept_spec.rb'
- 'spec/lib/gitlab/ci/pipeline/chain/pipeline/process_spec.rb'
- 'spec/lib/gitlab/cleanup/orphan_lfs_file_references_spec.rb'
- 'spec/lib/gitlab/database/migrations/background_migration_helpers_spec.rb'
- 'spec/lib/gitlab/github_import/importer/diff_notes_importer_spec.rb'
- 'spec/lib/gitlab/github_import/importer/issues_importer_spec.rb'
- 'spec/lib/gitlab/github_import/importer/lfs_objects_importer_spec.rb'
- 'spec/lib/gitlab/github_import/importer/notes_importer_spec.rb'
- 'spec/lib/gitlab/github_import/importer/pull_requests_importer_spec.rb'
- 'spec/lib/gitlab/jira_import/issues_importer_spec.rb'
- 'spec/lib/gitlab/pages_transfer_spec.rb'
- 'spec/models/abuse_report_spec.rb'
- 'spec/models/ci/build_spec.rb'
- 'spec/models/ci/build_trace_chunk_spec.rb'
- 'spec/models/ci/job_artifact_spec.rb'
- 'spec/models/ci/pipeline_spec.rb'
- 'spec/models/ci/processable_spec.rb'
- 'spec/models/clusters/integrations/prometheus_spec.rb'
- 'spec/models/commit_status_spec.rb'
- 'spec/models/concerns/reactive_caching_spec.rb'
- 'spec/models/deployment_spec.rb'
- 'spec/models/diff_note_spec.rb'
- 'spec/models/integrations/emails_on_push_spec.rb'
- 'spec/models/issue_spec.rb'
- 'spec/models/jira_import_state_spec.rb'
- 'spec/models/key_spec.rb'
- 'spec/models/lfs_object_spec.rb'
- 'spec/models/lfs_objects_project_spec.rb'
- 'spec/models/merge_request_spec.rb'
- 'spec/models/namespace/aggregation_schedule_spec.rb'
- 'spec/models/namespace_spec.rb'
- 'spec/models/operations/feature_flag_spec.rb'
- 'spec/models/packages/package_spec.rb'
- 'spec/models/pages_domain_spec.rb'
- 'spec/models/pool_repository_spec.rb'
- 'spec/models/project_import_state_spec.rb'
- 'spec/models/project_services/prometheus_service_spec.rb'
- 'spec/models/project_spec.rb'
- 'spec/models/project_statistics_spec.rb'
- 'spec/models/project_wiki_spec.rb'
- 'spec/models/remote_mirror_spec.rb'
- 'spec/models/sentry_issue_spec.rb'
- 'spec/models/snippet_statistics_spec.rb'
- 'spec/models/user_spec.rb'
- 'spec/models/x509_certificate_spec.rb'
- 'spec/requests/api/dependency_proxy_spec.rb'
- 'spec/requests/api/graphql/mutations/container_repository/destroy_spec.rb'
- 'spec/requests/api/group_import_spec.rb'
- 'spec/requests/api/merge_requests_spec.rb'
- 'spec/requests/api/project_container_repositories_spec.rb'
- 'spec/requests/api/project_packages_spec.rb'
- 'spec/requests/lfs_http_spec.rb'
- 'spec/services/admin/propagate_integration_service_spec.rb'
- 'spec/services/admin/propagate_service_template_spec.rb'
- 'spec/services/application_settings/update_service_spec.rb'
- 'spec/services/auto_merge/merge_when_pipeline_succeeds_service_spec.rb'
- 'spec/services/branches/delete_merged_service_spec.rb'
- 'spec/services/branches/delete_service_spec.rb'
- 'spec/services/bulk_import_service_spec.rb'
- 'spec/services/bulk_imports/export_service_spec.rb'
- 'spec/services/ci/create_pipeline_service_spec.rb'
- 'spec/services/ci/list_config_variables_service_spec.rb'
- 'spec/services/ci/pipeline_schedule_service_spec.rb'
- 'spec/services/ci/play_bridge_service_spec.rb'
- 'spec/services/ci/test_failure_history_service_spec.rb'
- 'spec/services/ci/update_build_state_service_spec.rb'
- 'spec/services/clusters/applications/create_service_spec.rb'
- 'spec/services/clusters/applications/schedule_update_service_spec.rb'
- 'spec/services/clusters/applications/update_service_spec.rb'
- 'spec/services/clusters/cleanup/app_service_spec.rb'
- 'spec/services/clusters/cleanup/project_namespace_service_spec.rb'
- 'spec/services/clusters/create_service_spec.rb'
- 'spec/services/container_expiration_policy_service_spec.rb'
- 'spec/services/deployments/create_service_spec.rb'
- 'spec/services/design_management/delete_designs_service_spec.rb'
- 'spec/services/discussions/resolve_service_spec.rb'
- 'spec/services/feature_flags/create_service_spec.rb'
- 'spec/services/feature_flags/update_service_spec.rb'
- 'spec/services/git/branch_hooks_service_spec.rb'
- 'spec/services/git/branch_push_service_spec.rb'
- 'spec/services/git/tag_push_service_spec.rb'
- 'spec/services/grafana/proxy_service_spec.rb'
- 'spec/services/groups/import_export/import_service_spec.rb'
- 'spec/services/groups/transfer_service_spec.rb'
- 'spec/services/groups/update_service_spec.rb'
- 'spec/services/incident_management/pager_duty/process_webhook_service_spec.rb'
- 'spec/services/issuable/bulk_update_service_spec.rb'
- 'spec/services/issues/create_service_spec.rb'
- 'spec/services/issues/update_service_spec.rb'
- 'spec/services/members/destroy_service_spec.rb'
- 'spec/services/merge_requests/handle_assignees_change_service_spec.rb'
- 'spec/services/merge_requests/merge_service_spec.rb'
- 'spec/services/merge_requests/mergeability_check_service_spec.rb'
- 'spec/services/merge_requests/resolve_todos_service_spec.rb'
- 'spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb'
- 'spec/services/notes/create_service_spec.rb'
- 'spec/services/notification_service_spec.rb'
- 'spec/services/onboarding_progress_service_spec.rb'
- 'spec/services/packages/composer/create_package_service_spec.rb'
- 'spec/services/packages/debian/process_changes_service_spec.rb'
- 'spec/services/packages/go/sync_packages_service_spec.rb'
- 'spec/services/pages/delete_service_spec.rb'
- 'spec/services/post_receive_service_spec.rb'
- 'spec/services/projects/after_rename_service_spec.rb'
- 'spec/services/projects/create_service_spec.rb'
- 'spec/services/projects/group_links/create_service_spec.rb'
- 'spec/services/projects/group_links/destroy_service_spec.rb'
- 'spec/services/projects/repository_languages_service_spec.rb'
- 'spec/services/projects/transfer_service_spec.rb'
- 'spec/services/projects/update_service_spec.rb'
- 'spec/services/prometheus/proxy_service_spec.rb'
- 'spec/services/resource_access_tokens/revoke_service_spec.rb'
- 'spec/services/snippets/destroy_service_spec.rb'
- 'spec/services/snippets/update_statistics_service_spec.rb'
- 'spec/services/tags/destroy_service_spec.rb'
- 'spec/services/todos/destroy/entity_leave_service_spec.rb'
- 'spec/services/web_hook_service_spec.rb'
- 'spec/support/services/clusters/create_service_shared.rb'
- 'spec/support/shared_examples/controllers/access_tokens_controller_shared_examples.rb'
- 'spec/support/shared_examples/models/update_project_statistics_shared_examples.rb'
- 'spec/support/shared_examples/requests/api/conan_packages_shared_examples.rb'
- 'spec/support/shared_examples/requests/api/debian_packages_shared_examples.rb'
- 'spec/support/shared_examples/requests/api/nuget_packages_shared_examples.rb'
- 'spec/support/shared_examples/requests/api/rubygems_packages_shared_examples.rb'
- 'spec/support/shared_examples/services/alert_management/alert_processing/incident_creation_shared_examples.rb'
- 'spec/support/shared_examples/services/issuable/destroy_service_shared_examples.rb'
- 'spec/support/shared_examples/services/packages_shared_examples.rb'
- 'spec/support/shared_examples/workers/gitlab/jira_import/jira_import_workers_shared_examples.rb'
- 'spec/tasks/gitlab/storage_rake_spec.rb'
- 'spec/uploaders/external_diff_uploader_spec.rb'
- 'spec/uploaders/lfs_object_uploader_spec.rb'
- 'spec/workers/authorized_project_update/user_refresh_over_user_range_worker_spec.rb'
- 'spec/workers/build_finished_worker_spec.rb'
- 'spec/workers/bulk_import_worker_spec.rb'
- 'spec/workers/bulk_imports/entity_worker_spec.rb'
- 'spec/workers/bulk_imports/pipeline_worker_spec.rb'
- 'spec/workers/deployments/execute_hooks_worker_spec.rb'
- 'spec/workers/deployments/hooks_worker_spec.rb'
- 'spec/workers/export_csv_worker_spec.rb'
- 'spec/workers/gitlab/github_import/advance_stage_worker_spec.rb'
- 'spec/workers/gitlab/github_import/stage/import_base_data_worker_spec.rb'
- 'spec/workers/gitlab/github_import/stage/import_issues_and_diff_notes_worker_spec.rb'
- 'spec/workers/gitlab/github_import/stage/import_lfs_objects_worker_spec.rb'
- 'spec/workers/gitlab/github_import/stage/import_notes_worker_spec.rb'
- 'spec/workers/gitlab/github_import/stage/import_pull_requests_merged_by_worker_spec.rb'
- 'spec/workers/gitlab/github_import/stage/import_pull_requests_reviews_worker_spec.rb'
- 'spec/workers/gitlab/github_import/stage/import_pull_requests_worker_spec.rb'
- 'spec/workers/gitlab/github_import/stage/import_repository_worker_spec.rb'
- 'spec/workers/gitlab/jira_import/stage/start_import_worker_spec.rb'
- 'spec/workers/issue_placement_worker_spec.rb'
- 'spec/workers/metrics/dashboard/schedule_annotations_prune_worker_spec.rb'
- 'spec/workers/pages_domain_ssl_renewal_cron_worker_spec.rb'
- 'spec/workers/pages_domain_verification_cron_worker_spec.rb'
- 'spec/workers/post_receive_spec.rb'
- 'spec/workers/project_cache_worker_spec.rb'
- 'spec/workers/repository_check/dispatch_worker_spec.rb'
- 'ee/spec/lib/gitlab/geo/log_cursor/daemon_spec.rb'
- 'ee/spec/lib/gitlab/geo/log_cursor/events/repository_created_event_spec.rb'
- 'ee/spec/models/merge_train_spec.rb'
- 'ee/spec/services/iterations/cadences/create_service_spec.rb'
- 'ee/spec/workers/incident_management/incident_sla_exceeded_check_worker_spec.rb'
- 'ee/spec/workers/iterations/cadences/schedule_create_iterations_worker_spec.rb'
- 'spec/controllers/admin/clusters/applications_controller_spec.rb'
- 'spec/controllers/groups/clusters/applications_controller_spec.rb'
- 'spec/controllers/projects/clusters/applications_controller_spec.rb'
- 'spec/controllers/projects/mirrors_controller_spec.rb'
- 'spec/lib/gitlab/background_migration_spec.rb'
- 'spec/lib/gitlab/github_import/parallel_importer_spec.rb'
- 'spec/requests/api/ci/pipeline_schedules_spec.rb'
- 'spec/services/auto_merge/base_service_spec.rb'
- 'spec/services/git/process_ref_changes_service_spec.rb'
- 'spec/services/merge_requests/base_service_spec.rb'
- 'spec/services/web_hooks/destroy_service_spec.rb'
- 'spec/workers/container_expiration_policy_worker_spec.rb'
- 'spec/workers/namespaces/prune_aggregation_schedules_worker_spec.rb'
- 'ee/spec/services/elastic/indexing_control_service_spec.rb'
- 'ee/spec/support/shared_examples/lib/gitlab/geo/geo_log_cursor_event_shared_examples.rb'
- 'ee/spec/workers/geo/repository_sync_worker_spec.rb'
- 'ee/spec/workers/update_all_mirrors_worker_spec.rb'
- 'spec/lib/gitlab/github_import/parallel_scheduling_spec.rb'
- 'spec/models/clusters/cluster_spec.rb'
- 'spec/services/clusters/applications/destroy_service_spec.rb'
- 'spec/support/shared_examples/models/concerns/repository_storage_movable_shared_examples.rb'
- 'spec/support/shared_examples/requests/api/repository_storage_moves_shared_examples.rb'
- 'spec/support/shared_examples/requests/self_monitoring_shared_examples.rb'
- 'spec/support/shared_examples/services/repositories/housekeeping_shared_examples.rb'
- 'spec/support/shared_examples/services/schedule_bulk_repository_shard_moves_shared_examples.rb'
- 'spec/uploaders/workers/object_storage/migrate_uploads_worker_spec.rb'
- 'spec/workers/concerns/limited_capacity/worker_spec.rb'
- 'spec/workers/concerns/reenqueuer_spec.rb'
- 'spec/workers/gitlab/phabricator_import/base_worker_spec.rb'
- 'spec/workers/metrics/dashboard/prune_old_annotations_worker_spec.rb'
# WIP: https://gitlab.com/gitlab-org/gitlab/-/issues/321982
Gitlab/NamespacedClass:
Exclude:
......
......@@ -432,6 +432,7 @@ group :test do
gem 'concurrent-ruby', '~> 1.1'
gem 'test-prof', '~> 0.12.0'
gem 'rspec_junit_formatter'
gem 'rspec-sidekiq'
gem 'guard-rspec'
# Moved in `test` because https://gitlab.com/gitlab-org/gitlab/-/issues/217527
......
......@@ -1090,6 +1090,9 @@ GEM
rspec-support (~> 3.10)
rspec-retry (0.6.1)
rspec-core (> 3.3)
rspec-sidekiq (3.1.0)
rspec-core (~> 3.0, >= 3.0.0)
sidekiq (>= 2.4.0)
rspec-support (3.10.2)
rspec_junit_formatter (0.4.1)
rspec-core (>= 2, < 4, != 2.12.0)
......@@ -1605,6 +1608,7 @@ DEPENDENCIES
rspec-parameterized
rspec-rails (~> 5.0.1)
rspec-retry (~> 0.6.1)
rspec-sidekiq
rspec_junit_formatter
rspec_profiling (~> 0.0.6)
ruby-fogbugz (~> 0.2.1)
......
......@@ -259,9 +259,9 @@ it 'sets the frobulance' do
end
it 'schedules a background job' do
expect(BackgroundJob).to receive(:perform_async)
subject.execute
expect(BackgroundJob).to have_enqueued_sidekiq_job
end
```
......@@ -271,11 +271,11 @@ combining the examples:
```ruby
it 'performs the expected side-effects' do
expect(BackgroundJob).to receive(:perform_async)
expect { subject.execute }
.to change(Event, :count).by(1)
.and change { arg_0.frobulance }.to('wibble')
expect(BackgroundJob).to have_enqueued_sidekiq_job
end
```
......@@ -738,6 +738,28 @@ The usage of `perform_enqueued_jobs` is useful only for testing delayed mail
deliveries, because our Sidekiq workers aren't inheriting from `ApplicationJob`
/ `ActiveJob::Base`.
GitLab uses the [RSpec-Sidekiq](https://github.com/philostler/rspec-sidekiq) gem for expectations.
We prefer you check that a job has been enqueued, rather than checking the worker has
`received` the `perform_async` method:
```ruby
# bad
expect(Worker).to receive(:perform_async).with(1, 'string')
# Good
expect(Worker).to have_enqueued_sidekiq_job(1, 'string')
```
The only exception to this rule: if the spec actually needs to make sure the job is
enqueued only once, or a specific number of times:
```ruby
# good
expect(Worker).to receive(:perform_async).with(1, 'string').once
```
If you need test that the job is only enqueued a specific number of times, you will have to disable the cop
that enforces usage of `have_enqueued_sidekiq_job` (`RSpec/HaveEnqueuedSidekiqJob`) in that test.
#### DNS
DNS requests are stubbed universally in the test suite
......
......@@ -146,10 +146,9 @@ RSpec.describe RegistrationsController do
end
it 'redirects without deleting the account' do
expect(DeleteUserWorker).not_to receive(:perform_async)
post :destroy, params: { username: user.username }
expect(DeleteUserWorker).not_to have_enqueued_sidekiq_job
expect(flash[:alert]).to eq 'Account could not be deleted. GitLab was unable to verify your identity.'
expect(response).to have_gitlab_http_status(:see_other)
expect(response).to redirect_to profile_account_path
......
......@@ -328,17 +328,16 @@ RSpec.describe Projects::MergeRequestsController do
end
def expect_rebase_worker_for(user)
expect(RebaseWorker).to receive(:perform_async).with(merge_request.id, user.id, false)
expect(RebaseWorker).to have_enqueued_sidekiq_job(merge_request.id, user.id, false)
end
context 'approvals pending' do
let(:project) { create(:project, :repository, approvals_before_merge: 1) }
it 'returns 200' do
expect_rebase_worker_for(viewer)
post_rebase
expect_rebase_worker_for(viewer)
expect(response).to have_gitlab_http_status(:ok)
end
end
......
......@@ -55,10 +55,14 @@ RSpec.describe Mutations::RequirementsManagement::ExportRequirements do
end
it 'exports requirements' do
expect(IssuableExportCsvWorker).to receive(:perform_async)
.with(:requirement, user.id, project.id, args.except(:project_path))
subject
expect(IssuableExportCsvWorker).to have_enqueued_sidekiq_job(
:requirement,
user.id,
project.id,
args.except(:project_path)
)
end
end
......
......@@ -55,11 +55,10 @@ RSpec.describe Analytics::MergeRequestMetricsRefresh do
describe '#execute_async' do
it 'schedules CodeReviewMetricsWorker with params' do
expect(Analytics::CodeReviewMetricsWorker)
.to receive(:perform_async)
.with('MyTestClass', merge_request.id, force: true)
subject.execute_async(force: true)
expect(Analytics::CodeReviewMetricsWorker)
.to have_enqueued_sidekiq_job('MyTestClass', merge_request.id, force: true)
end
end
end
......@@ -39,11 +39,11 @@ RSpec.describe Gitlab::BackgroundMigration::PopulateNamespaceStatistics do
expect(namespace_statistics.count).to eq 1
expect(Namespaces::ScheduleAggregationWorker).to receive(:perform_async).with(group1.id)
expect(Namespaces::ScheduleAggregationWorker).to receive(:perform_async).with(group2.id)
subject
expect(Namespaces::ScheduleAggregationWorker).to have_enqueued_sidekiq_job(group1.id)
expect(Namespaces::ScheduleAggregationWorker).to have_enqueued_sidekiq_job(group2.id)
expect(namespace_statistics.count).to eq 2
namespace_statistics.all.each do |stat|
......
......@@ -54,9 +54,9 @@ RSpec.describe Gitlab::Auth::GroupSaml::MembershipUpdater do
end
it 'does not enqueue group sync' do
expect(GroupSamlGroupSyncWorker).not_to receive(:perform_async)
update_membership
expect(GroupSamlGroupSyncWorker).not_to have_enqueued_sidekiq_job
end
context 'when SAML group links exist' do
......@@ -73,30 +73,34 @@ RSpec.describe Gitlab::Auth::GroupSaml::MembershipUpdater do
end
it 'does not enqueue group sync' do
expect(GroupSamlGroupSyncWorker).not_to receive(:perform_async)
expect(GroupSamlGroupSyncWorker).not_to have_enqueued_sidekiq_job
end
end
context 'when group sync is available' do
before do
stub_saml_group_sync_available(true)
group_link
subgroup_link
end
it 'enqueues group sync' do
expect(GroupSamlGroupSyncWorker).to receive(:perform_async).with(user.id, group.id, match_array([group_link.id, subgroup_link.id]))
update_membership
expect(GroupSamlGroupSyncWorker).to have_enqueued_sidekiq_job(user.id, group.id, match_array([group_link.id, subgroup_link.id]))
end
context 'with a group link outside the top-level group' do
before do
create(:saml_group_link, saml_group_name: 'Developers', group: create(:group))
group_link
subgroup_link
end
it 'enqueues group sync without the outside group' do
expect(GroupSamlGroupSyncWorker).to receive(:perform_async).with(user.id, group.id, match_array([group_link.id, subgroup_link.id]))
update_membership
expect(GroupSamlGroupSyncWorker).to have_enqueued_sidekiq_job(user.id, group.id, match_array([group_link.id, subgroup_link.id]))
end
end
end
......
......@@ -233,10 +233,9 @@ RSpec.describe Gitlab::Auth::Ldap::Access do
group_link_2 = create(:ldap_group_link, cn: 'Group2', provider: provider)
group_ids = [group_link_1, group_link_2].map(&:group_id)
expect(LdapGroupSyncWorker).to receive(:perform_async)
.with(a_collection_containing_exactly(*group_ids), provider)
access.update_user
expect(LdapGroupSyncWorker).to have_enqueued_sidekiq_job(a_collection_containing_exactly(*group_ids), provider)
end
it "doesn't trigger a sync when in a read-only GitLab instance" do
......@@ -244,9 +243,9 @@ RSpec.describe Gitlab::Auth::Ldap::Access do
create(:ldap_group_link, cn: 'Group1', provider: provider)
create(:ldap_group_link, cn: 'Group2', provider: provider)
expect(LdapGroupSyncWorker).not_to receive(:perform_async)
access.update_user
expect(LdapGroupSyncWorker).not_to have_enqueued_sidekiq_job
end
it "doesn't trigger a sync when there are no links for the provider" do
......@@ -254,9 +253,9 @@ RSpec.describe Gitlab::Auth::Ldap::Access do
cn: 'Group1',
provider: 'not-this-ldap')
expect(LdapGroupSyncWorker).not_to receive(:perform_async)
access.update_user
expect(LdapGroupSyncWorker).not_to have_enqueued_sidekiq_job
end
it 'does not performs the membership update for existing users' do
......@@ -265,9 +264,10 @@ RSpec.describe Gitlab::Auth::Ldap::Access do
user.save
expect(LdapGroupLink).not_to receive(:where)
expect(LdapGroupSyncWorker).not_to receive(:perform_async)
access.update_user
expect(LdapGroupSyncWorker).not_to have_enqueued_sidekiq_job
end
end
......@@ -275,9 +275,10 @@ RSpec.describe Gitlab::Auth::Ldap::Access do
stub_ldap_person_find_by_dn(entry, provider)
expect(LdapGroupLink).not_to receive(:where)
expect(LdapGroupSyncWorker).not_to receive(:perform_async)
access.update_user
expect(LdapGroupSyncWorker).not_to have_enqueued_sidekiq_job
end
end
......
......@@ -173,9 +173,9 @@ RSpec.describe Gitlab::Geo::LogCursor::Daemon, :clean_gitlab_redis_shared_state
it 'does not replay events for projects that do not belong to selected namespaces to replicate' do
secondary.update!(selective_sync_type: 'namespaces', namespaces: [group_2])
expect(Geo::ProjectSyncWorker).not_to receive(:perform_async).with(project.id, anything)
daemon.find_and_handle_events!
expect(Geo::ProjectSyncWorker).not_to have_enqueued_sidekiq_job(project.id, anything)
end
it 'detects when an event was skipped' do
......@@ -240,9 +240,9 @@ RSpec.describe Gitlab::Geo::LogCursor::Daemon, :clean_gitlab_redis_shared_state
it 'does not replay events for projects that do not belong to selected shards to replicate' do
secondary.update!(selective_sync_type: 'shards', selective_sync_shards: ['broken'])
expect(Geo::ProjectSyncWorker).not_to receive(:perform_async).with(project.id, anything)
daemon.find_and_handle_events!
expect(Geo::ProjectSyncWorker).not_to have_enqueued_sidekiq_job(project.id, anything)
end
end
end
......
......@@ -13,13 +13,13 @@ RSpec.describe Gitlab::Geo::LogCursor::Events::Event, :clean_gitlab_redis_shared
describe "#process" do
it "enqueues Geo::EventWorker" do
expect(::Geo::EventWorker).to receive(:perform_async).with(
subject.process
expect(::Geo::EventWorker).to have_enqueued_sidekiq_job(
"package_file",
"created",
{ "model_record_id" => replicable.id }
)
subject.process
end
it "eventually calls Replicator#consume", :sidekiq_inline do
......
......@@ -13,20 +13,19 @@ RSpec.describe Gitlab::Geo::LogCursor::Events::HashedStorageAttachmentsEvent, :c
subject { described_class.new(hashed_storage_attachments_event, Time.now, logger) }
around do |example|
Sidekiq::Testing.fake! { example.run }
end
describe '#process' do
it 'does not create a new project registry' do
expect { subject.process }.not_to change(Geo::ProjectRegistry, :count)
end
it 'schedules a Geo::HashedStorageAttachmentsMigrationWorker' do
expect(::Geo::HashedStorageAttachmentsMigrationWorker).to receive(:perform_async)
.with(project.id, old_attachments_path, new_attachments_path)
subject.process
expect(::Geo::HashedStorageAttachmentsMigrationWorker).to have_enqueued_sidekiq_job(
project.id,
old_attachments_path,
new_attachments_path
)
end
it_behaves_like 'logs event source info'
......
......@@ -14,10 +14,6 @@ RSpec.describe Gitlab::Geo::LogCursor::Events::HashedStorageMigratedEvent, :clea
subject { described_class.new(hashed_storage_migrated_event, Time.now, logger) }
around do |example|
Sidekiq::Testing.fake! { example.run }
end
describe '#process' do
context 'when a tracking entry does not exist' do
it 'does not create a tracking entry' do
......@@ -25,20 +21,23 @@ RSpec.describe Gitlab::Geo::LogCursor::Events::HashedStorageMigratedEvent, :clea
end
it 'does not schedule a Geo::HashedStorageMigrationWorker' do
expect(::Geo::HashedStorageMigrationWorker).not_to receive(:perform_async)
.with(project.id, old_disk_path, new_disk_path, old_storage_version)
subject.process
expect(::Geo::HashedStorageMigrationWorker).not_to have_enqueued_sidekiq_job
end
end
it 'schedules a Geo::HashedStorageMigrationWorker' do
create(:geo_project_registry, project: project)
expect(::Geo::HashedStorageMigrationWorker).to receive(:perform_async)
.with(project.id, old_disk_path, new_disk_path, old_storage_version)
subject.process
expect(::Geo::HashedStorageMigrationWorker).to have_enqueued_sidekiq_job(
project.id,
old_disk_path,
new_disk_path,
old_storage_version
)
end
it_behaves_like 'logs event source info'
......
......@@ -82,9 +82,9 @@ RSpec.describe Gitlab::Geo::LogCursor::Events::RepositoryCreatedEvent, :clean_gi
it_behaves_like 'RepositoryCreatedEvent'
it 'does not schedule a Geo::ProjectSyncWorker job' do
expect(Geo::ProjectSyncWorker).not_to receive(:perform_async).with(project.id, anything)
subject.process
expect(Geo::ProjectSyncWorker).not_to have_enqueued_sidekiq_job
end
end
end
......
......@@ -270,11 +270,11 @@ RSpec.describe ApplicationSetting do
end
it 'resumes indexing' do
expect(ElasticIndexingControlWorker).to receive(:perform_async)
setting.save!
setting.elasticsearch_pause_indexing = false
setting.save!
expect(ElasticIndexingControlWorker).to have_enqueued_sidekiq_job
end
end
......
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# This cop checks for `expect(worker).to receive(:perform_async)` usage in specs
#
# @example
# # bad
# it "enqueues a worker" do
# expect(MyWorker).to receive(:perform_async)
# end
#
# # good
# it "enqueues a worker" do
# expect(MyWorker).to have_enqueued_sidekiq_job
# end
#
# # bad
# it "enqueues a worker" do
# expect(MyWorker).to receive(:perform_async).with(1, 2)
# end
#
# # good
# it "enqueues a worker" do
# expect(MyWorker).to have_enqueued_sidekiq_job(1, 2)
# end
#
class HaveEnqueuedSidekiqJob < RuboCop::Cop::Cop
MESSAGE = 'Do not use `receive(:perform_async)` to check a job has been enqueued, use `have_enqueued_sidekiq_job` instead.'
def_node_search :expect_to_receive_perform_async?, <<~PATTERN
(send
(send nil? :expect ...)
{:to :not_to :to_not}
{
(send nil? :receive (sym :perform_async))
(send
(send nil? :receive (sym :perform_async)) ...
)
(send
(send
(send nil? :receive (sym :perform_async)) ...
) ...
)
}
)
PATTERN
def on_send(node)
return unless expect_to_receive_perform_async?(node)
add_offense(node, location: :expression, message: MESSAGE)
end
end
end
end
end
......@@ -658,7 +658,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do
let(:config) { { script: 'deploy', when: 'delayed' } }
it 'is a delayed' do
expect(entry).to be_delayed
expect(entry.delayed?).to be_truthy
end
end
......@@ -666,7 +666,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do
let(:config) { { script: 'deploy' } }
it 'is not a delayed' do
expect(entry).not_to be_delayed
expect(entry.delayed?).to be_falsey
end
end
end
......
......@@ -69,15 +69,15 @@ RSpec.describe Gitlab::Database::PostgresIndex do
describe '#unique?' do
it 'returns true for a unique index' do
expect(find('public.bar_key')).to be_unique
expect(find('public.bar_key').unique?).to be_truthy
end
it 'returns false for a regular, non-unique index' do
expect(find('public.foo_idx')).not_to be_unique
expect(find('public.foo_idx').unique?).to be_falsey
end
it 'returns true for a primary key index' do
expect(find('public.example_table_pkey')).to be_unique
expect(find('public.example_table_pkey').unique?).to be_truthy
end
end
......
......@@ -1842,7 +1842,7 @@ RSpec.describe Ci::Build do
end
describe '#retryable?' do
subject { build }
subject { build.retryable? }
context 'when build is retryable' do
context 'when build is successful' do
......@@ -1850,7 +1850,7 @@ RSpec.describe Ci::Build do
build.success!
end
it { is_expected.to be_retryable }
it { is_expected.to be_truthy }
end
context 'when build is failed' do
......@@ -1858,7 +1858,7 @@ RSpec.describe Ci::Build do
build.drop!
end
it { is_expected.to be_retryable }
it { is_expected.to be_truthy }
end
context 'when build is canceled' do
......@@ -1866,7 +1866,7 @@ RSpec.describe Ci::Build do
build.cancel!
end
it { is_expected.to be_retryable }
it { is_expected.to be_truthy }
end
end
......@@ -1876,7 +1876,7 @@ RSpec.describe Ci::Build do
build.run!
end
it { is_expected.not_to be_retryable }
it { is_expected.to be_falsey }
end
context 'when build is skipped' do
......@@ -1884,7 +1884,7 @@ RSpec.describe Ci::Build do
build.skip!
end
it { is_expected.not_to be_retryable }
it { is_expected.to be_falsey }
end
context 'when build is degenerated' do
......@@ -1892,7 +1892,7 @@ RSpec.describe Ci::Build do
build.degenerate!
end
it { is_expected.not_to be_retryable }
it { is_expected.to be_falsey }
end
context 'when a canceled build has been retried already' do
......@@ -1903,7 +1903,7 @@ RSpec.describe Ci::Build do
end
context 'when prevent_retry_of_retried_jobs feature flag is enabled' do
it { is_expected.not_to be_retryable }
it { is_expected.to be_falsey }
end
context 'when prevent_retry_of_retried_jobs feature flag is disabled' do
......@@ -1911,7 +1911,7 @@ RSpec.describe Ci::Build do
stub_feature_flags(prevent_retry_of_retried_jobs: false)
end
it { is_expected.to be_retryable }
it { is_expected.to be_truthy }
end
end
end
......
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/rspec/have_enqueued_sidekiq_job'
RSpec.describe RuboCop::Cop::RSpec::HaveEnqueuedSidekiqJob do
let(:source_file) { 'spec/foo_spec.rb' }
subject(:cop) { described_class.new }
shared_examples 'cop' do |good:, bad:|
context "using #{bad} call" do
it 'registers an offense', :aggregate_failures do
expect_offense(<<~CODE, node: bad)
%{node}
^{node} Do not use `receive(:perform_async)` to check a job has been enqueued, use `have_enqueued_sidekiq_job` instead.
CODE
end
end
context "using #{good} call" do
it 'does not register an offense' do
expect_no_offenses(good)
end
end
end
it_behaves_like 'cop',
bad: 'expect(Worker).to receive(:perform_async)',
good: 'expect(Worker).to have_enqueued_sidekiq_job'
include_examples 'cop',
bad: 'expect(Worker).not_to receive(:perform_async)',
good: 'expect(Worker).not_to have_enqueued_sidekiq_job'
include_examples 'cop',
bad: 'expect(Worker).to_not receive(:perform_async)',
good: 'expect(Worker).to_not have_enqueued_sidekiq_job'
include_examples 'cop',
bad: 'expect(Worker).to receive(:perform_async).with(1)',
good: 'expect(Worker).to have_enqueued_sidekiq_job(1)'
include_examples 'cop',
bad: 'expect(Worker).to receive(:perform_async).with(1).once',
good: 'expect(Worker).to have_enqueued_sidekiq_job(1)'
include_examples 'cop',
bad: 'expect(any_variable_or_method).to receive(:perform_async).with(1)',
good: 'expect(any_variable_or_method).to have_enqueued_sidekiq_job(1)'
end
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