Skip to content

Commit 98f94da

Browse files
committed
Minor test suite reworks
Follows up #10 with a few minor reworks to the test suite. Prefer `job_args` over `args` as a variable name, and add a `job_args_to_row` helper for convenience.
1 parent 8f9b4da commit 98f94da

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

spec/client_spec.rb

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def transaction(&)
4040
yield
4141
end
4242

43-
def insert_params_to_jow_row(insert_params)
43+
private def insert_params_to_jow_row(insert_params)
4444
job = River::JobRow.new(
4545
id: (@next_id += 1),
4646
args: JSON.parse(insert_params.encoded_args),
@@ -120,15 +120,15 @@ class SimpleArgsWithInsertOpts < SimpleArgs
120120
end
121121

122122
it "inserts with job insert opts" do
123-
args = SimpleArgsWithInsertOpts.new(job_num: 1)
124-
args.insert_opts = River::InsertOpts.new(
123+
job_args = SimpleArgsWithInsertOpts.new(job_num: 1)
124+
job_args.insert_opts = River::InsertOpts.new(
125125
max_attempts: 23,
126126
priority: 2,
127127
queue: "job_custom_queue",
128128
tags: ["job_custom"]
129129
)
130130

131-
insert_res = client.insert(args)
131+
insert_res = client.insert(job_args)
132132
expect(insert_res.job).to have_attributes(
133133
max_attempts: 23,
134134
priority: 2,
@@ -140,15 +140,15 @@ class SimpleArgsWithInsertOpts < SimpleArgs
140140
it "inserts with insert opts" do
141141
# We set job insert opts in this spec too so that we can verify that the
142142
# options passed at insertion time take precedence.
143-
args = SimpleArgsWithInsertOpts.new(job_num: 1)
144-
args.insert_opts = River::InsertOpts.new(
143+
job_args = SimpleArgsWithInsertOpts.new(job_num: 1)
144+
job_args.insert_opts = River::InsertOpts.new(
145145
max_attempts: 23,
146146
priority: 2,
147147
queue: "job_custom_queue",
148148
tags: ["job_custom"]
149149
)
150150

151-
insert_res = client.insert(args, insert_opts: River::InsertOpts.new(
151+
insert_res = client.insert(job_args, insert_opts: River::InsertOpts.new(
152152
max_attempts: 17,
153153
priority: 3,
154154
queue: "my_queue",
@@ -208,26 +208,26 @@ def check_bigint_bounds(int)
208208
before { client.instance_variable_set(:@time_now_utc, -> { now }) }
209209

210210
it "inserts a new unique job with minimal options" do
211-
args = SimpleArgsWithInsertOpts.new(job_num: 1)
212-
args.insert_opts = River::InsertOpts.new(
211+
job_args = SimpleArgsWithInsertOpts.new(job_num: 1)
212+
job_args.insert_opts = River::InsertOpts.new(
213213
unique_opts: River::UniqueOpts.new(
214214
by_queue: true
215215
)
216216
)
217217

218-
insert_res = client.insert(args)
218+
insert_res = client.insert(job_args)
219219
expect(insert_res.job).to_not be_nil
220220
expect(insert_res.unique_skipped_as_duplicated).to be false
221221

222-
lock_str = "unique_keykind=#{args.kind}" \
222+
lock_str = "unique_keykind=#{job_args.kind}" \
223223
"&queue=#{River::QUEUE_DEFAULT}" \
224224
"&state=#{River::Client.const_get(:DEFAULT_UNIQUE_STATES).join(",")}"
225225
expect(mock_driver.advisory_lock_calls).to eq([check_bigint_bounds(client.send(:uint64_to_int64, Fnv::Hash.fnv_1(lock_str, size: 64)))])
226226
end
227227

228228
it "inserts a new unique job with all options" do
229-
args = SimpleArgsWithInsertOpts.new(job_num: 1)
230-
args.insert_opts = River::InsertOpts.new(
229+
job_args = SimpleArgsWithInsertOpts.new(job_num: 1)
230+
job_args.insert_opts = River::InsertOpts.new(
231231
unique_opts: River::UniqueOpts.new(
232232
by_args: true,
233233
by_period: 15 * 60,
@@ -236,11 +236,11 @@ def check_bigint_bounds(int)
236236
)
237237
)
238238

239-
insert_res = client.insert(args)
239+
insert_res = client.insert(job_args)
240240
expect(insert_res.job).to_not be_nil
241241
expect(insert_res.unique_skipped_as_duplicated).to be false
242242

243-
lock_str = "unique_keykind=#{args.kind}" \
243+
lock_str = "unique_keykind=#{job_args.kind}" \
244244
"&args=#{JSON.dump({job_num: 1})}" \
245245
"&period=#{client.send(:truncate_time, now, 15 * 60).utc.strftime("%FT%TZ")}" \
246246
"&queue=#{River::QUEUE_DEFAULT}" \
@@ -251,18 +251,18 @@ def check_bigint_bounds(int)
251251
it "inserts a new unique job with advisory lock prefix" do
252252
client = River::Client.new(mock_driver, advisory_lock_prefix: 123456)
253253

254-
args = SimpleArgsWithInsertOpts.new(job_num: 1)
255-
args.insert_opts = River::InsertOpts.new(
254+
job_args = SimpleArgsWithInsertOpts.new(job_num: 1)
255+
job_args.insert_opts = River::InsertOpts.new(
256256
unique_opts: River::UniqueOpts.new(
257257
by_queue: true
258258
)
259259
)
260260

261-
insert_res = client.insert(args)
261+
insert_res = client.insert(job_args)
262262
expect(insert_res.job).to_not be_nil
263263
expect(insert_res.unique_skipped_as_duplicated).to be false
264264

265-
lock_str = "unique_keykind=#{args.kind}" \
265+
lock_str = "unique_keykind=#{job_args.kind}" \
266266
"&queue=#{River::QUEUE_DEFAULT}" \
267267
"&state=#{River::Client.const_get(:DEFAULT_UNIQUE_STATES).join(",")}"
268268
expect(mock_driver.advisory_lock_calls).to eq([check_bigint_bounds(client.send(:uint64_to_int64, 123456 << 32 | Fnv::Hash.fnv_1(lock_str, size: 32)))])
@@ -271,9 +271,13 @@ def check_bigint_bounds(int)
271271
expect(lock_key >> 32).to eq(123456)
272272
end
273273

274+
def job_args_to_row(job_args, insert_opts: River::InsertOpts.new)
275+
mock_driver.send(:insert_params_to_jow_row, client.send(:make_insert_params, job_args, insert_opts)[0])
276+
end
277+
274278
it "gets an existing unique job" do
275-
args = SimpleArgsWithInsertOpts.new(job_num: 1)
276-
args.insert_opts = River::InsertOpts.new(
279+
job_args = SimpleArgsWithInsertOpts.new(job_num: 1)
280+
job_args.insert_opts = River::InsertOpts.new(
277281
unique_opts: River::UniqueOpts.new(
278282
by_args: true,
279283
by_period: 15 * 60,
@@ -282,16 +286,16 @@ def check_bigint_bounds(int)
282286
)
283287
)
284288

285-
job = mock_driver.insert_params_to_jow_row(client.send(:make_insert_params, args, River::InsertOpts.new)[0])
289+
job = job_args_to_row(job_args)
286290
mock_driver.job_get_by_kind_and_unique_properties_returns << job
287291

288-
insert_res = client.insert(args)
292+
insert_res = client.insert(job_args)
289293
expect(insert_res).to have_attributes(
290294
job: job,
291295
unique_skipped_as_duplicated: true
292296
)
293297

294-
lock_str = "unique_keykind=#{args.kind}" \
298+
lock_str = "unique_keykind=#{job_args.kind}" \
295299
"&args=#{JSON.dump({job_num: 1})}" \
296300
"&period=#{client.send(:truncate_time, now, 15 * 60).utc.strftime("%FT%TZ")}" \
297301
"&queue=#{River::QUEUE_DEFAULT}" \
@@ -300,12 +304,12 @@ def check_bigint_bounds(int)
300304
end
301305

302306
it "skips unique check if unique opts empty" do
303-
args = SimpleArgsWithInsertOpts.new(job_num: 1)
304-
args.insert_opts = River::InsertOpts.new(
307+
job_args = SimpleArgsWithInsertOpts.new(job_num: 1)
308+
job_args.insert_opts = River::InsertOpts.new(
305309
unique_opts: River::UniqueOpts.new
306310
)
307311

308-
insert_res = client.insert(args)
312+
insert_res = client.insert(job_args)
309313
expect(insert_res.job).to_not be_nil
310314
expect(insert_res.unique_skipped_as_duplicated).to be false
311315
end
@@ -472,19 +476,19 @@ def check_bigint_bounds(int)
472476

473477
RSpec.describe River::InsertManyParams do
474478
it "initializes" do
475-
args = SimpleArgs.new(job_num: 1)
479+
job_args = SimpleArgs.new(job_num: 1)
476480

477-
params = River::InsertManyParams.new(args)
478-
expect(params.args).to eq(args)
481+
params = River::InsertManyParams.new(job_args)
482+
expect(params.args).to eq(job_args)
479483
expect(params.insert_opts).to be_nil
480484
end
481485

482486
it "initializes with insert opts" do
483-
args = SimpleArgs.new(job_num: 1)
487+
job_args = SimpleArgs.new(job_num: 1)
484488
insert_opts = River::InsertOpts.new(queue: "other")
485489

486-
params = River::InsertManyParams.new(args, insert_opts: insert_opts)
487-
expect(params.args).to eq(args)
490+
params = River::InsertManyParams.new(job_args, insert_opts: insert_opts)
491+
expect(params.args).to eq(job_args)
488492
expect(params.insert_opts).to eq(insert_opts)
489493
end
490494
end

0 commit comments

Comments
 (0)