propono 0.5.6 → 0.6.0

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.
Files changed (43) hide show
  1. checksums.yaml +8 -8
  2. data/.gitignore +1 -0
  3. data/.travis.yml +1 -1
  4. data/CONTRIBUTING.md +10 -7
  5. data/README.md +102 -28
  6. data/Rakefile +10 -0
  7. data/lib/propono/components/queue_subscription.rb +1 -5
  8. data/lib/propono/components/sns.rb +3 -7
  9. data/lib/propono/components/sqs.rb +3 -7
  10. data/lib/propono/configuration.rb +4 -10
  11. data/lib/propono/logger.rb +19 -0
  12. data/lib/propono/propono_error.rb +7 -0
  13. data/lib/propono/services/publisher.rb +5 -8
  14. data/lib/propono/services/queue_creator.rb +1 -1
  15. data/lib/propono/services/queue_listener.rb +9 -10
  16. data/lib/propono/services/topic_creator.rb +1 -1
  17. data/lib/propono/services/udp_listener.rb +12 -14
  18. data/lib/propono/version.rb +1 -1
  19. data/lib/propono.rb +95 -11
  20. data/propono.gemspec +1 -0
  21. data/test/{post_subscription_test.rb → components/post_subscription_test.rb} +1 -1
  22. data/test/{queue_subscription_test.rb → components/queue_subscription_test.rb} +3 -3
  23. data/test/{queue_test.rb → components/queue_test.rb} +1 -1
  24. data/test/{sns_test.rb → components/sns_test.rb} +4 -10
  25. data/test/{sqs_test.rb → components/sqs_test.rb} +4 -10
  26. data/test/{topic_test.rb → components/topic_test.rb} +1 -1
  27. data/test/config.yml.example +4 -0
  28. data/test/configuration_test.rb +25 -22
  29. data/test/integration/integration_test.rb +31 -0
  30. data/test/integration/sns_to_sqs_test.rb +21 -0
  31. data/test/integration/udp_proxy_test.rb +27 -0
  32. data/test/integration/udp_to_sqs_test.rb +30 -0
  33. data/test/logger_test.rb +35 -0
  34. data/test/propono_test.rb +32 -11
  35. data/test/{publisher_test.rb → services/publisher_test.rb} +22 -9
  36. data/test/{queue_creator_test.rb → services/queue_creator_test.rb} +1 -1
  37. data/test/services/queue_listener_test.rb +90 -0
  38. data/test/{subscriber_test.rb → services/subscriber_test.rb} +1 -1
  39. data/test/{topic_creator_test.rb → services/topic_creator_test.rb} +1 -1
  40. data/test/{udp_listener.rb → services/udp_listener_test.rb} +23 -14
  41. data/test/test_helper.rb +10 -29
  42. metadata +55 -26
  43. data/test/queue_listener_test.rb +0 -119
@@ -1,4 +1,4 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
4
  class QueueSubscriptionTest < Minitest::Test
@@ -23,7 +23,7 @@ module Propono
23
23
  end
24
24
 
25
25
  def test_subscription_queue_name
26
- config.application_name = "MyApp"
26
+ Propono.config.application_name = "MyApp"
27
27
 
28
28
  topic_id = "Foobar"
29
29
  subscription = QueueSubscription.new(topic_id)
@@ -32,7 +32,7 @@ module Propono
32
32
  end
33
33
 
34
34
  def test_subscription_queue_name_with_spaces
35
- config.application_name = "My App"
35
+ Propono.config.application_name = "My App"
36
36
 
37
37
  topic_id = "Foobar"
38
38
  subscription = QueueSubscription.new(topic_id)
@@ -1,4 +1,4 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
4
  class QueueTest < Minitest::Test
@@ -1,4 +1,4 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
4
  class SnsTest < Minitest::Test
@@ -7,9 +7,9 @@ module Propono
7
7
  end
8
8
 
9
9
  def setup
10
- config.access_key = "test-access-key"
11
- config.secret_key = "test-secret-key"
12
- config.queue_region = "test-queue-region"
10
+ Propono.config.access_key = "test-access-key"
11
+ Propono.config.secret_key = "test-secret-key"
12
+ Propono.config.queue_region = "test-queue-region"
13
13
  end
