onstomp 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -65,30 +65,6 @@ module OnStomp::Connections
65
65
  end
66
66
  end
67
67
 
68
- describe ".duration_since_transmitted" do
69
- it "should be nil if last_transmitted_at is nil" do
70
- connection.stub(:last_transmitted_at => nil)
71
- connection.duration_since_transmitted.should be_nil
72
- end
73
- it "should be the difference between now and the last_transmitted_at in milliseconds" do
74
- Time.stub(:now => 10)
75
- connection.stub(:last_transmitted_at => 8.5)
76
- connection.duration_since_transmitted.should == 1500
77
- end
78
- end
79
-
80
- describe ".duration_since_received" do
81
- it "should be nil if last_received_at is nil" do
82
- connection.stub(:last_received_at => nil)
83
- connection.duration_since_received.should be_nil
84
- end
85
- it "should be the difference between now and the last_received_at in milliseconds" do
86
- Time.stub(:now => 10)
87
- connection.stub(:last_received_at => 6)
88
- connection.duration_since_received.should == 4000
89
- end
90
- end
91
-
92
68
  describe ".client_pulse?" do
93
69
  it "should be true if client heartbeating is disabled" do
94
70
  connection.stub(:heartbeat_client_limit => 0)
@@ -24,12 +24,12 @@ module OnStomp
24
24
  let(:stomp_1_0_class) { mock('stomp 1.0 class') }
25
25
  let(:stomp_1_1_class) { mock('stomp 1.1 class') }
26
26
  let(:stomp_1_0) {
27
- mock('stomp 1.0', :is_a? => false).tap do |m|
27
+ mock('stomp 1.0', :is_a? => false, :read_timeout => 30, :write_timeout => 20).tap do |m|
28
28
  m.stub(:is_a?).with(stomp_1_0_class).and_return(true)
29
29
  end
30
30
  }
31
31
  let(:stomp_1_1) {
32
- mock('stomp 1.1', :is_a? => false).tap do |m|
32
+ mock('stomp 1.1', :is_a? => false, :read_timeout => 30, :write_timeout => 20).tap do |m|
33
33
  m.stub(:is_a?).with(stomp_1_1_class).and_return(true)
34
34
  end
35
35
  }
@@ -77,14 +77,20 @@ module OnStomp
77
77
  it "should create a 1.0 connection" do
78
78
  stomp_1_0.should_receive(:connect).and_return(['1.0', connected_frame])
79
79
  stomp_1_0.should_receive(:configure).with(connected_frame, pend_events)
80
- Connections.connect client, user_headers, connect_headers, pend_events
80
+ stomp_1_0.should_receive(:read_timeout=).with(5)
81
+ stomp_1_0.should_receive(:write_timeout=).with(10)
82
+ Connections.connect client, user_headers, connect_headers, pend_events, 5, 10
81
83
  end
82
84
  it "should create a 1.1 connection" do
83
85
  stomp_1_0.should_receive(:connect).and_return(['1.1', connected_frame])
84
86
  stomp_1_0.should_receive(:socket).and_return(tcp_socket)
87
+ stomp_1_0.should_receive(:read_timeout=).with(10)
88
+ stomp_1_0.should_receive(:write_timeout=).with(5)
85
89
  stomp_1_1_class.should_receive(:new).with(tcp_socket, client).and_return(stomp_1_1)
86
90
  stomp_1_1.should_receive(:configure).with(connected_frame, pend_events)
87
- Connections.connect client, user_headers, connect_headers, pend_events
91
+ stomp_1_1.should_receive(:read_timeout=).with(30)
92
+ stomp_1_1.should_receive(:write_timeout=).with(20)
93
+ Connections.connect client, user_headers, connect_headers, pend_events, 10, 5
88
94
  end
89
95
  end
90
96
 
@@ -105,7 +111,9 @@ module OnStomp
105
111
  ssl_context.should_receive(:"#{k}=").with(v)
106
112
  end
