waithook 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5df3978af4bd4dc3330d86dd3a1948d082d1642882163066ec4e09d9b63245b2
4
- data.tar.gz: af869d9efc1604f24b3fd1455767f06d5af75d62c5a225611b87b450e21f4bc1
3
+ metadata.gz: c3df6454a557d19f38729b89f1874b43111ab2d367c9f6f018064ad012d3ff2a
4
+ data.tar.gz: f933b2e75524e3301afbe50c8e56bbb525cea7a5503a15071cfad2ab4bd7ff59
5
5
  SHA512:
6
- metadata.gz: f2aa1c1079788668a57c3483708aa5158f3fb2b9abdfa8fafb227027ed83c085d970b97d65068a34373cfcb5edcae0ffc481351b1ebc53ee726f7f707eaaadec
7
- data.tar.gz: 19c9797a9712c5df295515da8a5b299132ec2176364094d7b6d5f7ca96e502aa986e379a6be94fad05c5109114bc7b7423c9ce6550bf2e1d7d903b5209c7d5a5
6
+ metadata.gz: 8f6403f65b5bf735376deea1d9eb4157b66d87428e1c13717c71b80045bfa898487863e15d9a206c59575652cfaa6003dc14885746f1dcd22a2f6ebec03eac1a
7
+ data.tar.gz: b7f95a852f39039a4832f5a0f5889163e92fb855ade1b7e33a9c5b3ad5dbd0d587f393f8925156d5e8102f055eef0f7843e93be2f5122c1d9934bdbfca0ba70d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- waithook (0.4.0)
4
+ waithook (0.4.2)
5
5
  websocket (~> 1.2)
6
6
 
7
7
  GEM
data/example/example.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'looksee'
2
2
  require 'net/http'
3
3
 
4
- require './websocket-client'
4
+ require_relative '../lib/waithook/websocket_client'
5
5
 
6
6
  HOST = 'localhost'
7
7
  PORT = 3012
@@ -1,7 +1,7 @@
1
1
  require 'looksee'
2
2
  require 'excon'
3
3
 
4
- require './websocket-client'
4
+ require_relative '../lib/waithook/websocket_client'
5
5
 
6
6
  #HOST = 'localhost'
7
7
  #PORT = 3012
@@ -1,7 +1,7 @@
1
- require_relative './waithook'
1
+ require_relative '../lib/waithook'
2
2
  require 'excon'
3
3
 
4
- waithook = Waithook.subscribe('my-super-test', host: 'localhost', port: 3012)
4
+ waithook = Waithook.subscribe(path: 'my-super-test', host: 'localhost', port: 3012)
5
5
 
6
6
  Excon.post('http://localhost:3012/my-super-test', body: Time.now.to_s)
7
7
 
@@ -1,3 +1,3 @@
1
1
  class Waithook
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
@@ -79,7 +79,7 @@ class Waithook
79
79
  end
80
80
  end
81
81
 
82
- # Estabilish connection to server and send initial handshake
82
+ # Establish connection to server and send initial handshake
83
83
  def connect!
84
84
  logger.info "Connecting to #{@host} #{@port}"
85
85
 
@@ -118,7 +118,7 @@ class Waithook
118
118
  def _start_parser!
119
119
  @reader, @writter = IO.pipe
120
120
  @processing_thread = Thread.new do
121
- Thread.current.abort_on_exception = true
121
+ # Thread.current.abort_on_exception = true
122
122
  begin
123
123
  logger.debug "Start reading in thread"
124
124
  handshake_response = _wait_handshake_response
@@ -129,8 +129,14 @@ class Waithook
129
129
  _handshake_recieved!
130
130
  _wait_frames!
131
131
  rescue Object => error
132
+ # if got error - close socket and end thread
133
+ # @last_connection_error will be detected in #wait_message and raised to caller thread,
134
+ # so caller can decide to reconnect or do something else
135
+ @last_connection_error = error
136
+ close!(send_close: false, kill_processing_thread: false) rescue nil
137
+ @messages.close
138
+
132
139
  logger.error "#{error.class}: #{error.message}\n#{error.backtrace.join("\n")}"
