lolitra 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ *.swp
3
4
  .bundle
4
5
  .config
5
6
  .yardoc
Binary file
@@ -0,0 +1,7 @@
1
+ module Lolitra
2
+ class Future
3
+ def initialze(message_class)
4
+
5
+ end
6
+ end
7
+ end
@@ -62,20 +62,21 @@ module Lolitra
62
62
  private
63
63
  def message_handler(message_class, id = :id)
64
64
  message_class.new.send(id) #check if id exists for this class
65
- handlers.merge!(message_class.name => [message_class, get_method_by_class(message_class), id])
65
+ handlers.merge!(message_class.message_key => [message_class, get_method_by_class(message_class), id])
66
66
  end
67
67
 
68
- def started_by(message_class)
69
- starters << message_class.name
68
+ def started_by(message_class, id = :id)
69
+ starters << message_class.message_key
70
+ message_handler(message_class, id)
70
71
  end
71
72
 
72
73
  def search(message)
73
- id_method_name = handlers[message.class.name][2]
74
+ id_method_name = handlers[message.class.message_key][2]
74
75
  send("find_by_#{id_method_name}", message.send(id_method_name))
75
76
  end
76
77
 
77
78
  def is_starter?(message)
78
- starters.include? message.class.name
79
+ starters.include? message.class.message_key
79
80
  end
80
81
 
81
82
  def get_handler(message)
@@ -104,7 +105,7 @@ module Lolitra
104
105
  end
105
106
 
106
107
  def handle(message)
107
- handler_method = self.class.handlers[message.class.name][1]
108
+ handler_method = self.class.handlers[message.class.message_key][1]
108
109
  raise "Can't handle message #{message.class}" unless handler_method
109
110
  self.send(handler_method, message)
110
111
  end
@@ -124,12 +125,24 @@ module Lolitra
124
125
  instance.bus
125
126
  end
126
127
 
127
- def self.register(handler_class)
128
- instance.register(handler_class)
128
+ def self.register_subscriber(handler_class)
129
+ instance.register_subscriber(handler_class)
129
130
  end
130
131
 
131
- def register(handler_class)
132
- register_message_handler(handler_class)
132
+ def register_subscriber(handler_class)
133
+ handler_class.handle_messages.each do |message_class|
134
+ bus.subscribe(message_class, handler_class)
135
+ end
136
+ end
137
+
138
+ def self.register_pull_subscriber(handler_class)
139
+ instance.register_pull_subscriber(handler_class)
140
+ end
141
+
142
+ def register_pull_subscriber(handler_class)
143
+ handler_class.handle_messages.each do |message_class|
144
+ bus.pull_subscribe(message_class, handler_class)
145
+ end
133
146
  end
134
147
 
135
148
  def self.publish(message_instance)
@@ -139,17 +152,11 @@ module Lolitra
139
152
  def publish(message_instance)
140
153
  bus.publish(message_instance)
141
154
  end
142
- private
143
- def register_message_handler(handler_class)
144
- handler_class.handle_messages.each do |message_class|
145
- bus.subscribe(message_class, handler_class)
146
- end
147
- end
148
155
  end
149
156
 
150
- module AmqpMessage
157
+ module Message
151
158
 
152
- module AmqpMessageClass
159
+ module MessageClass
153
160
  def self.extended(base)
154
161
  class << base; attr_accessor :class_message_key; end
155
162
  end
@@ -158,7 +165,7 @@ module Lolitra
158
165
  if (key)
159
166
  self.class_message_key = key
160
167
  else
161
- self.class_message_key
168
+ self.class_message_key || "#{MessageHandler::Helper.underscore(self.class.name)}"
162
169
  end
163
170
  end
164
171
 
@@ -170,7 +177,7 @@ module Lolitra
170
177
  end
171
178
 
172
179
  def self.included(base)
173
- base.send :extend, AmqpMessageClass
180
+ base.send :extend, MessageClass
174
181
  end
175
182
 
176
183
  def initialize(hash={})
@@ -184,32 +191,6 @@ module Lolitra
184
191
  end
185
192
  end
186
193
 