107
113
  ssl_socket.should_receive(:post_connection_check).with(ssl_options[:post_connection_check])
108
- Connections.connect client, user_headers, connect_headers, pend_events
114
+ stomp_1_0.should_receive(:read_timeout=).with(5)
115
+ stomp_1_0.should_receive(:write_timeout=).with(10)
116
+ Connections.connect client, user_headers, connect_headers, pend_events, 5, 10
109
117
  end
110
118
  it "should create a 1.1 connection" do
111
119
  client.stub(:ssl => nil)
@@ -117,9 +125,13 @@ module OnStomp
117
125
  end
118
126
  ssl_socket.should_receive(:post_connection_check).with(client_uri.host)
119
127
  stomp_1_0.should_receive(:socket).and_return(ssl_socket)
128
+ stomp_1_0.should_receive(:read_timeout=).with(10)
129
+ stomp_1_0.should_receive(:write_timeout=).with(5)
120
130
  stomp_1_1_class.should_receive(:new).with(ssl_socket, client).and_return(stomp_1_1)
121
131
  stomp_1_1.should_receive(:configure).with(connected_frame, pend_events)
122
- Connections.connect client, user_headers, connect_headers, pend_events
132
+ stomp_1_1.should_receive(:read_timeout=).with(30)
133
+ stomp_1_1.should_receive(:write_timeout=).with(20)
134
+ Connections.connect client, user_headers, connect_headers, pend_events, 10, 5
123
135
  end
124
136
  end
125
137
 
@@ -130,7 +142,7 @@ module OnStomp
130
142
  Connections.stub(:negotiate_connection).and_raise OnStomp::OnStompError
131
143
  stomp_1_0.should_receive(:close).with(true)
132
144
  lambda {
133
- Connections.connect client, user_headers, connect_headers, pend_events
145
+ Connections.connect client, user_headers, connect_headers, pend_events, 5, 10
134
146
  }.should raise_error(OnStomp::OnStompError)
135
147
  end
136
148
  end
@@ -31,76 +31,92 @@ describe OnStomp::Failover, "full stack test", :fullstack => true, :failover =>
31
31
  #
32
32
  it "should failover" do
33
33
  # Occasionally generates: ["CONNECT", "DISCONNECT", "BEGIN", "SEND", "SUBSCRIBE", "SEND", "COMMIT"]
34
- committed = false
35
34
  brokers.first.kill_on_command 'SUBSCRIBE'
36
- killed = false
35
+ brokers.last.accept_delay = 1
37
36
 
38
37
  client = OnStomp::Failover::Client.new('failover:(stomp://localhost:10102,stomp://localhost:10103)')
39
38
  client.on_subscribe do |s, rc|
40
39
  # This frame will almost always get written to the first broker before the hangup occurs
41
- client.send '/queue/onstomp/failover/test', 'are you receiving?',
42
- :'x-onstomp-real-client' => rc.uri
40
+ client.send '/queue/onstomp/failover/test', 'are you receiving?'
43
41
  end
44
42
 
45
43
  client.connect
46
- client.send '/queue/onstomp/failover/test', '4-3-2-1 Earth Below Me',
47
- :'x-onstomp-real-client' => client.active_client.uri
48
- client.begin 't-1234', :'x-onstomp-real-client' => client.active_client.uri
44
+ client.send '/queue/onstomp/failover/test', '4-3-2-1 Earth Below Me'
45
+ client.begin 't-1234'
49
46
  client.send '/queue/onstomp/failover/test', 'hello major tom',
50
- :transaction => 't-1234', :'x-onstomp-real-client' => client.active_client.uri
51
- client.subscribe('/queue/onstomp/failover/test', :'x-onstomp-real-client' => client.active_client.uri) do |m|
47
+ :transaction => 't-1234'
48
+ client.subscribe('/queue/onstomp/failover/test') do |m|
52
49
  end
53
50
  Thread.pass while client.connected?
54
51
 
