Skip to content

Commit

Permalink
Fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
vankiru committed Jan 24, 2025
1 parent 9ce3f6a commit b58ff17
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 76 deletions.
8 changes: 4 additions & 4 deletions lib/nats/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
require_relative "service/stats"

# client = NATS.connect
#
#
# service = client.add_service(
# name: "name",
# version: "1.0.0",
Expand All @@ -27,7 +27,7 @@
#
# service.on_stats do |endpoint|
# end
#
#
# service.on_stop do |error|
# end
#
Expand All @@ -45,12 +45,12 @@ class Service
include MonitorMixin
include Extension

DEFAULT_QUEUE = "q".freeze
DEFAULT_QUEUE = "q"

attr_reader :client, :name, :id, :version, :description, :metadata, :queue
attr_reader :monitoring, :status, :callbacks, :groups, :endpoints

alias subject name
alias_method :subject, :name

def initialize(client, options)
super()
Expand Down
3 changes: 3 additions & 0 deletions lib/nats/service/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ class Service
class Error < StandardError; end

class InvalidNameError < Error; end

class InvalidVersionError < Error; end

class InvalidQueueError < Error; end

class InvalidSubjectError < Error; end
end
end
2 changes: 1 addition & 1 deletion lib/nats/service/monitoring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module NATS
class Service
class Monitoring
DEFAULT_PREFIX = "$SRV".freeze
DEFAULT_PREFIX = "$SRV"

VERBS = {
ping: "PING",
Expand Down
2 changes: 1 addition & 1 deletion lib/nats/service/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def error(error)
private

def to_nsec(seconds)
(seconds * 10 ** 9).to_i
(seconds * 10**9).to_i
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/service/callbacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

RSpec.describe NATS::Service::Callbacks do
subject { described_class.new(service) }

let(:service) { instance_double(NATS::Service) }

describe "#register" do
Expand Down
32 changes: 18 additions & 14 deletions spec/service/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
let(:options) { {} }
let(:block) { ->(msg) { msg.respond("bar") } }

after { service.stop }
after { service.stop }

describe "#initialize" do
it "sets service" do
Expand All @@ -45,63 +45,63 @@
end

context "when options[:subject] is present" do
let(:options) { { subject: "baz" } }
let(:options) { {subject: "baz"} }

it "builds subject based on options[:subject]" do
expect(subject.subject).to eq("foo.baz")
end
end

context "when options[:subject] is blank" do
let(:options) { { subject: nil } }
let(:options) { {subject: nil} }

it "builds subject based on endpoint name" do
expect(subject.subject).to eq("foo.bar")
end
end

context "when options[:subject] is invalid" do
let(:options) { { subject: ">baz" } }
let(:options) { {subject: ">baz"} }

it "raises InvalidSubjectError" do
expect { subject }.to raise_error(NATS::Service::InvalidSubjectError)
end
end

context "when options[:queue] is present" do
let(:options) { { queue: "qux" } }
let(:options) { {queue: "qux"} }

it "sets queue to options[:queue]" do
expect(subject.queue).to eq("qux")
end
end

context "when options[:queue] is blank" do
let(:options) { { queue: nil } }
let(:options) { {queue: nil} }

it "sets queue parent.queue" do
expect(subject.queue).to eq("queue")
end
end

context "when options[:queue] is invalid" do
let(:options) { { queue: ">qux" } }
let(:options) { {queue: ">qux"} }

it "raises InvalidQueueError" do
expect { subject }.to raise_error(NATS::Service::InvalidQueueError)
end
end

context "when options[:metadata] is present" do
let(:options) { { metadata: { foo: :bar } } }
let(:options) { {metadata: {foo: :bar}} }

it "sets metadata to options[:metadata]" do
expect(subject.metadata).to eq({ foo: :bar })
expect(subject.metadata).to eq({foo: :bar})
end
end

context "when options[:metadata] is blank" do
let(:options) { { metatada: nil } }
let(:options) { {metatada: nil} }

it "sets metadata to nil" do
expect(subject.metadata).to be_nil
Expand All @@ -118,14 +118,18 @@
describe "handler" do
let(:request) do
subject
client.request("foo.bar") rescue nil
begin
client.request("foo.bar")
rescue
nil
end
end

context "when there are no errors" do
it "executes endpoint" do
expect(request).to have_attributes(data: "bar")
end

it "does not record any errors" do
request

Expand All @@ -143,9 +147,9 @@
let(:block) { ->(msg) { raise "Endpoint Error" } }

it "responds with error" do
expect(request).to have_attributes(header: {"Nats-Service-Error"=>"Endpoint Error", "Nats-Service-Error-Code"=>"500"})
expect(request).to have_attributes(header: {"Nats-Service-Error" => "Endpoint Error", "Nats-Service-Error-Code" => "500"})
end

it "records error" do
request

Expand Down
18 changes: 9 additions & 9 deletions spec/service/monitoring_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
instance_double(NATS::Service::Status, basic: basic, info: info, stats: stats)
end

let(:basic) { { name: "foo", id: "bar", version: "1.0.0" } }
let(:info) { { **basic, description: "foo bar" } }
let(:stats) { { **basic, started: "2025-01-24T05:40:37Z" } }
let(:basic) { {name: "foo", id: "bar", version: "1.0.0"} }
let(:info) { {**basic, description: "foo bar"} }
let(:stats) { {**basic, started: "2025-01-24T05:40:37Z"} }

let(:client) { NATS.connect }

Expand All @@ -28,7 +28,7 @@
@server.kill_server
end

after { subject.stop }
after { subject.stop }

describe "#initialize" do
context "when prefix is specified" do
Expand All @@ -46,7 +46,7 @@
having_attributes(subject: "$FOO.INFO.foo.bar"),
having_attributes(subject: "$FOO.STATS"),
having_attributes(subject: "$FOO.STATS.foo"),
having_attributes(subject: "$FOO.STATS.foo.bar"),
having_attributes(subject: "$FOO.STATS.foo.bar")
)
end
end
Expand All @@ -66,13 +66,13 @@
having_attributes(subject: "$SRV.INFO.foo.bar"),
having_attributes(subject: "$SRV.STATS"),
having_attributes(subject: "$SRV.STATS.foo"),
having_attributes(subject: "$SRV.STATS.foo.bar"),
having_attributes(subject: "$SRV.STATS.foo.bar")
)
end
end