187
- module AmqpMessageHandler
188
- module AmqpMessageHandlerClass
189
-
190
- def self.extended(base)
191
- class << base
192
- attr_accessor :message_class_by_key
193
- alias_method :message_handler_without_class_by_key, :message_handler unless method_defined?(:message_handler_without_class_by_key)
194
- alias_method :message_handler, :message_handler_with_class_by_key
195
- end
196
- base.message_class_by_key = {}
197
- end
198
-
199
- def message_handler_with_class_by_key(message_class, id = :id)
200
- message_class_by_key.merge!({message_class.message_key => message_class})
201
- message_handler_without_class_by_key(message_class, id)
202
- end
203
-
204
- end
205
-
206
- def self.included(base)
207
- base.send :include, MessageHandler
208
- base.send :extend, AmqpMessageHandlerClass
209
- end
210
-
211
- end
212
-
213
194
  class AmqpBus
214
195
  attr_accessor :queue_prefix
215
196
  attr_accessor :connection
@@ -228,18 +209,26 @@ module Lolitra
228
209
  end
229
210
 
230
211
  def subscribe(message_class, handler_class)
231
- EM.next_tick do
232
- channel = AMQP::Channel.new(self.connection)
233
- channel.prefetch(1).queue(queue_prefix + MessageHandler::Helpers.underscore(handler_class.name), :dureable => true).bind(self.exchange, :routing_key => message_class.message_key).subscribe do |info, payload|
234
- message_class_tmp = handler_class.message_class_by_key[info.routing_key]
235
- handler_class.handle(message_class_tmp.unmarshall(payload))
236
- end
237
- end
212
+ create_queue(message_class, handler_class, {:exclusive => true, :durable => false}, "")
213
+ end
214
+
215
+ def pull_subscribe(message_class, handler_class)
216
+ create_queue(message_class, handler_class, {:durable => true}, queue_prefix + MessageHandler::Helpers.underscore(handler_class.name))
238
217
  end
239
218
 
240
219
  def publish(message)
241
220
  self.exchange.publish(message.marshall, :routing_key => message.class.message_key, :timestamp => Time.now.to_i)
242
221
  end
243
222
 
223
+ private
224
+ def create_queue(message_class, handler_class, options, queue_name)
225
+ EM.next_tick do
226
+ channel = AMQP::Channel.new(self.connection)
227
+ channel.prefetch(1).queue(queue_name, options).bind(self.exchange, :routing_key => message_class.message_key).subscribe do |info, payload|
228
+ message_class_tmp = handler_class.handlers[info.routing_key][0]
229
+ handler_class.handle(message_class_tmp.unmarshall(payload))
230
+ end
231
+ end
232
+ end
244
233
  end
245
234
  end
@@ -1,3 +1,3 @@
1
1
  module Lolitra
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/lolitra.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require "lolitra/version"
2
2
  require "lolitra/handler_base"
3
+ require "lolitra/future"
Binary file
data/spec/lolitra_spec.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
3
  class TestMessage
4
- include Lolitra::AmqpMessage
4
+ include Lolitra::Message
5
5
 
6
6
  message_key "test1"
7
7
 
@@ -11,7 +11,7 @@ class TestMessage
11
11
  end
12
12
 
13
13
  class TestMessage1
14
- include Lolitra::AmqpMessage
14
+ include Lolitra::Message
15
15
 
16
16
  message_key "test2"
17
17
 
@@ -24,7 +24,6 @@ class TestMessageHandler
24
24
  include Lolitra::MessageHandler
25
25
 
26
26
  started_by TestMessage
27
- message_handler TestMessage
28
27
  stateful true
29
28
 
30
29
  def self.find_by_id(id)
@@ -41,7 +40,6 @@ class TestMessageHandler1
41
40
 
42
41
  started_by TestMessage1
43
42
  message_handler TestMessage
44
- message_handler TestMessage1
45
43
 
46
44
  stateful true
47
45
 
@@ -59,19 +57,23 @@ class TestMessageHandler1
59
57
 
60
58
  end
61
59
 
62
- class TestAmqpMessageHandler
63
- include Lolitra::AmqpMessageHandler
60
+ class TestMessageHandlerAmqp
61
+ include Lolitra::MessageHandler
64
62
 
65
63
  message_handler TestMessage
66
64
  message_handler TestMessage1