55
- client.send '/queue/onstomp/failover/test', 'Are you receiving?',
56
- :'x-onstomp-real-client' => client.active_client.uri
57
- sub = client.subscribe('/queue/onstomp/failover/test2', :'x-onstomp-real-client' => client.active_client.uri) do |m|
52
+ client.send '/queue/onstomp/failover/test', 'Are you receiving?'
53
+ sub = client.subscribe('/queue/onstomp/failover/test2') do |m|
58
54
  end
59
- client.unsubscribe sub, :'x-onstomp-real-client' => client.active_client.uri
60
- client.commit 't-1234', :'x-onstomp-real-client' => client.active_client.uri
61
- #Thread.pass until client.connected?
62
- client.disconnect :'x-onstomp-real-client' => client.active_client.uri
55
+ client.unsubscribe sub
56
+ client.commit 't-1234'
57
+
58
+ # Need to ensure that the DISCONNECT frame was actually received by
59
+ # the broker
60
+ client.disconnect :receipt => 'rcpt-disconnect'
63
61
  brokers.each(&:join)
64
62
  brokers.first.frames_received.map(&:command).should == ["CONNECT", "SEND", "BEGIN", "SEND", "SUBSCRIBE"]
63
+ # The reason there is only one SUBSCRIBE and no UNSUBSCRIBE is that
64
+ # the pair will cancel out before the connection has been re-established.
65
65
  brokers.last.frames_received.map(&:command).should == ["CONNECT", "BEGIN", "SEND", "SUBSCRIBE", "SEND", "COMMIT", "SEND", "DISCONNECT"]
66
66
  end
67
67
  end
68
+
68
69
  describe "failing over with a Receipts buffer" do
69
70
  it "should failover" do
70
- committed = false
71
- killed = false
72
71
  brokers.first.kill_on_command 'SUBSCRIBE'
72
+ brokers.last.accept_delay = 1
73
73
 
74
74
  client = OnStomp::Failover::Client.new('failover:(stomp://localhost:10102,stomp://localhost:10103)',
75
75
  :buffer => OnStomp::Failover::Buffers::Receipts)
76
- client.on_subscribe do |s, rc|
76
+ check_for_maybes = []
77
+ client.after_transmitting do |f, c, *_|
78
+ #puts "Sending: #{f.command} to #{c.uri}"
79
+ if c.uri.to_s == 'stomp://localhost:10102' && f.headers?(:receipt,:may_be_receipted)
80
+ check_for_maybes << f
81
+ end
82
+ end
83
+ client.on_receipt do |r, c, *_|
84
+ if c.uri.to_s == 'stomp://localhost:10102'
85
+ check_for_maybes.reject! { |f| f[:receipt] == r[:'receipt-id'] }
86
+ end
87
+ end
88
+ client.on_subscribe do |s, *_|
77
89
  # This frame will get written, but the broker will hang up before
78
90
  # a RECEIPT is given, so it will get repeated
79
- client.send '/queue/onstomp/failover/test', 'are you receiving?',
80
- :'x-onstomp-real-client' => rc.uri
91
+ client.send '/queue/onstomp/failover/test', 'are you receiving?'
81
92
  end
82
93
 
83
94
  client.connect
84
95
  client.send '/queue/onstomp/failover/test', '4-3-2-1 Earth Below Me',
85
- :'x-onstomp-real-client' => client.active_client.uri
86
- client.begin 't-1234', :'x-onstomp-real-client' => client.active_client.uri
96
+ :may_be_receipted => '1'
97
+ client.begin 't-1234'
87
98
  client.send '/queue/onstomp/failover/test', 'hello major tom',
88
- :transaction => 't-1234', :'x-onstomp-real-client' => client.active_client.uri
89
- client.subscribe('/queue/onstomp/failover/test', :'x-onstomp-real-client' => client.active_client.uri) do |m|
99
+ :transaction => 't-1234'
100
+ client.subscribe('/queue/onstomp/failover/test') do |m|
90
101
  end
