propono 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MTA1OTNkMDY5NmY4YzRjNDAxYjRiOGJiMWFhNGM0NzRkMmFkYTY5OQ==
5
- data.tar.gz: !binary |-
6
- NjY0MDAzODFkZmEyMDAzYWI3ZDQwYmQyMWE4ZTEyNWUwYTlmYjc3OQ==
2
+ SHA1:
3
+ metadata.gz: e24f186302b02c6e3a2896ab707fbbcc2cd6b447
4
+ data.tar.gz: 813ea1ee8356ae861fc0f5b77f84eb08b5497b26
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZDM2MzM5ZWUxZGIxODMyMGRiZjBjNmRjZjI1NDg0MTM2Y2I2M2ZhY2U2NDBh
10
- MTA4ZTRiNDhkMmRiZjM1MWFmMWI5OGQ4YTU2MzI3N2E5ODdkNGRhMTBlMTM1
11
- ZjJiYWFhYTgyN2Y3YjI3MzZmZjJhMzU2YmFjOGY0OGM2MTUzMWQ=
12
- data.tar.gz: !binary |-
13
- N2VhYTFjZTNmOGJiNGMyZjY2OGViNjZmODZlY2Y2OWM5OGExNjRkOTAxYWJm
14
- Y2JkNjQ1YzY4NWRlZWUzOWNkZTA0YzJiYzBjOTU4ZmE3MzgzZTNhNmNlZWU1
15
- NTY2NDE5MDE4YzJjZTVjMzNkMWUyZDBmZTdiYjE2NzNjYjU3ODc=
6
+ metadata.gz: 28b340921d2d6681824f219864f1875c9f3136670ea869aa621a8a3a5d23326a4fcac618abb39e1313b8075497906a95fa7d5da573839f01766b82436019f1b7
7
+ data.tar.gz: 0adc3c6ef53887926106b62c60ca6a4fd0800a5519ffd48bd9ea792ecc0320404d37d679668955d81fdb5e0d50e9760369e5074b35c864d4c64447d18136b974
@@ -20,6 +20,7 @@ module Propono
20
20
  @topic = TopicCreator.find_or_create(@topic_id)
21
21
  @queue = QueueCreator.find_or_create(queue_name)
22
22
  sns.subscribe(@topic.arn, @queue.arn, 'sqs')
23
+ sqs.set_queue_attributes(@queue.url, "Policy", policy)
23
24
  end
24
25
 
25
26
  def queue_name
@@ -31,5 +32,30 @@ module Propono
31
32
  def config
32
33
  Configuration.instance
33
34
  end
35
+
36
+ def policy
37
+ <<-EOS
38
+ {
39
+ "Version": "2008-10-17",
40
+ "Id": "arn:aws:sqs:eu-west-1:950417255687:manual_queue/SQSDefaultPolicy",
41
+ "Statement": [
42
+ {
43
+ "Sid": "Sid1382106399628",
44
+ "Effect": "Allow",
45
+ "Principal": {
46
+ "AWS": "*"
47
+ },
48
+ "Action": "SQS:SendMessage",
49
+ "Resource": "arn:aws:sqs:eu-west-1:950417255687:manual_queue",
50
+ "Condition": {
51
+ "ArnEquals": {
52
+ "aws:SourceArn": "arn:aws:sns:eu-west-1:950417255687:metrics"
53
+ }
54
+ }
55
+ }
56
+ ]
57
+ }
58
+ EOS
59
+ end
34
60
  end
35
61
  end
@@ -9,10 +9,9 @@ module Propono
9
9
  include Singleton
10
10
 
11
11
  SETTINGS = [
12
- :access_key,
13
- :secret_key,
14
- :queue_region,
12
+ :access_key, :secret_key, :queue_region,
15
13
  :application_name,
14
+ :udp_host, :udp_port,
16
15
  :logger
17
16
  ]
18
17
  attr_writer *SETTINGS
@@ -5,16 +5,41 @@ module Propono
5
5
  class Publisher
6
6
  include Sns
7
7
 
8
- def self.publish(topic, message)
9
- new.publish(topic, message)
8
+ def self.publish(topic_id, message, options = {})
9
+ new(topic_id, message, options).publish
10
10
  end
11
11
 
12
- def publish(topic_id, message)
12
+ attr_reader :topic_id, :message, :protocol
13
+
14
+ def initialize(topic_id, message, options = {})
13
15
  raise PublisherError.new("Topic is nil") if topic_id.nil?
14
16
  raise PublisherError.new("Message is nil") if message.nil?
15
17
 
18
+ @topic_id = topic_id
19
+ @message = message
20
+ @protocol = options.fetch(:protocol, :sns).to_sym
21
+ end
22
+
23
+ def publish
24
+ send("publish_via_#{protocol}")
25
+ end
26
+
27
+ private
28
+
29
+ def publish_via_sns
16
30
  topic = TopicCreator.find_or_create(topic_id)
17
31
  sns.publish(topic.arn, message)
18
32
  end
33
+
34
+ def publish_via_udp
35
+ payload = {topic: topic_id, message: message}.to_json
36
+ UDPSocket.new.send(payload, 0, config.udp_host, config.udp_port)
37
+ rescue SocketError => e
38
+ config.logger.puts "Udp2sqs failed to send : #{e}"
39
+ end
40
+
41
+ def config
42
+ Configuration.instance
43
+ end
19
44
  end
20
45
  end
@@ -1,3 +1,3 @@
1
1
  module Propono
2
- VERSION = "0.5.4"
2
+ VERSION = "0.5.5"
3
3
  end
@@ -4,51 +4,103 @@ module Propono
4
4
  class PublisherTest < Minitest::Test
5
5
 
6
6
  def test_initialization
7
- notifier = Publisher.new
8
- refute notifier.nil?
7
+ publisher = Publisher.new('topic', 'message')
8
+ refute publisher.nil?
9
9
  end
10
10
 
11
- def test_self_publish_calls_publish
11
+ def test_self_publish_calls_new
12
12
  topic = "topic123"
13
13
  message = "message123"
14
- Publisher.any_instance.expects(:publish).with(topic, message)
15
- Publisher.new.publish(topic, message)
14
+ Publisher.expects(:new).with(topic, message, {}).returns(mock(publish: nil))
15
+ Publisher.publish(topic, message)
16
16
  end
17
17
 
18
- def test_publish_should_call_sns_on_correct_topic_and_message
18
+ def test_self_publish_calls_publish
19
+ Publisher.any_instance.expects(:publish)
20
+ Publisher.publish("topic", "message")
21
+ end
22
+
23
+ def test_protocol_should_be_sns_by_default
24
+ publisher = Publisher.new('topic', 'message')
25
+ assert_equal :sns, publisher.protocol
26
+ end
27
+
28
+ def test_publish_proxies_to_sns
29
+ publisher = Publisher.new('topic', 'message')
30
+ publisher.expects(:publish_via_sns)
31
+ publisher.publish
32
+ end
33
+
34
+ def test_publish_proxies_to_udp
35
+ publisher = Publisher.new('topic', 'message', protocol: :udp)
36
+ publisher.expects(:publish_via_udp)
37
+ publisher.publish
38
+ end
39
+
40
+ def test_publish_via_sns_should_call_sns_on_correct_topic_and_message
19
41
  topic = "topic123"
20
42
  message = "message123"
21
43
  topic_arn = "arn123"
44
+ topic = Topic.new(topic_arn)
22
45
 
23
- TopicCreator.stubs(find_or_create: topic_arn)
46
+ TopicCreator.stubs(find_or_create: topic)
24
47
 
25
48
  sns = mock()
26
49
  sns.expects(:publish).with(topic_arn, message)
27
- publisher = Publisher.new
50
+ publisher = Publisher.new(topic, message)
28
51
  publisher.stubs(sns: sns)
29
-
30
- publisher.publish(topic, message)
52
+ publisher.send(:publish_via_sns)
31
53
  end
32
54
 
33
- def test_publish_should_propogate_exception_on_topic_creation_error
55
+ def test_publish_via_sns_should_propogate_exception_on_topic_creation_error
34
56
  TopicCreator.stubs(:find_or_create).raises(TopicCreatorError)
35
57
 
36
58
  assert_raises(TopicCreatorError) do
37
- Publisher.publish("topic", "message")
59
+ publisher = Publisher.new("topic", "message")
60
+ publisher.send(:publish_via_sns)
38
61
  end
39
62
  end
40
63
 
41
- def test_publish_creates_a_topic
42
- topic = "Malcs_topic"
64
+ def test_publish_via_sns_creates_a_topic
65
+ topic_id = "Malcs_topic_id"
66
+ topic_arn = "Malcs_topic_arn"
67
+ topic = Topic.new(topic_arn)
43
68
 
44
- TopicCreator.expects(:find_or_create).with(topic)
69
+ TopicCreator.expects(:find_or_create).with(topic_id).returns(topic)
45
70
 
46
71
  sns = mock()
47
72
  sns.stubs(:publish)
48
- publisher = Publisher.new
73
+ publisher = Publisher.new(topic_id, "Foobar")
49
74
  publisher.stubs(sns: sns)
50
75
 
51
- publisher.publish(topic, "Foobar")
76
+ publisher.send(:publish_via_sns)
77
+ end
78
+
79
+ def test_udp_uses_correct_message_host_and_port
80
+ host = "http://meducation.net"
81
+ port = 1234
82
+ config.udp_host = host
83
+ config.udp_port = port
84
+ topic_id = "my-fav-topic"
85
+ message = "foobar"
86
+ payload = {topic: topic_id, message: message}.to_json
87
+ UDPSocket.any_instance.expects(:send).with(payload, 0, host, port)
88
+
89
+ publisher = Publisher.new(topic_id, message)
90
+ publisher.send(:publish_via_udp)
91
+ end
92
+
93
+ def test_client_with_bad_host_logs_error
94
+ host = "http://meducation.net"
95
+ port = 1234
96
+ config.udp_host = host
97
+ config.udp_port = port
98
+
99
+ 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)
52
104
  end
53
105
 
54
106
  def test_publish_should_raise_exception_if_topic_is_nil
@@ -85,14 +85,9 @@ module Propono
85
85
  queue_listener.stubs(sqs: sqs)
86
86
  queue_listener.stubs(queue_url: "http://example.com")
87
87
 
88
- # capture_io reasigns stderr. Assign the config.logger
89
- # to where capture_io has redirected it to for this test.
90
88
  out, err = capture_io do
91
- config.logger = $stderr
92
89
  queue_listener.send(:read_messages)
93
90
  end
94
- # Reassign config.logger to the correct stderr
95
- config.logger = $stderr
96
91
  assert_equal "Unexpected error reading from queue http://example.com\n", err
97
92
  end
98
93
 
@@ -40,7 +40,7 @@ module Propono
40
40
  assert_equal subscription.send(:queue_name), "My_App-Foobar"
41
41
  end
42
42
 
43
- def test_create_calls_create
43
+ def test_create_calls_subscribe
44
44
  arn = "arn123"
45
45
 
46
46
  TopicCreator.stubs(find_or_create: Topic.new(arn))
@@ -53,6 +53,21 @@ module Propono
53
53
  subscription.create
54
54
  end
55
55
 
56
+ def test_create_calls_set_queue_attributes
57
+ arn = "arn123"
58
+ policy = "{foobar: 123}"
59
+
60
+ TopicCreator.stubs(find_or_create: Topic.new(arn))
61
+ QueueCreator.stubs(find_or_create: Queue.new(Fog::AWS::SQS::Mock::QueueUrl))
62
+
63
+ sqs = mock()
64
+ sqs.expects(:set_queue_attributes).with(Fog::AWS::SQS::Mock::QueueUrl, "Policy", policy)
65
+ subscription = QueueSubscription.new("Some topic")
66
+ subscription.stubs(sqs: sqs)
67
+ subscription.stubs(policy: policy)
68
+ subscription.create
69
+ end
70
+
56
71
  def test_create_saves_queue
57
72
  queue = Queue.new(Fog::AWS::SQS::Mock::QueueUrl)
58
73
 
data/test/test_helper.rb CHANGED
@@ -25,8 +25,28 @@ class Minitest::Test
25
25
  def config
26
26
  Propono::Configuration.instance
27
27
  end
28
+
29
+ # capture_io reasigns stderr. Assign the config.logger
30
+ # to where capture_io has redirected it to for this test.
31
+ def capture_io(&block)
32
+ require 'stringio'
33
+
34
+ orig_stdout, orig_stderr = $stdout, $stderr
35
+ captured_stdout, captured_stderr = StringIO.new, StringIO.new
36
+ $stdout, $stderr = captured_stdout, captured_stderr
37
+
38
+ config.logger = $stderr
39
+ yield
40
+
41
+ return captured_stdout.string, captured_stderr.string
42
+ ensure
43
+ $stdout = orig_stdout
44
+ $stderr = orig_stderr
45
+ config.logger = $stderr
46
+ end
28
47
  end
29
48
 
49
+ require 'fog'
30
50
  class Fog::AWS::SNS::Mock
31
51
  def create_topic(*args)
32
52
  foo = Object.new
@@ -42,10 +62,11 @@ class Fog::AWS::SNS::Mock
42
62
  end
43
63
  end
44
64
 
45
- require 'fog'
46
65
  class Fog::AWS::SQS::Mock
47
66
  def create_queue(*args)
48
67
  end
68
+ def set_queue_attributes(*args)
69
+ end
49
70
  end
50
71
 
51
72
  Fog::AWS::SQS::Mock::QueueUrl = 'https://meducation.net/foobar'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: propono
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - MalcyL
@@ -43,28 +43,28 @@ dependencies:
43
43
  name: rake
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ! '>='
46
+ - - '>='
47
47
  - !ruby/object:Gem::Version
48
48
  version: '0'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ! '>='
53
+ - - '>='
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: mocha
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ! '>='
60
+ - - '>='
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ! '>='
67
+ - - '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  - !ruby/object:Gem::Dependency
@@ -137,12 +137,12 @@ require_paths:
137
137
  - lib
138
138
  required_ruby_version: !ruby/object:Gem::Requirement
139
139
  requirements:
140
- - - ! '>='
140
+ - - '>='
141
141
  - !ruby/object:Gem::Version
142
142
  version: '0'
143
143
  required_rubygems_version: !ruby/object:Gem::Requirement
144
144
  requirements:
145
- - - ! '>='
145
+ - - '>='
146
146
  - !ruby/object:Gem::Version
147
147
  version: '0'
148
148
  requirements: []