67
65
 
68
66
  stateful false
69
67
 
70
- def test_message(message)
71
- EM.stop { exit }
72
- end
73
68
  end
74
69
 
70
+ class TestMessageHandlerAmqp1
71
+ include Lolitra::MessageHandler
72
+
73
+ message_handler TestMessage
74
+
75
+ stateful false
76
+ end
75
77
 
76
78
  describe Lolitra::MessageHandler,'#create_handler' do
77
79
  it "returns old instance of the handler for the same message id" do
@@ -115,7 +117,7 @@ describe Lolitra::MessageHandler, '#publish' do
115
117
  message = TestMessage.new
116
118
  bus = TestBus.new
117
119
  Lolitra::MessageHandlerManager.bus = bus
118
- Lolitra::MessageHandlerManager.register(TestMessageHandler)
120
+ Lolitra::MessageHandlerManager.register_subscriber(TestMessageHandler)
119
121
 
120
122
 
121
123
  TestMessageHandler.should_receive(:handle).with(message)
@@ -127,7 +129,7 @@ describe Lolitra::MessageHandlerManager, '#publish' do
127
129
  it "can send message to the bus" do
128
130
  bus = TestBus.new
129
131
  Lolitra::MessageHandlerManager.bus = bus
130
- Lolitra::MessageHandlerManager.register(TestMessageHandler)
132
+ Lolitra::MessageHandlerManager.register_subscriber(TestMessageHandler)
131
133
 
132
134
  message = TestMessage.new
133
135
 
@@ -144,8 +146,8 @@ describe Lolitra::MessageHandlerManager, '#handle_message' do
144
146
 
145
147
  Lolitra::MessageHandlerManager.bus = bus
146
148
 
147
- Lolitra::MessageHandlerManager.register(TestMessageHandler)
148
- Lolitra::MessageHandlerManager.register(TestMessageHandler1)
149
+ Lolitra::MessageHandlerManager.register_subscriber(TestMessageHandler)
150
+ Lolitra::MessageHandlerManager.register_subscriber(TestMessageHandler1)
149
151
 
150
152
  message = TestMessage.new
151
153
  message1 = TestMessage1.new
@@ -161,7 +163,7 @@ describe Lolitra::MessageHandlerManager, '#handle_message' do
161
163
  end
162
164
  end
163
165
 
164
- describe Lolitra::AmqpMessage, '#message_key' do
166
+ describe Lolitra::Message, '#message_key' do
165
167
  it "message_key has constant key for a class" do
166
168
  TestMessage.message_key.should eq "test1"
167
169
  TestMessage1.message_key.should eq "test2"
@@ -169,24 +171,27 @@ describe Lolitra::AmqpMessage, '#message_key' do
169
171
  end
170
172
  end
171
173
 
172
- describe Lolitra::AmqpMessageHandler, '#message_class_by_key' do
174
+ describe Lolitra::MessageHandler, '#message_class_by_key' do
173
175
  it "should return the message_class that belongs to key" do
174
- TestAmqpMessageHandler.message_class_by_key[TestMessage.message_key].name.should eq "TestMessage"
175
- TestAmqpMessageHandler.message_class_by_key[TestMessage1.message_key].name.should eq "TestMessage1"
176
+ TestMessageHandler1.handlers[TestMessage.message_key][0].name.should eq "TestMessage"
177
+ TestMessageHandler1.handlers[TestMessage1.message_key][0].name.should eq "TestMessage1"
176
178
  end
177
179
  end
178
180
 
179
181
  describe Lolitra::AmqpBus do
180
182
  it "should recive message when publish a message" do
181
183
  Lolitra::MessageHandlerManager.bus = Lolitra::AmqpBus.new(:host => "127.0.0.1", :exchange => "test_exchange")
182
- Lolitra::MessageHandlerManager.register(TestAmqpMessageHandler)
184
+ Lolitra::MessageHandlerManager.register_pull_subscriber(TestMessageHandlerAmqp)
185
+ Lolitra::MessageHandlerManager.register_pull_subscriber(TestMessageHandlerAmqp1)
183
186
 
184
- TestAmqpMessageHandler.should_receive(:handle) do |message|
187
+ TestMessageHandlerAmqp.should_receive(:handle) do |message|
185
188
  message.should be_an_instance_of TestMessage
