brown 2.1.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5cc95726eacf9732ee7d020ffc841e087f5b057a
4
- data.tar.gz: 708dc32a8fe3830acf136ed7d6830e5b8df80fae
3
+ metadata.gz: 831f8702bad84d8fe35c098f0a624632ab06f835
4
+ data.tar.gz: 55684d48c7e8233a7cd6d68621f77116e2b05f49
5
5
  SHA512:
6
- metadata.gz: e91292e3193b4ed6f25971c76acb83afc69f6c015bc7c63f10e8c13770de1e18c9d9c00aad183149d9a7dc33189ada46cd71a60f5c66c420817f321fb81267d0
7
- data.tar.gz: dbffc92d30352eb5c7c66b0e20c4806e796bc5eeb5292e2910ffccceb013f8303488bd73c9056613761e6d4924430daf58e43b632d35e944eeaf03b22fd30e2a
6
+ metadata.gz: f879eb251cb5b9e511d4331a2b5e84c1c8590a80a846bf7c1d7ff813f50bf359be28a7421ffa5b718e5190d6a2fa59e25139c47f9399529d203bd8427a238096
7
+ data.tar.gz: cbf37ca7b491d245d85e389d74ad91187d6824ec86503010b054b5eb9e0a7c5ab00950eaaa871df283a487f684843b59bdae48f18d8f0d6d854c3cb216d4ac52
data/README.md CHANGED
@@ -161,15 +161,14 @@ simply receive stimuli and act on them, testing is quite simple in
161
161
  principle, but the parallelism inherent in agents can make them hard to test
162
162
  without some extra helpers.
163
163
 
164
- To enable the additional testing helpers, you must `require
165
- 'brown/test_helpers'` somewhere in your testing setup, before you define
166
- your agents. This will add a bunch of extra methods, defined in
167
- {Brown::TestHelpers} to {Brown::Agent}, which you can then call to examine
168
- certain aspects of the agent (such as `memo?(name)` and
169
- `amqp_publisher?(name)`) as well as send stimuli to the agent and have it
170
- behave appropriately, which you can then make assertions about (either by
171
- examining the new state of the overall system, or through the use of
172
- mocks/spies).
164
+ To enable the additional testing helpers, you must `require 'brown/test'`
165
+ somewhere in your testing setup, before you define your agents. This will
166
+ add a bunch of extra methods, defined in {Brown::TestHelpers} to
167
+ {Brown::Agent}, which you can then call to examine certain aspects of the
168
+ agent (such as `memo?(name)` and `amqp_publisher?(name)`) as well as send
169
+ stimuli to the agent and have it behave appropriately, which you can then
170
+ make assertions about (either by examining the new state of the overall
171
+ system, or through the use of mocks/spies).
173
172
 
174
173
  While full documentation for all of the helper methods are available in the
175
174
  YARD docs for {Brown::TestHelpers}, here are some specific tips for using
@@ -178,6 +177,10 @@ them to test certain aspects of your agents in popular testing frameworks.
178
177
 
179
178
  ### RSpec
180
179
 
180
+ To enable additional RSpec-specific test integration (resetting memos at the
181
+ end of each test), then **instead** of `require 'brown/test'`, you should
182
+ `require 'brown/rspec'` before your agent code is loaded.
183
+
181
184
  To test a directly declared stimulus, you don't need to do very much -- you
182
185
  can just instantiate the agent class and call the method you want:
183
186
 
@@ -0,0 +1,11 @@
1
+ require 'brown/test'
2
+
3
+ RSpec.configure do |c|
4
+ c.after(:each) do
5
+ ObjectSpace.each_object(Class).each do |klass|
6
+ if klass != Brown::Agent and klass.ancestors.include?(Brown::Agent)
7
+ klass.reset_memos
8
+ end
9
+ end
10
+ end
11
+ end
@@ -5,7 +5,7 @@ require 'brown/agent/amqp_message_mock'
5
5
  #
6
6
  # You can cause these methods to become part of {Brown::Agent} with
7
7
  #
8
- # require 'brown/test_helpers'
8
+ # require 'brown/test'
9
9
  #
10
10
  module Brown::TestHelpers
11
11
  #:nodoc:
@@ -51,6 +51,23 @@ module Brown::TestHelpers
51
51
  @memos[name] = Brown::Agent::Memo.new(generator, safe, true)
52
52
  end
53
53
 
54
+ # Reset all memos to "undefined" values.
55
+ #
56
+ # Because memo values are cached at the class level, this means that they're
57
+ # cached between test cases. Often, this isn't what you want (because if the
58
+ # value of the memo was changed by a test case, the next test won't run in a
59
+ # pristine environment). Calling this method will cause all of the memos in
60
+ # the agent to be reset to a state which is the same as if the memo had never
61
+ # been called at all.
62
+ #
63
+ def reset_memos
64
+ @memos ||= {}
65
+
66
+ @memos.values.each do |memo|
67
+ memo.instance_variable_set(:@cached_value, nil)
68
+ end
69
+ end
70
+
54
71
  # Is there a timer which will go off every `n` seconds registered on this
55
72
  # agent?
56
73
  #
@@ -67,7 +84,14 @@ module Brown::TestHelpers
67
84
  # @param n [Integer]
68
85
  #
69
86
  def trigger(n)
70
- self.instance_methods.select { |m| m =~ /^every_#{n}__/ }.each do |m|
87
+ trigger_methods = self.instance_methods.select { |m| m =~ /^every_#{n}__/ }
88
+
89
+ if trigger_methods.empty?
90
+ raise RuntimeError,
91
+ "Nothing is set to run every #{n} second#{n == 1 ? "" : "s"}"
92
+ end
93
+
94
+ trigger_methods.each do |m|
71
95
  self.new.__send__(m)
72
96
  end
73
97
  end
@@ -146,7 +170,11 @@ module Brown::TestHelpers
146
170
 
147
171
  msg = Brown::Agent::AMQPMessageMock.new(opts.merge(payload: payload))
148
172
 
149
- @amqp_listeners[exchange_name].call(msg)
173
+ self.new.tap do |p|
174
+ uuid = SecureRandom.uuid
175
+ p.singleton_class.__send__(:define_method, uuid, &@amqp_listeners[exchange_name])
176
+ p.__send__(uuid, msg)
177
+ end
150
178
 
151
179
  msg.acked?
152
180
  end
@@ -155,4 +183,3 @@ end
155
183
  class << Brown::Agent
156
184
  include Brown::TestHelpers
157
185
  end
158
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brown
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Palmer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-03 00:00:00.000000000 Z
11
+ date: 2015-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -206,6 +206,7 @@ files:
206
206
  - lib/brown/agent/amqp_publisher.rb
207
207
  - lib/brown/agent/memo.rb
208
208
  - lib/brown/agent/stimulus.rb
209
+ - lib/brown/rspec.rb
209
210
  - lib/brown/test.rb
210
211
  homepage: http://theshed.hezmatt.org/brown
211
212
  licenses: []