Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
Cover deliverer
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulik committed Feb 10, 2023
1 parent 38d1fec commit 616ecaa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
4 changes: 0 additions & 4 deletions lib/nats_streamer/deliverer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ class NatsStreamer::Deliverer
include NatsStreamer::Logger
include Memery

# TODO: how to propely unsubscribe?
option :subscriber

def deliver(**)
info_measure(-> { connection.post(".", **) }) { "Event delivered to #{subscriber.name}: #{_1.round(2)}s" }
rescue StandardError => e
warn { "Event failed to be delivered to #{subscriber.name}" }
warn { e }
end

memoize def connection = NatsStreamer::Connection.build(subscriber.url)
Expand Down
36 changes: 36 additions & 0 deletions spec/nats_streamer/deliverer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

RSpec.describe NatsStreamer::Deliverer do
let(:deliverer) { described_class.new(subscriber:) }
let(:subscriber) { instance_double(NatsStreamer::Config::Subscriber, url: "https://example.com", name: "test") }

describe "#deliver" do
subject { deliverer.deliver(test: "value") }

context "when delivered successfully" do
let!(:stub) do
stub_request(:post, "https://example.com/")
.with(body: { test: "value" })
.to_return(status: 200)
end

it "calls url" do
subject
expect(stub).to have_been_requested
end
end

context "when delivery errored" do
let!(:stub) do
stub_request(:post, "https://example.com/")
.with(body: { test: "value" })
.to_return(status: 500)
end

it "raises Faraday::ServerError" do # rubocop:disable RSpec/MultipleExpectations
expect { subject }.to raise_error(Faraday::ServerError)
expect(stub).to have_been_requested
end
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

ENV["CONSOLE_LEVEL"] ||= "fatal"
require "webmock/rspec"
require "simplecov"

SimpleCov.start
Expand Down

0 comments on commit 616ecaa

Please sign in to comment.