186
189
  EM.stop { exit }
187
190
  end
188
191
 
189
- Lolitra::MessageHandlerManager.publish(TestMessage.new)
192
+ EM.add_timer(0.25) do
193
+ Lolitra::MessageHandlerManager.publish(TestMessage.new)
194
+ end
190
195
 
191
196
  EM.next_tick{ EM.add_timer(60) { EM.stop { exit } }}
192
197
  AMQP::Utilities::EventLoopHelper.eventmachine_thread.join
@@ -195,7 +200,48 @@ end
195
200
 
196
201
  describe Lolitra::AmqpBus do
197
202
  it "should return a unable to connect when specify a wrong amqp host" do
198
- expect { Lolitra::AmqpBus.new(:host => "192.168.123.123") }.to raise_error(UnableToConnect) #TODO
199
- pending
203
+ expect { Lolitra::AmqpBus.new(:host => "192.168.123.123") }.to raise_error(Exception) #TODO
204
+ end
205
+ end
206
+
207
+ describe Lolitra::AmqpBus do
208
+ it "should send all messages to all subscribers in non pull mode" do
209
+ Lolitra::MessageHandlerManager.bus = Lolitra::AmqpBus.new(:host => "127.0.0.1", :exchange => "test_exchange")
210
+ Lolitra::MessageHandlerManager.register_subscriber(TestMessageHandlerAmqp)
211
+ Lolitra::MessageHandlerManager.register_subscriber(TestMessageHandlerAmqp1)
212
+
213
+ @handled_message_1 = false
214
+ @handled_message_2 = false
215
+
216
+ TestMessageHandlerAmqp.should_receive(:handle) do |message|
217
+ message.should be_an_instance_of TestMessage
218
+ @handled_message_1 = true
219
+ EM.stop { exit } if @handled_message_1 && @handled_message_2
220
+ end
221
+
222
+ TestMessageHandlerAmqp1.should_receive(:handle) do |message|
223
+ message.should be_an_instance_of TestMessage
224
+ @handled_message_2 = true
225
+ EM.stop { exit } if @handled_message_1 && @handled_message_2
226
+ end
227
+
228
+ EM.add_timer(0.25){
229
+ Lolitra::MessageHandlerManager.publish(TestMessage.new)
230
+ }
231
+
232
+ EM.next_tick{ EM.add_timer(60) { EM.stop { exit } }}
233
+ AMQP::Utilities::EventLoopHelper.eventmachine_thread.join
234
+ end
235
+ end
236
+
237
+ describe Lolitra::Future,"#wait_message" do
238
+ it "should return the message" do
239
+ Lolitra::MessageHandlerManager.bus = Lolitra::AmqpBus.new(:host => "127.0.0.1", :exchange => "test_exchange")
240
+ EM.next_tick{
241
+ Lolitra::MessageHandlerManager.publish(TestMessage.new)
242
+ }
243
+ future = Lolitra::Future.new
244
+ message = future.wait_for(TestMessage)
245
+ message.should be_an_instance_of TestMessage
200
246
  end
201
247
  end
data/spec/spec_helper.rb CHANGED
@@ -21,6 +21,10 @@ class TestBus
21
21
  end
22
22
  end
23
23
 
24
+ def pull_subscribe(message_class, handler_class)
25
+ subscribe(message_class, handler_class)
26
+ end
27
+
24
28
  def subscribe(message_class, handler_class)
25
29
  @unmarshallers[message_class.message_key] = message_class
26
30
  @handlers[message_class.name] ||= []
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Hugo Freire
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-05-26 00:00:00 +02:00
17
+ date: 2012-05-28 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -73,8 +73,9 @@ files:
73
73
  - README.md
74
74
  - Rakefile
75
75
  - lib/lolitra.rb
76
+ - lib/lolitra/.future.rb.swp
76
77
  - lib/lolitra/.handler_base.rb.swp
77
- - lib/lolitra/.version.rb.swp
78
+ - lib/lolitra/future.rb
78
79
  - lib/lolitra/handler_base.rb
79
80
  - lib/lolitra/version.rb
80
81
  - lolitra.gemspec