activemessaging 0.13.0 → 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,220 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class InitializeFilter
4
-
5
- attr_accessor :options
6
-
7
- def initialize(options)
8
- @options = options
9
- end
10
-
11
- def process(message, details={})
12
- puts "ObjectFilter process called!"
13
- end
14
- end
15
-
16
- class GatewayTest < Test::Unit::TestCase
17
- class ClassFilter
18
- def initialize
19
- raise "Don't try and construct one of these please"
20
- end
21
-
22
- class << self
23
- def process(message, details={})
24
- puts "ClassFilter process called!"
25
- end
26
- end
27
- end
28
-
29
- class ObjectFilter
30
- def process(message, details={})
31
- puts "ObjectFilter process called!"
32
- end
33
- end
34
-
35
- class MessageManglingFilter
36
- class << self
37
- def process(message, details={})
38
- message.body.upcase!
39
- message.id.upcase!
40
- message.headers['the-mangler'] = 'run'
41
- message.destination.gsub! '/', '#'
42
- end
43
- end
44
- end
45
-
46
- class TestProcessor < ActiveMessaging::Processor
47
- include ActiveMessaging::MessageSender
48
- #subscribes_to :testqueue
49
- def on_message(message)
50
- @test_message = true
51
- end
52
- end
53
-
54
- class TestRetryProcessor < ActiveMessaging::Processor
55
- include ActiveMessaging::MessageSender
56
- #subscribes_to :testqueue
57
- def on_message(message)
58
- #puts "TestRetryProcessor - about to raise exception"
59
- raise ActiveMessaging::AbortMessageException.new("Cause a retry!")
60
- end
61
- end
62
-
63
- class TestAdapter
64
- end
65
-
66
- def setup
67
- end
68
-
69
- def teardown
70
- ActiveMessaging::Gateway.reset
71
- end
72
-
73
-
74
- def test_create_filter
75
- filter_obj = ActiveMessaging::Gateway.create_filter('gateway_test/object_filter', {:direction=>:incoming, :name=>'test1'})
76
- assert filter_obj
77
- assert filter_obj.is_a?(GatewayTest::ObjectFilter)
78
-
79
- filter_obj = ActiveMessaging::Gateway.create_filter('initialize_filter', {:direction=>:incoming, :name=>'test2'})
80
- assert filter_obj
81
- assert filter_obj.is_a?(InitializeFilter)
82
- assert_equal filter_obj.options, {:direction=>:incoming, :name=>'test2'}
83
-
84
- filter_obj = ActiveMessaging::Gateway.create_filter(:initialize_filter, {:direction=>:incoming, :name=>'test2'})
85
- assert filter_obj
86
- assert filter_obj.is_a?(InitializeFilter)
87
- assert_equal filter_obj.options, {:direction=>:incoming, :name=>'test2'}
88
-
89
- filter_obj = ActiveMessaging::Gateway.create_filter(:'gateway_test/class_filter', {:direction=>:incoming, :name=>'test2'})
90
- assert filter_obj
91
- assert filter_obj.is_a?(Class)
92
- assert_equal filter_obj.name, "GatewayTest::ClassFilter"
93
- end
94
-
95
- def test_register_adapter
96
- ActiveMessaging::Gateway.register_adapter :test_register_adapter, TestAdapter
97
- assert_equal TestAdapter, ActiveMessaging::Gateway.adapters[:test_register_adapter]
98
- end
99
-
100
- def test_destination
101
- ActiveMessaging::Gateway.destination :hello_world, '/queue/helloWorld'
102
- dest = ActiveMessaging::Gateway.named_destinations[:hello_world]
103
- assert_equal :hello_world, dest.name
104
- end
105
-
106
- def test_destination_duplicates
107
- ActiveMessaging::Gateway.destination :hello_world, '/queue/helloWorld'
108
- dest = ActiveMessaging::Gateway.named_destinations[:hello_world]
109
- assert_equal :hello_world, dest.name
110
-
111
- # make sure a dupe name causes an error
112
- assert_raises RuntimeError do
113
- ActiveMessaging::Gateway.destination :hello_world, '/queue/helloWorld2'
114
- end
115
- end
116
-
117
- def test_connection
118
- conn = ActiveMessaging::Gateway.connection
119
- assert_equal conn.class, ActiveMessaging::Adapters::Test::Connection
120
- end
121
-
122
- def test_subscribe_and_unsubscribe
123
- ActiveMessaging::Gateway.destination :hello_world, '/queue/helloWorld'
124
- ActiveMessaging::Gateway.subscribe_to :hello_world, TestProcessor, headers={}
125
- sub = ActiveMessaging::Gateway.subscriptions.values.last
126
- assert_equal :hello_world, sub.destination.name
127
- assert_equal TestProcessor, sub.processor_class
128
-
129
- ActiveMessaging::Gateway.subscribe
130
- assert_not_nil ActiveMessaging::Gateway.connection.find_subscription(sub.destination.value)
131
-
132
- ActiveMessaging::Gateway.unsubscribe
133
- assert_nil ActiveMessaging::Gateway.connection.find_subscription(sub.destination.value)
134
- end
135
-
136
- def test_disconnect
137
- assert_equal 0, ActiveMessaging::Gateway.connections.keys.size
138
-
139
- conn = ActiveMessaging::Gateway.connection
140
- assert_equal 1, ActiveMessaging::Gateway.connections.keys.size
141
- assert_equal true, conn.connected
142
-
143
- ActiveMessaging::Gateway.disconnect
144
-
145
- assert_equal 0, ActiveMessaging::Gateway.connections.keys.size
146
- assert_equal false, conn.connected
147
- end
148
-
149
- def test_publish
150
- ActiveMessaging::Gateway.destination :hello_world, '/queue/helloWorld'
151
- ActiveMessaging::Gateway.publish :hello_world, "test_publish body", self.class, headers={}, timeout=10
152
- assert_not_nil ActiveMessaging::Gateway.connection.find_message('/queue/helloWorld', "test_publish body")
153
-
154
- assert_raise(RuntimeError) do
155
- ActiveMessaging::Gateway.publish :hello_world, nil, self.class, headers={}, timeout=10
156
- end
157
- assert_raise(RuntimeError) do
158
- ActiveMessaging::Gateway.publish :hello_world, '', self.class, headers={}, timeout=10
159
- end
160
- end
161
-
162
- def test_acknowledge_message
163
- ActiveMessaging::Gateway.destination :hello_world, '/queue/helloWorld'
164
- ActiveMessaging::Gateway.subscribe_to :hello_world, TestProcessor, headers={}
165
- sub = ActiveMessaging::Gateway.subscriptions.values.last
166
- dest = ActiveMessaging::Adapters::Test::Destination.new '/queue/helloWorld'
167
- msg = ActiveMessaging::Adapters::Test::Message.new("message_body", nil, {}, dest.name)
168
- ActiveMessaging::Gateway.acknowledge_message sub, msg
169
- assert_equal msg, ActiveMessaging::Gateway.connection.received_messages.first
170
- end
171
-
172
- def test_abort_message
173
- ActiveMessaging::Gateway.destination :hello_world, '/queue/helloWorld'
174
- ActiveMessaging::Gateway.subscribe_to :hello_world, TestRetryProcessor, headers={}
175
- sub = ActiveMessaging::Gateway.subscriptions.values.last
176
- dest = ActiveMessaging::Adapters::Test::Destination.new '/queue/helloWorld'
177
- msg = ActiveMessaging::Adapters::Test::Message.new("message_body", nil, {}, dest.name)
178
- ActiveMessaging::Gateway.dispatch(msg)
179
- assert_equal msg, ActiveMessaging::Gateway.connection.unreceived_messages.first
180
- end
181
-
182
- def test_aborted_messages_retry_original_message
183
- ActiveMessaging::Gateway.destination :hello_world, '/queue/helloWorld'
184
- ActiveMessaging::Gateway.filter 'gateway_test/message_mangling_filter'
185
- ActiveMessaging::Gateway.subscribe_to :hello_world, TestRetryProcessor, headers={}
186
-
187
- message_body = "message_body"
188
- message_id = "55-a-msg"
189
- message_headers = { 'x-my-header' => 'value' }
190
- message_destination = '/queue/helloWorld'
191
- msg = ActiveMessaging::Adapters::Test::Message.new(message_body.dup, message_id.dup, message_headers.dup, message_destination.dup)
192
- ActiveMessaging::Gateway.dispatch(msg)
193
- assert_equal message_body, ActiveMessaging::Gateway.connection.unreceived_messages.first.body
194
- assert_equal message_id, ActiveMessaging::Gateway.connection.unreceived_messages.first.id
195
- assert_equal message_headers, ActiveMessaging::Gateway.connection.unreceived_messages.first.headers
196
- assert_equal message_destination, ActiveMessaging::Gateway.connection.unreceived_messages.first.destination
197
- end
198
-
199
- def test_receive
200
- ActiveMessaging::Gateway.destination :hello_world, '/queue/helloWorld'
201
- ActiveMessaging::Gateway.publish :hello_world, "test_publish body", self.class, headers={}, timeout=10
202
- msg = ActiveMessaging::Gateway.receive :hello_world, self.class, headers={}, timeout=10
203
- assert_not_nil ActiveMessaging::Gateway.connection.find_message('/queue/helloWorld', "test_publish body")
204
- end
205
-
206
- # def test_reload
207
- # ActiveMessaging.reload_activemessaging
208
- # size = ActiveMessaging::Gateway.named_destinations.size
209
- # ActiveMessaging.reload_activemessaging
210
- # assert_equal size, ActiveMessaging::Gateway.named_destinations.size
211
- # end
212
-
213
- ## figure out how to test these better - start in a thread perhaps?
214
- # def test_start
215
- # end
216
- #
217
- # def test_stop
218
- # end
219
-
220
- end
@@ -1,64 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- if defined?(JRUBY_VERSION)
4
-
5
- require 'activemessaging/adapters/jms'
6
-
7
- class JmsTest < Test::Unit::TestCase
8
-
9
- def setup
10
- @test_txt = 'Yo Homie!'
11
- @isolation_const = rand(99999999)
12
- @connection = ActiveMessaging::Adapters::Jms::Connection.new(:url => 'vm://jms_test',
13
- :login => '',
14
- :passcode => '',
15
- :connection_factory => 'org.apache.activemq.ActiveMQConnectionFactory')
16
- end
17
-
18
- def test_send
19
- @connection.send "/queue/TestQueue#{@isolation_const}", @test_txt, {}
20
- end
21
-
22
- def test_receive_with_one
23
- @connection.send "/queue/TestQueue#{@isolation_const}", @test_txt
24
- @connection.subscribe "/queue/TestQueue#{@isolation_const}"
25
- message = @connection.receive
26
- assert_equal @test_txt, message.body
27
- end
28
-
29
- def test_receive_multi
30
- 10.times do |i|
31
- @connection.send "/queue/MultiQueue#{@isolation_const}", @test_txt
32
- end
33
-
34
- counter=0
35
- @connection.subscribe "/queue/MultiQueue#{@isolation_const}"
36
- while message = @connection.receive
37
- assert_equal @test_txt, message.body
38
- counter += 1
39
- end
40
- assert_equal 10, counter
41
- end
42
-
43
- def test_one_off_receive
44
- @connection.send "/queue/OneOff#{@isolation_const}", "one off message"
45
- sleep 2
46
- message = @connection.receive "/queue/OneOff#{@isolation_const}"
47
- assert_equal "one off message", message.body
48
- assert_equal "MESSAGE", message.command
49
- assert_equal "/queue/OneOff#{@isolation_const}", message.headers['destination']
50
- end
51
-
52
- def test_unsubscribe
53
- @connection.subscribe "/queue/TestSubQueue#{@isolation_const}"
54
- @connection.unsubscribe "/queue/TestSubQueue#{@isolation_const}"
55
- assert_nil @connection.consumers["TestSubQueue#{@isolation_const}"]
56
- end
57
-
58
- def teardown
59
- @connection.close unless @connection.nil?
60
- end
61
-
62
- end
63
-
64
- end
@@ -1,83 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
- loaded = true
3
- begin
4
- require 'reliable-msg'
5
- rescue Object => e
6
- loaded = false
7
- end
8
- if loaded
9
-
10
- require 'activemessaging/adapters/reliable_msg'
11
-
12
- class ReliableMsgTest < Test::Unit::TestCase
13
-
14
- def setup
15
- @qm = ReliableMsg::QueueManager.new
16
- @qm.start
17
- @connection = ActiveMessaging::Adapters::ReliableMsgConnection.new(:reliable=>false, :poll_interval=>2)
18
- @d = "/queue/reliable.msg.test}."
19
- @message = "mary had a little lamb"
20
- @message2 = "whose fleece was white as snow"
21
- end
22
-
23
- def teardown
24
- @connection.disconnect unless @connection.nil?
25
- @qm.stop unless @qm.nil?
26
- end
27
-
28
- def test_subscribe_and_unsubscribe
29
- assert_nil @connection.subscriptions["#{@d}test_subscribe"]
30
- @connection.subscribe "#{@d}test_subscribe"
31
- assert_equal 1, @connection.subscriptions["#{@d}test_subscribe"].count
32
- @connection.subscribe "#{@d}test_subscribe"
33
- assert_equal 2, @connection.subscriptions["#{@d}test_subscribe"].count
34
- @connection.unsubscribe "#{@d}test_subscribe"
35
- assert_equal 1, @connection.subscriptions["#{@d}test_subscribe"].count
36
- @connection.unsubscribe "#{@d}test_subscribe"
37
- assert_nil @connection.subscriptions["#{@d}test_subscribe"]
38
- end
39
-
40
- def test_send_and_receive
41
- @connection.subscribe "#{@d}test_send_and_receive"
42
- @connection.send "#{@d}test_send_and_receive", @message
43
- message = @connection.receive
44
- @connection.received message
45
- assert_equal @message, message.body
46
- end
47
-
48
-
49
- def test_send_and_receive_multiple_subscriptions
50
- @connection.subscribe "#{@d}test_send_and_receive1"
51
- @connection.subscribe "#{@d}test_send_and_receive2"
52
- @connection.subscribe "#{@d}test_send_and_receive3"
53
-
54
- @connection.send "#{@d}test_send_and_receive2", "message2"
55
- message = @connection.receive
56
- @connection.received message
57
- assert_equal "message2", message.body
58
-
59
- @connection.send "#{@d}test_send_and_receive3", "message3"
60
- message = @connection.receive
61
- @connection.received message
62
- assert_equal "message3", message.body
63
-
64
- end
65
-
66
-
67
- def test_will_cause_sleep
68
-
69
- begin
70
- Timeout.timeout 10 do
71
- @connection.subscribe "#{@d}test_will_cause_sleep"
72
- message = @connection.receive
73
- @connection.received message
74
- assert false
75
- end
76
- rescue Timeout::Error=>toe
77
- assert true
78
- end
79
- end
80
-
81
- end
82
-
83
- end # if loaded
@@ -1,168 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
- require 'activemessaging/adapters/stomp'
3
-
4
- loaded = true
5
- begin
6
- require 'stomp'
7
- rescue Object => e
8
- loaded = false
9
- end
10
- if loaded #only run these test if stomp gem installed
11
-
12
-
13
- class FakeTCPSocket
14
- attr_accessor :sent_messages
15
- def initialize; @sent_messages=[]; end
16
- def puts(s=""); @sent_messages << s; end
17
- def write(s=""); self.puts s; end
18
- def ready?; true; end
19
- end
20
-
21
-
22
- module Stomp
23
- class Connection
24
-
25
- attr_accessor :subscriptions
26
-
27
- def socket
28
- @socket = FakeTCPSocket.new if @socket.nil?
29
- @socket
30
- end
31
-
32
- def receive=(msg)
33
- # stomp 1.0.5 code, now no longer works
34
- # sm = Stomp::Message.new do |m|
35
- # m.command = 'MESSAGE'
36
- # m.body = msg
37
- # m.headers = {'message-id'=>'testmessage1', 'content-length'=>msg.length, 'destination'=>'destination1'}
38
- # end
39
-
40
- sm = Stomp::Message.new("MESSAGE\ndestination:/queue/stomp/destination/1\nmessage-id: messageid1\ncontent-length:#{msg.length}\n\n#{msg}\0\n")
41
-
42
- sm.command = 'MESSAGE'
43
- sm.headers = {'message-id'=>'testmessage1', 'content-length'=>msg.length, 'destination'=>'destination1'}
44
-
45
- @test_message = ActiveMessaging::Adapters::Stomp::Message.new(sm)
46
- end
47
-
48
- def receive
49
- @test_message
50
- end
51
- end
52
- end
53
-
54
- class StompTest < Test::Unit::TestCase
55
-
56
- def setup
57
- @connection = ActiveMessaging::Adapters::Stomp::Connection.new({})
58
- @d = "/queue/stomp/destination/1"
59
- @message = "mary had a little lamb"
60
- @connection.stomp_connection.receive = @message
61
- end
62
-
63
- def sent_command
64
- @connection.stomp_connection.socket.sent_messages[0]
65
- end
66
-
67
- def sent_headers
68
- @connection.stomp_connection.socket.sent_messages.drop(1).take_while { |line| !line.empty? }
69
- end
70
-
71
- def sent_body
72
- (@connection.stomp_connection.socket.sent_messages.drop_while {|line| !line.empty?}).drop(1).first
73
- end
74
-
75
- def test_initialize
76
- i = { :retryMax => 4,
77
- :deadLetterQueue=>'/queue/dlq',
78
- :login=>"",
79
- :passcode=> "",
80
- :host=> "localhost",
81
- :port=> "61613",
82
- :reliable=>FALSE,
83
- :reconnectDelay=> 5,
84
- :clientId=> 'cid',
85
- :deadLetterQueuePrefix=>"DLQ."}
86
-
87
- @connection = ActiveMessaging::Adapters::Stomp::Connection.new(i)
88
- assert_equal 4, @connection.retryMax
89
- assert_equal '/queue/dlq', @connection.deadLetterQueue
90
- assert_equal "DLQ.", @connection.deadLetterQueuePrefix
91
- assert_equal true, @connection.supports_dlq?
92
- end
93
-
94
- def test_disconnect
95
- @connection.disconnect
96
- assert_equal "DISCONNECT", sent_command
97
- end
98
-
99
- def test_subscribe
100
- @connection.subscribe @d, {}
101
- assert_equal "SUBSCRIBE", sent_command
102
- assert sent_headers.include?("content-length:0"), "No content-length header was sent"
103
- assert sent_headers.include?("destination:#{@d}"), "No destination header was sent"
104
- assert_equal 1, @connection.stomp_connection.subscriptions.count
105
- assert_equal({:'content-type'=>'text/plain; charset=UTF-8', :'content-length'=>'0', :destination=>@d}, @connection.stomp_connection.subscriptions[@d])
106
- end
107
-
108
- def test_unsubscribe
109
- @connection.subscribe @d, {}
110
- @connection.stomp_connection.socket.sent_messages = []
111
- @connection.unsubscribe @d, {}
112
- assert_equal "UNSUBSCRIBE", sent_command
113
- assert sent_headers.include?("content-length:0"), "No content-length header was sent"
114
- assert sent_headers.include?("destination:#{@d}"), "No destination header was sent"
115
- assert_equal 0, @connection.stomp_connection.subscriptions.count
116
- end
117
-
118
- def test_send
119
- @connection.send(@d, @message, {})
120
- assert_equal 'SEND', sent_command
121
- assert sent_headers.include?("content-length:#{@message.length}"), "No content-length header was sent"
122
- assert sent_headers.include?("destination:#{@d}"), "No destination header was sent"
123
- # assert_equal @message, @connection.stomp_connection.socket.sent_messages[5]
124
- assert_equal @message, sent_body
125
- end
126
-
127
- def test_receive
128
- m = @connection.receive
129
- assert_equal @message, m.body
130
- end
131
-
132
- def test_received
133
- m = @connection.receive
134
- m.headers[:transaction] = 'test-transaction'
135
- @connection.received m, {:ack=>'client'}
136
- end
137
-
138
- def test_unreceive
139
- @connection = ActiveMessaging::Adapters::Stomp::Connection.new({:retryMax=>4, :deadLetterQueue=>'/queue/dlq'})
140
- @connection.stomp_connection.receive = @message
141
- m = @connection.receive
142
- m.headers["a13g-retry-count"] = 5
143
- @connection.unreceive m, {:ack=>'client'}
144
- end
145
-
146
- def test_unreceive_with_dlq_prefix
147
- @connection = ActiveMessaging::Adapters::Stomp::Connection.new({:retryMax=>4, :deadLetterQueuePrefix=>'DLQ.'})
148
- @connection.stomp_connection.receive = @message
149
- m = @connection.receive
150
- m.headers["a13g-retry-count"] = 5
151
- @connection.unreceive m, {:ack=>'client', :destination=>"/queue/myqueue"}
152
- end
153
-
154
- def test_add_dlq_prefix
155
- @connection = ActiveMessaging::Adapters::Stomp::Connection.new({:deadLetterQueuePrefix=>'DLQ.'})
156
- dlq = @connection.add_dlq_prefix("/queue/myqueue")
157
- assert_equal "/queue/DLQ.myqueue", dlq
158
- dlq = @connection.add_dlq_prefix("/queue/something/myqueue")
159
- assert_equal "/queue/something/DLQ.myqueue", dlq
160
- dlq = @connection.add_dlq_prefix("/topic/myqueue")
161
- assert_equal "/topic/DLQ.myqueue", dlq
162
- dlq = @connection.add_dlq_prefix("myqueue")
163
- assert_equal "DLQ.myqueue", dlq
164
- end
165
-
166
- end
167
-
168
- end # if loaded