cpi-event-connector 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,27 +7,30 @@ module Cpi
7
7
  @params = params
8
8
  end
9
9
 
10
- def start
11
- @bunny = Bunny.new @params
12
- @bunny.start
13
- @bunny.tx_select
14
- @exchange = @bunny.exchange('')
15
-
16
- @queue = @bunny.queue(@queue_name, :durable => true)
17
- end
18
-
19
10
  def transmit(message)
20
11
  begin
12
+ start
21
13
  @exchange.publish(message, :key => @queue_name)
22
14
  @bunny.tx_commit
23
15
  rescue Bunny::ServerDownError, Bunny::ForcedConnectionCloseError, Bunny::ConnectionError
16
+ @started = false
24
17
  start
25
18
  retry
26
19
  end
27
20
  end
28
21
 
29
- def stop
30
- @bunny.stop
22
+ private
23
+
24
+ def start
25
+ return if @started
26
+
27
+ @bunny = Bunny.new @params
28
+ @bunny.start
29
+ @bunny.tx_select
30
+ @exchange = @bunny.exchange('')
31
+
32
+ @queue = @bunny.queue(@queue_name, :durable => true)
33
+ @started = true
31
34
  end
32
35
  end
33
- end
36
+ end
@@ -7,7 +7,6 @@ module Cpi
7
7
  @@enabled = !!params.delete(:enabled)
8
8
  return unless @@enabled
9
9
  @@connector = Cpi::Connector.new(params)
10
- @@connector.start
11
10
  end
12
11
 
13
12
  def self.transmit(event_type, tenant_uid, root_path)
@@ -1,3 +1,3 @@
1
1
  module Cpi
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -18,63 +18,64 @@ describe Cpi::Connector do
18
18
  bunny.stub(:exchange).and_return(exchange)
19
19
  end
20
20
 
21
- context "before established connection" do
22
- it "connects to an amqp server" do
23
- Bunny.should_receive(:new).with(params).and_return(bunny)
24
- bunny.should_receive(:start)
21
+ describe "#transmit" do
22
+ let(:connector) { Cpi::Connector.new params }
23
+ let(:event) { "event" }
25
24
 
26
- connector = Cpi::Connector.new params
27
- connector.start
25
+ before do
26
+ exchange.stub(:publish)
27
+ bunny.stub(:tx_commit)
28
28
  end
29
29
 
30
- it "connects to the specified queue" do
31
- bunny.should_receive(:queue).with("cpi", :durable => true).and_return(queue)
30
+ context "before connection established" do
31
+ it "connects to an amqp server" do
32
+ Bunny.should_receive(:new).with(params).and_return(bunny)
33
+ bunny.should_receive(:start)
32
34
 
33
- connector = Cpi::Connector.new params
34
- connector.start
35
- end
35
+ connector.transmit(event)
36
+ end
36
37
 
37
- it "creates a transaction before sending an event" do
38
- bunny.should_receive(:tx_select)
38
+ it "connects to the specified queue" do
39
+ bunny.should_receive(:queue).with("cpi", :durable => true).and_return(queue)
39
40
 
40
- connector = Cpi::Connector.new params
41
- connector.start
42
- end
41
+ connector.transmit(event)
42
+ end
43
43
 
44
- it "gets an exhange" do
45
- bunny.should_receive(:exchange).with('').and_return(exchange)
44
+ it "creates a transaction before sending an event" do
45
+ bunny.should_receive(:tx_select)
46
46
 
47
- connector = Cpi::Connector.new params
48
- connector.start
49
- end
50
- end
47
+ connector.transmit(event)
48
+ end
51
49
 
52
- context "with established connection" do
53
- before do
54
- @connector = Cpi::Connector.new params
55
- @connector.start
50
+ it "gets an exchange" do
51
+ bunny.should_receive(:exchange).with('').and_return(exchange)
56
52
 
57
- exchange.stub(:publish)
58
- bunny.stub(:tx_commit)
53
+ connector.transmit(event)
54
+ end
59
55
  end
60
56
 
61
- it "disconnects from an amqp server" do
62
- bunny.should_receive(:stop)
57
+ context "connection already established" do
58
+ before do
59
+ connector.transmit(event)
60
+ end
61
+
62
+ it "does not do anything" do
63
+ Bunny.should_not_receive(:new)
63
64
 
64
- @connector.stop
65
+ connector.transmit(event)
66
+ end
65
67
  end
66
68
 
67
69
  it "sends a message to an amqp server" do
68
- event = "an event"
69
70
  exchange.should_receive(:publish).with(event, :key => "cpi")
70
71
 
71
- @connector.transmit(event)
72
+ connector.transmit(event)
72
73
  end
73
74
 
74
75
  it "commits the transaction after a message has been published" do
75
76
  bunny.should_receive(:tx_commit)
76
77
 
77
- @connector.transmit("event")
78
+ connector.transmit("event")
78
79
  end
79
80
  end
80
81
  end
data/spec/event_spec.rb CHANGED
@@ -24,7 +24,6 @@ enabled: true
24
24
  expected_config = YAML::load(yaml_config).symbolize_keys!
25
25
  expected_config.delete(:enabled)
26
26
  Cpi::Connector.should_receive(:new).with(expected_config).and_return(connector)
27
- connector.should_receive(:start)
28
27
 
29
28
  Cpi::Event.configure(YAML::load(yaml_config).symbolize_keys!)
30
29
  end
@@ -33,7 +32,6 @@ enabled: true
33
32
  yaml_config = "---
34
33
  enabled: false"
35
34
  Cpi::Connector.should_not_receive(:new)
36
- connector.should_not_receive(:start)
37
35
 
38
36
  Cpi::Event.configure(YAML::load(yaml_config))
39
37
  end
@@ -16,15 +16,12 @@ queue: connector-test"}
16
16
 
17
17
  it "sends a message to an amqp server" do
18
18
  connector = Cpi::Connector.new(YAML::load(yaml_config).symbolize_keys!)
19
- connector.start
20
19
  connector.transmit("message")
21
- connector.stop
22
20
 
23
21
  b = Bunny.new
24
22
  b.start
25
23
  q = b.queue("connector-test", :durable => true)
26
24
  message = q.pop
27
25
  message[:payload].should == "message"
28
- b.stop
29
26
  end
30
- end
27
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cpi-event-connector
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nulogy
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-03-15 00:00:00 Z
18
+ date: 2012-03-28 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec
@@ -146,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  requirements: []
147
147
 
148
148
  rubyforge_project:
149
- rubygems_version: 1.8.15
149
+ rubygems_version: 1.8.10
150
150
  signing_key:
151
151
  specification_version: 3
152
152
  summary: Sends events to CPI