-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
286 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ def reset | |
@average_processing_time = 0 | ||
|
||
@num_errors = 0 | ||
@last_error = nil | ||
@last_error = "" | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
module NATS | ||
class Compability | ||
class ServiceApi | ||
class Service | ||
attr_reader :service, :client | ||
|
||
def initialize(client) | ||
@client = client | ||
end | ||
|
||
def start | ||
add_service | ||
add_endpoints | ||
end | ||
|
||
def stop | ||
@service.stop | ||
end | ||
|
||
private | ||
|
||
def add_service | ||
@service = client.add_service( | ||
name: "demo", | ||
version: "1.0.0", | ||
description: "demo service", | ||
metadata: {workload: "cpu"} | ||
) | ||
|
||
service.on_stats do |endpoint| | ||
{endpoint: endpoint.name} | ||
end | ||
end | ||
|
||
def add_endpoints | ||
group1 = service.add_group("g1") | ||
group2 = service.add_group("g2", queue: "group-queue") | ||
|
||
add_endpoint(service, "demo-default-queue", subject: "demo.default", metadata: {key: "value"}) | ||
add_endpoint(service, "demo-custom-queue", subject: "demo.default", queue: "endpoint-group") | ||
|
||
add_endpoint(group1, "g1-parent-queue", subject: "parent.queue") | ||
add_endpoint(group1, "g1-custom-queue", subject: "custom.queue", queue: "endpoint-group") | ||
|
||
add_endpoint(group2, "g2-parent-queue", subject: "parent.queue") | ||
add_endpoint(group2, "g2-custom-queue", subject: "custom.queue", queue: "endpoint-group") | ||
|
||
service.add_endpoint("faulty", subject: "faulty") do |msg| | ||
raise "handler error" | ||
end | ||
end | ||
|
||
def add_endpoint(parent, name, options) | ||
parent.add_endpoint(name, options) do |msg| | ||
msg.respond(msg.data) | ||
end | ||
end | ||
end | ||
|
||
def run | ||
service = Service.new(client) | ||
|
||
client.subscribe("tests.service.core.>") do |msg| | ||
if msg.data.empty? | ||
service.stop | ||
else | ||
service.start | ||
end | ||
|
||
msg.respond("") | ||
end | ||
end | ||
|
||
def client | ||
@client ||= NATS.connect | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe NATS::Msg do | ||
subject { described_class.new(options) } | ||
|
||
let(:options) do | ||
{ | ||
subject: "foo", | ||
reply: "bar", | ||
data: "foo.bar", | ||
header: {}, | ||
nc: nil, | ||
sub: nil | ||
} | ||
end | ||
|
||
describe "#respond_with_error" do | ||
let(:respond_with_error) { subject.respond_with_error(error) } | ||
|
||
before do | ||
allow(subject).to receive(:respond_msg) | ||
end | ||
|
||
let(:respond) do | ||
{ | ||
subject: "bar", | ||
header: { | ||
"Nats-Service-Error" => "error", | ||
"Nats-Service-Error-Code" => 500 | ||
}, | ||
reply: "", | ||
data: "" | ||
} | ||
end | ||
|
||
context "when argument is a string" do | ||
let(:error) { "error" } | ||
|
||
it "responds with service error message" do | ||
respond_with_error | ||
|
||
expect(subject).to have_received(:respond_msg).with have_attributes(respond) | ||
end | ||
end | ||
|
||
context "when argument is a hash" do | ||
let(:error) { {code: 503, description: "error", data: "data"} } | ||
|
||
let(:respond) do | ||
{ | ||
subject: "bar", | ||
header: { | ||
"Nats-Service-Error" => "error", | ||
"Nats-Service-Error-Code" => 503 | ||
}, | ||
reply: "", | ||
data: "data" | ||
} | ||
end | ||
|
||
it "responds with service error message" do | ||
respond_with_error | ||
|
||
expect(subject).to have_received(:respond_msg).with have_attributes(respond) | ||
end | ||
end | ||
|
||
context "when argument is an error" do | ||
let(:error) { StandardError.new("error") } | ||
|
||
it "responds with service error message" do | ||
respond_with_error | ||
|
||
expect(subject).to have_received(:respond_msg).with have_attributes(respond) | ||
end | ||
end | ||
|
||
context "when argument is a random object" do | ||
let(:error) { [1, 2, 3] } | ||
|
||
let(:respond) do | ||
{ | ||
subject: "bar", | ||
header: { | ||
"Nats-Service-Error" => "[1, 2, 3]", | ||
"Nats-Service-Error-Code" => 500 | ||
}, | ||
reply: "", | ||
data: "" | ||
} | ||
end | ||
|
||
it "responds with argument " do | ||
respond_with_error | ||
|
||
expect(subject).to have_received(:respond_msg).with have_attributes(respond) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.