tubesock 0.2.5 → 0.2.6

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
  SHA1:
3
- metadata.gz: 5dcfa51a0e1492175493e56346c8a623ae7d3945
4
- data.tar.gz: 223777060aa74001df982d0c736349051060ba3a
3
+ metadata.gz: d841d7d707c6478a67478ceae2ed26d3b25b7b78
4
+ data.tar.gz: 21d0276fe0b599b83d1bcf5867bf5f2a280a3116
5
5
  SHA512:
6
- metadata.gz: 55dbb0c0136509521c4176a0f9cd546f77a36efbde56abd1bc253d6bb7054073e1bf65f45b483d26c87cd6dcdc9bbbf937f478d33aafa237b55f062e4c6f363b
7
- data.tar.gz: 095f6094b95b7bcf29aac098afc889baefe0b09339ed41d474a3b272dadf7d315ec243f0dc76d87d564be4e1c835e32bd97b3ed138d66ab2698ea1432af29a3f
6
+ metadata.gz: c3cfc1848b42e5d889a901a90279f123e9a53f2d87df6a86215cf7668e5644b7c17c39142ae51adafec517888dca0fa466344fbacfd9026aff7527aab37a4d98
7
+ data.tar.gz: 512157a89fb5f832e5b2b70cf16671cf96d8845048fa44307cf00de1131acc73b4340b02b5b1cd5ef7b3d2b8db6a0538b3cee21085a89b2fbb0b8020f7f78049
@@ -1,6 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
- - 2.0.0
5
- - 2.1.1
6
- - jruby-19mode
3
+ - "2.1.6"
4
+ - "2.2.2"
@@ -14,6 +14,11 @@ class Tubesock
14
14
  @open_handlers = []
15
15
  @message_handlers = []
16
16
  @close_handlers = []
17
+ @error_handlers = []
18
+
19
+ @close_on_error = true
20
+
21
+ @active = true
17
22
  end
18
23
 
19
24
  def self.hijack(env)
@@ -32,6 +37,10 @@ class Tubesock
32
37
  end
33
38
  end
34
39
 
40
+ def prevent_close_on_error
41
+ @close_on_error = false
42
+ end
43
+
35
44
  def send_data data, type = :text
36
45
  frame = WebSocket::Frame::Outgoing::Server.new(
37
46
  version: @version,
@@ -39,7 +48,7 @@ class Tubesock
39
48
  type: type
40
49
  )
41
50
  @socket.write frame.to_s
42
- rescue IOError, Errno::EPIPE
51
+ rescue IOError, Errno::EPIPE, Errno::ETIMEDOUT
43
52
  close
44
53
  end
45
54
 
@@ -55,20 +64,41 @@ class Tubesock
55
64
  @close_handlers << block
56
65
  end
57
66
 
67
+ def onerror(&block)
68
+ @error_handlers << block
69
+ end
70
+
58
71
  def listen
59
72
  keepalive
60
73
  Thread.new do
61
- Thread.current.abort_on_exception = true
62
- @open_handlers.each(&:call)
63
- each_frame do |data|
64
- @message_handlers.each{|h| h.call(data) }
74
+ begin
75
+ @open_handlers.each(&:call)
76
+ each_frame do |data|
77
+ @message_handlers.each do |h|
78
+ begin
79
+ h.call(data)
80
+ rescue => e
81
+ @error_handlers.each{|eh| eh.call(e,data)}
82
+ close if @close_on_error
83
+ end
84
+ end
85
+ end
86
+ ensure
87
+ close
65
88
  end
66
- close
67
89
  end
68
90
  end
69
91
 
70
92
  def close
93
+ return unless @active
94
+
71
95
  @close_handlers.each(&:call)
96
+ close!
97
+
98
+ @active = false
99
+ end
100
+
101
+ def close!
72
102
  if @socket.respond_to?(:closed?)
73
103
  @socket.close unless @socket.closed?
74
104
  else
@@ -1,3 +1,3 @@
1
1
  class Tubesock
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
@@ -54,4 +54,103 @@ class TubesockTest < Tubesock::TestCase
54
54
 
55
55
  closed.called.must_equal true
56
56
  end
57
+
58
+ def test_onerror_callback
59
+ interaction = TestInteraction.new
60
+
61
+ closed = MockProc.new
62
+ errored = MockProc.new
63
+
64
+ interaction.tubesock do |tubesock|
65
+ tubesock.onclose &closed
66
+
67
+ tubesock.onerror &errored
68
+ tubesock.onmessage do |message|
69
+ raise "Really really really bad error"
70
+ end
71
+ end
72
+
73
+ interaction.write "Hello world"
74
+ interaction.join
75
+
76
+ errored.called.must_equal true
77
+
78
+ end
79
+
80
+ def test_prevent_close_on_error
81
+ interaction = TestInteraction.new
82
+
83
+ closed = MockProc.new
84
+
85
+ interaction.tubesock do |tubesock|
86
+ tubesock.onclose &closed
87
+
88
+ tubesock.onerror do |error, message|
89
+ tubesock.prevent_close_on_error
90
+ end
91
+ tubesock.onmessage do |message|
92
+ raise "Really really really bad error"
93
+ end
94
+ end
95
+
96
+ interaction.write "Hello world"
97
+ closed.called.must_equal nil
98
+
99
+ interaction.close
100
+ end
101
+
102
+ def test_default_close_on_error
103
+ interaction = TestInteraction.new
104
+
105
+ closed = MockProc.new
106
+
107
+ interaction.tubesock do |tubesock|
108
+ tubesock.onclose &closed
109
+
110
+ tubesock.onmessage do |message|
111
+ raise "Really really really bad error"
112
+ end
113
+ end
114
+
115
+ interaction.write "Hello world"
116
+ interaction.join
117
+
118
+ closed.called.must_equal true
119
+ end
120
+
121
+ def test_multi_frame_no_close
122
+ interaction = TestInteraction.new
123
+
124
+ closed = MockProc.new
125
+
126
+ messaged = MockProc.new
127
+
128
+ interaction.tubesock do |tubesock|
129
+ tubesock.onclose &closed
130
+ tubesock.onmessage &messaged
131
+ end
132
+
133
+ interaction.write "Hello world"
134
+ sleep 1
135
+ closed.called.must_equal nil
136
+
137
+ interaction.close
138
+ end
139
+
140
+ def test_close_on_open_handler
141
+ interaction = TestInteraction.new
142
+
143
+ closed = MockProc.new
144
+
145
+ interaction.tubesock do |tubesock|
146
+ tubesock.onclose &closed
147
+ tubesock.onopen do
148
+ raise "Error opening socket"
149
+ end
150
+ end
151
+
152
+ proc {interaction.join}.must_raise RuntimeError
153
+ closed.called.must_equal true
154
+ end
155
+
57
156
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tubesock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Gauthier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-02 00:00:00.000000000 Z
11
+ date: 2016-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -148,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  version: '0'
149
149
  requirements: []
150
150
  rubyforge_project:
151
- rubygems_version: 2.4.5
151
+ rubygems_version: 2.6.1
152
152
  signing_key:
153
153
  specification_version: 4
154
154
  summary: Handle websocket connections via Rack and Rails 4 using concurrency