14
14
 
15
15
  def test_sns
@@ -20,11 +20,5 @@ module Propono
20
20
 
21
21
  SnsTestClass.new.send :sns
22
22
  end
23
-
24
- private
25
-
26
- def config
27
- Configuration.instance
28
- end
29
23
  end
30
24
  end
@@ -1,4 +1,4 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
4
  class SqsTest < Minitest::Test
@@ -7,9 +7,9 @@ module Propono
7
7
  end
8
8
 
9
9
  def setup
10
- config.access_key = "test-access-key"
11
- config.secret_key = "test-secret-key"
12
- config.queue_region = "us-east-1"
10
+ Propono.config.access_key = "test-access-key"
11
+ Propono.config.secret_key = "test-secret-key"
12
+ Propono.config.queue_region = "us-east-1"
13
13
  end
14
14
 
15
15
  def test_sqs
@@ -20,12 +20,6 @@ module Propono
20
20
 
21
21
  SqsTestClass.new.send :sqs
22
22
  end
23
-
24
- private
25
-
26
- def config
27
- Configuration.instance
28
- end
29
23
  end
30
24
  end
31
25
 
@@ -1,4 +1,4 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
4
  class TopicTest < Minitest::Test
@@ -0,0 +1,4 @@
1
+ access_key: test-aws-access-key
2
+ secret_key: test-aws-secret-key
3
+ queue_region: test-aws-region
4
+ application_name: tests-yourinitials
@@ -2,65 +2,68 @@ require File.expand_path('../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
4
  class ConfigurationTest < Minitest::Test
5
- def config
6
- @config ||= Configuration.send(:new)
5
+
6
+ def setup
7
+ Propono.instance_variable_set("@config", nil)
7
8
  end
8
9
 
9
10
  def test_obtaining_singletion
10
- refute config.nil?
11
+ refute Propono.config.nil?
11
12
  end
12
13
 
13
- def test_unable_to_create_instance
14
- assert_raises(NoMethodError) do
15
- config = Configuration.new
14
+ def test_block_syntax
15
+ test_key = "foobar-123-access"
16
+ Propono.config do |config|
17
+ config.access_key = test_key
16
18
  end
19
+ assert_equal test_key, Propono.config.access_key
17
20
  end
18
21
 
19
22
  def test_access_key
20
23
  access_key = "test-access-key"
21
- config.access_key = access_key
22
- assert_equal access_key, config.access_key
24
+ Propono.config.access_key = access_key
25
+ assert_equal access_key, Propono.config.access_key
23
26
  end
24
27
 
25
28
  def test_secret_key
26
29
  secret_key = "test-secret-key"
27
- config.secret_key = secret_key
28
- assert_equal secret_key, config.secret_key
30
+ Propono.config.secret_key = secret_key
31
+ assert_equal secret_key, Propono.config.secret_key
29
32
  end
30
33
 
31
34
  def test_queue_region
32
35
  queue_region = "test-queue-region"
33
- config.queue_region = queue_region
34
- assert_equal queue_region, config.queue_region
36
+ Propono.config.queue_region = queue_region
37
+ assert_equal queue_region, Propono.config.queue_region
35
38
  end
36
39
 
37
40
  def test_application_name
38
41
  application_name = "test-application-name"
39
- config.application_name = application_name
40
- assert_equal application_name, config.application_name
42
+ Propono.config.application_name = application_name
43
+ assert_equal application_name, Propono.config.application_name
41
44
  end
42
45
 
43
46
  def test_missing_access_key_throws_exception
44
- assert_raises(ConfigurationError) do
45
- config.access_key
47
+ assert_raises(ProponoConfigurationError) do
48
+ Propono.config.access_key
46
49
  end
47
50
  end
48
51
 
49
52
  def test_missing_secret_key_throws_exception
50
- assert_raises(ConfigurationError) do
51
- config.secret_key
53
+ assert_raises(ProponoConfigurationError) do
54
+ Propono.config.secret_key
52
55
  end
53
56
  end
54
57
 
55
58
  def test_missing_queue_region_throws_exception
56
- assert_raises(ConfigurationError) do
57
- config.queue_region
59
+ assert_raises(ProponoConfigurationError) do
60
+ Propono.config.queue_region
58
61
  end
59
62
  end
60
63
 
61
64
  def test_missing_application_name_throws_exception
62
- assert_raises(ConfigurationError) do
63
- config.application_name
65
+ assert_raises(ProponoConfigurationError) do
66
+ Propono.config.application_name
64
67
  end
65
68
  end
66
69
  end
@@ -0,0 +1,31 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Propono
4
+ class IntegrationTest < Minitest::Test
5
+ def setup
6
+ super
7
+ Fog.unmock!
8
+
9
+ config_file = YAML.load_file( File.expand_path('../../config.yml', __FILE__))
10
+ Propono.config do |config|
11
+ config.access_key = config_file['access_key']
12
+ config.secret_key = config_file['secret_key']
13
+ config.queue_region = config_file['queue_region']
14
+ config.application_name = config_file['application_name']
15
+ config.udp_host = "localhost"
16
+ config.udp_port = 12543
17
+ end
18
+ end
19
+
20
+ # Wait a max of 30secs before failing the test
21
+ def wait_for_thread(thread)
22
+ 300.times do |x|
23
+ return true unless thread.alive?
24
+ sleep(0.1)
25
+ end
26
+ false
27
+ end
28
+ end
29
+ end
30
+
31
+
@@ -0,0 +1,21 @@
1
+ require File.expand_path('../integration_test', __FILE__)
2
+
3
+ module Propono
4
+ class SnsToSqsTest < IntegrationTest
5
+ def test_the_message_gets_there
6
+ topic = "test-topic"
7
+ text = "This is my message"
8
+
9
+ thread = Thread.new do
10
+ Propono.listen_to_queue(topic) do |message|
11
+ assert_equal text, message
12
+ break
13
+ end
14
+ end
15
+ Propono.publish(topic, text)
16
+ flunk unless wait_for_thread(thread)
17
+ ensure
18
+ thread.terminate
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path('../integration_test', __FILE__)
2
+
3
+ module Propono
4
+ class UdpProxyTest < IntegrationTest
5
+ def test_the_message_gets_there
6
+ topic = "test-topic"
7
+ message = "This is my message"
8
+
9
+ udp_thread = Thread.new do
10
+ Propono.proxy_udp
11
+ end
12
+
13
+ sqs_thread = Thread.new do
14
+ Propono.listen_to_queue(topic) do |sqs_message|
15
+ assert_equal message, sqs_message
16
+ sqs_thread.terminate
17
+ end
18
+ end
19
+
20
+ Propono.publish(topic, message, protocol: :udp)
21
+ flunk unless wait_for_thread(sqs_thread)
22
+ ensure
23
+ udp_thread.terminate
24
+ sqs_thread.terminate
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path('../integration_test', __FILE__)
2
+
3
+ module Propono
4
+ class UdpToSqsTest < IntegrationTest
5
+ def test_the_message_gets_there
6
+ topic = "test-topic"
7
+ message = "This is my message"
8
+
9
+ udp_thread = Thread.new do
10
+ Propono.listen_to_udp do |udp_topic, udp_message|
11
+ Propono.publish(udp_topic, udp_message)
12
+ udp_thread.terminate
13
+ end
14
+ end
15
+
16
+ sqs_thread = Thread.new do
17
+ Propono.listen_to_queue(topic) do |sqs_message|
18
+ assert_equal message, sqs_message
19
+ sqs_thread.terminate
20
+ end
21
+ end
22
+
23
+ Propono.publish(topic, message, protocol: :udp)
24
+ flunk unless wait_for_thread(udp_thread) && wait_for_thread(sqs_thread)
25
+ ensure
26
+ udp_thread.terminate
27
+ sqs_thread.terminate
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ module Propono
4
+ class LoggerTest < Minitest::Test
5
+ def setup
6
+ super
7
+ @logger = Logger.new
8
+ end
9
+
10
+ def test_debug
11
+ $stdout.expects(:puts).with("foobar")
12
+ @logger.debug "foobar"
13
+ end
14
+
15
+ def test_info
16
+ $stdout.expects(:puts).with("foobar")
17
+ @logger.info "foobar"
18
+ end
19
+
20
+ def test_warn
21
+ $stdout.expects(:puts).with("foobar")
22
+ @logger.warn "foobar"
23
+ end
24
+
25
+ def test_error
26
+ $stderr.expects(:puts).with("foobar")
27
+ @logger.error "foobar"
28
+ end
29
+
30
+ def test_fatal
31
+ $stderr.expects(:puts).with("foobar")
32
+ @logger.fatal "foobar"
33
+ end
34
+ end
35
+ end
data/test/propono_test.rb CHANGED
@@ -3,25 +3,46 @@ require File.expand_path('../test_helper', __FILE__)
3
3
  module Propono
4
4
  class ProponoTest < Minitest::Test
5
5
 
6
- def setup
7
- super
8
- @var1 = "Foobar"
9
- @var2 = 123
6
+ def test_publish_calls_publisher_publish
7
+ topic, message = "Foo", "Bar"
8
+ Publisher.expects(:publish).with(topic, message, {})
9
+ Propono.publish(topic, message)
10
10
  end
11
11
 
12
- def test_publish_calls_publisher_public
13
- Publisher.expects(:publish).with(@var1, @var2)
14
- Propono.publish(@var1, @var2)
12
+ def test_subscribe_by_queue_calls_subscribe
13
+ topic = 'foobar'
14
+ Subscriber.expects(:subscribe_by_queue).with(topic)
15
+ Propono.subscribe_by_queue(topic)
16
+ end
17
+
18
+ def test_subscribe_by_post_calls_subscribe
19
+ topic, endpoint = 'foo', 'bar'
20
+ Subscriber.expects(:subscribe_by_post).with(topic, endpoint)
21
+ Propono.subscribe_by_post(topic, endpoint)
15
22
  end
16
23
 
17
24
  def test_listen_to_queue_calls_queue_listener
18
- QueueListener.expects(:listen).with(@var1, @var2)
19
- Propono.listen_to_queue(@var1, @var2)
25
+ topic = 'foobar'
26
+ QueueListener.expects(:listen).with(topic)
27
+ Propono.listen_to_queue(topic)
20
28
  end
21
29
 
22
30
  def test_listen_to_udp_calls_udp_listener
23
- UdpListener.expects(:listen).with(@var1, @var2)
24
- Propono.listen_to_udp(@var1, @var2)
31
+ UdpListener.expects(:listen).with()
32
+ Propono.listen_to_udp()
33
+ end
34
+
35
+ def test_proxy_udp_calls_listen
36
+ UdpListener.expects(:listen).with()
37
+ Propono.proxy_udp()
38
+ end
39
+
40
+ def test_proxy_udp_calls_publish_in_the_block
41
+ topic = "foobar"
42
+ message = "message"
43
+ Propono.stubs(:listen_to_udp).yields(topic, message)
44
+ Publisher.expects(:publish).with(topic, message, {})
45
+ Propono.proxy_udp
25
46
  end
26
47
  end
27
48
  end
@@ -1,4 +1,4 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
4
  class PublisherTest < Minitest::Test
@@ -52,6 +52,21 @@ module Propono
52
52
  publisher.send(:publish_via_sns)
53
53
  end
54
54
 
55
+ def test_publish_via_sns_should_accept_a_hash_for_message
56
+ topic = "topic123"
57
+ message = {something: ['some', 123, true]}
58
+ topic_arn = "arn123"
59
+ topic = Topic.new(topic_arn)
60
+
61
+ TopicCreator.stubs(find_or_create: topic)
62
+
63
+ sns = mock()
64
+ sns.expects(:publish).with(topic_arn, message.to_json)
65
+ publisher = Publisher.new(topic, message)
66
+ publisher.stubs(sns: sns)
67
+ publisher.send(:publish_via_sns)
68
+ end
69
+
55
70
  def test_publish_via_sns_should_propogate_exception_on_topic_creation_error
56
71
  TopicCreator.stubs(:find_or_create).raises(TopicCreatorError)
57
72
 
@@ -79,8 +94,8 @@ module Propono
79
94
  def test_udp_uses_correct_message_host_and_port
80
95
  host = "http://meducation.net"
81
96
  port = 1234
82
- config.udp_host = host
83
- config.udp_port = port
97
+ Propono.config.udp_host = host
98
+ Propono.config.udp_port = port
84
99
  topic_id = "my-fav-topic"
85
100
  message = "foobar"
86
101
  payload = {topic: topic_id, message: message}.to_json
@@ -93,14 +108,12 @@ module Propono
93
108
  def test_client_with_bad_host_logs_error
94
109
  host = "http://meducation.net"
95
110
  port = 1234
96
- config.udp_host = host
97
- config.udp_port = port
111
+ Propono.config.udp_host = host
112
+ Propono.config.udp_port = port
98
113
 
99
114
  client = Publisher.new("topic_id", "message")
100
- _, err = capture_io do
101
- client.send(:publish_via_udp)
102
- end
103
- assert_match("Udp2sqs failed to send : getaddrinfo:", err)
115
+ Propono.config.logger.expects(:error).with() {|x| x =~ /^Propono failed to send : getaddrinfo:.*/}
116
+ client.send(:publish_via_udp)
104
117
  end
105
118
 
106
119
  def test_publish_should_raise_exception_if_topic_is_nil
@@ -1,4 +1,4 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
4
  class QueueCreatorTest < Minitest::Test
@@ -0,0 +1,90 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Propono
4
+ class QueueListenerTest < Minitest::Test
5
+
6
+ def setup
7
+ super
8
+ @topic_id = "some-topic"
9
+
10
+ @receipt_handle1 = "test-receipt-handle1"
11
+ @receipt_handle2 = "test-receipt-handle2"
12
+ @message1 = "Foobar 123"
13
+ @message2 = "Barfoo 543"
14
+ @sqs_message1 = { "ReceiptHandle" => @receipt_handle1, "Body" => {"Message" => @message1}.to_json}
15
+ @sqs_message2 = { "ReceiptHandle" => @receipt_handle2, "Body" => {"Message" => @message2}.to_json}
16
+ @messages = { "Message" => [ @sqs_message1, @sqs_message2 ] }
17
+ @sqs_response = mock().tap{|m|m.stubs(body: @messages)}
18
+ @sqs = mock()
19
+ @sqs.stubs(receive_message: @sqs_response)
20
+ @sqs.stubs(:delete_message)
21
+
22
+ @listener = QueueListener.new(@topic_id) {}
23
+ @listener.stubs(sqs: @sqs)
24
+ end
25
+
26
+ def test_listen_should_loop
27
+ @listener.expects(:loop)
28
+ @listener.listen
29
+ end
30
+
31
+ def test_read_messages_should_subscribe
32
+ QueueSubscription.expects(create: mock(queue: mock(url: {})))
33
+ @listener.send(:read_messages)
34
+ end
35
+
36
+ def test_read_message_from_sqs
37
+ queue_url = @listener.send(:queue_url)
38
+ options = { 'MaxNumberOfMessages' => 10 }
39
+ @sqs.expects(:receive_message).with(queue_url, options).returns(@sqs_response)
40
+ @listener.send(:read_messages)
41
+ end
42
+
43
+ def test_read_messages_calls_process_message_for_each_msg
44
+ @listener.expects(:process_sqs_message).with(@sqs_message1)
45
+ @listener.expects(:process_sqs_message).with(@sqs_message2)
46
+ @listener.send(:read_messages)
47
+ end
48
+
49
+ def test_read_messages_does_not_call_process_messages_if_there_are_none
50
+ @sqs_response.stubs(body: {"Message" => []})
51
+ @listener.expects(:process_sqs_message).never
52
+ @listener.send(:read_messages)
53
+ end
54
+
55
+ def test_exception_from_sqs_is_logged
56
+ @listener.stubs(queue_url: "http://example.com")
57
+ @sqs.stubs(:receive_message).raises(StandardError)
58
+ Propono.config.logger.expects(:error).with("Unexpected error reading from queue http://example.com")
59
+ Propono.config.logger.expects(:error).with() {|x| x.is_a?(StandardError)}
60
+ @listener.send(:read_messages)
61
+ end
62
+
63
+ def test_exception_from_sqs_returns_false
64
+ @sqs.stubs(:receive_message).raises(StandardError)
65
+ refute @listener.send(:read_messages)
66
+ end
67
+
68
+ def test_each_message_processor_is_yielded
69
+ messages_yielded = [ ]
70
+ @listener = QueueListener.new(@topic_id) { |m| messages_yielded.push(m) }
71
+ @listener.stubs(sqs: @sqs)
72
+
73
+ @listener.send(:read_messages)
74
+
75
+ assert_equal messages_yielded.size, 2
76
+ assert messages_yielded.include?(@message1)
77
+ assert messages_yielded.include?(@message2)
78
+ end
79
+
80
+ def test_each_message_is_deleted
81
+ queue_url = "test-queue-url"
82
+
83
+ @sqs.expects(:delete_message).with(queue_url, @receipt_handle1)
84
+ @sqs.expects(:delete_message).with(queue_url, @receipt_handle2)
85
+
86
+ @listener.stubs(queue_url: queue_url)
87
+ @listener.send(:read_messages)
88
+ end
89
+ end
90
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
4
  class SubscriberTest < Minitest::Test
@@ -1,4 +1,4 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
4
  class TopicCreatorTest < Minitest::Test
@@ -1,16 +1,11 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Propono
4
4
  class UdpListenerTest < Minitest::Test
5
5
 
6
6
  def test_intialize_sets_locals
7
- host = "my-host"
8
- port = 1234
9
7
  block = Proc.new {}
10
-
11
- listener = UdpListener.new(host, port, &block)
12
- assert_equal host, listener.instance_variable_get("@host")
13
- assert_equal port, listener.instance_variable_get("@port")
8
+ listener = UdpListener.new(&block)
14
9
  assert_equal block, listener.instance_variable_get("@processor")
15
10
  end
16
11
 
@@ -18,31 +13,45 @@ module Propono
18
13
  host = "my-host"
19
14
  port = 1234
20
15
 
16
+ Propono.config.udp_host = host
17
+ Propono.config.udp_port = port
18
+
21
19
  UDPSocket.any_instance.expects(:bind).with(host, port)
22
20
 
23
- listener = UdpListener.new(host, port) {}
21
+ listener = UdpListener.new() {}
24
22
  socket = listener.send(:socket)
25
23
  end
26
24
 
27
25
  def test_initialize_should_fail_without_a_block
28
26
  assert_raises(UdpListenerError) do
29
- UdpListener.new("qwe", 123)
27
+ UdpListener.new
30
28
  end
31
29
  end
32
30
 
33
31
  def test_message_is_processed
34
- text = "Foobar123"
32
+ udp_msg = "Foobar"
35
33
  processor = Proc.new {}
36
- server = UdpListener.new("qwewqe", 123, &processor)
37
- socket = mock(recvfrom: [text])
34
+ server = UdpListener.new(&processor)
35
+ socket = mock(recvfrom: [udp_msg])
38
36
  server.stubs(socket: socket)
39
- processor.expects(:call).with(text)
37
+ server.expects(:process_udp_data).with(udp_msg)
40
38
  thread = server.send(:receive_and_process)
41
39
  thread.join
42
40
  end
43
41
 
42
+ def test_processor_is_called_correctly
43
+ topic = "my-topic"
44
+ message = "my-message"
45
+ processor = Proc.new {}
46
+ udp_data = {topic: topic, message: message}.to_json
47
+ processor.expects(:call).with(topic, message)
48
+
49
+ server = UdpListener.new(&processor)
50
+ server.send(:process_udp_data, udp_data)
51
+ end
52
+
44
53
  def test_listen_should_loop
45
- listener = UdpListener.new("qwewqe", 123) {}
54
+ listener = UdpListener.new {}
46
55
  listener.expects(:loop)
47
56
  listener.listen
48
57
  end