sidekiq-unique-jobs 5.0.2 → 5.0.11

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sidekiq-unique-jobs might be problematic. Click here for more details.

Files changed (207) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +8 -0
  3. data/.rubocop.yml +20 -13
  4. data/.simplecov +1 -1
  5. data/.travis.yml +30 -20
  6. data/Appraisals +10 -2
  7. data/CHANGELOG.md +33 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/Gemfile +13 -12
  10. data/LICENSE.txt +21 -0
  11. data/README.md +48 -4
  12. data/Rakefile +0 -1
  13. data/bin/bench +1 -1
  14. data/lib/sidekiq/simulator.rb +4 -10
  15. data/lib/sidekiq-unique-jobs.rb +16 -6
  16. data/lib/sidekiq_unique_jobs/cli.rb +14 -12
  17. data/lib/sidekiq_unique_jobs/client/middleware.rb +2 -0
  18. data/lib/sidekiq_unique_jobs/config.rb +2 -0
  19. data/lib/sidekiq_unique_jobs/constants.rb +20 -17
  20. data/lib/sidekiq_unique_jobs/core_ext.rb +18 -0
  21. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +2 -0
  22. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +4 -5
  23. data/lib/sidekiq_unique_jobs/lock/until_executing.rb +2 -0
  24. data/lib/sidekiq_unique_jobs/lock/until_timeout.rb +2 -0
  25. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +10 -9
  26. data/lib/sidekiq_unique_jobs/lock.rb +2 -0
  27. data/lib/sidekiq_unique_jobs/middleware.rb +2 -0
  28. data/lib/sidekiq_unique_jobs/normalizer.rb +3 -1
  29. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +22 -3
  30. data/lib/sidekiq_unique_jobs/run_lock_failed.rb +2 -0
  31. data/lib/sidekiq_unique_jobs/script_mock.rb +16 -15
  32. data/lib/sidekiq_unique_jobs/scripts/acquire_lock.rb +2 -0
  33. data/lib/sidekiq_unique_jobs/scripts/release_lock.rb +2 -0
  34. data/lib/sidekiq_unique_jobs/scripts.rb +17 -12
  35. data/lib/sidekiq_unique_jobs/server/middleware.rb +2 -0
  36. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +4 -14
  37. data/lib/sidekiq_unique_jobs/testing/sidekiq_overrides.rb +2 -0
  38. data/lib/sidekiq_unique_jobs/testing.rb +4 -2
  39. data/lib/sidekiq_unique_jobs/timeout_calculator.rb +2 -0
  40. data/lib/sidekiq_unique_jobs/unique_args.rb +27 -14
  41. data/lib/sidekiq_unique_jobs/unlockable.rb +6 -6
  42. data/lib/sidekiq_unique_jobs/util.rb +34 -14
  43. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  44. data/redis/acquire_lock.lua +1 -1
  45. data/sidekiq-unique-jobs.gemspec +27 -23
  46. metadata +44 -212
  47. data/LICENSE +0 -13
  48. data/circle.yml +0 -41
  49. data/gemfiles/sidekiq_4.0.gemfile +0 -22
  50. data/gemfiles/sidekiq_4.1.gemfile +0 -22
  51. data/gemfiles/sidekiq_4.2.gemfile +0 -22
  52. data/gemfiles/sidekiq_5.0.gemfile +0 -22
  53. data/gemfiles/sidekiq_develop.gemfile +0 -22
  54. data/rails_example/.env +0 -12
  55. data/rails_example/.env.test +0 -12
  56. data/rails_example/.gitignore +0 -13
  57. data/rails_example/.rspec +0 -2
  58. data/rails_example/Gemfile +0 -38
  59. data/rails_example/Procfile +0 -2
  60. data/rails_example/README.rdoc +0 -28
  61. data/rails_example/Rakefile +0 -6
  62. data/rails_example/app/assets/images/.keep +0 -0
  63. data/rails_example/app/assets/javascripts/application.js +0 -16
  64. data/rails_example/app/assets/stylesheets/application.css +0 -15
  65. data/rails_example/app/channels/appearance_channel.rb +0 -17
  66. data/rails_example/app/channels/application_cable/channel.rb +0 -4
  67. data/rails_example/app/channels/application_cable/connection.rb +0 -9
  68. data/rails_example/app/channels/post_channel.rb +0 -5
  69. data/rails_example/app/controllers/application_controller.rb +0 -5
  70. data/rails_example/app/controllers/concerns/.keep +0 -0
  71. data/rails_example/app/controllers/work_controller.rb +0 -39
  72. data/rails_example/app/helpers/application_helper.rb +0 -2
  73. data/rails_example/app/mailers/.keep +0 -0
  74. data/rails_example/app/models/.keep +0 -0
  75. data/rails_example/app/models/application_record.rb +0 -3
  76. data/rails_example/app/models/concerns/.keep +0 -0
  77. data/rails_example/app/models/guest.rb +0 -18
  78. data/rails_example/app/models/post.rb +0 -2
  79. data/rails_example/app/views/layouts/application.html.erb +0 -15
  80. data/rails_example/app/workers/simple_worker.rb +0 -15
  81. data/rails_example/app/workers/slow_until_executing_worker.rb +0 -15
  82. data/rails_example/app/workers/spawn_simple_worker.rb +0 -10
  83. data/rails_example/app/workers/while_executing_worker.rb +0 -12
  84. data/rails_example/app/workers/without_args_worker.rb +0 -16
  85. data/rails_example/app/workers/without_argument_worker.rb +0 -12
  86. data/rails_example/bin/bundle +0 -3
  87. data/rails_example/bin/check_or_setup_db +0 -56
  88. data/rails_example/bin/docker-setup +0 -20
  89. data/rails_example/bin/rails +0 -4
  90. data/rails_example/bin/rake +0 -4
  91. data/rails_example/bin/setup +0 -34
  92. data/rails_example/bin/update +0 -29
  93. data/rails_example/cable.ru +0 -6
  94. data/rails_example/common-services.yml +0 -52
  95. data/rails_example/config/application.rb +0 -27
  96. data/rails_example/config/boot.rb +0 -3
  97. data/rails_example/config/cable.yml +0 -9
  98. data/rails_example/config/database.docker.yml +0 -12
  99. data/rails_example/config/database.yml +0 -19
  100. data/rails_example/config/environment.rb +0 -5
  101. data/rails_example/config/environments/development.rb +0 -54
  102. data/rails_example/config/environments/production.rb +0 -86
  103. data/rails_example/config/environments/test.rb +0 -42
  104. data/rails_example/config/initializers/application_controller_renderer.rb +0 -6
  105. data/rails_example/config/initializers/assets.rb +0 -11
  106. data/rails_example/config/initializers/backtrace_silencers.rb +0 -9
  107. data/rails_example/config/initializers/cookies_serializer.rb +0 -5
  108. data/rails_example/config/initializers/filter_parameter_logging.rb +0 -4
  109. data/rails_example/config/initializers/inflections.rb +0 -16
  110. data/rails_example/config/initializers/mime_types.rb +0 -4
  111. data/rails_example/config/initializers/new_framework_defaults.rb +0 -23
  112. data/rails_example/config/initializers/session_store.rb +0 -3
  113. data/rails_example/config/initializers/sidekiq.rb +0 -13
  114. data/rails_example/config/initializers/wrap_parameters.rb +0 -14
  115. data/rails_example/config/locales/en.yml +0 -23
  116. data/rails_example/config/puma.rb +0 -38
  117. data/rails_example/config/routes.rb +0 -8
  118. data/rails_example/config/secrets.yml +0 -22
  119. data/rails_example/config/sidekiq.yml +0 -6
  120. data/rails_example/config/spring.rb +0 -6
  121. data/rails_example/config.ru +0 -4
  122. data/rails_example/db/migrate/20160724111322_create_posts.rb +0 -12
  123. data/rails_example/db/schema.rb +0 -25
  124. data/rails_example/db/seeds.rb +0 -7
  125. data/rails_example/dev-entrypoint.sh +0 -55
  126. data/rails_example/dev.env +0 -12
  127. data/rails_example/docker/rails.Dockerfile +0 -27
  128. data/rails_example/docker-compose.yml +0 -71
  129. data/rails_example/lib/assets/.keep +0 -0
  130. data/rails_example/lib/tasks/.keep +0 -0
  131. data/rails_example/log/.keep +0 -0
  132. data/rails_example/public/404.html +0 -67
  133. data/rails_example/public/422.html +0 -67
  134. data/rails_example/public/500.html +0 -66
  135. data/rails_example/public/favicon.ico +0 -0
  136. data/rails_example/public/robots.txt +0 -5
  137. data/rails_example/simple.ru +0 -12
  138. data/rails_example/spec/controllers/work_controller_mock_spec.rb +0 -91
  139. data/rails_example/spec/controllers/work_controller_spec.rb +0 -77
  140. data/rails_example/spec/factories/posts.rb +0 -8
  141. data/rails_example/spec/models/post_spec.rb +0 -4
  142. data/rails_example/spec/rails_helper.rb +0 -20
  143. data/rails_example/spec/spec_helper.rb +0 -20
  144. data/rails_example/spec/support/sidekiq_meta.rb +0 -12
  145. data/rails_example/spec/workers/simple_worker_spec.rb +0 -4
  146. data/rails_example/vendor/assets/javascripts/.keep +0 -0
  147. data/rails_example/vendor/assets/stylesheets/.keep +0 -0
  148. data/spec/jobs/another_unique_job.rb +0 -13
  149. data/spec/jobs/custom_queue_job.rb +0 -5
  150. data/spec/jobs/custom_queue_job_with_filter_method.rb +0 -7
  151. data/spec/jobs/custom_queue_job_with_filter_proc.rb +0 -10
  152. data/spec/jobs/expiring_job.rb +0 -4
  153. data/spec/jobs/inline_worker.rb +0 -8
  154. data/spec/jobs/just_a_worker.rb +0 -7
  155. data/spec/jobs/long_running_job.rb +0 -6
  156. data/spec/jobs/main_job.rb +0 -7
  157. data/spec/jobs/my_job.rb +0 -12
  158. data/spec/jobs/my_unique_job.rb +0 -11
  159. data/spec/jobs/my_unique_job_with_filter_method.rb +0 -17
  160. data/spec/jobs/my_unique_job_with_filter_proc.rb +0 -15
  161. data/spec/jobs/notify_worker.rb +0 -10
  162. data/spec/jobs/plain_class.rb +0 -3
  163. data/spec/jobs/simple_worker.rb +0 -11
  164. data/spec/jobs/spawn_simple_worker.rb +0 -8
  165. data/spec/jobs/test_class.rb +0 -3
  166. data/spec/jobs/unique_job_with_conditional_parameter.rb +0 -14
  167. data/spec/jobs/unique_job_with_filter_method.rb +0 -18
  168. data/spec/jobs/unique_job_with_nil_unique_args.rb +0 -16
  169. data/spec/jobs/unique_job_with_no_unique_args_method.rb +0 -12
  170. data/spec/jobs/unique_job_withthout_unique_args_parameter.rb +0 -14
  171. data/spec/jobs/unique_on_all_queues_job.rb +0 -13
  172. data/spec/jobs/until_and_while_executing_job.rb +0 -7
  173. data/spec/jobs/until_executed_job.rb +0 -17
  174. data/spec/jobs/until_executing_job.rb +0 -7
  175. data/spec/jobs/until_global_timeout_job.rb +0 -8
  176. data/spec/jobs/until_timeout_job.rb +0 -8
  177. data/spec/jobs/while_executing_job.rb +0 -12
  178. data/spec/jobs/without_argument_job.rb +0 -9
  179. data/spec/lib/sidekiq_unique_jobs/cli_spec.rb +0 -183
  180. data/spec/lib/sidekiq_unique_jobs/client/middleware_spec.rb +0 -316
  181. data/spec/lib/sidekiq_unique_jobs/config_spec.rb +0 -84
  182. data/spec/lib/sidekiq_unique_jobs/core_ext_spec.rb +0 -25
  183. data/spec/lib/sidekiq_unique_jobs/lock/until_and_while_executing_spec.rb +0 -42
  184. data/spec/lib/sidekiq_unique_jobs/lock/until_executed_spec.rb +0 -37
  185. data/spec/lib/sidekiq_unique_jobs/lock/until_timeout_spec.rb +0 -26
  186. data/spec/lib/sidekiq_unique_jobs/lock/while_executing_spec.rb +0 -86
  187. data/spec/lib/sidekiq_unique_jobs/normalizer_spec.rb +0 -21
  188. data/spec/lib/sidekiq_unique_jobs/options_with_fallback_spec.rb +0 -113
  189. data/spec/lib/sidekiq_unique_jobs/queue_lock_timeout_calculator_spec.rb +0 -47
  190. data/spec/lib/sidekiq_unique_jobs/run_lock_timeout_calculator_spec.rb +0 -36
  191. data/spec/lib/sidekiq_unique_jobs/script_mock_spec.rb +0 -88
  192. data/spec/lib/sidekiq_unique_jobs/scripts/acquire_lock_spec.rb +0 -50
  193. data/spec/lib/sidekiq_unique_jobs/scripts/release_lock_spec.rb +0 -41
  194. data/spec/lib/sidekiq_unique_jobs/scripts_spec.rb +0 -68
  195. data/spec/lib/sidekiq_unique_jobs/server/middleware_spec.rb +0 -78
  196. data/spec/lib/sidekiq_unique_jobs/sidekiq_testing_enabled_spec.rb +0 -164
  197. data/spec/lib/sidekiq_unique_jobs/sidekiq_unique_ext_spec.rb +0 -75
  198. data/spec/lib/sidekiq_unique_jobs/sidekiq_unique_jobs_spec.rb +0 -41
  199. data/spec/lib/sidekiq_unique_jobs/timeout_calculator_spec.rb +0 -70
  200. data/spec/lib/sidekiq_unique_jobs/unique_args_spec.rb +0 -257
  201. data/spec/lib/sidekiq_unique_jobs/unlockable_spec.rb +0 -37
  202. data/spec/lib/sidekiq_unique_jobs/util_spec.rb +0 -81
  203. data/spec/spec_helper.rb +0 -92
  204. data/spec/support/matchers/redis_matchers.rb +0 -29
  205. data/spec/support/ruby_meta.rb +0 -17
  206. data/spec/support/sidekiq_meta.rb +0 -30
  207. data/spec/support/unique_macros.rb +0 -71
