diff options
author | James Edwards-Jones <jedwardsjones@gitlab.com> | 2018-01-18 15:05:47 +0000 |
---|---|---|
committer | James Edwards-Jones <jedwardsjones@gitlab.com> | 2018-01-22 14:18:31 +0000 |
commit | 711ec2e7d945e62c426de17007f26a375ae23c7f (patch) | |
tree | 182cdd7c3feb429dbeedcc095a46462be838ce22 | |
parent | be623ef3c1a867d23e9625fe372c17fcad3c47ce (diff) | |
download | gitlab-ce-jej/impove-be_scheduled_migration.tar.gz |
Improve rspec matcher for delayed scheduled migrationsjej/impove-be_scheduled_migration
Failure message includes difference in arguments
and in scheduled times.
-rw-r--r-- | spec/support/background_migrations_matchers.rb | 56 |
1 files changed, 51 insertions, 5 deletions
diff --git a/spec/support/background_migrations_matchers.rb b/spec/support/background_migrations_matchers.rb index f4127efc6ae..3d93ed61c60 100644 --- a/spec/support/background_migrations_matchers.rb +++ b/spec/support/background_migrations_matchers.rb @@ -1,14 +1,60 @@ RSpec::Matchers.define :be_scheduled_delayed_migration do |delay, *expected| + include ActionView::Helpers::DateHelper + match do |migration| - BackgroundMigrationWorker.jobs.any? do |job| - job['args'] == [migration, expected] && - job['at'].to_i == (delay.to_i + Time.now.to_i) + @delay = delay + @migration = migration + @expected = expected + + jobs_with_expected_args.any? do |job| + job['at'].to_i == expected_time end end + def expected_time + (@delay.to_i + Time.now.to_i) + end + + def expected_args + [migration_name, @expected] + end + + def migration_name + @migration.is_a?(Class) ? @migration.to_s.demodulize : @migration + end + + def jobs_with_expected_args + BackgroundMigrationWorker.jobs.select do |job| + job['args'] == expected_args + end + end + + def found_args + BackgroundMigrationWorker.jobs.map {|job| job['args'] } + end + + def found_times + jobs_with_expected_args.map {|job| job['at'].to_i } + .map {|time| time_from_now(time) } + end + + def expected_time_in_words + time_from_now(expected_time) + end + + def time_from_now(time) + distance_of_time_in_words(Time.now, Time.at(time)) + end + failure_message do |migration| - "Migration `#{migration}` with args `#{expected.inspect}` " \ - 'not scheduled in expected time!' + if jobs_with_expected_args.any? + "Migration `#{migration_name}` with args `#{@expected.inspect}`" \ + " should have been scheduled in #{expected_time_in_words} but" \ + " was found in #{found_times}" + else + "Migration `#{migration_name}` not found scheduled with args `#{@expected.inspect}`" \ + " but was found with args #{found_args}" + end end end |