describe "PING" do
let(:ping_response) { { type: "io.nats.micro.v1.ping_response", **basic }.to_json }
let(:ping_response) { {type: "io.nats.micro.v1.ping_response", **basic}.to_json }

it "returns PING response" do
subject
Expand All @@ -84,7 +84,7 @@
end

describe "INFO" do
let(:info_response) { { type: "io.nats.micro.v1.info_response", **info }.to_json }
let(:info_response) { {type: "io.nats.micro.v1.info_response", **info}.to_json }

it "returns INFO response" do
subject
Expand All @@ -96,7 +96,7 @@
end

describe "STATS" do
let(:stats_response) { { type: "io.nats.micro.v1.stats_response", **stats }.to_json }
let(:stats_response) { {type: "io.nats.micro.v1.stats_response", **stats}.to_json }

it "returns STATS response" do
subject
Expand Down
40 changes: 20 additions & 20 deletions spec/service/status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ def endpoint(values)

let(:service) do
instance_double(
NATS::Service,
name: "foo",
NATS::Service,
name: "foo",
id: "bar",
version: "1.0.0",
description: "foo bar",
metadata: { foo: :bar },
metadata: {foo: :bar},
endpoints: endpoints,
callbacks: NATS::Service::Callbacks.new(nil)
)
Expand All @@ -35,7 +35,7 @@ def endpoint(values)
name: "foo",
id: "bar",
version: "1.0.0",
metadata: { foo: :bar }
metadata: {foo: :bar}
}
end

Expand Down Expand Up @@ -63,8 +63,8 @@ def endpoint(values)
context "when service has some endpoints" do
let(:endpoints) do
[
endpoint(name: "baz", subject: "foo.baz", queue: "q", metadata: { baz: :qux }),
endpoint(name: "qux", subject: "foo.qux", queue: "q", metadata: { qux: :baz })
endpoint(name: "baz", subject: "foo.baz", queue: "q", metadata: {baz: :qux}),
endpoint(name: "qux", subject: "foo.qux", queue: "q", metadata: {qux: :baz})
]
end

Expand All @@ -73,8 +73,8 @@ def endpoint(values)
**basic,
description: "foo bar",
endpoints: [
{ name: "baz", subject: "foo.baz", queue_group: "q", metadata: { baz: :qux } },
{ name: "qux", subject: "foo.qux", queue_group: "q", metadata: { qux: :baz } }
{name: "baz", subject: "foo.baz", queue_group: "q", metadata: {baz: :qux}},
{name: "qux", subject: "foo.qux", queue_group: "q", metadata: {qux: :baz}}
]
})
end
Expand Down Expand Up @@ -107,7 +107,7 @@ def endpoint(values)
name: "baz",
subject: "foo.baz",
queue: "q",
stats: {
stats: {
num_requests: 1,
processing_time: 300,
average_processing_time: 300,
Expand Down Expand Up @@ -136,9 +136,9 @@ def endpoint(values)
**basic,
started: "2025-01-25T07:45:10Z",
endpoints: [
{
name: "baz",
subject: "foo.baz",
{
name: "baz",
subject: "foo.baz",
queue_group: "q",
num_requests: 1,
processing_time: 300,
Expand All @@ -147,7 +147,7 @@ def endpoint(values)
last_error: nil,
data: nil
},
{
{
name: "qux",
subject: "foo.qux",
queue_group: "q",
Expand All @@ -169,7 +169,7 @@ def endpoint(values)
errors = endpoint.stats.num_errors
requests = endpoint.stats.num_requests

{ errors_rate: (errors.to_f / requests).round(2) }
{errors_rate: (errors.to_f / requests).round(2)}
end
end

Expand All @@ -178,18 +178,18 @@ def endpoint(values)
**basic,
started: "2025-01-25T07:45:10Z",
endpoints: [
{
name: "baz",
subject: "foo.baz",
{
name: "baz",
subject: "foo.baz",
queue_group: "q",
num_requests: 1,
processing_time: 300,
average_processing_time: 300,
num_errors: 0,
last_error: nil,
data: { errors_rate: 0 }
data: {errors_rate: 0}
},
{
{
name: "qux",
subject: "foo.qux",
queue_group: "q",
Expand All @@ -198,7 +198,7 @@ def endpoint(values)
average_processing_time: 500,
num_errors: 1,
last_error: "StandardError",
data: { errors_rate: 0.33 }
data: {errors_rate: 0.33}
}
]
})
Expand Down
Loading

0 comments on commit b58ff17

Please sign in to comment.