@@ -1,41 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe SidekiqUniqueJobs::Scripts::ReleaseLock do
4
- let(:redis_pool) { nil }
5
- let(:jid) { 'abcdefab' }
6
- let(:unique_key) { 'uniquejobs:123asdasd2134' }
7
- let(:max_lock_time) { 1 }
8
-
9
- describe '.execute' do
10
- subject { instance_double(described_class) }
11
-
12
- it 'delegates to instance' do
13
- expect(described_class).to receive(:new)
14
- .with(redis_pool, unique_key, jid)
15
- .and_return(subject)
16
- expect(subject).to receive(:execute).and_return(true)
17
-
18
- described_class.execute(redis_pool, unique_key, jid)
19
- end
20
- end
21
-
22
- describe '#execute' do
23
- context 'when exists' do
24
- subject { described_class.execute(redis_pool, unique_key, jid) }
25
-
26
- before do
27
- SidekiqUniqueJobs::Scripts::AcquireLock.execute(redis_pool, unique_key, jid, max_lock_time)
28
- end
29
-
30
- specify do
31
- expect(SidekiqUniqueJobs)
32
- .to have_key(unique_key)
33
- .for_seconds(max_lock_time)
34
- .with_value(jid)
35
-
36
- expect(subject).to eq(true)
37
- expect(SidekiqUniqueJobs).not_to have_key(unique_key)
38
- end
39
- end
40
- end
41
- end
@@ -1,68 +0,0 @@
1
- require 'spec_helper'
2
- RSpec.describe SidekiqUniqueJobs::Scripts do
3
- MD5_DIGEST ||= 'unique'.freeze
4
- UNIQUE_KEY ||= 'uniquejobs:unique'.freeze
5
- JID ||= 'fuckit'.freeze
6
- ANOTHER_JID ||= 'anotherjid'.freeze
7
-
8
- context 'class methods' do
9
- subject { SidekiqUniqueJobs::Scripts }
10
-
11
- it { is_expected.to respond_to(:call).with(3).arguments }
12
- it { is_expected.to respond_to(:logger) }
13
- it { is_expected.to respond_to(:connection).with(1).arguments }
14
- it { is_expected.to respond_to(:script_source).with(1).arguments }
15
- it { is_expected.to respond_to(:script_path).with(1).arguments }
16
-
17
- describe '.logger' do
18
- its(:logger) { is_expected.to eq(Sidekiq.logger) }
19
- end
20
-
21
- def lock_for(seconds = 1, jid = JID, key = UNIQUE_KEY)
22
- subject.call(:acquire_lock, nil, keys: [key], argv: [jid, seconds])
23
- end
24
-
25
- def unlock(key = UNIQUE_KEY, jid = JID)
26
- subject.call(:release_lock, nil, keys: [key], argv: [jid])
27
- end
28
-
29
- describe '.acquire_lock' do
30
- context 'when job is unique' do
31
- specify { expect(lock_for).to eq(1) }
32
- specify do
33
- expect(lock_for(2)).to eq(1)
34
- expect(SidekiqUniqueJobs)
35
- .to have_key(UNIQUE_KEY)
36
- .for_seconds(1)
37
- .with_value(JID)
38
- sleep 2
39
- expect(lock_for).to eq(1)
40
- end
41
-
42
- context 'when job is locked' do
43
- before { expect(lock_for(10)).to eq(1) }
44
- specify { expect(lock_for(5, 'anotherjid')).to eq(0) }
45
- end
46
- end
47
- end
48
-
49
- describe '.release_lock' do
50
- context 'when job is locked by another jid' do
51
- before { expect(lock_for(10, 'anotherjid')).to eq(1) }
52
- specify { expect(unlock).to eq(0) }
53
- after { unlock(UNIQUE_KEY, ANOTHER_JID) }
54
- end
55
-
56
- context 'when job is not locked at all' do
57
- specify { expect(unlock).to eq(-1) }
58
- end
59
-
60
- context 'when job is locked by the same jid' do
61
- specify do
62
- expect(lock_for(10)).to eq(1)
63
- expect(unlock).to eq(1)
64
- end
65
- end
66
- end
67
- end
68
- end
@@ -1,78 +0,0 @@
1
- require 'spec_helper'
2
- require 'sidekiq/api'
3
- require 'sidekiq/cli'
4
- require 'sidekiq/worker'
5
- require 'sidekiq_unique_jobs/server/middleware'
6
-
7
- RSpec.describe SidekiqUniqueJobs::Server::Middleware do
8
- QUEUE ||= 'working'.freeze
9
-
10
- def digest_for(item)
11
- SidekiqUniqueJobs::UniqueArgs.digest(item)
12
- end
13
-
14
- describe '#call' do
15
- context 'when unique is disabled' do
16
- it 'does not use locking' do
17
- allow(subject).to receive(:unique_enabled?).and_return(false)
18
- expect(subject).not_to receive(:lock)
19
- args = [WhileExecutingJob, { 'class' => 'WhileExecutingJob' }, 'working', nil]
20
- subject.call(*args) {}
21
- end
22
- end
23
-
24
- context 'when unique is enabled' do
25
- it 'executes the lock' do
26
- allow(subject).to receive(:unique_enabled?).and_return(true)
27
- lock = instance_spy(SidekiqUniqueJobs::Lock::WhileExecuting)
28
- expect(lock).to receive(:send).with(:execute, instance_of(Proc)).and_yield
29
- expect(subject).to receive(:lock).and_return(lock)
30
-
31
- args = [WhileExecutingJob, { 'class' => 'WhileExecutingJob' }, 'working', nil]
32
- subject.call(*args) {}
33
- end
34
- end
35
-
36
- describe '#unlock' do
37
- it 'does not unlock mutexes it does not own' do
38
- jid = UntilExecutedJob.perform_async
39
- item = Sidekiq::Queue.new(QUEUE).find_job(jid).item
40
- Sidekiq.redis do |c|
41
- c.set(digest_for(item), 'NOT_DELETED')
42
- end
43
-
44
- subject.call(UntilExecutedJob.new, item, QUEUE) do
45
- Sidekiq.redis do |c|
46
- expect(c.get(digest_for(item))).to eq('NOT_DELETED')
47
- end
48
- end
49
- end
50
- end
51
-
52
- describe ':before_yield' do
53
- it 'removes the lock before yielding to the worker' do
54
- jid = UntilExecutingJob.perform_async
55
- item = Sidekiq::Queue.new(QUEUE).find_job(jid).item
56
- worker = UntilExecutingJob.new
57
- subject.call(worker, item, QUEUE) do
58
- Sidekiq.redis do |c|
59
- expect(c.ttl(digest_for(item))).to eq(-2) # key does not exist
60
- end
61
- end
62
- end
63
- end
64
-
65
- describe ':after_yield' do
66
- it 'removes the lock after yielding to the worker' do
67
- jid = UntilExecutedJob.perform_async
68
- item = Sidekiq::Queue.new(QUEUE).find_job(jid).item
69
-
70
- subject.call('UntilExecutedJob', item, QUEUE) do
71
- Sidekiq.redis do |c|
72
- expect(c.get(digest_for(item))).to eq jid
73
- end
74
- end
75
- end
76
- end
77
- end
78
- end
@@ -1,164 +0,0 @@
1
- require 'spec_helper'
2
- require 'sidekiq/worker'
3
- require 'sidekiq-unique-jobs'
4
- require 'sidekiq/scheduled'
5
-
6
- RSpec.describe 'When Sidekiq::Testing is enabled' do
7
- describe 'when set to :fake!', sidekiq: :fake do
8
- context 'with unique worker' do
9
- it 'does not push duplicate messages' do
10
- param = 'work'
11
- expect(UntilExecutedJob.jobs.size).to eq(0)
12
- expect(UntilExecutedJob.perform_async(param)).to_not be_nil
13
- expect(UntilExecutedJob.jobs.size).to eq(1)
14
- expect(UntilExecutedJob.perform_async(param)).to be_nil
15
- expect(UntilExecutedJob.jobs.size).to eq(1)
16
- end
17
-
18
- it 'unlocks jobs after draining a worker' do
19
- param = 'work'
20
- param2 = 'more work'
21
-
22
- expect(UntilExecutedJob.jobs.size).to eq(0)
23
- UntilExecutedJob.perform_async(param)
24
- UntilExecutedJob.perform_async(param2)
25
- expect(UntilExecutedJob.jobs.size).to eq(2)
26
- UntilExecutedJob.drain
27
- expect(UntilExecutedJob.jobs.size).to eq(0)
28
- UntilExecutedJob.perform_async(param)
29
- UntilExecutedJob.perform_async(param2)
30
- expect(UntilExecutedJob.jobs.size).to eq(2)
31
- end
32
-
33
- it 'unlocks a single job when calling perform_one' do
34
- param = 'work'
35
- param2 = 'more work'
36
- expect(UntilExecutedJob.jobs.size).to eq(0)
37
- UntilExecutedJob.perform_async(param)
38
- UntilExecutedJob.perform_async(param2)
39
- expect(UntilExecutedJob.jobs.size).to eq(2)
40
- UntilExecutedJob.perform_one
41
- expect(UntilExecutedJob.jobs.size).to eq(1)
42
- UntilExecutedJob.perform_async(param2)
43
- expect(UntilExecutedJob.jobs.size).to eq(1)
44
- UntilExecutedJob.perform_async(param)
45
- expect(UntilExecutedJob.jobs.size).to eq(2)
46
- end
47
-
48
- it 'unlocks jobs cleared from a single worker' do
49
- param = 'work'
50
- param2 = 'more work'
51
- expect(UntilExecutedJob.jobs.size).to eq(0)
52
- expect(AnotherUniqueJob.jobs.size).to eq(0)
53
- UntilExecutedJob.perform_async(param)
54
- UntilExecutedJob.perform_async(param2)
55
- AnotherUniqueJob.perform_async(param)
56
- expect(UntilExecutedJob.jobs.size).to eq(2)
57
- expect(AnotherUniqueJob.jobs.size).to eq(1)
58
- UntilExecutedJob.clear
59
- expect(UntilExecutedJob.jobs.size).to eq(0)
60
- expect(AnotherUniqueJob.jobs.size).to eq(1)
61
- UntilExecutedJob.perform_async(param)
62
- UntilExecutedJob.perform_async(param2)
63
- AnotherUniqueJob.perform_async(param)
64
- expect(UntilExecutedJob.jobs.size).to eq(2)
65
- expect(AnotherUniqueJob.jobs.size).to eq(1)
66
- end
67
-
68
- it 'handles clearing an empty worker queue' do
69
- param = 'work'
70
- UntilExecutedJob.perform_async(param)
71
- UntilExecutedJob.drain
72
- expect(UntilExecutedJob.jobs.size).to eq(0)
73
- expect { UntilExecutedJob.clear }.not_to raise_error
74
- end
75
-
76
- it 'unlocks jobs when all workers are cleared' do
77
- param = 'work'
78
- expect(UntilExecutedJob.jobs.size).to eq(0)
79
- expect(AnotherUniqueJob.jobs.size).to eq(0)
80
- UntilExecutedJob.perform_async(param)
81
- AnotherUniqueJob.perform_async(param)
82
- expect(UntilExecutedJob.jobs.size).to eq(1)
83
- expect(AnotherUniqueJob.jobs.size).to eq(1)
84
- Sidekiq::Worker.clear_all
85
- expect(UntilExecutedJob.jobs.size).to eq(0)
86
- expect(AnotherUniqueJob.jobs.size).to eq(0)
87
- UntilExecutedJob.perform_async(param)
88
- expect(UntilExecutedJob.jobs.size).to eq(1)
89
- AnotherUniqueJob.perform_async(param)
90
- expect(AnotherUniqueJob.jobs.size).to eq(1)
91
- end
92
-
93
- it 'handles clearing all workers when there are no jobs' do
94
- param = 'work'
95
- UntilExecutedJob.perform_async(param)
96
- AnotherUniqueJob.perform_async(param)
97
- Sidekiq::Worker.clear_all
98
- expect(UntilExecutedJob.jobs.size).to eq(0)
99
- expect(AnotherUniqueJob.jobs.size).to eq(0)
100
- expect { Sidekiq::Worker.jobs.size }.not_to raise_error
101
- end
102
-
103
- it 'adds the unique_digest to the message' do
104
- param = 'hash'
105
- item = { 'class' => 'UntilExecutedJob', 'queue' => 'working', 'args' => [param] }
106
- hash = SidekiqUniqueJobs::UniqueArgs.digest(item)
107
- expect(UntilExecutedJob.perform_async(param)).to_not be_nil
108
- expect(UntilExecutedJob.jobs.size).to eq(1)
109
- expect(UntilExecutedJob.jobs.last['unique_digest']).to eq(hash)
110
- end
111
- end
112
-
113
- context 'with non-unique worker' do
114
- it 'pushes duplicates messages' do
115
- param = 'work'
116
- expect(MyJob.jobs.size).to eq(0)
117
- MyJob.perform_async(param)
118
- expect(MyJob.jobs.size).to eq(1)
119
- MyJob.perform_async(param)
120
- expect(MyJob.jobs.size).to eq(2)
121
- end
122
- end
123
- end
124
-
125
- describe 'when set to :inline!', sidekiq: :inline do
126
- it 'once the job is completed allows to run another one' do
127
- expect(TestClass).to receive(:run).with('test').twice
128
- InlineWorker.perform_async('test')
129
- InlineWorker.perform_async('test')
130
- end
131
-
132
- it 'if the unique is kept forever it does not allows to run the job again' do
133
- expect(TestClass).to receive(:run).with('args').once
134
-
135
- UntilGlobalTimeoutJob.perform_async('args')
136
- UntilGlobalTimeoutJob.perform_async('args')
137
- end
138
-
139
- describe 'when a job is set to run once in 10 minutes' do
140
- context 'when spammed' do
141
- it 'only allows 1 call per 10 minutes' do
142
- expect(TestClass).to receive(:run).with(1).once
143
- 100.times do
144
- UntilTimeoutJob.perform_async(1)
145
- end
146
- end
147
- end
148
-
149
- context 'with different arguments' do
150
- it 'only allows 1 call per 10 minutes' do
151
- expect(TestClass).to receive(:run).with(9).once
152
- 2.times do
153
- UntilTimeoutJob.perform_async(9)
154
- end
155
-
156
- expect(TestClass).to receive(:run).with(2).once
157
- 2.times do
158
- UntilTimeoutJob.perform_async(2)
159
- end
160
- end
161
- end
162
- end
163
- end
164
- end
@@ -1,75 +0,0 @@
1
- require 'spec_helper'
2
- require 'sidekiq/api'
3
- require 'sidekiq/worker'
4
- require 'sidekiq_unique_jobs/server/middleware'
5
- require 'sidekiq_unique_jobs/client/middleware'
6
- require 'sidekiq_unique_jobs/sidekiq_unique_ext'
7
-
8
- RSpec.describe 'Sidekiq::Api' do
9
- let(:item) do
10
- { 'class' => JustAWorker,
11
- 'queue' => 'testqueue',
12
- 'args' => [foo: 'bar'] }
13
- end
14
-
15
- def unique_key
16
- SidekiqUniqueJobs::UniqueArgs.digest(
17
- 'class' => JustAWorker,
18
- 'queue' => 'testqueue',
19
- 'args' => [foo: 'bar'],
20
- 'at' => (Date.today + 1).to_time.to_i,
21
- )
22
- end
23
-
24
- def schedule_job
25
- JustAWorker.perform_in(60 * 60 * 3, foo: 'bar')
26
- end
27
-
28
- def perform_async
29
- JustAWorker.perform_async(foo: 'bar')
30
- end
31
-
32
- describe Sidekiq::SortedEntry::UniqueExtension do
33
- it 'deletes uniqueness lock on delete' do
34
- expect(schedule_job).to be_truthy
35
-
36
- Sidekiq::ScheduledSet.new.each(&:delete)
37
- Sidekiq.redis do |c|
38
- expect(c.exists(unique_key)).to be_falsy
39
- end
40
-
41
- expect(schedule_job).to be_truthy
42
- end
43
- end
44
-
45
- describe Sidekiq::Job::UniqueExtension do
46
- it 'deletes uniqueness lock on delete' do
47
- jid = perform_async
48
- Sidekiq::Queue.new('testqueue').find_job(jid).delete
49
- Sidekiq.redis do |c|
50
- expect(c.exists(unique_key)).to be_falsy
51
- end
52
- expect(true).to be_truthy
53
- end
54
- end
55
-
56
- describe Sidekiq::Queue::UniqueExtension do
57
- it 'deletes uniqueness locks on clear' do
58
- perform_async
59
- Sidekiq::Queue.new('testqueue').clear
60
- Sidekiq.redis do |c|
61
- expect(c.exists(unique_key)).to be_falsy
62
- end
63
- end
64
- end
65
-
66
- describe Sidekiq::JobSet::UniqueExtension do
67
- it 'deletes uniqueness locks on clear' do
68
- schedule_job
69
- Sidekiq::JobSet.new('schedule').clear
70
- Sidekiq.redis do |c|
71
- expect(c.exists(unique_key)).to be_falsy
72
- end
73
- end
74
- end
75
- end
@@ -1,41 +0,0 @@
1
- require 'spec_helper'
2
- RSpec.describe SidekiqUniqueJobs do
3
- describe '.configure_middleware' do
4
- it 'configures both client and server middleware' do
5
- expect(described_class).to receive(:configure_server_middleware)
6
- expect(described_class).to receive(:configure_client_middleware)
7
-
8
- described_class.configure_middleware
9
- end
10
- end
11
-
12
- describe '.configure_server_middleware' do
13
- let(:server_config) { class_double(Sidekiq) }
14
- let(:server_middleware) { double(Sidekiq::Middleware::Chain) }
15
- let(:client_middleware) { double(Sidekiq::Middleware::Chain) }
16
-
17
- it 'adds client and server middleware when required' do
18
- expect(Sidekiq).to receive(:configure_server).and_yield(server_config)
19
-
20
- expect(server_config).to receive(:client_middleware).and_yield(client_middleware)
21
- expect(client_middleware).to receive(:add).with(SidekiqUniqueJobs::Client::Middleware)
22
-
23
- expect(server_config).to receive(:server_middleware).and_yield(server_middleware)
24
- expect(server_middleware).to receive(:add).with(SidekiqUniqueJobs::Server::Middleware)
25
- described_class.configure_server_middleware
26
- end
27
- end
28
-
29
- describe '.configure_client_middleware' do
30
- let(:client_config) { class_double(Sidekiq) }
31
- let(:client_middleware) { double(Sidekiq::Middleware::Chain) }
32
-
33
- it 'adds client middleware when required' do
34
- expect(Sidekiq).to receive(:configure_client).and_yield(client_config)
35
- expect(client_config).to receive(:client_middleware).and_yield(client_middleware)
36
- expect(client_middleware).to receive(:add).with(SidekiqUniqueJobs::Client::Middleware)
37
-
38
- described_class.configure_client_middleware
39
- end
40
- end
41
- end
@@ -1,70 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe SidekiqUniqueJobs::TimeoutCalculator do
4
- shared_context 'undefined worker class' do
5
- subject { described_class.new('class' => 'test') }
6
- end
7
-
8
- shared_context 'item not scheduled' do
9
- subject { described_class.new('class' => 'MyUniqueJob') }
10
- end
11
-
12
- describe 'public api' do
13
- subject { described_class.new(nil) }
14
- it { is_expected.to respond_to(:time_until_scheduled) }
15
- it { is_expected.to respond_to(:worker_class_queue_lock_expiration) }
16
- it { is_expected.to respond_to(:worker_class_run_lock_expiration) }
17
- it { is_expected.to respond_to(:worker_class) }
18
- it { is_expected.to respond_to(:seconds) }
19
- end
20
-
21
- describe '.for_item' do
22
- it 'initializes a new calculator' do
23
- expect(described_class).to receive(:new).with('WAT')
24
- described_class.for_item('WAT')
25
- end
26
- end
27
-
28
- describe '#time_until_scheduled' do
29
- it_behaves_like 'item not scheduled' do
30
- its(:time_until_scheduled) { is_expected.to eq(0) }
31
- end
32
-
33
- subject { described_class.new('class' => 'MyUniqueJob', 'at' => schedule_time) }
34
- let(:schedule_time) { Time.now.utc.to_i + 24 * 60 * 60 }
35
- let(:now_in_utc) { Time.now.utc.to_i }
36
-
37
- its(:time_until_scheduled) do
38
- Timecop.travel(Time.at(now_in_utc)) do
39
- is_expected.to be_within(1).of(schedule_time - now_in_utc)
40
- end
41
- end
42
- end
43
-
44
- describe '#worker_class_queue_lock_expiration' do
45
- it_behaves_like 'undefined worker class' do
46
- its(:worker_class_queue_lock_expiration) { is_expected.to eq(nil) }
47
- end
48
-
49
- subject { described_class.new('class' => 'MyUniqueJob') }
50
- its(:worker_class_queue_lock_expiration) { is_expected.to eq(7_200) }
51
- end
52
-
53
- describe '#worker_class_run_lock_expiration' do
54
- it_behaves_like 'undefined worker class' do
55
- its(:worker_class_run_lock_expiration) { is_expected.to eq(nil) }
56
- end
57
-
58
- subject { described_class.new('class' => 'LongRunningJob') }
59
- its(:worker_class_run_lock_expiration) { is_expected.to eq(7_200) }
60
- end
61
-
62
- describe '#worker_class' do
63
- it_behaves_like 'undefined worker class' do
64
- its(:worker_class) { is_expected.to eq('test') }
65
- end
66
-
67
- subject { described_class.new('class' => 'MyJob') }
68
- its(:worker_class) { is_expected.to eq(MyJob) }
69
- end
70
- end