133
- raise error
134
140
  end
135
141
  end
136
142
  end
@@ -150,7 +156,7 @@ class Waithook
150
156
  _send_frame(:text, payload)
151
157
  end
152
158
 
153
- # Wait for new message (thread safe, all waiting threads will recieve a message)
159
+ # Wait for new message (thread safe, all waiting threads will receive a message)
154
160
  # Message format [type, data], e.g. <tt>[:text, "hello world"]</tt>
155
161
  def wait_new_message
156
162
  waiter = Waiter.new
@@ -158,14 +164,27 @@ class Waithook
158
164
  waiter.wait
159
165
  end
160
166
 
161
- # Synchroniously waiting for new message. Not thread safe, only one thread will receive message
167
+ # Synchronously waiting for new message. Not thread safe, only one thread will receive message
162
168
  # Message format [type, data], e.g. <tt>[:text, "hello world"]</tt>
163
169
  def wait_message
164
- @messages.pop
170
+ begin
171
+ message = @messages.pop
172
+ if (!message || message.first == nil) && @messages.closed? && @last_connection_error
173
+ raise @last_connection_error
174
+ end
175
+
176
+ return message
177
+ rescue => error
178
+ if @last_connection_error
179
+ raise @last_connection_error
180
+ else
181
+ raise error
182
+ end
183
+ end
165
184
  end
166
185
 
167
186
  # Wait until server handshake recieved.
168
- # Once it's recieved - we can are listening for new messages
187
+ # Once it's received - we can are listening for new messages
169
188
  # and connection is ready for sending data
170
189
  def wait_handshake!
171
190
  return true if @handshake_received
@@ -240,16 +259,20 @@ class Waithook
240
259
  data.join("")
241
260
  end
242
261
 
262
+ def socket_open?
263
+ @socket && !@socket.closed?
264
+ end
265
+
243
266
  # Send <tt>:close</tt> message to socket and close socket connection
244
- def close!(options = {send_close: true})
267
+ def close!(send_close: true, kill_processing_thread: true)
245
268
  unless @is_open
246
269
  logger.info "Already closed"
247
270
  return false
248
271
  end
249
272
 
250
273
  logger.info "Disconnecting from #{@host} #{@port}"
251
- @processing_thread.kill
252
- _send_frame(:close) if options[:send_close]
274
+ @processing_thread.kill if kill_processing_thread
275
+ _send_frame(:close) if send_close
253
276
  @socket.close
254
277
  @is_open = false
255
278
 
data/lib/waithook.rb CHANGED
@@ -118,7 +118,11 @@ class Waithook
118
118
  client.ping_sender
119
119
  logger.debug("Exit ping sender thread")
120
120
  rescue => error
121
- logger.warn("Error in ping sender thread: #{error.message} (#{error.class})\n#{error.backtrace.join("\n")}")
121
+ if error.message == "closed stream" && !@client.socket_open?
122
+ logger.debug("Connection closed, stopping ping sender thread")
123
+ else
124
+ logger.warn("Error in ping sender thread: #{error.message} (#{error.class})\n#{error.backtrace.join("\n")}")
125
+ end
122
126
  end
123
127
  end
124
128
  end
@@ -96,4 +96,20 @@ describe "Waithook" do
96
96
 
97
97
  assert_includes(out, "Timeout::Error: execution expired")
98
98
  end
99
+
100
+ it "should throw exception when got socket error" do
101
+ waithook = default_client(logger: true)
102
+ socket = waithook.client.instance_variable_get(:@socket)
103
+ socket.close
104
+
105
+ captured_error = nil
106
+ begin
107
+ waithook.wait_message(raise_on_timeout: false)
108
+ rescue => error
109
+ captured_error = error
110
+ end
111
+
112
+ assert_instance_of(IOError, captured_error)
113
+ assert_equal("stream closed in another thread", captured_error.message)
114
+ end
99
115
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: waithook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Evstigneev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-22 00:00:00.000000000 Z
11
+ date: 2024-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket