Skip to content

Commit

Permalink
Merge pull request #2 from tamashii-io/release/0.2.0
Browse files Browse the repository at this point in the history
Release/0.2.0
  • Loading branch information
elct9620 authored Nov 20, 2017
2 parents 1d2fef0 + 244c050 commit a7aeb16
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 43 deletions.
8 changes: 5 additions & 3 deletions lib/tamashii/client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "tamashii/config"
require "tamashii/common"
require "tamashii/client/version"
require "tamashii/client/config"
Expand All @@ -7,12 +8,13 @@ module Client
autoload :Base, "tamashii/client/base"

def self.config(&block)
return Config.class_eval(&block) if block_given?
Config
@config ||= Config.new
return instance_exec(@config, &block) if block_given?
@config
end

def self.logger
@logger ||= Tamashii::Logger.new(Config.log_file).tap do |logger|
@logger ||= Tamashii::Logger.new(self.config.log_file).tap do |logger|
logger.progname = "WebSocket Client"
end
end
Expand Down
21 changes: 10 additions & 11 deletions lib/tamashii/client/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require 'websocket/driver'


require "tamashii/common"
require "tamashii/client/config"

module Tamashii
Expand All @@ -20,8 +19,8 @@ def logger
end

def initialize
entry_point_with_slash = Config.entry_point.start_with?("/") ? Config.entry_point : "/#{Config.entry_point}"
@url = "#{Config.use_ssl ? "wss" : "ws"}://#{Config.host}:#{Config.port}#{entry_point_with_slash}"
entry_point_with_slash = Tamashii::Client.config.entry_point.start_with?("/") ? Tamashii::Client.config.entry_point : "/#{Tamashii::Client.config.entry_point}"
@url = "#{Tamashii::Client.config.use_ssl ? "wss" : "ws"}://#{Tamashii::Client.config.host}:#{Tamashii::Client.config.port}#{entry_point_with_slash}"

@callbacks = {}

Expand Down Expand Up @@ -118,8 +117,8 @@ def kill_worker_thread
end

def wait_for_worker_thread
if !@thread.join(Config.closing_timeout)
logger.error "Unable to stop worker thread in #{Config.closing_timeout} second! Force kill the worker thread"
if !@thread.join(Tamashii::Client.config.closing_timeout)
logger.error "Unable to stop worker thread in #{Tamashii::Client.config.closing_timeout} second! Force kill the worker thread"
kill_worker_thread
end
end
Expand All @@ -131,15 +130,15 @@ def wakeup
end

def open_socket
Timeout::timeout(Config.opening_timeout) do
if Config.use_ssl
OpenSSL::SSL::SSLSocket.new(TCPSocket.new(Config.host, Config.port)).connect
Timeout::timeout(Tamashii::Client.config.opening_timeout) do
if Tamashii::Client.config.use_ssl
OpenSSL::SSL::SSLSocket.new(TCPSocket.new(Tamashii::Client.config.host, Tamashii::Client.config.port)).connect
else
TCPSocket.new(Config.host, Config.port)
TCPSocket.new(Tamashii::Client.config.host, Tamashii::Client.config.port)
end
end
rescue Timeout::Error => e
logger.error "Opening timeout after #{Config.opening_timeout} seconds"
logger.error "Opening timeout after #{Tamashii::Client.config.opening_timeout} seconds"
nil
rescue => e
nil
Expand All @@ -163,7 +162,7 @@ def open_socket_runner

def open_socket_async
if !closing? && !stopped?
@open_socket_task = Concurrent::ScheduledTask.execute(Config.opening_retry_interval, &method(:open_socket_runner))
@open_socket_task = Concurrent::ScheduledTask.execute(Tamashii::Client.config.opening_retry_interval, &method(:open_socket_runner))
else
logger.warn "Client is closing, no longer need to create socket"
end
Expand Down
21 changes: 11 additions & 10 deletions lib/tamashii/client/config.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
require 'tamashii/common'
module Tamashii
module Client
class Config < Tamashii::Config
register :log_file, STDOUT
class Config
include Tamashii::Configurable

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
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
23 changes: 12 additions & 11 deletions spec/tamashii/client/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@

context "use ssl" do
before do
allow(Tamashii::Client::Config).to receive(:use_ssl).and_return(true)
allow_any_instance_of(Tamashii::Client::Config).to receive(:use_ssl).and_return(true)
end
it "create the socket by called OpenSSL::SSL::SSLSocket with a TCP socket" do
expect(TCPSocket).to receive(:new).and_return(tcp_socket)
Expand All @@ -88,7 +88,7 @@

context "does not use ssl" do
before do
allow(Tamashii::Client::Config).to receive(:use_ssl).and_return(false)
allow_any_instance_of(Tamashii::Client::Config).to receive(:use_ssl).and_return(false)
end
it "create the socket by called OpenSSL::SSL::SSLSocket" do
expect(TCPSocket).to receive(:new).and_return(tcp_socket)
Expand All @@ -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,11 +376,10 @@ 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

it "calls server gone" do
expect(subject).to receive(:server_gone)
subject.read
end
end
end
Expand All @@ -389,7 +388,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 +399,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 +453,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
6 changes: 4 additions & 2 deletions spec/tamashii/client/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

RSpec.describe Tamashii::Client::Config do

describe ".log_file" do
subject { Tamashii::Client::Config.new }

describe "#log_file" do
it "default output to STDOUT" do
expect(subject.log_file).to eq(STDOUT)
end
end

describe ".log_level" do
describe "#log_level" do
it "default to DEBUG" do
expect(subject.log_level).to eq(Logger::DEBUG)
end
Expand Down
4 changes: 0 additions & 4 deletions spec/tamashii/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
expect(Tamashii::Client::VERSION).not_to be nil
end

it "can get config" do
expect(Tamashii::Client.config).to be(Tamashii::Client::Config)
end

it "can get logger" do
expect(Tamashii::Client.logger).to be_instance_of(Tamashii::Logger)
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

0 comments on commit a7aeb16

Please sign in to comment.