cpi-event-connector 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -7,22 +7,22 @@ GIT
7
7
  PATH
8
8
  remote: .
9
9
  specs:
10
- cpi-event-connector (0.0.10)
11
- activesupport (>= 3.1)
10
+ cpi-event-connector (0.0.12)
11
+ activesupport (~> 3)
12
12
  bunny
13
13
  uuidtools (~> 2.1.2)
14
14
 
15
15
  GEM
16
16
  remote: http://rubygems.org/
17
17
  specs:
18
- activesupport (3.2.3)
18
+ activesupport (3.2.8)
19
19
  i18n (~> 0.6)
20
20
  multi_json (~> 1.0)
21
21
  backports (2.5.1)
22
22
  diff-lcs (1.1.3)
23
23
  i18n (0.6.0)
24
24
  justinf-unification_assertion (0.0.2)
25
- multi_json (1.3.2)
25
+ multi_json (1.3.6)
26
26
  rspec (2.8.0)
27
27
  rspec-core (~> 2.8.0)
28
28
  rspec-expectations (~> 2.8.0)
@@ -22,6 +22,6 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency "justinf-unification_assertion"
23
23
 
24
24
  s.add_runtime_dependency "bunny"
25
- s.add_runtime_dependency "activesupport", ">= 3.1"
25
+ s.add_runtime_dependency "activesupport", "~> 3"
26
26
  s.add_runtime_dependency "uuidtools", "~> 2.1.2"
27
27
  end
@@ -1,9 +1,10 @@
1
+ require 'active_support/core_ext/array/extract_options'
1
2
  require 'bunny'
2
3
 
3
4
  module Cpi
4
5
  class Connector
5
6
  def initialize params={}
6
- @queue_name = params.delete(:queue)
7
+ @queue = params.delete(:queue)
7
8
  @params = params
8
9
  end
9
10
 
@@ -13,7 +14,9 @@ module Cpi
13
14
 
14
15
  with_retries options do
15
16
  start
16
- @exchange.publish(message, :key => @queue_name, :persistent => true)
17
+ queue = options[:queue] || @queue
18
+ @bunny.queue(queue, :durable => true)
19
+ @exchange.publish(message, :key => queue, :persistent => true)
17
20
  @bunny.tx_commit
18
21
  end
19
22
  end
@@ -28,7 +31,6 @@ module Cpi
28
31
  @bunny.tx_select
29
32
  @exchange = @bunny.exchange('')
30
33
 
31
- @queue = @bunny.queue(@queue_name, :durable => true)
32
34
  @started = true
33
35
  end
34
36
 
@@ -2,20 +2,29 @@ require 'active_support/core_ext/string'
2
2
 
3
3
  module Cpi
4
4
  class Event
5
+ class << self
6
+ attr_accessor :connection_params
7
+ end
5
8
 
6
- @@enabled = false
9
+ @enabled = false
7
10
 
8
11
  def self.configure(params)
9
- @@enabled = !!params.delete(:enabled)
10
- return unless @@enabled
11
- @@connector = Cpi::Connector.new(params)
12
+ @enabled = !!params.delete(:enabled)
13
+ self.connection_params = params
12
14
  end
13
15
 
14
- def self.transmit(event_type, tenant_uid, root_path)
15
- return unless @@enabled
16
+ def self.transmit(event_type, tenant_uid, root_path, options=nil)
17
+ return unless @enabled
16
18
  raise ArgumentError, "Tenant UID must be specified" if tenant_uid.blank?
19
+
17
20
  event_json = Cpi::Generator.create_event(event_type, tenant_uid, root_path)
18
- @@connector.transmit(event_json)
21
+ self.connector.transmit(event_json, options)
22
+ end
23
+
24
+ private
25
+
26
+ def self.connector
27
+ Thread.current[:cpi_connector] ||= Cpi::Connector.new(self.connection_params)
19
28
  end
20
29
  end
21
30
  end
@@ -1,3 +1,3 @@
1
1
  module Cpi
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
@@ -27,7 +27,7 @@ describe Cpi::Connector do
27
27
  bunny.stub(:tx_commit)
28
28
  end
29
29
 
30
- context "before connection established" do
30
+ context "before a connection is established" do
31
31
  it "connects to an amqp server" do
32
32
  Bunny.should_receive(:new).with(params).and_return(bunny)
33
33
  bunny.should_receive(:start)
@@ -35,12 +35,6 @@ describe Cpi::Connector do
35
35
  connector.transmit(event)
36
36
  end
37
37
 
38
- it "connects to the specified queue" do
39
- bunny.should_receive(:queue).with("cpi", :durable => true).and_return(queue)
40
-
41
- connector.transmit(event)
42
- end
43
-
44
38
  it "creates a transaction before sending an event" do
45
39
  bunny.should_receive(:tx_select)
46
40
 
@@ -54,18 +48,30 @@ describe Cpi::Connector do
54
48
  end
55
49
  end
56
50
 
57
- context "connection already established" do
51
+ context "when a connection is already established" do
58
52
  before do
59
53
  connector.transmit(event)
60
54
  end
61
55
 
62
- it "does not do anything" do
56
+ it "does not reconnect to an amqp server" do
63
57
  Bunny.should_not_receive(:new)
64
58
 
65
59
  connector.transmit(event)
66
60
  end
67
61
  end
68
62
 
63
+ it "connects to the default queue" do
64
+ bunny.should_receive(:queue).with("cpi", :durable => true).and_return(queue)
65
+
66
+ connector.transmit(event)
67
+ end
68
+
69
+ it "connects to the specified queue" do
70
+ bunny.should_receive(:queue).with("my queue", :durable => true).and_return(queue)
71
+
72
+ connector.transmit(event, :queue => "my queue")
73
+ end
74
+
69
75
  it "sends a message to an amqp server" do
70
76
  exchange.should_receive(:publish).with(event, :key => "cpi", :persistent => true)
71
77
 
data/spec/event_spec.rb CHANGED
@@ -1,48 +1,34 @@
1
1
  require 'spec_helper'
2
2
  require_relative '../lib/cpi_event_connector'
3
- require 'yaml'
4
3
  require 'active_support'
5
4
  require 'unification_assertion'
6
- require 'active_support/core_ext/hash/keys' # for symbolize keys
7
5
 
8
6
  describe Cpi::Event do
9
- let(:yaml_config) { "
10
- ---
11
- host: localhost
12
- port: 5672
13
- username: guest
14
- password: guest
15
- queue: connector-test
16
- enabled: true
17
- "}
7
+ let(:config) { { :enabled => true, :key => "value" } }
18
8
 
19
9
  describe ".configure" do
20
-
21
10
  let(:connector) { double(Cpi::Connector) }
22
11
 
23
12
  it "accepts a hash of connection information" do
24
- expected_config = YAML::load(yaml_config).symbolize_keys!
25
- expected_config.delete(:enabled)
26
- Cpi::Connector.should_receive(:new).with(expected_config).and_return(connector)
27
-
28
- Cpi::Event.configure(YAML::load(yaml_config).symbolize_keys!)
29
- end
30
-
31
- it "does nothing if enabled is false" do
32
- yaml_config = "---
33
- enabled: false"
34
- Cpi::Connector.should_not_receive(:new)
13
+ Cpi::Event.configure(config)
35
14
 
36
- Cpi::Event.configure(YAML::load(yaml_config))
15
+ Cpi::Event.connection_params.should == { :key => "value" }
37
16
  end
38
17
  end
39
18
 
40
19
  describe ".transmit" do
41
-
20
+ let(:real_config) { {
21
+ host: "localhost",
22
+ port: "5672",
23
+ username: "guest",
24
+ password: "guest",
25
+ queue: "connector-test",
26
+ enabled: "true"
27
+ } }
42
28
  let(:content) { "<test />" }
43
29
 
44
30
  it "generates and sends an event" do
45
- Cpi::Event.configure(YAML::load(yaml_config).symbolize_keys!)
31
+ Cpi::Event.configure(real_config)
46
32
 
47
33
  Cpi::Event.transmit("shipment_shipped", "stuff", content)
48
34
 
@@ -56,11 +42,29 @@ enabled: false"
56
42
  h.should unify({"event_type" => "shipment_shipped", "tenant" => "stuff", "content" => content, "uid" => :_x})
57
43
  end
58
44
 
45
+ it "creates a connection for each thread" do
46
+ Cpi::Event.configure(config)
47
+
48
+ connector1 = double("connector 1")
49
+ connector2 = double("connector 2")
50
+ Cpi::Connector.stub(:new).and_return(connector1, connector2)
51
+
52
+ connector1.should_receive(:transmit)
53
+ connector2.should_receive(:transmit)
54
+
55
+ Thread.new do
56
+ Cpi::Event.transmit("message", "tenant_uid", "content")
57
+ end.join
58
+
59
+ Thread.new do
60
+ Cpi::Event.transmit("message", "tenant_uid", "content")
61
+ end.join
62
+ end
63
+
59
64
  it "does not transmit when it is not enabled" do
60
- config = YAML::load(yaml_config).symbolize_keys!
61
- config[:enabled] = false
65
+ real_config[:enabled] = false
62
66
 
63
- Cpi::Event.configure(config)
67
+ Cpi::Event.configure(real_config)
64
68
  Cpi::Generator.should_not_receive(:create_event)
65
69
  # And there shouldn't be an exception
66
70
 
@@ -73,7 +77,7 @@ enabled: false"
73
77
  end
74
78
 
75
79
  it "raises exception when no tenant_uid is passed in" do
76
- Cpi::Event.configure(YAML::load(yaml_config).symbolize_keys!)
80
+ Cpi::Event.configure(real_config)
77
81
  Cpi::Generator.should_not_receive(:create_event)
78
82
  expect {Cpi::Event.transmit("message", nil, "content")}.to raise_error(ArgumentError)
79
83
  end
@@ -24,4 +24,15 @@ queue: connector-test"}
24
24
  message = q.pop
25
25
  message[:payload].should == "message"
26
26
  end
27
+
28
+ it "sends a message when a queue is specified" do
29
+ connector = Cpi::Connector.new(YAML::load(yaml_config).symbolize_keys!)
30
+ connector.transmit("message", :queue => "my queue")
31
+
32
+ b = Bunny.new
33
+ b.start
34
+ q = b.queue("my queue", :durable => true)
35
+ message = q.pop
36
+ message[:payload].should == "message"
37
+ end
27
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cpi-event-connector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-23 00:00:00.000000000 Z
12
+ date: 2012-08-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -80,17 +80,17 @@ dependencies:
80
80
  requirement: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements:
83
- - - ! '>='
83
+ - - ~>
84
84
  - !ruby/object:Gem::Version
85
- version: '3.1'
85
+ version: '3'
86
86
  type: :runtime
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
89
89
  none: false
90
90
  requirements:
91
- - - ! '>='
91
+ - - ~>
92
92
  - !ruby/object:Gem::Version
93
- version: '3.1'
93
+ version: '3'
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: uuidtools
96
96
  requirement: !ruby/object:Gem::Requirement