Skip to content

Commit 892307e

Browse files
committed
add specs
1 parent 8a6c035 commit 892307e

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
require "rails_helper"
2+
3+
module Outboxer
4+
module Models
5+
RSpec.describe Thread, type: :model do
6+
let(:now) { Time.now.utc }
7+
8+
describe ".update_message_counts_by!" do
9+
context "when the row does not exist (insert path)" do
10+
it "creates a new row with correct counters and timestamps" do
11+
Thread.update_message_counts_by!(
12+
queued_count: 1,
13+
publishing_count: 2,
14+
published_count: 3,
15+
failed_count: 4,
16+
current_utc_time: now
17+
)
18+
19+
thread = Thread.find_by!(
20+
hostname: ::Socket.gethostname,
21+
process_id: ::Process.pid,
22+
thread_id: ::Thread.current.object_id)
23+
24+
expect(thread.queued_count).to eq(1)
25+
expect(thread.publishing_count).to eq(2)
26+
expect(thread.published_count).to eq(3)
27+
expect(thread.failed_count).to eq(4)
28+
29+
expect(thread.queued_count_last_updated_at.to_i).to eq(now.to_i)
30+
expect(thread.publishing_count_last_updated_at.to_i).to eq(now.to_i)
31+
expect(thread.published_count_last_updated_at.to_i).to eq(now.to_i)
32+
expect(thread.failed_count_last_updated_at.to_i).to eq(now.to_i)
33+
34+
expect(thread.created_at.to_i).to eq(now.to_i)
35+
expect(thread.updated_at.to_i).to eq(now.to_i)
36+
end
37+
end
38+
39+
context "when the row already exists (update path)" do
40+
let!(:thread) do
41+
create(
42+
:outboxer_thread, :current,
43+
queued_count: 10,
44+
publishing_count: 20,
45+
published_count: 30,
46+
failed_count: 40,
47+
created_at: now - 60,
48+
updated_at: now - 60)
49+
end
50+
51+
it "atomically increments only the provided counters" do
52+
later = now + 30
53+
54+
Thread.update_message_counts_by!(
55+
queued_count: 5, failed_count: 2, current_utc_time: later)
56+
57+
thread.reload
58+
59+
expect(thread.queued_count).to eq(15)
60+
expect(thread.failed_count).to eq(42)
61+
62+
expect(thread.publishing_count).to eq(20)
63+
expect(thread.published_count).to eq(30)
64+
65+
expect(thread.queued_count_last_updated_at.to_i).to eq(later.to_i)
66+
expect(thread.failed_count_last_updated_at.to_i).to eq(later.to_i)
67+
expect(thread.updated_at.to_i).to eq(later.to_i)
68+
end
69+
end
70+
71+
context "when all deltas are zero" do
72+
let!(:thread) do
73+
create(:outboxer_thread, :current,
74+
queued_count: 1,
75+
publishing_count: 1,
76+
published_count: 1,
77+
failed_count: 1,
78+
created_at: now - 60,
79+
updated_at: now - 60
80+
)
81+
end
82+
83+
it "only updates updated_at" do
84+
later = now + 10
85+
86+
Thread.update_message_counts_by!(current_utc_time: later)
87+
88+
thread.reload
89+
90+
expect(thread.queued_count).to eq(1)
91+
expect(thread.publishing_count).to eq(1)
92+
expect(thread.published_count).to eq(1)
93+
expect(thread.failed_count).to eq(1)
94+
95+
expect(thread.updated_at.to_i).to eq(later.to_i)
96+
end
97+
end
98+
end
99+
end
100+
end
101+
end

0 commit comments

Comments
 (0)