91
102
  Thread.pass while client.connected?
92
103
 
93
- client.send '/queue/onstomp/failover/test', 'Are you receiving?',
94
- :'x-onstomp-real-client' => client.active_client.uri
95
- sub = client.subscribe('/queue/onstomp/failover/test2', :'x-onstomp-real-client' => client.active_client.uri) do |m|
104
+ client.send '/queue/onstomp/failover/test', 'Are you receiving?'
105
+ sub = client.subscribe('/queue/onstomp/failover/test2') do |m|
96
106
  end
97
- client.unsubscribe sub, :'x-onstomp-real-client' => client.active_client.uri
98
- client.commit 't-1234', :'x-onstomp-real-client' => client.active_client.uri
99
- #Thread.pass until client.connected?
100
- client.disconnect :'x-onstomp-real-client' => client.active_client.uri
107
+ client.unsubscribe sub
108
+ client.commit 't-1234'
109
+
110
+ # Need to ensure that the DISCONNECT frame was actually received by
111
+ # the broker
112
+ client.disconnect :receipt => 'rcpt-disconnect'
101
113
  brokers.each(&:join)
114
+ may_be_present = check_for_maybes.map(&:command)
115
+ # The reason there is only one SUBSCRIBE and no UNSUBSCRIBE is that
116
+ # the pair will cancel out before the connection has been re-established.
102
117
  brokers.first.frames_received.map(&:command).should == ["CONNECT", "SEND", "BEGIN", "SEND", "SUBSCRIBE"]
103
- brokers.last.frames_received.map(&:command).should == ["CONNECT", "SEND", "BEGIN", "SEND", "SUBSCRIBE", "SEND", "SEND", "COMMIT", "SEND", "DISCONNECT"]
118
+ brokers.last.frames_received.map(&:command).should == (["CONNECT"] +
119
+ may_be_present + ["BEGIN", "SEND", "SUBSCRIBE", "SEND", "SEND", "COMMIT", "SEND", "DISCONNECT"])
104
120
  end
105
121
  end
106
122
  end
@@ -3,6 +3,14 @@ require 'spec_helper'
3
3
  require File.expand_path('../test_broker', __FILE__)
4
4
 
5
5
  describe OnStomp::Client, "full stack test (stomp+ssl:)", :fullstack => true do
6
+ def encode_body body, encoding
7
+ if RUBY_VERSION >= '1.9'
8
+ body.encode(encoding)
9
+ else
10
+ body
11
+ end
12
+ end
13
+
6
14
  describe "STOMP 1.0" do
