spec/sidekiq/merger/merge_spec.rb in sidekiq-merger-0.0.8 vs spec/sidekiq/merger/merge_spec.rb in sidekiq-merger-0.0.9
- old
+ new
@@ -1,9 +1,10 @@
require "spec_helper"
describe Sidekiq::Merger::Merge do
- subject { described_class.new(worker_class, queue, "foo", redis: redis) }
+ subject { described_class.new(worker_class, queue, args, redis: redis) }
+ let(:args) { "foo" }
let(:redis) { Sidekiq::Merger::Redis.new }
let(:queue) { "queue" }
let(:now) { Time.now }
let(:execution_time) { now + 10.seconds }
let(:options) { { key: -> (args) { args.to_json } } }
@@ -59,10 +60,43 @@
expect(described_class).to receive(:new).with(worker_class, queue, anything, { redis: 1 })
described_class.initialize_with_args(worker_class, queue, anything, redis: 1)
end
end
+ describe ".merge_key" do
+ let(:args) { "foo" }
+ let(:options) { {} }
+ it "returns an empty string" do
+ expect(described_class.merge_key(worker_class, args)).to eq ""
+ end
+ context "string key" do
+ let(:options) { { key: "bar" } }
+ it "returns the string" do
+ expect(described_class.merge_key(worker_class, args)).to eq "bar"
+ end
+ end
+ context "other type key" do
+ let(:options) { { key: [1, 2, 3] } }
+ it "returns nil" do
+ expect(described_class.merge_key(worker_class, args)).to eq "[1,2,3]"
+ end
+ end
+ context "proc key" do
+ let(:args) { [1, 2, 3] }
+ let(:options) { { key: -> (args) { args[0].to_s } } }
+ it "returns the result of the proc" do
+ expect(described_class.merge_key(worker_class, args)).to eq "1"
+ end
+ context "non-string result" do
+ let(:options) { { key: -> (args) { args[0] } } }
+ it "returns nil" do
+ expect(described_class.merge_key(worker_class, args)).to eq "1"
+ end
+ end
+ end
+ end
+
describe "#add" do
it "adds the args in lazy merge" do
expect(redis).to receive(:push_message).with("name:queue:foo", [1, 2, 3], execution_time)
subject.add([1, 2, 3], execution_time)
end
@@ -87,10 +121,39 @@
expect(redis).to receive(:delete_message).with("name:queue:foo", [1, 2, 3])
subject.delete([1, 2, 3])
end
end
+ describe "#delete_all" do
+ before do
+ subject.add([1, 2, 3], execution_time)
+ subject.add([2, 3, 4], execution_time)
+ end
+ it "deletes all" do
+ expect {
+ subject.delete_all
+ }.to change { subject.size }.from(2).to(0)
+ end
+ end
+
describe "#size" do
+ before do
+ subject.add([1, 2, 3], execution_time)
+ subject.add([2, 3, 4], execution_time)
+ end
+ it "returns the size" do
+ expect(subject.size).to eq 2
+ end
+ end
+
+ describe "#all_args" do
+ before do
+ subject.add([1, 2, 3], execution_time)
+ subject.add([2, 3, 4], execution_time)
+ end
+ it "returns all args" do
+ expect(subject.all_args).to contain_exactly [1, 2, 3], [2, 3, 4]
+ end
end
describe "#flush" do
before do
subject.add([1, 2, 3], execution_time)