Module: RSpec::Goodies::Helpers::Sidekiq

Defined in:
lib/rspec/goodies/helpers/sidekiq.rb

Instance Method Summary collapse

Instance Method Details

#add_enqueued_sidekiq_job(worker_class:, args:, metadata: {}, jid: SecureRandom.hex, queue: :default) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rspec/goodies/helpers/sidekiq.rb', line 55

def add_enqueued_sidekiq_job(worker_class:, args:, metadata: {}, jid: SecureRandom.hex, queue: :default)
  payload = ::Sidekiq.dump_json(
    .merge(
      "jid" => jid,
      "class" => worker_class.to_s,
      "args" => args,
      "queue" => queue.to_s,
      "enqueued_at" => Time.current.to_f,
    ),
  )

  ::Sidekiq.redis do |redis|
    redis.sadd("queues", queue.to_s)
    redis.lpush("queue:#{queue}", payload)
  end

  payload
end

#add_in_progress_sidekiq_job(worker_class:, args:, jid: SecureRandom.hex) ⇒ Object

Simulate job being worked on a worker so that’s available via the Sidekiq::Workers.new API (based off Sidekiq source code)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rspec/goodies/helpers/sidekiq.rb', line 121

def add_in_progress_sidekiq_job(worker_class:, args:, jid: SecureRandom.hex)
  @sidekiq_job_in_progress_thread_id ||= 1000
  @sidekiq_job_in_progress_count ||= 0

  process_id = "foo:#{SecureRandom.hex}"
  job_data = ::Sidekiq.dump_json(
    "queue" => "default",
    "payload" => {
      "jid" => jid,
      "class" => worker_class.to_s,
      "args" => args,
    },
    "run_at" => Time.current.to_i,
  )
  process_data = ::Sidekiq.dump_json(
    "hostname" => "foo",
    "started_at" => Time.now.to_f,
    "queues" => ["default"],
  )

  ::Sidekiq.redis do |redis|
    redis.incr("busy")
    redis.sadd("processes", process_id)
    redis.hset(process_id, "info", process_data)
    redis.hset(process_id, "at", Time.current.to_f)
    redis.hset(process_id, "busy", @sidekiq_job_in_progress_count += 1)
    redis.hset("#{process_id}:work", @sidekiq_job_in_progress_thread_id += 1, job_data)
  end
end

#add_retry_sidekiq_job(worker_class:, args:, metadata: {}, jid: SecureRandom.hex, retry_count: 2, retry_at: 1.hour.from_now) ⇒ Object

Simulate job being added to retry queue (based off Sidekiq source code)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rspec/goodies/helpers/sidekiq.rb', line 75

def add_retry_sidekiq_job(
  worker_class:,
  args:,
  metadata: {},
  jid: SecureRandom.hex,
  retry_count: 2,
  retry_at: 1.hour.from_now
)
  payload = ::Sidekiq.dump_json(
    .merge(
      "jid" => jid,
      "class" => worker_class.to_s,
      "args" => args,
      "queue" => "default",
      "failed_at" => Time.now.to_f,
      "retry_count" => retry_count,
      "error_backtrace" => ["line1", "line2"],
    ),
  )

  ::Sidekiq.redis do |redis|
    redis.zadd("retry", retry_at.to_f.to_s, payload)
  end

  payload
end

#add_scheduled_sidekiq_job(worker_class:, args:, metadata: {}, jid: SecureRandom.hex) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rspec/goodies/helpers/sidekiq.rb', line 102

def add_scheduled_sidekiq_job(worker_class:, args:, metadata: {}, jid: SecureRandom.hex)
  payload = ::Sidekiq.dump_json(
    .merge(
      "jid" => jid,
      "class" => worker_class.to_s,
      "args" => args,
    ),
  )
  score = Time.now.to_f

  ::Sidekiq.redis do |redis|
    redis.zadd("schedule", score, payload)
  end

  payload
end

#disable_sidekiq_unique!Object


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rspec/goodies/helpers/sidekiq.rb', line 18

def disable_sidekiq_unique!
  ::Sidekiq.configure_server do |config|
    config.server_middleware do |chain|
      chain.remove(::Sidekiq::Enterprise::Unique::Server)
    end
  end

  ::Sidekiq.configure_client do |config|
    config.client_middleware do |chain|
      chain.remove(::Sidekiq::Enterprise::Unique::Client)
    end
  end
end

#perform_sidekiq_job(worker_class, *args) ⇒ Object

Simulate job being performed within the entire middleware stack


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rspec/goodies/helpers/sidekiq.rb', line 33

def perform_sidekiq_job(worker_class, *args)
  worker_class.perform_async(*args)

  actual_worker_class =
    if worker_class.is_a?(::Sidekiq::Worker::Setter)
      worker_class.instance_variable_get("@klass")
    else
      worker_class
    end

  actual_worker_class.process_job(actual_worker_class.jobs.last)
end

#process_sidekiq_payloads(payloads) ⇒ Object


46
47
48
49
50
51
52
53
# File 'lib/rspec/goodies/helpers/sidekiq.rb', line 46

def process_sidekiq_payloads(payloads)
  payloads.each do |payload|
    payload_hash = ::Sidekiq.load_json(payload)
    worker_class = Object.const_get(payload_hash["class"])

    worker_class.process_job(payload_hash)
  end
end

#within_sidekiq_uniqueObject

Helper to briefly enable unique jobs functionality Only use this if you’re explicitly testing unique job behavior otherwise it is very unpredictable and it’s best to disable it for normal tests


10
11
12
13
14
15
16
# File 'lib/rspec/goodies/helpers/sidekiq.rb', line 10

def within_sidekiq_unique
  ::Sidekiq::Enterprise.unique!

  yield
ensure
  disable_sidekiq_unique!
end