Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tamashii config #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/tamashii/client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "tamashii/hookable"
require "tamashii/config"
require "tamashii/common"
require "tamashii/client/version"
require "tamashii/client/config"
Expand All @@ -7,7 +9,7 @@ module Client
autoload :Base, "tamashii/client/base"

def self.config(&block)
return Config.class_eval(&block) if block_given?
return instance_exec(Config.instance, &block) if block_given?
Config
end

Expand All @@ -18,3 +20,7 @@ def self.logger
end
end
end

Tamashii::Hook.after(:config) do |config|
config.register(:client, Tamashii::Client.config)
end
35 changes: 26 additions & 9 deletions lib/tamashii/client/config.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
require 'tamashii/common'
module Tamashii
module Client
class Config < Tamashii::Config
register :log_file, STDOUT
class Config
class << self
def instance
@instance ||= Config.new
end

register :use_ssl, false
register :entry_point, ""
register :host, "localhost"
register :port, 3000
register :opening_timeout, 10
register :opening_retry_interval, 1
register :closing_timeout, 10
def respond_to_missing?(name, _all = false)
super
end

def method_missing(name, *args, &block)
return instance.send(name, *args, &block) if instance.respond_to?(name)
super
end
end

include Tamashii::Configurable

config :log_file, default: STDOUT

config :use_ssl, default: false
config :entry_point, default: ""
config :host, default: "localhost"
config :port, default: 3000
config :opening_timeout, default: 10
config :opening_retry_interval, default: 1
config :closing_timeout, default: 10

def log_level(level = nil)
return Client.logger.level if level.nil?
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
require "tamashii/client"

Tamashii::Client.config do
log_file Tempfile.new.path
log_file = Tempfile.new.path
end
14 changes: 8 additions & 6 deletions spec/tamashii/client/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@

context "when timeout is reach" do
before do
allow(Timeout).to receive(:timeout).and_raise(Timeout::Error)
allow(Timeout).to receive(:timeout).and_raise(Timeout::Error)
end

it "returns nil" do
expect(subject.open_socket).to be nil
end
end

context "when other error happens in the block" do
before do
allow(Timeout).to receive(:timeout) do
Expand Down Expand Up @@ -290,7 +290,7 @@ def write_head_instance
it "terminates the loop and call nio#close, does not call select anymore" do
expect(nio).not_to receive(:select)
expect(nio).to receive(:close)
subject.run
subject.run
end
end

Expand Down Expand Up @@ -376,7 +376,7 @@ def write_head_instance
before do
allow(driver).to receive(:parse).and_raise RuntimeError
end

it "calls server gone" do
expect(subject).to receive(:server_gone)
subject.read
Expand All @@ -389,7 +389,7 @@ def write_head_instance

describe "#server_gone" do
let(:socket_closed_callback) { proc { "socket closed" } }

before do
subject.instance_variable_set(:@driver, driver)
subject.instance_variable_set(:@io, io)
Expand All @@ -400,11 +400,13 @@ def write_head_instance
end

it "makes opened? become false" do
allow(subject).to receive(:open_socket_async).and_return(nil)
subject.server_gone
expect(subject.opened?).to be false
end

it "call the callback 'socket_closed'" do
allow(subject).to receive(:open_socket_async).and_return(nil)
subject.on(:socket_closed, &socket_closed_callback)
expect(socket_closed_callback).to receive(:call)
subject.server_gone
Expand Down Expand Up @@ -452,7 +454,7 @@ def write_head_instance

describe "#wakeup" do
it "call the nio#wakeup" do
expect(nio).to receive(:wakeup)
expect(nio).to receive(:wakeup)
subject.wakeup
end
end
Expand Down
3 changes: 2 additions & 1 deletion tamashii-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "websocket-driver"
spec.add_runtime_dependency "nio4r"
spec.add_runtime_dependency "logger-colors"
spec.add_runtime_dependency "tamashii-common"
spec.add_runtime_dependency "tamashii-config"
spec.add_runtime_dependency "tamashii-common", ">= 0.2"
spec.add_runtime_dependency "concurrent-ruby"

spec.add_development_dependency "bundler", "~> 1.13"
Expand Down