amqp-spec 0.1.10 → 0.1.11

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.
data/HISTORY CHANGED
@@ -49,3 +49,7 @@
49
49
  == 0.1.10 / 2010-10-15
50
50
 
51
51
  * Cleanup
52
+
53
+ == 0.1.11 / 2010-10-18
54
+
55
+ * Optional delay added to done
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.10
1
+ 0.1.11
@@ -24,7 +24,7 @@ require 'mq'
24
24
  module AMQP
25
25
 
26
26
  # Initializes new AMQP client/connection without starting another EM loop
27
- def self.start_connection opts={}, &block
27
+ def self.start_connection(opts={}, &block)
28
28
  # puts "!!!!!!!!! Existing connection: #{@conn}" if @conn
29
29
  @conn = connect opts
30
30
  @conn.callback(&block) if block
@@ -91,7 +91,6 @@ module AMQP
91
91
  opts = @@_em_default_options.merge opts
92
92
  begin
93
93
  EM.run do
94
- # begin ?
95
94
  @_em_spec_with_amqp = true
96
95
  @_em_spec_exception = nil
97
96
  spec_timeout = opts.delete(:spec_timeout) || @@_em_default_timeout
@@ -110,7 +109,7 @@ module AMQP
110
109
  end
111
110
  rescue Exception => outer_spec_exception
112
111
  # p "outer", outer_spec_exception unless outer_spec_exception.is_a? SpecTimeoutExceededError
113
- # Makes sure AMQP state is cleaned even after Rspec failures
112
+ # Make sure AMQP state is cleaned even after Rspec failures
114
113
  AMQP.cleanup_state
115
114
  raise outer_spec_exception
116
115
  end
@@ -146,21 +145,36 @@ module AMQP
146
145
  end
147
146
  end
148
147
 
149
- # Stops EM event loop, for amqp specs stops AMQP and cleans up its state
150
- def done
151
- EM.next_tick do
152
- if @_em_spec_with_amqp
153
- if AMQP.conn and not AMQP.closing
154
- AMQP.stop_connection do
148
+ # Breaks the event loop and finishes the spec. This should be called after
149
+ # you are reasonably sure that your expectations either succeeded or failed.
150
+ # Done yields to any given block first, then stops EM event loop.
151
+ # For amqp specs, stops AMQP and cleans up AMQP state.
152
+ #
153
+ # You may pass delay (in seconds) to done. If you do so, please keep in mind
154
+ # that your (default or explicit) spec timeout may fire before your delayed done
155
+ # callback is due, leading to SpecTimeoutExceededError
156
+ def done(delay=nil)
157
+ done_proc = proc do
158
+ yield if block_given?
159
+ EM.next_tick do
160
+ if @_em_spec_with_amqp
161
+ if AMQP.conn and not AMQP.closing
162
+ AMQP.stop_connection do
163
+ finish_em_spec_fiber { AMQP.cleanup_state }
164
+ end
165
+ else
155
166
  finish_em_spec_fiber { AMQP.cleanup_state }
156
167
  end
157
168
  else
158
- finish_em_spec_fiber { AMQP.cleanup_state }
169
+ finish_em_spec_fiber
159
170
  end
160
- else
161
- finish_em_spec_fiber
162
171
  end
163
172
  end
173
+ if delay
174
+ EM.add_timer delay, &done_proc
175
+ else
176
+ done_proc.call
177
+ end
164
178
  end
165
179
 
166
180
  private
@@ -59,6 +59,8 @@ shared_examples_for 'SpecHelper examples' do
59
59
  }.should raise_error EventMachine::ConnectionError
60
60
  AMQP.conn.should be_nil
61
61
  end
62
+
63
+ it_should_behave_like 'done examples'
62
64
  end
63
65
 
64
66
  shared_examples_for 'Spec examples' do
@@ -103,6 +105,42 @@ shared_examples_for 'Spec examples' do
103
105
  end
104
106
  end
105
107
 
108
+ shared_examples_for 'done examples' do
109
+
110
+ it 'should yield to block given to done (when amqp is used)' do
111
+ amqp do
112
+ done { @block_called = true; EM.reactor_running?.should == true }
113
+ end
114
+ @block_called.should == true
115
+ end
116
+
117
+ it 'should yield to block given to done (when em is used)' do
118
+ em do
119
+ done { @block_called = true; EM.reactor_running?.should == true }
120
+ end
121
+ @block_called.should == true
122
+ end
123
+
124
+ it 'should have delayed done (when amqp is used)' do
125
+ start = Time.now
126
+ amqp do
127
+ done(0.2) { @block_called = true; EM.reactor_running?.should == true }
128
+ end
129
+ @block_called.should == true
130
+ (Time.now-start).should be_close(0.2, 0.05)
131
+ end
132
+
133
+ it 'should have delayed done (when em is used)' do
134
+ start = Time.now
135
+ em do
136
+ done(0.2) { @block_called = true; EM.reactor_running?.should == true }
137
+ end
138
+ @block_called.should == true
139
+ (Time.now-start).should be_close(0.2, 0.05)
140
+ end
141
+ end
142
+
143
+
106
144
  shared_examples_for 'timeout examples' do
107
145
  before(:each) { @start = Time.now }
108
146
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amqp-spec
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 10
10
- version: 0.1.10
9
+ - 11
10
+ version: 0.1.11
11
11
  platform: ruby
12
12
  authors:
13
13
  - Arvicco
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-15 00:00:00 +04:00
18
+ date: 2010-10-18 00:00:00 +04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency