freddy 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Freddy::MessageHandler do
4
+ subject(:handler) { described_class.new(adapter, delivery) }
5
+
6
+ let(:adapter) { double }
7
+ let(:delivery) { double(properties: properties) }
8
+ let(:properties) { {reply_to: reply_to} }
9
+
10
+ let(:reply_to) { double }
11
+
12
+ describe '#success' do
13
+ it 'delegates to the adapter' do
14
+ expect(adapter).to receive(:success).with(reply_to, x: 'y')
15
+
16
+ subject.success(x: 'y')
17
+ end
18
+ end
19
+
20
+ describe '#error' do
21
+ it 'delegates to the adapter' do
22
+ expect(adapter).to receive(:error).with(reply_to, error: 'text')
23
+
24
+ subject.error(error: 'text')
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Freddy::Request do
4
+ let(:freddy) { Freddy.build(logger, config) }
5
+
6
+ let(:destination) { random_destination }
7
+ let(:payload) { {pay: 'load'} }
8
+
9
+ let(:request) { freddy.request }
10
+
11
+ it 'raises empty responder exception when responding without callback' do
12
+ expect {@responder = request.respond_to destination }.to raise_error described_class::EmptyResponder
13
+ end
14
+
15
+ context 'requesting from multiple threads' do
16
+ let(:nr_of_threads) { 10 }
17
+
18
+ before do
19
+ freddy.respond_to 'thread-queue' do |payload, msg_handler|
20
+ msg_handler.success(payload)
21
+ end
22
+ end
23
+
24
+ it 'handles multiple threads' do
25
+ msg_counter = 0
26
+ nr_of_threads.times.map do
27
+ Thread.new do
28
+ response = freddy.deliver_with_response 'thread-queue', payload
29
+ msg_counter += 1
30
+ expect(response).to eq(payload)
31
+ end
32
+ end.each(&:join)
33
+ expect(msg_counter).to eq(nr_of_threads)
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Freddy::ResponderHandler do
4
+ let(:freddy) { Freddy.build(logger, config) }
5
+
6
+ let(:destination) { random_destination }
7
+ let(:payload) { {pay: 'load'} }
8
+
9
+ it 'can cancel listening for messages' do
10
+ consumer_handler = freddy.respond_to destination do
11
+ @messages_count ||= 0
12
+ @messages_count += 1
13
+ end
14
+ deliver
15
+ consumer_handler.cancel
16
+ deliver
17
+
18
+ expect(@messages_count).to eq 1
19
+ end
20
+
21
+ it 'can join the thread to the consumer' do
22
+ consumer_handler = freddy.respond_to destination do
23
+ end
24
+ unreachable = true
25
+ Thread.new do
26
+ consumer_handler.join
27
+ unreachable = false
28
+ end
29
+ wait_for { unreachable }
30
+ expect(unreachable).to be(true)
31
+ end
32
+
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Logging' do
4
+ let(:freddy1) { Freddy.build(logger1, config) }
5
+ let(:freddy2) { Freddy.build(logger2, config) }
6
+
7
+ let(:logger1) { spy('logger1') }
8
+ let(:logger2) { spy('logger2') }
9
+
10
+ let(:destination) { random_destination }
11
+ let(:payload) { {pay: 'load'} }
12
+
13
+ before do
14
+ freddy1.respond_to destination do |payload, msg_handler|
15
+ msg_handler.success
16
+ end
17
+
18
+ freddy2.deliver_with_response(destination, payload) { }
19
+ default_sleep
20
+ end
21
+
22
+ it 'logs all consumed messages' do
23
+ expect(logger1).to have_received(:debug).with(/Listening for requests on \S+/)
24
+ expect(logger1).to have_received(:debug).with(/Consuming messages on \S+/)
25
+ expect(logger1).to have_received(:debug).with(/Received message on \S+ with payload {:pay=>"load"}/)
26
+ end
27
+
28
+ it 'logs all produced messages' do
29
+ expect(logger2).to have_received(:debug).with(/Consuming messages on \S+/)
30
+ expect(logger2).to have_received(:debug).with(/Publishing request to \S+, waiting for response on amq.gen-\S+ with correlation_id .*/)
31
+ expect(logger2).to have_received(:debug).with(/Producing message {:pay=>"load"} to \S+/)
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ require 'pry'
2
+ require 'securerandom'
3
+ require 'freddy'
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ config.order = 'random'
10
+ end
11
+
12
+ class Freddy::Consumer
13
+ def create_queue(queue_name)
14
+ #want to auto_delete queues while testing
15
+ @channel.queue(queue_name, auto_delete: true)
16
+ end
17
+ end
18
+
19
+ def random_destination
20
+ SecureRandom.hex
21
+ end
22
+
23
+ def default_sleep
24
+ sleep 0.05
25
+ end
26
+
27
+ def wait_for(&block)
28
+ 100.times do
29
+ return if block.call
30
+ sleep 0.005
31
+ end
32
+ end
33
+
34
+ def deliver(custom_destination = destination)
35
+ freddy.deliver custom_destination, payload
36
+ default_sleep
37
+ end
38
+
39
+ def logger
40
+ Logger.new(STDOUT).tap { |l| l.level = Logger::ERROR }
41
+ end
42
+
43
+ def config
44
+ {host: 'localhost', port: 5672, user: 'guest', pass: 'guest'}
45
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: freddy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Urmas Talimaa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bunny
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.6.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.6.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: symbolizer
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: hamster
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.1.pre.rc3
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.0.1.pre.rc3
83
+ description: Messaging API
84
+ email:
85
+ - urmas.talimaa@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".npmignore"
92
+ - ".rspec"
93
+ - ".ruby-gemset"
94
+ - ".ruby-version"
95
+ - ".travis.yml"
96
+ - Gemfile
97
+ - Gemfile.lock
98
+ - LICENCE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - freddy.gemspec
102
+ - lib/freddy.rb
103
+ - lib/freddy/consumer.rb
104
+ - lib/freddy/delivery.rb
105
+ - lib/freddy/message_handler.rb
106
+ - lib/freddy/message_handlers.rb
107
+ - lib/freddy/producer.rb
108
+ - lib/freddy/request.rb
109
+ - lib/freddy/request_manager.rb
110
+ - lib/freddy/responder_handler.rb
111
+ - lib/freddy/sync_response_container.rb
112
+ - spec/freddy/consumer_spec.rb
113
+ - spec/freddy/freddy_spec.rb
114
+ - spec/freddy/message_handler_spec.rb
115
+ - spec/freddy/request_spec.rb
116
+ - spec/freddy/responder_handler_spec.rb
117
+ - spec/integration/logging_spec.rb
118
+ - spec/spec_helper.rb
119
+ homepage:
120
+ licenses:
121
+ - Private
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.2.2
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: API for inter-application messaging supporting acknowledgements and request-response
143
+ test_files:
144
+ - spec/freddy/consumer_spec.rb
145
+ - spec/freddy/freddy_spec.rb
146
+ - spec/freddy/message_handler_spec.rb
147
+ - spec/freddy/request_spec.rb
148
+ - spec/freddy/responder_handler_spec.rb
149
+ - spec/integration/logging_spec.rb
150
+ - spec/spec_helper.rb