Skip to content

Commit e173af9

Browse files
authored
F2: Test BatchPublisher (#598)
* Parameterize `PostPublisher` * Fix class reference * Drop unused variable * Test `BatchPublisher` * Rubocop
1 parent 064756e commit e173af9

File tree

4 files changed

+93
-8
lines changed

4 files changed

+93
-8
lines changed

app/services/batch_publisher.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
class BatchPublisher
55
include Logging
66

7-
attr_reader :posts, :freefeed_client
7+
attr_reader :posts, :freefeed_client, :publisher_class
88

9-
def initialize(posts:, freefeed_client:)
9+
def initialize(posts:, freefeed_client:, publisher_class: PostPublisher)
1010
@posts = posts
1111
@freefeed_client = freefeed_client
12+
@publisher_class = publisher_class
1213
end
1314

1415
def publish
15-
logger.info("publishing #{TextHelpers.pluralize(pending_posts.count, "posts")}")
16+
logger.info("publishing #{TextHelpers.pluralize(posts.count, "posts")}")
1617

1718
posts.each do |post|
1819
logger.info("publishing post: #{post.id}")
@@ -25,7 +26,7 @@ def publish
2526
def publish_post(post)
2627
post.with_lock do
2728
next unless post.reload.enqueued?
28-
PostPublisher.new(post: post, freefeed_client: freefeed_client).publish
29+
publisher_class.new(post: post, freefeed_client: freefeed_client).publish
2930
end
3031
end
3132
end

app/services/feed_processor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def initialize(feeds:)
1414
def perform
1515
feeds.each do |feed|
1616
Importer.new(feed).import
17-
Publisher.new(posts: feed.posts.pending, freefeed_client: build_freefeed_client).publish
17+
BatchPublisher.new(posts: feed.posts.pending, freefeed_client: build_freefeed_client).publish
1818

1919
# TBD: Handle errors
2020
end

spec/services/batch_publisher_spec.rb

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,87 @@
1-
require "rails_helper"
1+
RSpec.describe BatchPublisher do
2+
subject(:batch_publisher) do
3+
described_class.new(
4+
posts: [first_post, second_post],
5+
freefeed_client: freefeed_client,
6+
publisher_class: publisher_class
7+
)
8+
end
9+
10+
let(:freefeed_client) do
11+
Freefeed::Client.new(
12+
token: "TEST_TOKEN",
13+
base_url: "https://freefeed.test"
14+
)
15+
end
16+
17+
let(:publisher_class) { class_double(PostPublisher) }
18+
let(:publisher_instance) { instance_double(PostPublisher) }
19+
20+
let(:first_post) { create(:post, state: "enqueued", feed: feed) }
21+
let(:second_post) { create(:post, state: "enqueued", feed: feed) }
22+
let(:feed) { create(:feed) }
23+
24+
describe "#publish" do
25+
before do
26+
allow(publisher_class).to receive(:new)
27+
.with(post: anything, freefeed_client: freefeed_client)
28+
.and_return(publisher_instance)
29+
30+
allow(publisher_instance).to receive(:publish)
31+
end
32+
33+
context "when posts are enqueued" do
34+
it "attempts to publish all enqueued posts" do
35+
allow(publisher_class).to receive(:new)
36+
.with(post: first_post, freefeed_client: freefeed_client)
37+
.and_return(publisher_instance)
38+
39+
allow(publisher_class).to receive(:new)
40+
.with(post: second_post, freefeed_client: freefeed_client)
41+
.and_return(publisher_instance)
42+
43+
expect(publisher_instance).to receive(:publish).twice
44+
45+
batch_publisher.publish
46+
end
47+
end
48+
49+
context "when some posts are not enqueued" do
50+
let(:second_post) { create(:post, state: "draft", feed: feed) }
51+
52+
it "only publishes enqueued posts" do
53+
expect(publisher_class).to receive(:new)
54+
.with(post: first_post, freefeed_client: freefeed_client)
55+
.once
56+
.and_return(publisher_instance)
57+
58+
expect(publisher_instance).to receive(:publish).once
59+
60+
expect(publisher_class).not_to receive(:new)
61+
.with(post: second_post, freefeed_client: freefeed_client)
62+
63+
batch_publisher.publish
64+
end
65+
end
66+
67+
context "when post state changes during processing" do
68+
it "skips posts that are no longer enqueued" do
69+
allow(first_post).to receive(:reload).and_return(
70+
create(:post, state: "published", feed: feed)
71+
)
72+
73+
expect(publisher_class).not_to receive(:new)
74+
.with(post: first_post, freefeed_client: freefeed_client)
75+
76+
expect(publisher_class).to receive(:new)
77+
.with(post: second_post, freefeed_client: freefeed_client)
78+
.once
79+
.and_return(publisher_instance)
80+
81+
expect(publisher_instance).to receive(:publish).once
82+
83+
batch_publisher.publish
84+
end
85+
end
86+
end
87+
end

spec/services/post_publisher_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
RSpec.describe PostPublisher do
2-
subject(:service) { described_class.new(post: post, freefeed_client: freefeed_client) }
3-
42
let(:freefeed_client) do
53
Freefeed::Client.new(
64
token: "TEST_TOKEN",

0 commit comments

Comments
 (0)