From 31105144a555f4b59735c3cf30a659bb87634bf5 Mon Sep 17 00:00:00 2001 From: Jake Peterson Date: Thu, 6 May 2021 18:23:12 -0700 Subject: [PATCH] 100% tests again --- .gitignore | 1 + binance-ruby.gemspec | 2 +- lib/binance/websocket.rb | 19 +++++++---- spec/binance/websocket_spec.rb | 60 ++++++++++++++++++++++++++++++++++ spec/fixtures/kline.json | 24 ++++++++++++++ spec/spec_helper.rb | 16 ++++----- 6 files changed, 106 insertions(+), 16 deletions(-) create mode 100644 spec/binance/websocket_spec.rb create mode 100644 spec/fixtures/kline.json diff --git a/.gitignore b/.gitignore index 46c5961..4bc0a94 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ *.gem .idea/ +.byebug* diff --git a/binance-ruby.gemspec b/binance-ruby.gemspec index fae4240..35bb5c1 100644 --- a/binance-ruby.gemspec +++ b/binance-ruby.gemspec @@ -34,7 +34,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'timecop', '~> 0.9' spec.add_development_dependency "webmock", '~> 3.0' spec.add_development_dependency 'rake', '~> 12.0' - spec.add_development_dependency 'pry', '~> 0.11.3' + spec.add_development_dependency 'byebug', '~> 11.1.3' spec.add_development_dependency 'simplecov', '~> 0.20.0' spec.add_dependency 'activesupport', '>= 5.1.0' diff --git a/lib/binance/websocket.rb b/lib/binance/websocket.rb index f4fcb2e..ad3e4c1 100644 --- a/lib/binance/websocket.rb +++ b/lib/binance/websocket.rb @@ -1,5 +1,7 @@ module Binance class WebSocket < Faye::WebSocket::Client + class Error < StandardError; end + def initialize(on_open: nil, on_close: nil) super "wss://stream.binance.com:9443/stream", nil, ping: 180 @@ -78,12 +80,15 @@ def subscribe(streams) }.to_json) end - def unsubscribe(streams) - send({ - method: "UNSUBSCRIBE", - params: streams, - id: request_id, - }.to_json) - end + # Terminating socket connection achieves the same result. + # If you have a use-case for this, please create a GitHub issue. + # + # def unsubscribe(streams) + # send({ + # method: "UNSUBSCRIBE", + # params: streams, + # id: request_id, + # }.to_json) + # end end end diff --git a/spec/binance/websocket_spec.rb b/spec/binance/websocket_spec.rb new file mode 100644 index 0000000..17e372f --- /dev/null +++ b/spec/binance/websocket_spec.rb @@ -0,0 +1,60 @@ +require "spec_helper" + +RSpec.describe Binance::WebSocket do + let(:interval) { "1h" } + let(:json_string) { '{ "result": null, "id": 312 }' } + let(:stream_name) { nil } + let(:symbols) { %w(ETHBTC) } + let(:websocket) { Binance::WebSocket.new } + + describe "#candlesticks" do + let(:stream_name) { "#{symbols.first.downcase}@kline_#{interval}" } + + before do + stub_request(:any, "wss://stream.binance.com:9443/stream") + .to_return(status: 200, body: "") + events = OpenStruct.new( + open: nil, + message: nil, + close: nil, + ) + allow_any_instance_of(Binance::WebSocket).to receive(:on) do |_ws, kind, &block| + event = case kind + when :open + when :message + OpenStruct.new(data: json_string) + when :close + end + events.send("#{kind}=", -> { block.call(event) }) + end + allow_any_instance_of(Binance::WebSocket).to receive(:send) do + events.open&.call + events.message&.call + events.close&.call + end + end + + context "error" do + let(:json_string) { '{ "error": {"code": 0, "msg": "Unknown property","id": 123} }' } + + subject { websocket.candlesticks!(symbols, interval) } + + it { is_expected_block.to raise_error Binance::WebSocket::Error } + end + + context "kline" do + let(:json_string) do + { + stream: stream_name, + data: JSON.parse(File.read("spec/fixtures/kline.json"), symbolize_names: true), + }.to_json + end + + it "calls on_receive" do + inc = 0 + websocket.candlesticks!(symbols, interval) { inc = 1 } + expect(inc).to eq 1 + end + end + end +end diff --git a/spec/fixtures/kline.json b/spec/fixtures/kline.json new file mode 100644 index 0000000..4a481a5 --- /dev/null +++ b/spec/fixtures/kline.json @@ -0,0 +1,24 @@ +{ + "e": "kline", + "E": 123456789, + "s": "BNBBTC", + "k": { + "t": 123400000, + "T": 123460000, + "s": "BNBBTC", + "i": "1m", + "f": 100, + "L": 200, + "o": "0.0010", + "c": "0.0020", + "h": "0.0025", + "l": "0.0015", + "v": "1000", + "n": 100, + "x": false, + "q": "1.0000", + "V": "500", + "Q": "0.500", + "B": "123456" + } +} diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5644b2e..b29f517 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,18 +1,18 @@ # Needs to be called before app code is required! -require 'simplecov' +require "simplecov" SimpleCov.start -require 'dotenv/load' +require "dotenv/load" require "bundler/setup" -require 'webmock/rspec' -require 'codecov' -require 'timecop' -require 'pry' +require "webmock/rspec" +require "codecov" +require "timecop" +require "byebug" -SimpleCov.formatter = SimpleCov::Formatter::Codecov unless ENV['CODECOV_TOKEN'].nil? +SimpleCov.formatter = SimpleCov::Formatter::Codecov unless ENV["CODECOV_TOKEN"].nil? # Require support files -Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each { |file| require file } +Dir[File.expand_path(File.join(File.dirname(__FILE__), "support", "**", "*.rb"))].each { |file| require file } WebMock.disable_net_connect!