7
15
  let(:broker) {
8
16
  TestBroker.new 10101
@@ -15,16 +23,49 @@ describe OnStomp::Client, "full stack test (stomp+ssl:)", :fullstack => true do
15
23
  end
16
24
 
17
25
  describe "connecting" do
18
- it "should connect to the broker given a CA path" do
26
+ it "should run just fine" do
27
+ blocked_up = false
19
28
  client = OnStomp::Client.new('stomp://localhost:10101')
29
+ client.on_connection_blocked do |*_|
30
+ blocked_up = true
31
+ end
32
+ client.write_timeout = 1
20
33
  client.connect
21
34
  client.send '/queue/test', 'my message body', {
22
35
  "this:is\na \\fun\\ header" => 'blather matter'
23
36
  }
24
- client.send '/queue/test', "\x01\x02\x03\x04\x05\x06".encode('BINARY'),
37
+ client.send '/queue/test', encode_body("\x01\x02\x03\x04\x05\x06", 'BINARY'),
25
38
  :'content-type' => 'application/octet-stream'
26
- client.disconnect
39
+ sleep 1.5
40
+ client.send '/queue/test', encode_body("hëllo", 'ISO-8859-1')
41
+ client.disconnect :receipt => 'rcpt-disconnect'
27
42
  broker.join
43
+ blocked_up.should be_false
44
+ end
45
+
46
+ it "should block on write" do
47
+ blocked_up = false
48
+ client = OnStomp::Client.new('stomp://localhost:10101')
49
+ client.on_connection_blocked do |*_|
50
+ blocked_up = true
51
+ end
52
+ client.write_timeout = 1
53
+ client.connect
54
+ # Can't seem to make this happen, so we'll do the next best thing.
55
+ con = client.connection
56
+ def con.ready_for_write?
57
+ false
58
+ end
59
+ client.send '/queue/test', 'my message body', {
60
+ "this:is\na \\fun\\ header" => 'blather matter'
61
+ }
62
+ client.send '/queue/test', encode_body("\x01\x02\x03\x04\x05\x06", 'BINARY'),
63
+ :'content-type' => 'application/octet-stream'
64
+ sleep 1.5
65
+ client.send '/queue/test', encode_body("hëllo", 'ISO-8859-1')
66
+ client.disconnect
67
+ broker.stop
68
+ blocked_up.should be_true
28
69
  end
29
70
  end
30
71
  end
@@ -48,7 +89,7 @@ describe OnStomp::Client, "full stack test (stomp+ssl:)", :fullstack => true do
48
89
  client.send '/queue/test', 'my message body', {
49
90
  "this:is\na \\fun\\ header" => 'blather matter'
50
91
  }
51
- client.send '/queue/test', "\x01\x02\x03\x04\x05\x06".encode('BINARY'),
92
+ client.send '/queue/test', encode_body("\x01\x02\x03\x04\x05\x06", 'BINARY'),
52
93
  :'content-type' => 'application/octet-stream'
53
94
  client.disconnect
54
95
  broker.join
@@ -3,6 +3,14 @@ require 'spec_helper'
3
3
  require File.expand_path('../test_broker', __FILE__)
4
4
 
5
5
  describe OnStomp::Client, "full stack test (stomp+ssl:)", :fullstack => true do
6
+ def encode_body body, encoding
7
+ if RUBY_VERSION >= '1.9'
8
+ body.encode(encoding)
9
+ else
10
+ body
11
+ end
12
+ end
13
+
6
14
  describe "STOMP 1.0" do
7
15
  let(:broker) {
8
16
  TestSSLBroker.new 10103
@@ -24,7 +32,7 @@ describe OnStomp::Client, "full stack test (stomp+ssl:)", :fullstack => true do
24
32
  client.send '/queue/test', 'my message body', {
25
33
  "this:is\na \\fun\\ header" => 'blather matter'
26
34
  }
27
- client.send '/queue/test', "\x01\x02\x03\x04\x05\x06".encode('BINARY'),
35
+ client.send '/queue/test', encode_body("\x01\x02\x03\x04\x05\x06",'BINARY'),
28
36
  :'content-type' => 'application/octet-stream'
29
37
  client.disconnect
30
38
  broker.join
@@ -63,7 +71,7 @@ describe OnStomp::Client, "full stack test (stomp+ssl:)", :fullstack => true do
63
71
  client.send '/queue/test', 'my message body', {
64
72
  "this:is\na \\fun\\ header" => 'blather matter'
65
73
  }
66
- client.send '/queue/test', "\x01\x02\x03\x04\x05\x06".encode('BINARY'),
74
+ client.send '/queue/test', encode_body("\x01\x02\x03\x04\x05\x06",'BINARY'),
67
75
  :'content-type' => 'application/octet-stream'
68
76
  client.disconnect
69
77
  broker.join
@@ -7,7 +7,7 @@ class TestBroker
7
7
 
8
8
  attr_reader :messages, :sessions, :mau_dib
9
9
  attr_reader :frames_received, :frames_transmitted
10
- attr_accessor :session_class
10
+ attr_accessor :session_class, :accept_delay
11
11
 
12
12
  def initialize(port=61613)
13
13
  @frames_received = []
@@ -24,6 +24,7 @@ class TestBroker
24
24
  $stdout.puts "Could not bind: #{ex}"
25
25
  end
26
26
  @session_class = Session10
27
+ @accept_delay = nil
27
28
  end
28
29
 
29
30
  def kill_on_command command
@@ -38,7 +39,6 @@ class TestBroker
38
39
  end
39
40
  msg[:'message-id'] = "msg-#{Time.now.to_f}"
40
41
  dest = msg[:destination]
41
- #$stdout.puts "Enqueuing on #{dest}"
42
42
  if !@subscribes[dest].empty?
43
43
  session, subid = @subscribes[dest].first
44
44
  deliver_message msg, session, subid
@@ -46,7 +46,6 @@ class TestBroker
46
46
  @messages[dest] << msg
47
47
  end
48
48
  end
49
- #$stdout.puts "Done enqueueing!"
50
49
  end
51
50
 
52
51
  def deliver_message msg, sess, subid
@@ -88,6 +87,7 @@ class TestBroker
88
87
  begin
89
88
  loop do
90
89
  sock = @socket.accept
90
+ @accept_delay && sleep(@accept_delay)
91
91
  @session_mutex.synchronize do
92
92
  @sessions << @session_class.new(self, sock)
93
93
  end
@@ -133,6 +133,12 @@ class TestBroker
133
133
  def init_connection
134
134
  @connection = OnStomp::Connections::Stomp_1_0.new(socket, self)
135
135
  @processor = OnStomp::Components::ThreadedProcessor.new self
136
+ @killing = false
137
+ @session_killer = Thread.new do
138
+ Thread.pass until @killing
139
+ @socket.close rescue nil
140
+ @processor.stop rescue nil
141
+ end
136
142
  end
137
143
 
138
144
  def reply_to_connect connect_frame
@@ -150,22 +156,19 @@ class TestBroker
150
156
 
151
157
  def init_events
152
158
  on_subscribe do |s,_|
153
- #$stdout.puts "Got SUBSCRIBE: #{s.headers.to_hash.inspect}"
154
- @server.subscribe s, self
159
+ @server.subscribe(s, self) unless @killing
155
160
  end
156
161
 
157
162
  on_unsubscribe do |u, _|
158
- #$stdout.puts "Got UNSUBSCRIBE: #{u.headers.to_hash.inspect}"
159
- @server.unsubscribe u, self
163
+ @server.unsubscribe(u, self) unless @killing
160
164
  end
161
165
 
162
166
  on_send do |s,_|
163
- #$stdout.puts "Got a SEND frame! #{s.body.inspect}"
164
- @server.enqueue_message s
167
+ @server.enqueue_message(s) unless @killing
165
168
  end
166
169
 
167
170
  on_disconnect do |d,_|
168
- @connection.close
171
+ #@connection.close
169
172
  end
170
173
 
171
174
  after_transmitting do |f,_|
@@ -173,13 +176,14 @@ class TestBroker
173
176
  end
174
177
 
175
178
  before_receiving do |f,_|
176
- @server.frames_received << f
179
+ @server.frames_received << f unless @killing
177
180
  if @server.mau_dib && f.command == @server.mau_dib
178
181
  kill
179
- end
180
- if f.header? :receipt
181
- transmit OnStomp::Components::Frame.new('RECEIPT',
182
- :'receipt-id' => f[:receipt])
182
+ elsif !@killing
183
+ if f.header? :receipt
184
+ transmit OnStomp::Components::Frame.new('RECEIPT',
185
+ :'receipt-id' => f[:receipt])
186
+ end
183
187
  end
184
188
  end
185
189
  end
@@ -203,13 +207,12 @@ class TestBroker
203
207
  def join
204
208
  if @connection.connected?
205
209
  #@connection.close
206
- @processor.join
210
+ @processor.join rescue nil
207
211
  end
208
212
  end
209
213
 
210
214
  def kill
211
- @socket.close
212
- @processor.stop
215
+ @killing = true
213
216
  end
214
217
 
215
218
  def stop